There are a few variations on arcade drive, mostly the differences are about how to handle the clipping when control values are added and go over the maximum.
Here is probably the simplest version, no clipping, we let ROBOTC handle motor control values that are over 127.
task main()
{
int forward, turn;
int drive_l_motor;
int drive_r_motor;
while(1)
{
// get joystick
forward = vexRT Ch3 ];
turn = vexRT Ch4 ];
// Set drive
drive_l_motor = forward + turn;
drive_r_motor = forward - turn;
// set motors
motor port1 ] = drive_l_motor;
motor port10 ] = drive_r_motor;
// Don't hog the cpu
wait1Msec(25);
}
}
I’ve written that in a verbose fashion so you can see what each part means and does. Others may write as follows.
task main()
{
while(1)
{
// set motors
motor port1 ] = vexRT Ch3 ] + vexRT Ch4 ];
motor port10 ] = vexRT Ch3 ] - vexRT Ch4 ];
// Don't hog the cpu
wait1Msec(25);
}
}
The format of the code I generally use is like this.
void
DriveSystemArcadeDrive( int forward, int turn )
{
long drive_l_motor;
long drive_r_motor;
// Set drive
drive_l_motor = forward + turn;
drive_r_motor = forward - turn;
// normalize drive so max is 127 if any drive is over 127
int max = abs(drive_l_motor);
if (abs(drive_r_motor) > max)
max = abs(drive_r_motor);
if (max>127) {
drive_l_motor = 127 * drive_l_motor / max;
drive_r_motor = 127 * drive_r_motor / max;
}
// set motors
motor port1 ] = drive_l_motor;
motor port10 ] = drive_r_motor;
}
task main()
{
int forward, turn;
while(1)
{
// get joystick
forward = vexRT Ch3 ];
turn = vexRT Ch4 ];
// Arcade drive
DriveSystemArcadeDrive( forward, turn );
// Don;t hog cpu
wait1Msec(25);
}
}
I’m using a function to implement the arcade drive, the function also has some control value limiting. The advantage with using a function is that when creating your autonomous code you can also call this with fixed values, for example, to drive forwards.
DriveSystemArcadeDrive( 127, 0 );
To Stop
DriveSystemArcadeDrive( 0, 0 );
To rotate right
DriveSystemArcadeDrive( 0, 127 );