In the following member function, I think there might be improvement.
BTW, this is from AFMotor-08_12_2009.zip
void AF_Stepper::setSpeed(uint16_t rpm) {
usperstep = 60000000 / (revsteps * rpm);
steppingcounter = 0;
}
I had been using a 48 step motor (7.5 degrees/step) and all was fine.
Then I started using a 200 step motor (1.8 degrees/step) and I encountered problems.
The revsteps and rpm are correctly both of type uint16_t.
As I advanced the speed of the motor from 320 rpm to 330 rpm, the function misbehaved.
The product of two int16_t returns an int16_t.
The product of 200*330 overflows a 16-bit result.
I added a couple of casts and it seemed to do the trick.
usperstep = 60000000 / ((uint32_t)revsteps * (uint32_t)rpm);
AFMotor.cpp setspeed()
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 12151
- Joined: Thu Apr 06, 2006 4:21 pm
Re: AFMotor.cpp setspeed()
we're revamping the library, and will fix this soon!
-
- Posts: 12151
- Joined: Thu Apr 06, 2006 4:21 pm
Re: AFMotor.cpp setspeed()
k we've made this mod
Please be positive and constructive with your questions and comments.