Version 8 (modified by 6 years ago) (diff) | ,
---|
Java for FRC Robotics
Writing Java programs to control a robot is similar to the Java programming you've already learned; there are three key differences:
- The program you write must be transferred to the robot's computer (the roboRIO) where it will run
- FIRST and WPI provide a framework that your robot program must run within; the framework manages many aspects of the robot for you.
- WPI also provides a rich library of pre-written classes called WPIlib that makes robot programming faster and easier.
First Program: Xbox Controller
We write robot software in VSCode just as we did for Java, but we'll make use of the WPILib extensions to create, build, and run our robot programs. The WPILib extension is available via the W icon in the toolbar. Click on the icon and then:
- Select Create a new project
- Select Project Type->Template->java->Iterative Robot
- Select a New Project Folder and select (or create and then select) Documents\Robot Projects
- Enter project name: Xbox1
- Enter team number: 2537
- Press Generate Project
- At the dialog select Yes (Current WIndow)
The WPILib extension then creates a new Robot project for you. The project contains many files and they're listed and organized in the left tree. The file we're intersted in is src->main->java->Robot.java open that file by double-clicking on it.
The Robot.java class that was created for you contains the minimal set of functions required for a robot program operating in the WPIlib framework. The functions include:
- robotInit() - called once when the robot starts
- robotPeriodic() - called periodically (e.g. every 20ms)
- autonomousInit() called one when the robot first enters autonomous mode
- autonomousPeriodic() - called periodically (e.g. every 20ms) while the robot is in autonomous mode
- teleopPeriodic() - called periodically while robot is in teleoperated mode
- testPeriodic() - called periodically while robot is in test mode
You will put your setup code in the Init() functions and the code that should run repeatedly in the Periodic() functions.
Modify the newly created program as follows:
- Add to the imports
import edu.wpi.first.wpilibj.XboxController; import edu.wpi.first.wpilibj.GenericHID.Hand;
- In the Robot class definition where variables are defined, add
XboxController xbox;
- In the function robotInit() add:
xbox = new XboxController(0);
- In the function robotPeriodic() addd:
SmartDashboard.putNumber("Left Joystick X", xbox.getX(Hand.kLeft));
Your program should look like this?
- Use the WPILib icon to build your new program: W->Build Robot Code
- Connect to the Peanut WiFi Hotspot (the default password is password)
- Use the WPILib icon to deploy your program to the robot: W->Deploy Robot Code
- Launch the FRC Driver Station on your laptop. Two windows should open: FRC Driver Station and FRC PC Dashboard.
- Plug in an x-box controller and make sure it is detected
- Make sure the Communications, Robot Code, and Joysticks LED icons are all green
- In the FRC PC Dashboard, select the Variables tab and scroll down to Smart Dashboard->Left Joystick X (that's the variable you created!)
- Move the left X-box joystick right and left and watch the value shown for Left Joystick X change!
You can even debug your program on the robot using the VSCode debugger just as you did with Java programs running on your laptop:
- Set a breakpoint on the line:
xbox = new XboxController(0);
- Use the WPILib icon to start debugging: W->Debug Robot Code
- The program will run until it gets to that line and then stop. Press the continue button to continue running.
- Even though the program is running on the robot, you can set breakpoints, step through code, examine and change variables, etc. from your laptop.
Second program: Ultrasonic Rangefinder
Create another program using the TimedRobot java template and name it UltrasonicTest. Modify the generated code as follows:
- import the Ultrasonic class
import edu.wpi.first.wpilibj.Ultrasonic;
- declare a ultrasonic variable in the Robot class
private Ultrasonic f_ultrasonic;
- in robotInit() instantiate an ultrasonic object and set it to start automatically ranging
f_ultrasonic = new Ultrasonic(1,0); f_ultrasonic.setAutomaticMode(true);
- in robotPeriodic() read and display the range
if (f_ultrasonic.isRangeValid()) { SmartDashboard.putNumber("Front range", f_ultrasonic.getRangeInches()); }
Run the program and observe the "Front range" value in the Smart Dashboard as you move the robot towards and away from the wall.
Study some examples
Learn more about robot programming by studying some simple example programs. Run them, study what they do, and incrementally expand their functionality. The WPILib software you installed includes example programs that you can compile and run on a RoboRIO. Review how to use VSCode and then try creating a new robot project.
- WPILib provides 3 frameworks for building robot programs. Build your first program using the TimedRobot (previously had been IterativeRobot) framework as described here.
- Connect your laptop to the RoboRIO USB port or WiFi
- Connect your joystick or xbox controller to another USB port on your laptop
- Launch the NI Driver Station Software. For details see here
- Confirm that the software detects your joystick/controller and responds to button presses and stick movement
- Use Microsoft Internet Explorer (not Chrome or Firefox) to connect to the RoboRIO's web interface (usually at http://172.22.11.2) and confirm that you have connectivity with the RobORIO
- Create a test project and follow these instructions to build, load, and run it on your RoboRIO/Robot
- Create another WPILib project based on the TimedRobot framework
- Study an example program. This is a simple program for one of our test robots: Peanut 2 (Hazelnut)
- 2-wheel-drive (2WD) robot with two CIM motors driving plaction wheels
- Motors are controlled by Talon SRX smart motor controllers; these are networked to the roboRIO (robot control computer) using CAN bus and are at addresses 3 (left motor) and 4 (right motor)
- An ultrasonic sensor for forward collision avoidance connected to roboRIO digital I/O pins (DIO) 0 and 1
- A USB camera so you can drive even when you can't see the robot
- The driver controls the robot using the two analog sticks on an X-box game controller
- Copy the program below over the default Robot.java created by the framework
- Use the WPILib->Manage Vendor Libraries->Install New Libraries Offline->select the CTRE Framework
- Study an example program. This is a simple program for one of our test robots: Peanut 2 (Hazelnut)
- More sample code for Hazelnut (peanut robot) is here
- A sample program to use ultrasonic sensors is here
- A sample program to use xbox controllers is here
- You can learn more about FRC robot programming here
- You can learn more about game controllers here
- Note: the sample program assumes two !TalonSRX motor controllers at IDs 3 and 4. You can set them using the CTRE Phoneix Tuner
- Note: the !RoboRIO web web interface is available via browser at 10.25.37.2
Attachments (2)
-
GroundSymbol.png (16.3 KB) - added by 6 years ago.
Ground Symbol
- Auto tutorial.txt (96 bytes) - added by 6 years ago.
Download all attachments as: .zip