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.


Axis # to get values from

Syntax

Axis 1

controller_name.get_analog(ANALOG_RIGHT_X);

Axis 2

controller_name.get_analog(ANALOG_RIGHT_Y);

Axis 3

controller_name.get_analog(ANALOG_LEFT_Y);

Axis 4

controller_name.get_analog(ANALOG_LEFT_X);



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.



Button to get values from

Syntax

Button A

controller_name.get_digital(DIGITAL_A);

Button B

controller_name.get_digital(DIGITAL_B);

Button X

controller_name.get_digital(DIGITAL_X);

Button Y

controller_name.get_digital(DIGITAL_Y);

Button Up

controller_name.get_digital(DIGITAL_UP);

Button Left

controller_name.get_digital(DIGITAL_LEFT);

Button Right

controller_name.get_digital(DIGITAL_RIGHT);

Button Down

controller_name.get_digital(DIGITAL_DOWN);

Button R1

controller_name.get_digital(DIGITAL_R1);

Button R2

controller_name.get_digital(DIGITAL_R2);

Button L1

controller_name.get_digital(DIGITAL_L1);

Button L2

controller_name.get_digital(DIGITAL_L2);



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);

}

}