FRC 5549 | Programming Division
Chapter 3.1: Basic Structure
RobotPy programs follow a specific code structure that acts as the base of any FRC robot code. The code is sectioned into functions that are called by FMS (Field Management System) when the Driver Station is connected to the field through the ethernet cable at the competitions. However, you can manually call each section through the Driver Station software outside of competitions and during test phases.
Robot Initialization
Called upon robot startup and should be used for constructing robot objects such as motors and sensors. Variables that are used in other functions should also go here.
Autonomous Initialization
Called at the start of the autonomous period. Should be used for actions that need to be initialized specifically for the autonomous phase such as starting the timer and resetting sensor data.
Autonomous Periodic
Called periodically during the autonomous phase. Actions such as robot movement that only happen during autonomous phase should be placed here.
Teleop Initialization
Called during the start of teleoperated phase. Should be used for actions that need to be initialized or happen only once before teleop takes place.
Teleop Periodic
Called periodically during the teleoperated phase and should be utilized for robot movement and data collection from sensors that happen during teleop phase.
**Note: There are more functions and code sections but these are the basic ones that will get you started on your path to program a functional FRC robot.
''' import statements go at the top of the program '''
import wpilib
class MyRobot(wpilib.TimedRobot):
''' robot program starts here '''
def robotInit(self):
''' function that is run at the beginning of the match '''
pass
def autonomousInit(self):
''' function that is run at the beginning of the autonomous phase '''
pass
def autonomousPeriodic(self):
''' function that is run periodically during the autonomous phase '''
pass
def teleopInit(self):
''' function that is run at the beginning of the tele-operated phase '''
pass
def teleopPeriodic(self):
''' function that is run periodically during the tele-operated phase '''
pass
if __name__ == '__main__':
''' running the entire robot program '''
wpilib.run(MyRobot)
Code 3.1: This is basic code struture for FRC programming. At the top of the program, the WPI library is imported which allows the other functions to be recognized. def
declares a function under the overall class called MyRobot. As explained later, self
allows us to turn variables into global ones that are recognized in other functions.
Chapter 3.2: File Structure
While it is possible to write all code in just robot.py, it is recommended to split up the code into subsystems. This allows for the code to be more organized and readable. In addition, variables will only need to be change in one location as opposed to multiple locations.
This is an example of a structure that could be used.
main/
components/
drive.py
vision.py
dashboard.py
subsystem1.py
subsystem2.py
subsystem3.py
robot.py
README.txt
Inside of these files, make a class. For example, drive.py would have a Drive class. Inside of that class, make functions. For example,
forward. Now, this file can be imported into robot.py using import components/drive.py
. Once imported, just call the function in robot.py using
self.drive.forward()
.
import wpilib
class Drive(self):
def robotInit(self):
pass
def forward(self):
pass
Code 3.2: This is an example of a file in the components folder. Variables may also be passed into the function if needed.
Chapter resources: Code Structure