# Joystick Control Quick fix

Hi,

We have our robot controlled with 2 controllers and were considering switching to one controller because of batteries. We want our wheel base controlled on the left joystick. I.e. Ch2 foward/Backward, Ch1 Left/Right. We use robotIC. Can you please provide a code. Thanks,
God bless

You can find example code for what you are looking for if you go File > Open Sample Programs

I believe what you are asking for is Arcade Control. You use one joystick to control the movement of the drivetrain. It’s similar to almost all video games today.

Here is a link to the VEX Wiki. It provides a small explanation of what the code does. Here is a [/2;]table/graph]("motor[right) with a considerably larger amount of detail.
As far as the code goes:

``````motor[right] = vexRT[Ch1]/2 + vexRT[Ch2]/2;
motor[left] = vexRT[Ch1]/2 - vexRT[Ch2]/2;``````

Hope this helps!

Is this easyC or Robot C

It’s RobotC

Hey,

Channels 1 and 2 are on the right joystick, did you mean 3 and 4??

The code you are wanting is arcade control, which basically works like this:

``````left = forwards + turn
right = forwards - turn``````

So that would be

``````motor[left] = vexRT[Ch3] + vexRT[Ch4];
motor[right] = vexRT[Ch3] - vexRT[Ch4];``````

Additionally you could set variables like forwards to equal vexRT[Ch3] and turn to equal vexRT[Ch4].

**EDIT: **looks like I was beaten to it **EDIT 2: **personally I don’t bother with the /2 after the channel, but I do it a bit differently anyway.

~George

I could be wrong here, but say, for example, your left motor value should be 256 and your right value should be 127. The left motor value would limit itself to 127 and instead of going forward and to the left, you’re now going just forward. I realize that the 256/127 isn’t theoretically possible, but you can still have similar values.

Is it just me… or would the /2 make it so that if on the joystick turning equaled 0 and forwards equaled full speed, it would end up only going half speed? I’m pretty sure that’s why I used to not do /2 at all.

~George

Yes. There are ways around this. Particularly through the use of figuring out which value is higher and figuring out what to multiply the highest value by to get 127 and then doing that across both values. Then you have properly scaled values. But dividing by 2 is just as easier and I haven’t tested any of the code itself.

Here is the code that I would develop, but haven’t bothered to test because our team doesn’t use arcade ``````int multiplier;
int leftValue = vexRT[Ch1] - vexRT[Ch2];
int rightValue = vexRT[Ch1] + vexRT[Ch2];

if(abs(leftValue) >= abs(rightValue)) { multiplier = leftValue / 127; }
else if (abs(rightValue) > abs(leftValue)) { mutliplier = rightValue / 127; }

leftValue = leftValue * multiplier;
rightValue = rightValue * multiplier;

motor[left] = leftValue;
motor[right] = rightValue;``````

I usually normalize the drive values. Here is an excerpt from my arcade driove code.

``````    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;
}
``````

I did the same sort of thing when I made a mecanum bot to demonstrate. I used a macro:

``````
#define LIMIT(in,min,max) (in > max) ? max : (in < min) ? min : in

``````

Just add the values like above (forward +/- turn, etc), and do something like this:

``````
motor[someMotor] = LIMIT(forward + turn, -127, 127);

``````

That macro is nice for other applications, though 