Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • Creating an XboxController Object
  • Connecting an External Controller
  • Using the XboxController Object
  • Challenge: upate the MoveRobot() method
  • Optional Challenge: The ArcadeStyleeMoveRobot() Method
  1. Romi Curriculum- Timed Base
  2. Cool stuff i will rename this category later

External Controllers

PreviousPIDsNextEncoders

Last updated 2 months ago

Controllers such as Xbox or Playstation controllers are big parts of FRC robotics and robotics in general. Controllers are important in FRC because competitions always have a player-controlled section, during this section, you switch to teleoperated mode and a player controls the robot most likely through a controller. Other than just specifically FRC robotics, controllers are useful due to their various forms of inputs allowing users to control robots with ease. To get access to these controllers' various benefits, we will use the WPILIB XboxController object.

Though the object is an XboxController object, you can use any controller. Some controllers we know work are the Xbox controller, Switch Pro controller, and the PlayStation DualShock controller.


Creating an XboxController Object

Similar to Spark motors objects refer to motors on the Romi. The XboxController object refers to the controller that is connected to the computer.

Parameters:

  • Integer(int) -> Its ID

A controller's ID (called port in VS code) allows the code to differentiate connected controllers from one another just like the IDs of Spark motors and Encoders. Here is an example of an XboxController object:

private XboxController controller = new XboxController(1);

Connecting an External Controller

There are 2 ways to connect an external controller to your computer to use in our project.

  • Using Bluetooth

  • Using a cord

Make sure the controller is on once connected to the computer, otherwise the program won't detect anything.

After your controller is connected, you have to make sure the program can detect it in the simulation. To do this, simulate the code to open up the simulation interface. Then, if the controller is properly connected, a device should pop up in the Connected Devices tab on the left side of the window. drag that controller to the Controller Readings tab to the left of the Connected Devices tab. This should make one of the unassigned spots on the Connected Devices change to the controller's name and show various readings below it such as joystick values.

Once you add a controller to the joysticks. It will save until you disconnect your controller from your computer.


Using the XboxController Object

There are a multitude of methods in the XboxController class, some more important than others. Here is a link that has the list of all the methods in the class:

For the Romi though, you will only need to know 4 basic methods:


/* Gets the right joystick's position on the 
X-axis returning a double between -1 and 1. */
double rightX = controller.getRightX(); 

/* Gets the right joystick's position on the 
Y-axis returning a double between -1 and 1. */
double rightY = controller.getRightY();

/* Gets the left joystick's position on the 
X-axis returning a double between -1 and 1. */
double leftX = controller.getLeftX(); 

/* Gets the left joystick's position on the 
Y-axis returning a double between -1 and 1. */
double leftY = controller.getLeftY();

Challenge: upate the MoveRobot() method

Now that you know about the XboxController class, create a new XboxController object in the RomiDrivetrain class as well as a new method called goWithController.

Conditions:

Here is the answer code (the XboxController object is named controller). I also highly suggest testing your code by adding it to the Robot class in the teleopPeriodic() method.

Answer code
public void goWithController(){
    m_leftMotor.set(controller.getLeftY());
    m_rightMotor.set(controller.getRightY());

Optional Challenge: The ArcadeStyleeMoveRobot() Method

Make another method in the RomiDrivetrain class called goWithController1(). This method will act the same as the goWithController() method, but instead of using 2 joysticks, only use 1 joystick.

You will also need the joystick X-axis method.

🕑
XboxController (WPILib API 2023.4.3)
Logo
How to connect controller
Interface once a controller is added
Drawing
Drawing