Holonomic Drive Program with Tank Control Instead of Arcade Control

We are using an x-drive with the following code:

motor[frontLeft] = vexRT[Ch2] + vexRT[Ch3] - vexRT[Ch1];
motor[frontRight] = vexRT[Ch2] - vexRT[Ch3] + vexRT[Ch1];
motor[backLeft] = vexRT[Ch2] + vexRT[Ch3] + vexRT[Ch1];
motor[backRight]= vexRT[Ch2] - vexRT[Ch3] - vexRT[Ch1];

This code uses arcade control, but I drive better with tank control. I have been unsuccessful in creating or finding example code that uses tank control on an x-drive instead of arcade control. Does anyone have any example code to help me? Thanks in advance.

The general form for x-drive is like this.

    drive_l_front = forward + turn + right;
    drive_l_back  = forward + turn - right;

    drive_r_front = forward - turn - right;
    drive_r_back  = forward - turn + right;

If we reorder your code to be the same

motor[frontLeft]  = vexRT[Ch2] + vexRT[Ch3] - vexRT[Ch1];
motor[backLeft]   = vexRT[Ch2] + vexRT[Ch3] + vexRT[Ch1];
motor[frontRight] = vexRT[Ch2] - vexRT[Ch3] + vexRT[Ch1];
motor[backRight]  = vexRT[Ch2] - vexRT[Ch3] - vexRT[Ch1];

It looks like you are using Ch2 as forward, Ch3 as turning and Ch1 as right (strafe), although sign seems to be reversed on that axis. Expanding out the code we might have a function for drive that looks like this.

void
MecanumDrive( )
{
    long drive_l_front;
    long drive_l_back;
    long drive_r_front;
    long drive_r_back;

    long forward;
    long turn;
    long right;

    forward = vexRT Ch2 ];
    turn    = vexRT Ch3 ];
    right   = vexRT Ch1 ];

    // Set drive
    drive_l_front = forward + turn + right;
    drive_l_back  = forward + turn - right;

    drive_r_front = forward - turn - right;
    drive_r_back  = forward - turn + right;

    // Send to motors
    // left drive
    motor motorLF ] = drive_l_front;
    motor motorLB ] = drive_l_back;
    
    // right drive
    motor motorRF ] = drive_r_front;
    motor motorRB ] = drive_r_back;
}

What you want to do is create the forward and turn variables using tank style controls. You can do that like this.

    forward = (vexRT Ch3 ] + vexRT Ch2 ] ) / 2;
    turn    = (vexRT Ch3 ] - vexRT Ch2 ] ) / 2;

So the drive function would then be.

void
MecanumDrive( )
{
    long drive_l_front;
    long drive_l_back;
    long drive_r_front;
    long drive_r_back;

    long forward;
    long turn;
    long right;

    forward = (vexRT Ch3 ] + vexRT Ch2 ] ) / 2;
    turn    = (vexRT Ch3 ] - vexRT Ch2 ] ) / 2;
    right   = vexRT Ch1 ];

    // Set drive
    drive_l_front = forward + turn + right;
    drive_l_back  = forward + turn - right;

    drive_r_front = forward - turn - right;
    drive_r_back  = forward - turn + right;

    // Send to motors
    // left drive
    motor motorLF ] = drive_l_front;
    motor motorLB ] = drive_l_back;
    
    // right drive
    motor motorRF ] = drive_r_front;
    motor motorRB ] = drive_r_back;
}

You can use a little algebra if you want to fold all that back into a simplified version, start here.

motor[frontLeft]  = ((vexRT Ch3 ] + vexRT Ch2 ] ) / 2) + ((vexRT Ch3 ] - vexRT Ch2 ] ) / 2) - vexRT[Ch1];

and simplify

and you end up with this (in terms of your original code) :slight_smile:

motor[frontLeft]  = vexRT Ch3 ] - vexRT[Ch1];
motor[backLeft]   = vexRT Ch3 ] + vexRT[Ch1];
motor[frontRight] = vexRT Ch2 ] + vexRT[Ch1];
motor[backRight]  = vexRT Ch2 ] - vexRT[Ch1];

2 Likes