Joystick Values

How does the joystick interpret values? Does it use a 2D coordinate system in which (0,0) is where the joystick starts (the center)? Or does the joystick use only one quadrant of the coordinate system? Like if the minimun value was 0 and the maximun value was 100, is the joystick starting position (the center) on the point (50,50)? Thank you for answering in advance.

  • Inventor Inventor

Unless you’re writing firmware for the joystick, the first interpretation should be fine.

When acquiring the joystick values, we get a value for one joystick, in one axis (both going from -127 to 127). So a centered joystick would have a value in each axis of 0. The two axes are always taken separately and are for all intents and purposes independent.

In PROS, for example, when we say


joystickGetAnalog(1, 1)

we are returned a value for the primary controller’s horizontal position on the right joystick.

So if I wanted to power a motor based on how far right that stick is, I would write


motorSet(1, joystickGetAnalog(1, 1));

I’m not sure the compiler would be happy with you for writing that…

Instead:


motorSet(1, joystickGetAnalog(1, 1));

Oops, missed the parentheses. Atom fills them in for me. :stuck_out_tongue:

“Strengths: attention to detail

You mean you don’t do your coding in Notepad?! :stuck_out_tongue:

I have to do my version control through terminal, so that’s the only logical next step.

So let’s say I wanted my motor to run when the joystick goes past the halfway point on the vertical axis. I would right (this isn’t correct syntax cause I want to see if I get the idea right):

if(verticalAxisValue >= (127 / 2)) <— (does the joystick use float values? Or integers? Can it read 63.5?)
{
runMotor();
}

I’m using RobotC, so sorry if I don’t understand PROS.

The half-way point for all joystick axes is 0, not 127/2. 127/2 would be halfway between the half-way point (0) and the positive extreme (127 - either the highest or rightmost point).

In other words:

  • All axes go from -127 to +127, with 0 in the middle
  • Vertical axes have -127 on the bottom and +127 on top
  • Horizontal axes have -127 at the leftmost point and +127 at the rightmost point

Ok since I use RobotC, I’ll explain the code the way the way it would be written in RobotC. So if you want the motor to go forwards when the joystick is past the halfway point (or 127 / 2 = 63 because it only uses integers), it would be the following:


if(vexRT[Ch2] > 127 / 2) { //to go forwards
      motor[motorToRun] = 80;  //or whatever your desired speed is
}
else if(vexRT[Ch2] < -127 / 2) { //to go backwards
      motor[motorToRun] = 80;
}

I’m not sure we’re all on the same page. First of all, the coordinate system in PROS is no different from in RobotC.

Secondly, the halfway point is at 0 for all axes, as the range is from -127 to +127. Contrastingly, 127/2≈63 is halfway forward or halfway rightward, while -127/2≈-63 is halfway backward or halfway leftward.

@Inventor Inventor If you’re looking for, as your code would suggest, running the motor only while the joystick is in the forward-most 25% of it’s range, your code is correct.

I get what you’re saying and I agree, but I just think it’s kind of funny that you basically said “if your code is correct, your code is correct.” :stuck_out_tongue:

I think he does mean past the halfway point going upward. Otherwise, it would be nearly impossible to keep the motor from running (the joystick doesn’t always center on exactly (0, 0); that’s why we use deadbands, and I would guess you do too.

Yeah, I’m sorry if I misunderstood, but I assumed that @InventorInventor wanted it to be halfway up or down. Also the only reason I specified that I use RobotC and not Pros was to point out the slight difference in set up such as using vexRT[Ch#] instead of joystickGetAnalog(). Sorry again if I misunderstood.

No problem; I could just as well have misunderstood too. My last post was just to clarify and hopefully get everyone on the same page.

Sorry if I wasn’t clear. I wanted to go halfway up the positive vertical axis. I understand that the center is where the joystick starts.

Notepad is for noobs. Real programmers write with SMW code injection.

You should change your code in two ways.

First, it looks like you’re running forward and backward in the same direction. Switch one to -80 instead of 80.

Second you really want a third piece, an else in the end. Otherwise you have no way to stop the motor(s). Once you set them to ±80, you can only have them at ±80. At the end throw in an else to set it(them) to 0.

You might also consider using buttons for what you’re doing. You’re only giving the motors three values: 80, -80, or 0. That can be an up button, a down button, or 0. That’s really just two true-false checks, saving your axes for other stuff where you want variable control.