> For the complete documentation index, see [llms.txt](https://wiki.kscucf.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.kscucf.org/projects/knights-air-observation-satellite/kaos-1/software.md).

# Software

Documentation for the software used on KAOS-1.

The KAOS-1 flight software runs on a DOIT ESP32 DEVKIT V1 using the Arduino ESP32 core, which includes FreeRTOS. All code is in the [KAOS-1 repository](https://github.com/UCF-Knights-Satellite-Club/KAOS-1).

The LightAPRS tracker runs its own firmware and transmits position and telemetry to the APRS network independently of the flight computer. See [Electronics](https://wiki.kscucf.org/projects/knights-air-observation-satellite/kaos-1/electronics.md).

## Heritage from TSat

The KAOS-1 flight software was not written from scratch. On February 13, 2025, the [TSat-0 flight software](https://wiki.kscucf.org/projects/tethered-satellite/t-sat-0/software.md) was copied into the KAOS-1 repository as its starting point, a month after it flew on TSat-0. Both satellites use the same ESP32 devkit, BMP388 barometer, MMA8451 accelerometer, Arducam Mega cameras, and SD card, so most of the code carried over unchanged.

**Kept from TSat-0:**

* The five-state flight state machine (calibration, preflight, ascent, free fall, landing), including the same free-fall detection logic and filters.
* The three FreeRTOS tasks (Check Altitude, Camera Capture, Log Data) and their priorities. This structure came out of TSat-0's December 2024 rework, when image saving was starving the other tasks.
* The mutex around the shared SPI bus and the queue that lets the altitude task hand data to the logging task without waiting on the SD card.

**Changed for KAOS-1:**

* The parachute servo code was removed. A balloon payload's parachute is rigged in line and doesn't need to be deployed, so the LANDING state sounds a buzzer instead to help the recovery team find the payload.
* Logs go to `KAOS-logN` folders instead of TSat-0's `tsatlogN`.
* New sensors and inputs for the 4U stack: the SCD30 CO2, temperature, and humidity sensor, a real-time clock, a thermistor, and battery voltage monitoring.
* Pin assignments for the four-board PCB stack, with the SD card on its own SPI bus (HSPI) and pins for a second camera.
* A lower descent-speed threshold for detecting free fall (3 m/s instead of TSat-0's 5 m/s).

**Not carried over:** TSat-1's later fixes to the TSat-0 code (June 2025) were made after KAOS-1 copied it. These were faster image transfers (16 MHz SPI with a larger image buffer) and slowing the I2C bus to 50 kHz to fix sensor errors (see [TSat-1 Software](https://wiki.kscucf.org/projects/tethered-satellite/t-sat-1/software.md)). KAOS-1 kept the slower SPI clock and the default I2C speed. They are worth bringing into KAOS-2.

## Libraries

Unless stated otherwise, all libraries can be found in the Arduino IDE Library Manager.

* **Arduino ESP32 Core** ([installation instructions](https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html))
* **Adafruit BMP3XX Library** by Adafruit
* **Adafruit SCD30** by Adafruit
* **RTClib** by Adafruit
* **Adafruit MMA8451 Library** by Adafruit
* **Arducam\_Mega** by Arducam

## Flight states

The flight software is a state machine with five states:

1. **CALIBRATION** averages 20 altitude readings to set the launch altitude.
2. **PREFLIGHT** waits on the ground. High acceleration, or climbing past a small altitude threshold, moves it to ASCENT.
3. **ASCENT** monitors acceleration and descent rate to detect free fall, when the accelerometer reads close to zero.
4. **FREEFALL** compares altitude with the configured landing altitude.
5. **LANDING** is the final state. It sounds a buzzer to help the recovery team find the payload.

## Tasks

The software uses three FreeRTOS tasks, all on one ESP32 core:

| Task           | Priority      | What it does                                                                                |
| -------------- | ------------- | ------------------------------------------------------------------------------------------- |
| Check Altitude | 2 (highest)   | Every 50 ms, reads the sensors, runs the state machine, and queues a data point for logging |
| Log Data       | 1             | Every 100 ms, writes queued data points to the SD card                                      |
| Camera Capture | Idle (lowest) | Takes pictures continuously and saves them to the SD card                                   |

The cameras and SD card share SPI resources, so a mutex keeps the tasks from using them at the same time. The log queue holds 50 entries; if it fills up, new data points are dropped instead of blocking the altitude task.

The version of the code in the repository drives one camera; the code for the second camera is commented out.

## Output files

Each run creates a new folder on the SD card named `KAOS-logN`, with N incremented each run. Sensor data goes to `data.csv` and pictures to `picN.jpg`.

## Repository layout

| Folder                   | Contents                                                                             |
| ------------------------ | ------------------------------------------------------------------------------------ |
| `flight/KAOS-1-FSW`      | Flight software                                                                      |
| `flight/electronics`     | EAGLE schematics and board files for the four PCBs                                   |
| `ground/hardwareTesting` | Test programs: camera test, data logging test, flatsat, and a working LightAPRS test |
| `kaos-archive`           | Earlier sensor test and SD card reading sketches                                     |
