Code structure

Info on the software we use

We write the code that runs on the robot in python, we do this because for a few reasons: firstly it is very common for people to already have experience with python, it is often easier and requires less code to achieve similar results compared to other languages and it has a good developer experience across various devices.

All FRC teams are required to use a library called wpilib, which allows the robot to communicate with the driver station and fms, control motors, read common sensors and many other things. Wpilib is a Java and C++ library, as such most teams use these two languages.

RobotPy is a project that provides python bindings to the C++ version of Wpilib allowing us write python code that calls the C++ code in the background.

If you want API docs (information on the functions and classes you can use) use the RobotPy docs: https://robotpy.readthedocs.io/en/stable/index.html.

If you want information about things general to FRC or robotics use the Wpilib docs: https://docs.wpilib.org/en/stable/index.html


How we Structure our code

We use the MagicBot framework which is a framework for how you should structure your code and some utilities to make it generally better.

Files:

robot.py: This is the file that gets called on the robot, it should have a Robot class that only contains: instantiate components and other objects , and read from joysticks and call basic action methods on controllers and components. The robot class will have some methods that magicbot will call for us such as teleopPeriodic which will be called 50 times a second during teleop and disabledInit which will be called once when the robot is disabled.

components/thing.py: Components represent either physical subsystems or concept such as drivebase, arm, or vision. The components are instanciated in robot.py and have methods to perform actions such as drive(x velocity, y velocity) or shoot. Components also must have an execute method that will be called by magicbot 50 times a second.

controllers/thing_controller.py: Controllers (referred to as high level components by magicbot and automations by some of our old code) are

autonomous/auto.py: uhhhhh

utilities/: Folder for other code such as


(TODO: i feel that things like motors should be instanciated and owned by the component that deals with them, rather than in robot.py and maybe have a devices or robotmap file to make ids easy)

Glossary:

Class: A class is a tool we use to do any of three things: A organizational tool to separate things into conceptual objects, A blueprint to create multiple copies of something by instantiating something multiple times, and to share behavior between multiple objects through inheritance.

Method: A function that is attached to a class.

Member: A variable that is attached to a class.

Library: Code that other people have written to do stuff for us.

Common patterns

State machine controller

Have one or more components with action methods controlled by a controller running a state machine. Can have a superstructure controller to manage interaction between multiple controllers.


Networktables

Printing can be done but it gets lost easily in the console and is slow, instead you can put things on network tables where they are viewable in real time from the driver station and are logged as well (TODO: how to view those logs). With the magicbot framework you can either write a new method with the @feedback decorator and it will automatically we called every control loop with its return value being put on networktables, or you can construct a member variable as a tunable which also puts the variable on networktables but also allows it to be changed from the driver station. You can also manually write log files if you want.