Lesson 16 - Joystick Commands
Joystick Commands - The Basics
Joysticks are vital to control your robot in VEX, and properly getting joystick inputs and using them in your program is how your robot will function. This lesson will go over how to get button presses and joystick values and use them in your code.
User Control Commands
Getting Channel Values
Getting channel values is essential to programming a drive train or even a lift. The channel values provide an output of -127 to 127, depending on how far the joystick is pushed. In order to understand which direction is positive or negative, think of it as a graph like in geometry. To get the channel values, you will use these commands depending on which channel you would like the input from.
NOTE: In order to properly understand exactly which syntax uses which controller values, you can use this chart. Alternatively, the button and joystick names are also written on the controller itself for easier understanding.
Getting Button Presses
To get button presses on the controller, you can use this table to find the appropriate syntax. The buttons provide a boolean output of true and false, depending on if the button is pushed or not. Refer to the controller diagram above in order to familiarize yourself with the different buttons.
Virtual Robot Example
Some practical purposes for these are when programming driver control. This example code with a virtual robot shows how you can take the different motor values and incorporate them in a simple driver control code. This example is a very simplified version of something you would actually use for a VEX robot.
pros::Motor left_motor(1, MOTOR_GEARSET_36, false, MOTOR_ENCODER_DEGREES);
pros::Motor right_motor(2, MOTOR_GEARSET_36, true, MOTOR_ENCODER_DEGREES);
pros::Motor lift(3, MOTOR_GEARSET_36, false, MOTOR_ENCODER_DEGREES);
pros::Controller master(E_CONTROLLER_MASTER);
while (true){
left_motor.move(master.get_analog(E_CONTROLLER_ANALOG_LEFT_Y));
right_motor.move(master.get_analog(E_CONTROLLER_ANALOG_RIGHT_Y));
if (master.get_digital(DIGITAL_L1)){
lift.move(127);
}
else if (master.get_digital(DIGITAL_L2)){
lift.move(-127);
}
else{
lift.move(0);
}
}