How to use the position command to have a motor stop in the 'UP' position

Questions about using encoders with the Roboclaw product line
Post Reply
SouthernAtHeart
Posts: 32
Joined: Fri Jan 29, 2016 4:00 pm
How to use the position command to have a motor stop in the 'UP' position

Post by SouthernAtHeart »

My setup:
roboclaw 2x7; 3 amp motor; 600 PPR encoder
running the motor at full speed, I got the value of 57000 QPPS.
Arduino sends Serial Packet Commands to the Roboclaw.

I see how the speed command works, sending a speed command to the roboclaw like:
roboclaw.SpeedM1(address, speed); //send the speed value

As my Arduino is sending the commands to slow down and stop, when it gets to a slow speed, the Arduino starts monitoring an optical sensor that detects a white dot on the motor shaft/pulley, so my Arduino knows when the motor shaft is in the 'UP' position. When it detects this, I have it read the value of the motor's encoder like this:
unsigned long M1Encoder = roboclaw.ReadEncM1(address, &statusEnc1, &validEnc1); //read the Motor Encoder value
So, when it's time to stop the motor, instead of actually stopping it by sending the speed of 0, I want to tell it to go to the up position, at a moderately slow speed (like 25% of it's full speed).
I'm guessing I need something like:
roboclaw.SpeedDistanceM1(address, 5000, M1Encoder + somevalue, 1); //Send motor to the UP position
But this doesn't seem to produce consistent results. Do I have to set up the Position PID values like I did the PID values for the motor Encoder? And how do I calculate the exact value I need? For example, if, when the optical sensor picks up the white dot, I take a reading of 500,000 from the encoder, what distance value do I want to send it to make it go exactly one revolution? Is the acceleration and deceleration affecting it not stopping at the right point?
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: How to use the position command to have a motor stop in the 'UP' position

Post by Basicmicro Support »

There are two ways to do this.

Using speed/distance commands, note the encoder position(like you are doing) when the up sensor is tripped, then send a speed 0 command. After the motor stops send two need distance commands, one to accel and run at the slow speed and the other to deccel back to 0 speed precisely at the encoder count you noted earlier when you passed the sensor.

The distance you will need to move is the current encoder count - the sensor position encoder count. You need two distance commands because you want to produce a trapezoidal move(accel from 0 , constant speed, deccel back to 0). I go over the math in this post: http://forums.ionmc.com/viewtopic.php?f ... tance#p158

The second option is to use position commands(you iwll need to tune the position PID constants as well as the velocity constants to use this one). Just like the first option you note the encoder count when the sensor is triggers. Then send a 0 speed command. Once the motor has stopped, send a position command to move to the enccount you noted earlier. Thats it(assumign you have tuned the position PID constants well enough).
SouthernAtHeart
Posts: 32
Joined: Fri Jan 29, 2016 4:00 pm
Re: How to use the position command to have a motor stop in the 'UP' position

Post by SouthernAtHeart »

Thanks. I like the 2nd option, of tuning in the PID position properly, and then just telling it to go to an exact spot. As posted in another thread, I' not quite sure which commands are for position control. Is command 65 what I need here? Also, would I necessarily have to send the motor speed 0 and wait for it to stop?
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: How to use the position command to have a motor stop in the 'UP' position

Post by Basicmicro Support »

Yes, 65,66 and 67. Note the speed value for these commands in unsigned. It is a typo in the manual.

With position commands you give the movement speed not the end speed. It will stop when it reaches the specified position. Basically each position command is a complete trapezoidal movement(accel, run some at some speed until it is time to deccel to0 to reach the specified point exactly(or exactly as your tuning allows).

Post Reply