Motor Position based on Serial Command

Questions about using encoders with the Roboclaw product line
chanakaf
Posts: 9
Joined: Sun Apr 16, 2017 2:49 pm
Motor Position based on Serial Command

Post by chanakaf »

Hi All,

I am new to this forum and also a novice in programming....

I recently got RoboClaw 2X7A controller and have connected it via a Arduino Mega 2560. I have 2 motors and they both have Quadrature encoders.

I was able to get everything working correctly, Serial communication between Arduino, Roboclaw and the PC. Tried a few examples as well for position control and that too worked well.

However been struggling to achieve my end goal;

I receive via Serial port a position command (eg: 0-100 value) based on this value I want to be able to move the motor to a value between 0-100 for example, since the input value will be constantly changing, I want the motor to follow this value as close as possible. The new position could be forward or backward. Basically I want it to be absolute position. Lag between the input serial command and the actual motor position is acceptable. I am able to read the value and store it in a variable called "T1" and that value changes every time the a new values is received via Serial. Now I need to find a way to get this value into a motor control command. Velocity and acceleration can be fixed and there is no need to change these values dynamically.

Could anyone help provide some examples on how I might be able to do this?

Thank you!

Anthony
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Motor Position based on Serial Command

Post by Basicmicro Support »

You will need to scale your 0 to 100 value into a position value and then send a Roboclaw position command(assuming you have tuned the PID and made sure position control is working correctly in Ion Studio). You should be able to set it up so whenever the 0 to 100 value changes you send a new Roboclaw position command with the properly scaled value for the actual range of positions you want to move the motor to.
chanakaf
Posts: 9
Joined: Sun Apr 16, 2017 2:49 pm
Re: Motor Position based on Serial Command

Post by chanakaf »

Thank You. I am able to do this. I can send a position command and the motor will move to that position. However this only works the first time. For example; if I send my first command "50" the motor will move from "0" to "50" and stop. Then if I send "100", the motor moves forward another 100 so it end up at "150". Also if I then send "25" it keeps moving forward.

I guess I am trying to get the motors to behave in an absolute position mode. Here is my code.

Code: Select all

  roboclaw.SpeedDistanceM1(address,vel,V2,1); //V2 is my positon coming from Serial
  do{
    roboclaw.ReadBuffers(address,depth1,depth2);
  }while(depth1!=0x80 && depth2!=0x80);  //Loop until distance command has completed
  
  delay(1000);
I have also tried putting a check current encoder position and commanded position and then inverting the Velocity Value. Still can't get to go to a specific position.

Code: Select all

 int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
  
  if(V2 > enc1){
    vel=12000;
  }
    if(V2 < enc1){
    vel=-12000;
  }
Any advice on how to make this work.
chanakaf
Posts: 9
Joined: Sun Apr 16, 2017 2:49 pm
Re: Motor Position based on Serial Command

Post by chanakaf »

To add some details. I am using a Quadrature encoder that has 360 counts per rev. and a 139:1 gear box. so I have set my QPPS to 50004.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Motor Position based on Serial Command

Post by Basicmicro Support »

You are using distacne commands. The value you send each time is the distance you will move each time, so 50, then 100.

You have two choices. Use position commands instead of distance(requires the position PID to be tuned) or keep track of where you are at. If you want to get to 1000 and you are at 500 then you only want to move 500. Also with distance commands you need to take into account the ditance needed to slow down to a stop. Also with distance commands the speed value dictates direction. Positive speeds are forward, negative speeds are backwards. The advantage of distance commands is they can be chained together to create smooth movements of just about any kind you can think off. They just take more work than position commands.

I recommend you tune the position PID and use position commands if you need absolute positioning and your movements are from point a to point b(eg accel move deccel stop). With position commands you just give an absolute speed accel, deccel and the position you want to move to and it does the calculations for you.
chanakaf
Posts: 9
Joined: Sun Apr 16, 2017 2:49 pm
Re: Motor Position based on Serial Command

Post by chanakaf »

Thank you!

Is this the correct context to use position commands?

Code: Select all

    roboclaw.SpeedAccelDeccelPositionM1(address,0,12000,0,V2,1); //V2 is the position
Also per the example to I need this timeout/wait after the position command?

Code: Select all

	long last = millis();
	while(millis()-last<5000){
    delay(50);
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Motor Position based on Serial Command

Post by Basicmicro Support »

Yes, the position command looks correct.

No, you dont have to wait for the position command to complete. You can go do other things and come back and check if it is complete by using the GetBuffers function. A buffer value of 0x80 indicates the motor is idle. You can also send multipl position commands to the same motor buffered(eg the last argument would be 0) and the Roboclaw will execute each command in sequence. Again you can use the GetBuffers function to see which buffered command is executing(ef if you send 3 buffered commands you will get back 2, 1, 0 and 0x80 as command finishes in sequence).
chanakaf
Posts: 9
Joined: Sun Apr 16, 2017 2:49 pm
Re: Motor Position based on Serial Command

Post by chanakaf »

Excellent. Thank you for your help.
Ellimc
Posts: 5
Joined: Thu May 16, 2019 3:54 am
Re: Motor Position based on Serial Command

Post by Ellimc »

Hi
I'm trying to track a position on x y axis with two motor.
I am not sure to understand what you said. I got the position and i would track it in real time. So if I write this command :
roboclaw.SpeedAccelDeccelPositionM1(address,3200,12000,32000,posX,0);

and if I re-write it at every loop of the arduino mega with an updated posX. Will it go to the first posX and after having completed this command go to the second posX? Or will it try to go to the first posX and, as soon as the second posX command is given, go to the second posX and forget the first command?
In other words Do the driver need to finish an action to do another in the case of pid position control with this command roboclaw.SpeedAccelDeccelPositionM1(address,3200,12000,32000,posX,0); ?
I hope that my question is clear (english is not a strength for me). If it's not, i'll try to reexplain.
Thank you in advance for your answer
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Motor Position based on Serial Command

Post by Basicmicro Support »

If the buffer argument is set to 1 the new position command will happen immediately. If the buffer argument is 0 the command will be buffered and run in the order it was received.

Your example line of code has it set to 0 so it will be buffered.

Post Reply