I am doing a bit of motor control, and instead of saying 39553 encoder ticks, it would be easier for my human brain to say 6.5 inches. I would like to save processor overhead by converting this at compile time. Is there a way to do this with preprocessor directives or templates maybe? Thanks.
I would use a constexpr
for this. As @ChrisMM mentioned, it is not preprocessor, but it will be evaluated at compile time rather than run time. If the values you gave correspond to the actual relationship between ticks and inches, you can use:
constexpr int toTicks(double inches) {
return int(6085 * inches);
}
By doing the cast to an int, you can make sure you do not ever ask the motor to move some fraction of a tick. The only thing to be cautious of with an approach like this is that over time if you ask the motor to move to a bunch of different locations without returning to the origin between each move your origin can slowly shift with rounding errors from the conversion to an int.