28 | | |
29 | | == Start with an example |
30 | | The best way to learn about robot programming a robot is to start with a simple example program, run it, study what it does, and incrementally expand its functionality. The WPILib software you installed includes example programs that you can compile and run on a RoboRIO. Review [https://wpilib.screenstepslive.com/s/currentCS/m/79833/l/941601-vs-code-basics-and-wpilib-in-vs-code how to use VSCode] and then try [https://wpilib.screenstepslive.com/s/currentCS/m/79833/l/932465-creating-a-new-wpilib-project-in-vs-code creating a new robot project]. |
31 | | |
32 | | * WPILib provides [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599697-choosing-a-base-class 3 frameworks] for building robot programs. Build your first program using the !TimedRobot (previously had been !IterativeRobot) framework as described [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/145307-creating-your-benchtop-test-program here]. |
33 | | |
34 | | * Connect your laptop to the RoboRIO USB port |
35 | | * Connect your joystick or xbox controller to another USB port on your laptop |
36 | | * Launch the NI Driver Station Software. For details see [https://wpilib.screenstepslive.com/s/4485/m/24192/l/144976-frc-driver-station-powered-by-ni-labview here] |
37 | | * Confirm that the software detects your joystick/controller and responds to button presses and stick movement |
38 | | * 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 |
39 | | * Create a test project and follow [https://wpilib.screenstepslive.com/s/currentCS/m/79833/l/932465-creating-a-new-wpilib-project-in-vs-code these instructions] to build, load, and run it on your RoboRIO/Robot |
40 | | |
41 | | **Congratulations**, you've just built and run your first robot program. |
42 | | |
43 | | * Create another WPILib project based on the !TimedRobot framework |
44 | | * Study an example program. This is a simple program for one of our test robots: Peanut 2. |
45 | | * 2-wheel-drive (2WD) robot with two CIM motors driving plaction wheels |
46 | | * 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) |
47 | | * An ultrasonic sensor for forward collision avoidance connected to roboRIO digital I/O pins (DIO) 0 and 1 |
48 | | * A USB camera so you can drive even when you can't see the robot |
49 | | * The driver controls the robot using the two analog sticks on an X-box game controller |
50 | | * Copy the program below over the default Robot.java created by the framework |
51 | | * Use the WPILib->Manage Vendor Libraries->Install New Libraries Offline->select the CTRE Framework |
52 | | {{{ |
53 | | |
54 | | package frc.robot; |
55 | | |
56 | | // Import the classes of objects you will use |
57 | | import edu.wpi.first.wpilibj.TimedRobot; |
58 | | import edu.wpi.first.wpilibj.XboxController; |
59 | | import edu.wpi.first.wpilibj.GenericHID.Hand; |
60 | | import edu.wpi.first.wpilibj.Timer; |
61 | | import edu.wpi.first.wpilibj.drive.DifferentialDrive; |
62 | | import com.ctre.phoenix.motorcontrol.can.*; |
63 | | import edu.wpi.first.wpilibj.Ultrasonic; |
64 | | import edu.wpi.first.wpilibj.CameraServer; |
65 | | |
66 | | import edu.wpi.first.wpilibj.livewindow.LiveWindow; |
67 | | |
68 | | import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; |
69 | | import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
70 | | |
71 | | /** |
72 | | * The VM is configured to automatically run this class, and to call the |
73 | | * functions corresponding to each mode, as described in the TimedRobot |
74 | | * documentation. If you change the name of this class or the package after |
75 | | * creating this project, you must also update the build.gradle file in the |
76 | | * project. |
77 | | */ |
78 | | public class Robot extends TimedRobot { |
79 | | // Create instances of each object |
80 | | private static final String kDefaultAuto = "Default"; |
81 | | private static final String kCustomAuto = "My Auto"; |
82 | | private String m_autoSelected; |
83 | | private final SendableChooser<String> m_chooser = new SendableChooser<>(); |
84 | | private XboxController xbox; |
85 | | private Timer timer; |
86 | | private WPI_TalonSRX m_rearLeft; |
87 | | private WPI_TalonSRX m_rearRight; |
88 | | private DifferentialDrive m_drive; |
89 | | private boolean f_safetyStop; |
90 | | private Ultrasonic f_ultrasonic; |
91 | | |
92 | | /** |
93 | | * This function is run when the robot is first started up and should be |
94 | | * used for any initialization code. |
95 | | */ |
96 | | @Override |
97 | | public void robotInit() { |
98 | | // initialize the objects and connect them to their underlying hardware |
99 | | m_chooser.addDefault("Default Auto", kDefaultAuto); |
100 | | m_chooser.addObject("My Auto", kCustomAuto); |
101 | | SmartDashboard.putData("Auto choices", m_chooser); |
102 | | xbox = new XboxController(0); |
103 | | timer = new Timer(); |
104 | | m_rearLeft = new WPI_TalonSRX(4); |
105 | | m_rearRight = new WPI_TalonSRX(3); |
106 | | m_drive = new DifferentialDrive(m_rearLeft, m_rearRight); |
107 | | f_ultrasonic = new Ultrasonic(1,0); // ping, echo |
108 | | f_ultrasonic.setAutomaticMode(true); |
109 | | f_ultrasonic.setEnabled(true); |
110 | | CameraServer.getInstance().startAutomaticCapture(); |
111 | | } |
112 | | |
113 | | /** |
114 | | * This function is called every robot packet, no matter the mode. Use |
115 | | * this for items like diagnostics that you want ran during disabled, |
116 | | * autonomous, teleoperated and test. |
117 | | * |
118 | | * <p>This runs after the mode specific periodic functions, but before |
119 | | * LiveWindow and SmartDashboard integrated updating. |
120 | | */ |
121 | | @Override |
122 | | public void robotPeriodic() { |
123 | | } |
124 | | |
125 | | /** |
126 | | * This autonomous (along with the chooser code above) shows how to select |
127 | | * between different autonomous modes using the dashboard. The sendable |
128 | | * chooser code works with the Java SmartDashboard. If you prefer the |
129 | | * LabVIEW Dashboard, remove all of the chooser code and uncomment the |
130 | | * getString line to get the auto name from the text box below the Gyro |
131 | | * |
132 | | * <p>You can add additional auto modes by adding additional comparisons to |
133 | | * the switch structure below with additional strings. If using the |
134 | | * SendableChooser make sure to add them to the chooser code above as well. |
135 | | */ |
136 | | @Override |
137 | | public void autonomousInit() { |
138 | | m_autoSelected = m_chooser.getSelected(); |
139 | | // autoSelected = SmartDashboard.getString("Auto Selector", |
140 | | // defaultAuto); |
141 | | System.out.println("Auto selected: " + m_autoSelected); |
142 | | timer.reset(); |
143 | | timer.start(); |
144 | | } |
145 | | |
146 | | /*public void teleopInit() { |
147 | | |
148 | | }*/ |
149 | | |
150 | | /** |
151 | | * This function is called periodically during autonomous. |
152 | | */ |
153 | | @Override |
154 | | public void autonomousPeriodic() { |
155 | | if (timer.get() < 2.0) { |
156 | | m_drive.curvatureDrive(0.1, 0.0, false); |
157 | | } |
158 | | else |
159 | | { |
160 | | m_drive.curvatureDrive(0.0, 0.0,false); |
161 | | } |
162 | | switch (m_autoSelected) { |
163 | | case kCustomAuto: |
164 | | // Put custom auto code here |
165 | | break; |
166 | | case kDefaultAuto: |
167 | | default: |
168 | | // Put default auto code here |
169 | | break; |
170 | | } |
171 | | } |
172 | | |
173 | | /** |
174 | | * This function is called periodically during operator control. |
175 | | */ |
176 | | @Override |
177 | | public void teleopPeriodic() { |
178 | | |
179 | | // Use front-mounted ultrasonic sensor to stop robot |
180 | | // if it gets too close to an obstacle |
181 | | double f_range = f_ultrasonic.getRangeInches(); |
182 | | if (f_ultrasonic.isRangeValid()) { |
183 | | if ((f_range < 15.0) && !f_safetyStop) { |
184 | | System.out.println("Safety stopped due to front obstacle"); |
185 | | f_safetyStop = true; |
186 | | } else if (f_range >= 15.0 && f_safetyStop) { |
187 | | System.out.println("Resuming..."); |
188 | | f_safetyStop = false; |
189 | | } |
190 | | } |
191 | | |
192 | | // Use controller joysticks to set drive speed, but |
193 | | // safety stop if too close to an obstacle |
194 | | double leftSpeed = -0.5*xbox.getY(Hand.kLeft); |
195 | | double rightSpeed = -0.5*xbox.getY(Hand.kRight); |
196 | | // If there's an obstacle in front of us, don't |
197 | | // allow any more forward motion |
198 | | if (f_safetyStop && |
199 | | (leftSpeed > 0.0) && (rightSpeed > 0.0)) { |
200 | | m_drive.stopMotor(); |
201 | | } else { |
202 | | // otherwise, set motors according to joysticks |
203 | | m_drive.tankDrive(leftSpeed, rightSpeed); |
204 | | } |
205 | | Timer.delay(0.01); |
206 | | } |
207 | | |
208 | | /** |
209 | | * This function is called periodically during test mode. |
210 | | */ |
211 | | @Override |
212 | | public void testPeriodic() { |
213 | | // LiveWindow.run(); |
214 | | } |
215 | | } |
216 | | |
217 | | }}} |
218 | | |
219 | | * Sample code for Hazelnut (peanut robot) is [wiki:ControlSystems/SampleCode/Hazelnut here] |
220 | | * A sample program to use ultrasonic sensors is [wiki:ControlSystems/SampleCode/Ultrasonic here] |
221 | | * A sample program to use xbox controllers is [wiki:ControlSystems/SampleCode/XboxController here] |
222 | | * You can learn more about FRC robot programming [https://frc-pdr.readthedocs.io/en/latest/index.html here] |
223 | | * You can learn more about game controllers [https://github.com/MTHSRoboticsClub/Documentation/wiki/Drive-Controller-Comparison here] |
224 | | |
225 | | * Note: you can also program and operate a peanut robot wirelessly: |
226 | | * Find the Peanut's !WiFi access point and connect to it; the default password is "password" |
227 | | * From within VSCode, choose WPILib->Deploy Robot Code |
228 | | * Launch the Driver Station |
229 | | * Enable and then start driving! |
230 | | |
231 | | * Note: the sample program assumes two !TalonSRX motor controllers at IDs 3 and 4. You can set them using the CTRE Phoneix Tuner |
232 | | * Note: the !RoboRIO web web interface is available via browser at 10.25.37.2 |