Skip to content

2a. Controllers

Xiang Weng edited this page Aug 29, 2020 · 18 revisions

Overview

Controllers are important for the manually controlling the robot. This is a programming guide so we're assuming you have the robot properly set up (wifi, roborio, motors) and running.

What you need

  • Xbox Controller

Code

There is more useful methods in the javadocs, but these two basic methods are the basis of using a controller.

Get buttons

This method returns a boolean based on the button's status.

getRawButton(int buttonNumber)

Basic Button Map

  • "A" Button: int buttonNumber = 0
  • "B" Button: int buttonNumber = 1
  • "X" Button: int buttonNumber = 2
  • "Y" Button: int buttonNumber = 3

Get axis

This method returns a numerical value based on the joystick's position.

getRawAxis(int axis)

Axis Map

  • Left stick, x-axis: int axis = 0, returns values from -1 to 1
  • Left stick, y-axis: int axis = 1, returns values from -1 to 1
  • Left trigger: int axis = 2, returns values from 0 to 1
  • Right trigger: int axis = 3, returns values from 0 to 1
  • Right stick, x-axis: int axis = 4, returns values from -1 to 1
  • Right stick, y-axis: int axis = 5, returns values from -1 to 1

Use Cases

this is partly psuedo-code

  • Getting a motor to spin at a set speed.
Boolean spinWheel = getRawButton(3); //value is either true or false
if(spinWheel) motor.setSpeed(1.00);
  • Driving the motor at different speeds.
double drive = getRawAxis(1); //value is between -1 and 1.
motor.setSpeed(drive);

Note

You might see in the example code we have that the XboxController library is used rather than the generic "getRawAxis" type library. If the controller for the robot is still an xbox controller you can use that instead. I found the XboxController library methods of "getAPressed" much simpler than "getRawButton()". The Javadoc for the XboxController is in the resources below.

Resources