Page 1 of 1

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

Posted: Wed Aug 09, 2017 6:05 am
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.

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

Posted: Thu Aug 10, 2017 4:52 pm
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

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

Posted: Thu Aug 10, 2017 5:31 pm
by gannicus
Thank you very much, I will try it out.

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