can anyone check my rocket flight computer(RFC) if its good

The Rocketry Forum

Help Support The Rocketry Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

asadnemer231

Member
Joined
Dec 29, 2023
Messages
8
Reaction score
1
Location
beirut
i want any device to improve my code please.
RFC hardware: teensy 4.1&bmp280 & mpu9250 & pa1616s GPS & 4 pyro channel & 2 servo output & tri-color led & buzzer.

note: the code is attached
and its Arduino ide format.
update: i put it on txt format also
 

Attachments

  • new 3.txt
    17.9 KB · Views: 1
There is really too much code to easily go through it.
A quick scroll and I didn't see anything jump out.

You really need to Test the code both on the ground and in a rocket.
Work through all the code should do and the many scenarios that could exist during a flight. Then work out a Test Plan to ensure the features work as designed.

When you put this in a rocket do NOT plan on it doing everything correctly. Do not use it as the ONLY way to deploy recovery (have at least motor eject get a chute out).

At last months launch a person was going for they L1 cert with a DIY flight controller. She did have motor eject backup and it was what got the chute out. The deployment charges from the DIY controller did NOT Fire. If motor eject didn't get the chute out then this would have been a catastrophic failure.
 
There is really too much code to easily go through it.
Agreed. A few things caught my eye:

1) hardwired pressure at line 252. You should be doing everything relative to the pressure at startup with no need for an absolute pressure value.

2) isApogee, line 557, always returns false?

3) you're detecting flight state by looking at a digital signal? What's the plan there, a breakwire or something? You'd be better off detecting launch with acceleration or altitude.

4) You're relying on the BMP280's internal filter instead of doing your own? Maybe OK, maybe not.

5) You deploy the chute when currentAltitude > PARACHUTE_DEPLOYMENT_ALTITUDE? How does this not happen on ascent? Don't you want currentAltitude < PARACHUTE_DEPLOYMENT_ALTITUDE after detected apogee, maybe with some velocity threshold for mach lockout?

I don't know how you plan to test this. I like to do two things: first test the logic with simulated sensor inputs, derived from a flight sim or something, for lots of cases. Then figure out some way of doing a bench test, maybe by using a much lower threshold for launch detect and/or a fake multiplier for acceleration, and jerking and moving the system around to roughly simulate flight. Vacuum chamber for barometer, but it might be best to not rely on barometer or use sensor fusion with baro and accel.
 
Code:
void setup() {
  Serial.begin(115200);
  while (!Serial) {}
  ...
}
Serial is not gonna be set when your flight computer is not plugged into USB. Consider looking into precompiler stuff (ie, "#define debug", "#ifdef debug", etc) so that when you aren't building this for debugging, it won't break your code. Otherwise your computer is just going to get stuck on that while loop and you'll have a guaranteed lawn dart.

Code:
float currentAltitude = bmp.readAltitude(1019);  // Replace 1019 with your local pressure for accurate altitude readings
You'd need better input than a statically coded pressure. Barometric pressure can shift a lot over the course of the day so unless you plan on compiling your code right before flight, it won't be accurate. The better way to do it is take a reading at ground level/before launch and then take the difference between that and the latest altitude reading. That will give you the altitude above ground level which is what you need for safe deployment using a barometer.

Code:
254         if (currentAltitude > PARACHUTE_DEPLOYMENT_ALTITUDE) {
255             // Check if the parachute has not been deployed yet to avoid multiple deployments
256             if (!parachuteDeployed) {
257                 // Check for safety conditions before deploying the parachute
258                 if (isSafeToDeployParachute()) {
259                     // Deploy the parachute using pyro1
260                     deployParachute();
261                     parachuteDeployed = true;
262                 } else {
263                     Serial.println("Parachute deployment conditions not met. Safety check failed.");
264                 }
265             }
266         } else {
267             // Reset the flag when the altitude drops below the deployment threshold
268             parachuteDeployed = false;
269         }
On accent, this will deploy your parachute prematurely and you'll get a zippered rocket. What you need to do is a range: if you want to deploy at 300ft, your "currentAltitude" must be less than 325ft and greater than 275ft. That way you don't have to hit the specific altitude to a decimal point. Also, it doesn't really matter if you hit the deploy parachute trigger multiple times within an acceptable window because once the pyro, cord cutter, or chute release has deployed, you'll need to physically reinstall it for it to deploy again.
You may also want to monitor the vertical velocity because if it's not moving down, you absolutely should never deploy (it's a great safety... I have several programmed into my flight computer.) Also, if you're trying to deploy at apogee, you might not be able guaranteed to reach the desired altitude if you preprogram it. Sims are often right up to a certain margin. Ive gotten pretty good at accounting for everything on my rockets, but I still get about a 5% difference from the simulated altitude. You will have to monitor when you launch, and when you fall below apogee. The former is so you don't accidentally trigger your ejection charge by lifting and lowering your rocket on the ground, and the latter so that no matter what apogee you reach, it deploys after apogee.


Code:
273 bool isSafeToDeployParachute() {
274     // Implement safety checks here
275     // For example, check for noise voltage or other false triggers
276     float noiseVoltage = readNoiseVoltage();  // Implement a function to read noise voltage
277   
278     if (noiseVoltage < MAX_NOISE_VOLTAGE) {
279         return true;  // It's safe to deploy the parachute
280     } else {
281         Serial.println("Unsafe to deploy parachute. High noise voltage detected.");
282         return false;
283     }
284 }
285
286 float readNoiseVoltage() {
287     // Implement a function to read noise voltage
288     // Replace A1 with the actual analog pin used for measuring noise voltage
289     int sensorValue = analogRead(A1);
290     // Replace 1023 with the maximum value your analog-to-digital converter can produce
291     float voltage = sensorValue * (MAX_VOLTAGE / 1023.0);
292     return voltage;
293 }
What does "Noise Voltage" have to do with your parachute safety? You should check if the rocket is above the ground, falling, and is moving at a speed that is safe for parachute deployment (that can vary on the composition of the rocket because some tubes are more resilient than others; I use plastic/printed rockets and fiberglass and deploy around 30fps... ymmv).

Code:
356 void adjustFins() {
Motorized fins are cool, but get the basics first. Dynamic stabilization is notoriously difficult to do from what I have read. You need to figure out safe deployment first and demonstrate it works on your rocket.

Another recommendation, implement something like a Kalman filter. You can get away with a rolling average (and I have my own way of doing it), but Kalman is the way to go. It's lightweight, low-impact. Your sensor data is going to vary a little if it's not preprocessed.

Also, consider looking minimizing the data footprint. Rather than storing information as raw text in a file, learn how to write and read byte files. It helps a ton if you want to consider live telemetry.
 
I remember evaluating similar code for a STM-32 Flight Controller 3 years ago. Only half of the necessary code to make this an operational flight controller is present. Your code requires "Interrupt" routines to control the different operations, calibration routines for the MPU-9250, servo calibration routines to set fin position to zero, and several more VOID routines that are empty.

My recommendation for you is to Google SparkyVT github. SparkyVT is a member of The Rocketry Forum. He has published flight proven code that is the best example to follow. His code is fast and written for the Teensy 4.1. It does lack, at present, servo control routines.
 
Back
Top