Version 39 (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.
- Xbox Controller
- Ultrasonic Rangefinder
- Servo Motor
- DC Motor Control
- Encoders
- Autonomous
- Closed Loop Control
Seventh program: Closed Loop Control
In the last example, we saw why robot programming is challenging: robots work in the real world where wheels slip, motors have different powers, wheels aren't inflated equally, and floors aren't perfectly level. The robot didn't go straight and didn't stop at exactly 36". What makes robots interesting and powerful is their ability to sense their environment and respond to it. In the basic Autonomous example, one wheel was turning faster than the other so the robot turned. Since the encoders were reporting this, we can modify our program to compensate. The simplest way to do this is to proportionally increase the motor power of the wheel that's going slower.
MODIFY the last program as follows:
- Add the following imports:
import edu.wpi.first.wpilibj.Timer;
- Add variables for closed loop control in the Robot class
private double leftPower, rightPower; private Timer autoTimer; private boolean autoDone;
- In robotInit() instantiate the new Timer object
autoTimer = new Timer();
- In robotPeriodic() add the motor power settings to the smart dashboard
SmartDashboard.putNumber("Left power", leftPower); SmartDashboard.putNumber("Right power", rightPower);
- In autonomousInit() initialize the motor powers and start the timer we created
so we can periodically adjust the motor power to keep the robot going straight:
// start motors turning forward at 20% power leftPower = 0.20; leftMotor.set(leftPower); // to go forward, left motor turns clockwise, right motor counter-clockwise rightPower = -0.20; rightMotor.set(rightPower); autoTimer.start(); autoDone = false;
- In the default section of autonomousPeriodic() periodically adjust the motor powers
and stop if we've gone the desired distance.
if (!autoDone) { // adjust motor power every 1/4 second to keep us going straight if (autoTimer.hasPeriodPassed(0.25)) { // if we're going straight, leftDistance == rightDistance // otherwise, compute the error and adjust the motor powers to minimize it double error = leftEncoder.getDistance() - rightEncoder.getDistance(); double kP = 0.01; // proportion of error used to correct motor power // adjust motor power to try to keep left/right distances same leftPower -= kP*error; // positive error means left is going too fast rightPower -= kP*error; // right motor spins opposite direction of left leftMotor.set(leftPower); rightMotor.set(rightPower); } if ((leftEncoder.getDistance() > 36.0) || (rightEncoder.getDistance() > 36.0)) { leftMotor.stopMotor(); rightMotor.stopMotor(); autoDone = true; } }
This is the simplest form of closed-loop control: we are sensing position information (left, right distances) and adjusting powers proportionally (kP) to try to minimize the error. There are more sophisticated ways to do this that consider how quickly the robot is approaching its goal (derivative of error) and how long and how severely it has been off its target (integral of error). The PID control loop uses all of these for more precise and responsive closed loop control.
Notice that there are two critical values in this control loop:
- How often the control loop runs (every 0.25 seconds in the above example)
- How much the control loop adjusts the power based on the sensed inputs (kP=0.01 in the above example)
- Try printing the error and adjusted motor powers using System.out.println(...) to see what adjustments are taking place.
Try experimenting with these values (carefully and changing them a little at a time) to see what effect they have. Be prepared to hit the Disable button or space bar when testing!
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