= 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: 1. The program you write must be transferred to the robot's computer (the roboRIO) where it will run 2. FIRST and WPI provide a framework that your robot program must run within; the framework manages many aspects of the robot for you. 3. WPI also provides a rich library of pre-written classes called WPIlib that makes robot programming faster and easier. 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 [wiki://ControlSystems/IntroRobotJava/XboxExample this] * Use the WPILib icon to build your new program: W->Build Robot Code * Connect to the Peanut WiFi Hotspot * 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. == 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 [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]. * 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]. * 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 [https://wpilib.screenstepslive.com/s/4485/m/24192/l/144976-frc-driver-station-powered-by-ni-labview 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 [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 * Create another WPILib project based on the !TimedRobot framework * Study an [wiki://ControlSystems/IntroRobotJava/HazelnutSample1 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 * More sample code for Hazelnut (peanut robot) is [wiki:ControlSystems/SampleCode/Hazelnut here] * A sample program to use ultrasonic sensors is [wiki:ControlSystems/SampleCode/Ultrasonic here] * A sample program to use xbox controllers is [wiki:ControlSystems/SampleCode/XboxController here] * You can learn more about FRC robot programming [https://frc-pdr.readthedocs.io/en/latest/index.html here] * You can learn more about game controllers [https://github.com/MTHSRoboticsClub/Documentation/wiki/Drive-Controller-Comparison here] * Note: you can also program and operate a peanut robot wirelessly: * Find the Peanut's !WiFi access point and connect to it; the default password is "password" * From within VSCode, choose WPILib->Deploy Robot Code * Launch the Driver Station * Enable and then start driving! * 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