How to convert x,y to motor speed1&2?

General discussion of using Roboclaw motor controllers
Post Reply
gannicus
Posts: 28
Joined: Sat Aug 06, 2016 4:33 am
How to convert x,y to motor speed1&2?

Post by gannicus »

Hi,

I'm trying to use a joy stick control the motors, I can get the input x,y(both values range from -100 to 100). Is there a way I can convert the x,y to motorspeed1 and motorspeed2 so the motor drive smoothly?

What I need is : when I push joystic forward x=0,y=100, both motor move forward with maximum speed, when I push joystick right x=100,y=0 robot turns right with left motor forward speed max and right motor backward speed max etc...

Thanks.
jwatte
Posts: 45
Joined: Thu Apr 02, 2015 11:55 am
Re: How to convert x,y to motor speed1&2?

Post by jwatte »

Yes, there are drive modes in the RoboClaw that can support this. Also, you can do the appropriate math in your own code.

You don't say what code you're using to read the joystick. When you say you get "y=100" for forward, where do you get this? Are you using an Arduino, or a Linux box, or a Teensy, or what?

In general, you can turn "forward + turn" into "left + right" like so:

Code: Select all

#include <math.h>
#define MAX_SPEED 100.0f
void convert_forward_turn(float forward, float turn, float *left, float *right) {
    *left = forward + turn/2;
    *right = forward - turn/2;
    float mul = 1.0f;
    if (fabsf(*left) > MAX_SPEED) {
      mul = MAX_SPEED / fabsf(*left);
      *left *= mul;
      *right *= mul;
    }
    if (fabsf(*right) > MAX_SPEED) {
      mul = MAX_SPEED / fabsf(*right);
      *left *= mul;
      *right *= mul;
    }
}

/* use it: */

    float forward = read_joystick_y();
    float turn = read_joystick_x();
    float left = 0, right = 0;
    convert_forward_turn(forward, turn, &left, &right);
    // you now have left and right motor speeds
gannicus
Posts: 28
Joined: Sat Aug 06, 2016 4:33 am
Re: How to convert x,y to motor speed1&2?

Post by gannicus »

Thank you very much, I will try it out.

I'm using c# in windows, I get joystick input from DirectInput.

Post Reply