Skip to content

Commit

Permalink
Commits reflecting Launch branch on old N2-AvionicsFlightSoftware at …
Browse files Browse the repository at this point in the history
…this time
  • Loading branch information
kimanimichael committed Jun 27, 2022
1 parent 4cd860d commit baf9c02
Show file tree
Hide file tree
Showing 15 changed files with 1,175 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# N2-flight-software
## Introduction
This repository contains code used for flight software for Nakuja Rocket N2. The following description provides a detailed
description of program flow, project structure, library dependencies and/or any other necessary documentation needed to run this
code successfully.

## Project structure
***

```asm
├── data
│ └── flightdata.txt
├── include
│ ├── checkState.h
│ ├── defs.h
│ ├── functions.h
│ ├── kalmanfilter.h
│ ├── logdata.h
│ ├── readsensors.h
│ └── transmitwifi.h
├── lib
│ └── README
├── LICENSE
├── platformio.ini
├── README.md
├── src
│ └── main.cpp
└── test
└── README
```

### 1. Folders Description
| Folder | Description |
|---|---|
| include | Contains header files |
| lib | Project libraries|
|src| Contains main.cpp source file run by the flight computer |
|test| Contains unit test files |

### 2. Files Description
| File | Description |
| ---------| ----------- |
|defs.h | Contains constants declaration |
|functions.h| Contains general utility functions and prototype function declarations |
|logdata.h | code for SD card mounting and logging |
|checkState.h| Contains state machine logic |
|kalmanfilter.h | Contains kalman filter for accelerometer values |
|readsensors.h | Contains code for sensors on board including the MPU,BMP,GPS |
|transmitwifi.h | Contains WiFi set-up and MQTT code |


## Flight Logic
The flight program is structured as a state machine with the following states: adopted from BPS space
| State | Description | Waiting for event |
|---|---| -----|
| 0 | Pre-Flight Ground | 20m Above Ground Level |
| 1 | Powered Flight | accelerationZ <=2m/s<sup>2</sup> for 0.1s |
| 2 | Coasting | velecity less than zero to detect apogee|
| 3 | Ballistic Descent | 20m displacement below apogee to deploy parachute |
| 4 | Chute Descent| < 20m Above Ground|
| 5 | Post-flight Ground | No more events |


Empty file added data/flightdata.txt
Empty file.
120 changes: 120 additions & 0 deletions include/checkState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef CHECKSTATE_H
#define CHECKSTATE_H

#include "functions.h"
#include "defs.h"

// variable for detected apogee height
float MAX_ALTITUDE = 0;

// This checks that we have started ascent
// If we have a positive 20 metres displacement upwards
int checkInPoweredFlight(float altitude)
{
float displacement = altitude - BASE_ALTITUDE;
if (displacement > GROUND_STATE_DISPLACEMENT)
{
return POWERED_FLIGHT_STATE;
}
else
{
return PRE_FLIGHT_GROUND_STATE;
}
}

// This detects fuel burnout
// if z-acceleration is less than or equals 2m/s^2
int checkForBurnOut(float acceleration)
{
if (acceleration <= 2)
{
return COASTING_STATE;
}
else
{
return POWERED_FLIGHT_STATE;
}
}

// This checks that we have reached apogee
// At apogee velocity is zero so we check for velocity less than or equal to zero
// As redundancy we check if previous altitude is greater than current altitude
int checkForApogee(float velocity, float currentAltitude, float previousAltitude)
{
if (currentAltitude < previousAltitude)
{

MAX_ALTITUDE = currentAltitude;
return BALLISTIC_DESCENT_STATE;
}
else if (velocity <= 0)
{
MAX_ALTITUDE = currentAltitude;
return BALLISTIC_DESCENT_STATE;
}
else
{
return COASTING_STATE;
}
}

// Deploys parachute if we moved down 20 metres below apogee
int deployChute(float altitude)
{
float displacement = MAX_ALTITUDE - altitude;
if (displacement > BELOW_APOGEE_LEVEL_DISPLACEMENT)
{
// Fires ejection charge
ejection();
return CHUTE_DESCENT_STATE;
}
else
{
return BALLISTIC_DESCENT_STATE;
}
}

// This checks that we have reached the ground
// detects landing of the rocket
// TODO: BASE_ALTITUDE might be different from the original base altitude
int checkGround(float altitude)
{
float displacement = altitude - BASE_ALTITUDE;
if (displacement < GROUND_STATE_DISPLACEMENT)
{
return POST_FLIGHT_GROUND_STATE;
}
else
{
return CHUTE_DESCENT_STATE;
}
}

