Main - Terrapin-Rocket-Team/SRAD-Avionics GitHub Wiki

Home/Documentation/Main

Main (main.cpp)

This is our version of the ubiquitous main.cpp file. It is the entry point of the program and has a classic Arduino setup() and loop() structure. As the names might suggest, setup() is called once at the beginning of the program, and loop() is called ad infinitum, or until power is cut to the board.

Before setup()

First, it includes the necessary sensors and State class. It also includes the RecordData library, which is used to save the data to the SD card, and the BlinkBuzz library, which is used to indicate events that happen during the program to a user for debugging.

Next, it initializes the sensors, radio, and State class, and sets up the BlinkBuzz library. It #defines a few important constants, and indicates that it's going to later reference internal variables from Teensy for memory debugging (the _heap_start, _heap_end, and *__brkval variables).

Lastly, it defines a function called FreeMem() that prints out the amount of free memory on the Teensy. This is used to help debug memory leaks.

setup()

This is where we set up pins, the RecordData library, and State,

The pins need to be told whether they are input or output. Once that's done, we write the necessary values to the output pins to set up the sensors and the Raspberry Pi-based video recording system.

The RecordData library is set up by first attempting to start the SD card, then the PSRAM. If either of these fail, the program will print an error message.

Finally, the State class is set up. The sensors are added to it, and it is initialized. If the sensors fail to initialize, the program will print an error message.

The last line sends the finished CSV header to the SD card. This is done last because we need to know which sensors are working before including their columns in the header.

loop()

This is the meat of the program. It updates the state, transmits data, and handles the BlinkBuzz library. It also checks for a few time based events to trigger the Raspberry Pi to start recording.

The first line is a call to bb.update(). This is important for asynchronous buzzing of blinking events so that they can be turned off after they've started.

The next step is looking at the Radio. It attempts to transmit every half a second, and if the string is too long, it will transmit twice.

Next comes the State update. Every 100ms, it asks the state to update itself. This is where the sensors are queried, the data is fused together, and the state is updated, including determining which stage the rocket is in.

The last part is the check for time-based events. If the time is right, it will send a signal to the Raspberry Pi to start recording video.

See Also

⚠️ **GitHub.com Fallback** ⚠️