Motor Position based on Serial Command

Questions about using encoders with the Roboclaw product line
chanakaf
Posts: 5
Joined: Sun Apr 16, 2017 2:49 pm

Motor Position based on Serial Command

Postby chanakaf » Sun Apr 16, 2017 3:08 pm

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
IMC Support
Posts: 696
Joined: Thu Feb 26, 2015 9:45 pm

Re: Motor Position based on Serial Command

Postby IMC Support » Tue Apr 18, 2017 8:53 am

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: 5
Joined: Sun Apr 16, 2017 2:49 pm

Re: Motor Position based on Serial Command

Postby chanakaf » Fri Apr 21, 2017 5:06 am

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: 5
Joined: Sun Apr 16, 2017 2:49 pm

Re: Motor Position based on Serial Command

Postby chanakaf » Fri Apr 21, 2017 5:15 am

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
IMC Support
Posts: 696
Joined: Thu Feb 26, 2015 9:45 pm

Re: Motor Position based on Serial Command

Postby IMC Support » Fri Apr 21, 2017 9:42 am

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: 5
Joined: Sun Apr 16, 2017 2:49 pm

Re: Motor Position based on Serial Command

Postby chanakaf » Fri Apr 21, 2017 2:02 pm

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
IMC Support
Posts: 696
Joined: Thu Feb 26, 2015 9:45 pm

Re: Motor Position based on Serial Command

Postby IMC Support » Mon Apr 24, 2017 8:46 am

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: 5
Joined: Sun Apr 16, 2017 2:49 pm

Re: Motor Position based on Serial Command

Postby chanakaf » Mon Apr 24, 2017 1:29 pm

Excellent. Thank you for your help.


Return to “Velocity and Position Control”

Who is online

Users browsing this forum: No registered users and 2 guests

cron