Arcade Control

I would like to try an arcade control program on RobotC. Would it be possible to move forward, backward and turn left and right all on one analog stick?

I thought about it, coded it, and I can’t really see how this is possible. Is there a special code just for arcade control?

Yeah. I’m no programmer, but I think some arcade-drive code is included in the default code, so you can probably look there.

From what I remember our programmers saying, the up/down of a joystick (Z), and the left/right of a joystick (X) control whether you turn left or right, or go forward or backwards. Or a mixmatch of any of the options (ie. forward and right).

Hope this helps!

Yes.

http://www.chiefdelphi.com/forums/attachment.php?attachmentid=12085&d=1330054122

This code assumes that your robot has two powered wheels mounted opposite to each other and the left motor is reversed. Positive power to both motors would drive the robot forward.


#include "JoystickDriver.c" // Include the RobotC joystick driver file.

task main() {
  while (true) { // infinitely loop
    getJoystickSettings(joystick);  // Update buttons and joysticks.
    int turn = joystick.joy1_x1; // Get joystick 1's left x-axis value.
    int power = joystick.joy1_y1; // Get joystick 1's left y-axis value.

    motor[leftMotor] = power + turn;
    motor[rightMotor] = power - turn;
  }
}

To explain this, we can look at some of the different possible ways to push the joystick. For simplicity’s sake, I’m going to make all of the joysticks be pushed to a value of 100.

To drive forward, we push the joystick forward (positive y). This makes the value of the power variable +100. The turn variable is 0 because the joystick hasn’t moved left or right.

This effectively translate in the code to

motor[leftMotor] = 100 + 0;
motor[rightMotor] = 100 + 0;

which as we know, will drive the robot forward.

Likewise, to turn right in place, we can push the joystick to the right. This will cause the turn variable to be +100, but the power variable will be 0.

motor[leftMotor] = 0 + 100;
motor[rightMotor] = 0 - 100;

As you can see, this would make the right wheel spin backward and the left wheel spin forward, effectively turning the robot in place to the right.

Now, say we wanted to go forward and turn right at the same time. If we push the joystick to a positive value of 100 in both directions (x and y), we ge this:

motor[leftMotor] = 100 + 100;
motor[rightMotor] = 100 - 100;

The left motor will go forward at 100% power even though technically the code is trying to set it to 200. This is due to a physical limitation. However, the right motor will stay at 0, which will allow for what is sometimes called a swing turn.

Backing up and turning left work similarly.

I hope this helps. If you are doing something like holonomic omni wheel driving, let me know because that is quite different and requires a lot more explanation.

Oh my goodness, you just made variables understandable. I never liked using variables and tried to steer away from them as much as possible, but this just made so much sense. Thank you.

This is my first time building, until I get people for a team (building is such a pain I’d rather stick to programming) so I’m not sure what holonomic wheel driving is. I am planning on using a mix of omni-wheels and idlers for this.

I assume I just need to adjust the coding if I use more than 2 motors?

*My comment above &…

Also would I still need to include the “arcadeControl();” line in there ?

I added this comment after I had finished writing this entire post:

Waaaiiit wait wait. Are you using Natural Language? arcadeControl() is a Natural Language function, which is different from plain old RobotC. If you have no idea what Natural Language is, then please ignore the above comment and keep reading below. If you are using that, then all of my provided code is completely obsolete.


I’m glad that my post was able to better explain how variables work, as they are very important for programming outside of RobotC.

To answer your question, no. arcadeControl() is a pre-built function that basically does what I showed you earlier but with an added feature called a threshold. I don’t like using it because I prefer to work with the motors more directly since it allows me to tweak exactly how the motors respond to the joysticks, and I like to understand how it works. It also helped me figure out how to program holonomic drive, which I will explain later. arcadeControl() is a part of what is called Natural Language. It’s a simplified version of robotC that comes with commands like forward() and untilSensor(). You really don’t need to use it for FTC programming as my code essentially re-implements what arcadeControl() would do. It is good programming practice to separate your code into functions (if you haven’t learned what those are, I highly recommend reading the RobotC help documentation to learn more about them), but it usually isn’t necessary for FTC programming.

Regarding the threshold, basically, sometimes the joysticks don’t perfectly reset to 0. You can sometimes see it while you’re running your tests; after releasing the joysticks, the motor still vibrates and tries to move. When you’re not touching them, they might have a value of say, x=-3 and y=4. With my code, the motors would try to move with a speed of -3, but this isn’t enough to overcome the internal friction of the motor. This could lead to seriously bad problems, so we overcome that by only running the motors if the joystick values are smaller than pre-determined threshold.

See this example code:

int x = joystick.joy1_x1;
int y = joystick.joy1_y1;
int THRESHOLD = 10;

if (abs(x) > THRESHOLD && abs(y) > THRESHOLD) {
  motor[driveLeft] = y + x;
  motor[driveRight] = y - x;
} else {
  motor[driveLeft] = 0;
  motor[driveRight] = 0;
}

In English, that translates to:

If the absolute value of the joystick’s x-axis is greater than 10 (the threshold) AND the y-value is also greater than 10, run the motors according to the formulas I showed you earlier. Otherwise, don’t power the motors. You can also think of it like this: if either joystick value (x or y) is less than 10, don’t even bother running the motor because it’s too small to even move the motor anyway.

Also, holonomic drive is basically four omni wheels in a diagonal configuration, which lets you strafe left and right without needing to turn, much unlike a car or shopping cart.

1 Like

Oh no, I’m not looking to strafe, I want to reverse power on motors. These codes makes sense, plus the thresh hold would then just stop the motors from running unless they’re bigger than the thresh hold. I’ve seen thresh holds in my teammate’s codes before, but never understood them, so thank you again. I think that’s all. :slight_smile:

But please explain the holonomic drive if you don’t mind, it sounds like a possibility, I learned a lot already.