// Updates the state-machine state
// We check if rocket has launched to move from PRE_FLIGHT_GROUND_STATE to POWERED_FLIGHT_STATE
// We check if fuel has been burnt completely to move to COASTING_STATE
// We check if we have reached apogee to move to BALLISTIC_DESCENT_STATE
// We deploy parachute to move to CHUTE_DESCENT_STATE
// We check if we have reached the ground to move to POST_FLIGHT_GROUND_STATE
int checkState(float currentAltitude, float previousAltitude, float velocity, float acceleration, int state)
{
switch (state)
{
case PRE_FLIGHT_GROUND_STATE:
return checkInPoweredFlight(currentAltitude);
case POWERED_FLIGHT_STATE:
return checkForBurnOut(acceleration);
case COASTING_STATE:
return checkForApogee(velocity, currentAltitude, previousAltitude);
case BALLISTIC_DESCENT_STATE:
return deployChute(currentAltitude);
case CHUTE_DESCENT_STATE:
return checkGround(currentAltitude);
case POST_FLIGHT_GROUND_STATE:
return POST_FLIGHT_GROUND_STATE;
default:
return checkInPoweredFlight(currentAltitude);
}
}

#endif
128 changes: 128 additions & 0 deletions include/defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#ifndef DEFINITIONS_H
#define DEFINITIONS_H

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>

#define DEBUG 1
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#define debugf(x, y) Serial.printf(x, y)
#else
#define debug(x)
#define debugln(x)
#define debugf(x, y)
#endif

#define SEA_LEVEL_PRESSURE 102400

// Timing delays
#define SETUP_DELAY 5000

#define SHORT_DELAY 10

#define BAUD_RATE 115200

#define GPS_BAUD_RATE 9600

#define SD_CS_PIN 5

// Pin to start ejection charge
#define EJECTION_PIN 4

const uint8_t GPS_TX_PIN = 17;
const uint8_t GPS_RX_PIN = 16;

const BaseType_t pro_cpu = 0;
const BaseType_t app_cpu = 1;

const char *ssid = "S7 edge";
const char *password = "almg76061";

// MQTT Broker IP address
const char *mqtt_server = "192.168.43.133";

const int MQQT_PORT = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

extern float BASE_ALTITUDE;
extern float MAX_ALTITUDE;

const int PRE_FLIGHT_GROUND_STATE = 0;
const int POWERED_FLIGHT_STATE = 1;
const int COASTING_STATE = 2;
const int BALLISTIC_DESCENT_STATE = 3;
const int CHUTE_DESCENT_STATE = 4;
const int POST_FLIGHT_GROUND_STATE = 5;

const int GROUND_STATE_DISPLACEMENT = 20;
const int BELOW_APOGEE_LEVEL_DISPLACEMENT = 20;

// This struct is used to save all our datapoints.
// It includes rocket altitude, accelerations in the x, y and z directions
// Gryroscope values in the x, y and z direcion
// filtered altitude, velocity and acceleration
// GPS longitude, laltitude and altitude and state
struct LogData
{
uint64_t timeStamp;
float altitude;
float ax;
float ay;
float az;
float gx;
float gy;
float gz;
float filtered_s;
float filtered_v;
float filtered_a;
int state;
float latitude;
float longitude;
float gpsAltitude;
};
// SensorReadings contains the measurement we are getting
// from the sensors bmp and mpu
struct SensorReadings
{
float altitude;
float ax;
float ay;
float az;
float gx;
float gy;
float gz;
};
// GPSReadings contains the gps informations that is
// latitude, longitude, speed, number of satellites and altitude
struct GPSReadings
{
float latitude;
float longitude;
float speed;
int satellites;
float altitude;
};

// FilteredValues contains filtered values from the kalman filter
struct FilteredValues
{
float displacement;
float velocity;
float acceleration;
};
// SendValues contains the data points we will be sending over lora
struct SendValues
{
uint64_t timeStamp;
float altitude;
uint16_t state;
float latitude;
float longitude;
};

#endif
55 changes: 55 additions & 0 deletions include/functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include "defs.h"
#include "readsensors.h"

void ejection();
void ejectionTimerCallback(TimerHandle_t ejectionTimerHandle);

// formats data that we are going to save to SD card
// We save all the data points we are collecting
struct LogData formart_SD_data(SensorReadings readings, FilteredValues filtered_values)
{
struct LogData ld = {0};
ld.altitude = readings.altitude;
ld.ax = readings.ax;
ld.ay = readings.ay;
ld.az = readings.az;
ld.gx = readings.gx;
ld.gy = readings.gy;
ld.gz = readings.gz;
ld.filtered_s = filtered_values.displacement;
ld.filtered_a = filtered_values.acceleration;
ld.filtered_v = filtered_values.velocity;
return ld;
}

// formart_send_data This formats data we are going to send over LoRa
// Currently we are sending altitude, state, timeStamp, longitude and latitude
struct SendValues formart_send_data(LogData readings)
{
struct SendValues sv = {0};
sv.altitude = readings.altitude;
sv.state = readings.state;
sv.timeStamp = readings.timeStamp;
sv.latitude = readings.latitude;
sv.longitude = readings.longitude;
return sv;
}

// get_base_altitude Finds the average of the current altitude from 100 readings
float get_base_altitude()
{
float altitude = 0;
SensorReadings readings;
for (int i = 0; i < 100; i++)
{
readings = get_readings();
altitude = altitude + readings.altitude;
}
altitude = altitude / 100.0;
debugf("Base Altitude is %.3f\n", altitude);
return altitude;
}
#endif
Loading

0 comments on commit baf9c02

Please sign in to comment.