AFMotor.cpp setspeed()

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
inpnw
 
Posts: 1
Joined: Sun Dec 12, 2010 9:49 pm

AFMotor.cpp setspeed()

Post by inpnw »

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);

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: AFMotor.cpp setspeed()

Post by adafruit »

we're revamping the library, and will fix this soon!

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: AFMotor.cpp setspeed()

Post by adafruit »

k we've made this mod

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”