- Overview
- Design
- System Architecture Flowchart
- Launch Data
- File Structure
- Quick Start
- Local Setup
- Advanced Local Usage
- Pi Usage
- Contributing
- License
This project is for controlling our Air brakes system with the goal of making our rocket "hit" its target apogee. We have a Raspberry Pi 4 as the brains of our system which runs our code. It connects to a servo motor to control the extension of our air brakes and an IMU (basically an altimeter, accelerometer, and gyroscope). The code follows the finite state machine design pattern, using the AirbrakesContext
to manage interactions between the states, hardware, logging, and data processing.
AirBrakes.mp4
A video of our air brakes extending and retracting
As per the finite state machine design pattern, we have a context class which links everything together. Every loop, the context:
- Gets data from the IMU
- Processes the data in the Data Processor (calculates velocity, averages, maximums, etc.)
- Updates the current state with the processed data
- Controls the servo extension based on the current state's instructions (e.g., extends air brakes to slow down the rocket)
- Logs all data from the IMU, Data Processor, Servo, and States
%%{init: {'theme': 'dark'}}%%
flowchart TD
%% Define styles for different shapes
classDef circular fill:#44007e,stroke:#fff,stroke-width:2px,rx:50%,ry:50%; %% Ovals for classes
classDef bubble fill:#164b6c,stroke:#fff,stroke-width:2px,rx:10px,ry:10px; %% Bubbles for methods
classDef outputSquare fill:#044728,stroke:#fff,stroke-width:2px; %% Squares for outputs
classDef textStyle fill:none,color:#fff,font-weight:bold,font-size:16px; %% Text style for bold white text
classDef mainColor fill:#ac6600,stroke:#fff,stroke-width:2px,rx:50%,ry:50%; %% Color for main.py
%% Main structure with main.py at the top
subgraph mainFlow[Main Flow]
direction TB
mainpy((main.py)):::mainColor --> Airbrakes((Airbrakes Context)):::circular
end
Airbrakes --> Update[update]:::bubble
%% IMU Data Packet Flow
IMUDataPacket --> Update
Apogee_Predictor --> Update
%% States as individual nodes
State((State)):::circular
CoastState((Coast)):::circular
Standbystate((Standby)):::circular
FreefallState((Freefall)):::circular
LandedState((Landed)):::circular
MotorBurnState((Motor Burn)):::circular
%% Connections between States and Airbrakes
State((State)):::circular --> Airbrakes:::circular
State --> Update
CoastState((Coast)):::circular --> State
Standbystate((Standby)):::circular --> State
FreefallState((Freefall)):::circular --> State
LandedState((Landed)):::circular --> State
MotorBurnState((Motor Burn)):::circular --> State
%% Connections with Labels
Airbrakes ---|Child Process| Logger((Logger)):::circular
Airbrakes ---|Child Process| IMU((IMU)):::circular
Airbrakes ---|Child Process| Apogee_Predictor((Apogee Predictor)):::circular
IMU((IMU)):::circular ---|Fetch Packets| IMUDataPacket[(IMU Data Packet)]:::outputSquare
%% IMU Data Processing
IMUDataPacket --> IMUDataProcessor[IMU Data Processor]:::circular
IMUDataProcessor --> Velocity[(Velocity)]:::outputSquare
IMUDataProcessor --> Altitude[(Altitude)]:::outputSquare
IMUDataProcessor --> Rotated_Accel[(Rotated Acceleration)]:::outputSquare
Velocity --> ProcessedData[(Processor Data Packet)]:::outputSquare
Altitude --> ProcessedData[(Processor Data Packet)]:::outputSquare
Rotated_Accel --> ProcessorData[(Processor Data Packet)]:::outputSquare
ProcessorData[(Processor Data Packet)]:::outputSquare --> Update
%% Logging Mechanism
Logger --> LogFunction[log]:::bubble
Update --> LogData[(Logger Data Packet)]:::outputSquare
LogData --> LogFunction
%% Airbrake Control Methods with Parentheses Displayed
Update --> ExtendAirbrakes[extend_airbrakes]:::bubble
Update --> RetractAirbrakes[retract_airbrakes]:::bubble
%% Servo Connections
RetractAirbrakes --> Servo((Servo)):::circular
ExtendAirbrakes --> Servo
Airbrakes --> Servo
This is our interest launch flight data, altitude over time. The different colors of the line are different states the rocket goes through:
- Stand By - when the rocket is on the rail on the ground
- Motor Burn - when the motor is burning and the rocket is accelerating
- Coast - when the motor has burned out and the rocket is coasting, this is when air brakes will be deployed
- Free Fall - when the rocket is falling back to the ground after apogee, this is when the air brakes will be retracted
- Landed - when the rocket has landed on the ground
We have put great effort into keeping the file structure of this project organized and concise. Try to be intentional on where you place new files or directories.
AirbrakesV2/
├── airbrakes/
| ├── hardware/
│ │ ├── [files related to the connection of the pi with hardware ...]
| ├── mock/
│ │ ├── [files related to the connection of mock (or simulated) hardware ...]
| ├── simulation/
│ │ ├── [files related to our custom air brakes sim ...]
| ├── telemetry/
│ │ ├── [files related to the processing of data ...]
│ ├── [files which control the airbrakes at a high level ...]
| ├── main.py [main file used to run on the rocket]
| ├── constants.py [file for constants used in the project]
├── tests/ [used for testing all the code]
│ ├── ...
├── logs/ [log files made by the logger]
│ ├── ...
├── launch_data/ [real flight data collected from the rocket]
│ ├── ...
├── scripts/ [small files to test individual components like the servo]
│ ├── ...
├── pyproject.toml [configuration file for the project]
├── README.md
This project strongly recommends using uv
to manage and install
the project. To quickly run the mock replay, simply run:
uvx --from git+https://github.com/NCSU-High-Powered-Rocketry-Club/AirbrakesV2.git mock
You should see the mock replay running with a display!
Note: We will continue using uv
for the rest of this README, if you don't want to use uv
, you can set up the project using Python. See Legacy Project Setup for more information.
If you want to contribute to the project, you will need to set up the project locally. Luckily,
the only other thing you need to install is git
for version control.
git clone https://github.com/NCSU-High-Powered-Rocketry-Club/AirbrakesV2.git
cd AirbrakesV2
uv run mock
This will install the project, including development dependencies, activate the virtual environment and run the mock replay.
Note: It is important to use uv run
instead of uvx
since the uvx
environment is isolated from
the project. See the uv documentation for more information.
Note 2: The more "correct" command to run is uv sync
. This will install the project and its dependencies, but not run the mock replay.
uv run pre-commit install
This will install a pre-commit hook that will run the linter before you commit your changes.
After you have made your changes, you can commit them:
git add .
git commit -m "Your commit message"
You will see the linter run now. If one of the checks failed, you can resolve them by following the instructions in Running the Linter.
git push -u origin branch-name
Testing our code can be difficult, so we've developed a way to run mock launches based on previous flight data--the rocket pretends, in real-time, that it's flying through a previous launch.
To run a mock launch, run:
uv run mock
If you want to run a mock launch, but with the real servo running, run:
uv run mock -r
To run a mock launch with our custom simulation:
uv run sim
There are some additional options you can use when running a mock launch. To view them all, run:
uv run mock --help
Our CI pipeline uses pytest to run tests. You can run the tests locally to ensure that your changes are working as expected.
Note: Unit tests do not work on Windows (only test_integration.py
will work).
To run the tests, run this command from the project root directory:
uv run pytest
If your virtual environment is activated, you can simply run the tests with pytest
To generate a coverage report from the tests:
pytest --cov=airbrakes --cov-report=term
If you make a change to the code, please make sure to update or add the necessary tests.
Our CI also tries to maintain code quality by running a linter. We use Ruff.
To run the linter, and fix any issues it finds, run:
ruff check . --fix --unsafe-fixes
To format the code, run:
ruff format .
There are libraries that only fully work when running on the Pi (gpiozero, mscl, picamera2), so if you're having trouble importing them locally, program the best you can and test your changes on the pi.
In order to connect to the Pi, you need first to set up a mobile hotspot with the name HPRC
, password tacholycos
, and 2.4 GHz
band. Next, turn on the Pi and it will automatically connect to your hotspot. Once it's connected, find the Pi's IP Address, and in your terminal run:
ssh pi@[IP.ADDRESS]
# Its password is "raspberry"
cd AirbrakesV2/
Every time the pi boots up, you must run this in order for the servo to work. We have already added this command to run on startup, but you may want to confirm that it is running, e.g. by using htop
.
sudo pigpiod
sudo apt install libcap-dev libcamera-dev libkms++-dev libfmt-dev libdrm-dev
uv sync --all-groups
uv run real
During development, you may want to run individual scripts to test components. For example, to test the servo, run:
# Make sure you are in the root directory:
uv run scripts/run_servo.py
Feel free to submit issues or pull requests. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License. You are free to copy, distribute, and modify the software, provided that the original license notice is included in all copies or substantial portions of the software. See LICENSE for more.