I'm trying to find an efficient way of calculating an inverse on an AVR (or approximating it).
I'm trying to calculate the pulse period for a stepper motor so that I can vary the speed linearly. The period is proportional to the inverse of the speed (
p = K/v), but I can't think of a good way of calculating this on the fly.
My formula is
p = 202/v + 298; // p in us; v varies from 1->100
Testing on the Arduino, the division seems to be ignored completely leaving
p fixed at
298 (though perhaps this would be different in avr-gcc). I've also tried summing
v in a loop until it exceeds
202, and counting the loops, but this is quite slow.
I could generate a lookup table and store it in flash, but I was wondering if there was another way.
Edit: Maybe the title should be "efficient divide"...
Update: As pingswept points out, my formula for mapping period to velocity is incorrect. But the main problem is the divide operation.
Edit 2: On further investigation, divide is working on the arduino, the problem was due to both the incorrect formula above and an int overflow elsewhere.