Skip to content

Commit

Permalink
final working code
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmuchyri committed Jan 19, 2023
1 parent 5382186 commit 61d237e
Show file tree
Hide file tree
Showing 19 changed files with 401 additions and 423 deletions.
Empty file removed data/flightdata.txt
Empty file.
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
6 changes: 3 additions & 3 deletions include/checkState.h → include/checkstate.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef CHECKSTATE_H
#define CHECKSTATE_H

int checkInPoweredFlight(float altitude);
//we initialize functions that will be included in checkstate.cpp

int checkForBurnOut(float acceleration);
int checkInPoweredFlight(float altitude);

int checkForApogee(float velocity, float currentAltitude, float previousAltitude);

Expand All @@ -13,4 +13,4 @@ int checkGround(float altitude);

int checkState(float currentAltitude, float previousAltitude, float velocity, float acceleration, int state);

#endif
#endif
73 changes: 43 additions & 30 deletions include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <PubSubClient.h>
#include <Wire.h>

// this will be used in debugging the code by printing out the output
//the final code to be uploaded set DEBUG =0 to disable all debug statements

#define DEBUG 1
#if DEBUG == 1
#define debug(x) Serial.print(x)
Expand All @@ -16,37 +19,53 @@
#define debugf(x, y)
#endif

#define SEA_LEVEL_PRESSURE 102400
//the sea level pressure of the area we are at
#define SEA_LEVEL_PRESSURE 102400

// Timing delays
#define BAUD_RATE 115200
#define GPS_BAUD_RATE 9600
#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
#define EJECTION_PIN 4

//buzzer signal pin
#define buzzer_pin 32

#define PRE_FLIGHT_GROUND_STATE 0
#define POWERED_FLIGHT_STATE 1
#define COASTING_STATE 2
#define BALLISTIC_DESCENT_STATE 3
#define CHUTE_DESCENT_STATE 4
#define POST_FLIGHT_GROUND_STATE 5
//initializing the state machine states
#define PRE_FLIGHT_GROUND_STATE 0
#define POWERED_FLIGHT_STATE 1
#define BALLISTIC_DESCENT_STATE 2
#define CHUTE_DESCENT_STATE 3
#define POST_FLIGHT_GROUND_STATE 4

#define GROUND_STATE_DISPLACEMENT 20
#define BELOW_APOGEE_LEVEL_DISPLACEMENT 20
//define the thresholds of the various displacements that need to be achieved in the state machine
#define GROUND_STATE_DISPLACEMENT 10
#define BELOW_APOGEE_LEVEL_DISPLACEMENT 10

#define GPS_TX_PIN 17
#define GPS_RX_PIN 16
#define GPS_TX_PIN 17
#define GPS_RX_PIN 16

//defining the two CPU cores to be used
extern const BaseType_t pro_cpu;
extern const BaseType_t app_cpu;

#define ssid "S7 edge"
#define password "almg76061"
// network credentials
#define ssid "unknown-network"
#define password "4321,dcba"

// MQTT Broker IP address
#define mqtt_server "192.168.43.133"
#define MQQT_PORT 1883
#define mqtt_server "192.168.100.54"
//size of the data to be transmitted. can be increased or decreased based on the quantity of data you will transmit
#define MQTT_BUFFER_SIZE 300
#define MQQT_PORT 1883

extern WiFiClient espClient;
extern PubSubClient client;
Expand All @@ -58,8 +77,9 @@ extern float MAX_ALTITUDE;
// 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
// GPS longitude, laltitude, altitude and
//state and temperature
struct Data
{
uint64_t timeStamp;
float altitude;
Expand All @@ -76,12 +96,14 @@ struct LogData
float latitude;
float longitude;
float gpsAltitude;
float temperature;
};
// SensorReadings contains the measurement we are getting
// from the sensors bmp and mpu
struct SensorReadings
{
float altitude;
float temperature;
float ax;
float ay;
float az;
Expand All @@ -107,14 +129,5 @@ struct FilteredValues
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
#endif
6 changes: 2 additions & 4 deletions include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ void ejection();

void ejectionTimerCallback(TimerHandle_t ejectionTimerHandle);

struct LogData formart_SD_data(SensorReadings readings, FilteredValues filtered_values);

struct SendValues formart_send_data(LogData readings);
struct Data formart_data(SensorReadings readings, FilteredValues filtered_values);

float get_base_altitude();

#endif
#endif
2 changes: 1 addition & 1 deletion include/kalmanfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

struct FilteredValues kalmanUpdate(float altitude, float acceleration);

#endif
#endif
6 changes: 3 additions & 3 deletions include/logdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

void initSDCard();

char *printSDMessage(LogData ld);
char *printSDMessage(Data ld);

void appendToFile(LogData ld[5]);
void appendToFile(Data ld[5]);


#endif
#endif
2 changes: 1 addition & 1 deletion include/readsensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ struct GPSReadings get_gps_readings();

struct SensorReadings get_readings();

#endif
#endif
8 changes: 5 additions & 3 deletions include/transmitwifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#define TRANSMITWIFI_H

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

void mqttCallback(char *topic, byte *message, unsigned int length);

void setup_wifi();
void create_Accesspoint();

void reconnect();

void sendTelemetryWiFi(SendValues sv[5]);
void sendTelemetryWiFi(Data sv);

void handleWiFi(SendValues sv[5]);
void handleWiFi(Data sv);

#endif
#endif
4 changes: 1 addition & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
board_build.mcu = esp32
board_build.f_cpu = 240000000L
framework = arduino
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
Expand All @@ -26,4 +24,4 @@ lib_deps =
plerup/EspSoftwareSerial@^6.15.2
knolleary/PubSubClient@^2.8

board_build.filesystem = littlefs
board_build.filesystem = littlefs
59 changes: 22 additions & 37 deletions src/checkState.cpp → src/checkstate.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "functions.h"
#include "defs.h"
#include "checkState.h"
#include "checkstate.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
// compares the current displacement to the set threshold of the ground state displacement
//if found to be above, we have achieved lift off
int checkInPoweredFlight(float altitude)
{
float displacement = altitude - BASE_ALTITUDE;
Expand All @@ -20,26 +21,13 @@ int checkInPoweredFlight(float altitude)
}
}

// 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
// we also check if the current altitude is less than the previous altitude
//this would determine that the rocket has began descent
int checkForApogee(float velocity, float currentAltitude, float previousAltitude)
{
if (currentAltitude < previousAltitude)
if ((previousAltitude - currentAltitude) > 5)
{

MAX_ALTITUDE = currentAltitude;
Expand All @@ -52,17 +40,17 @@ int checkForApogee(float velocity, float currentAltitude, float previousAltitude
}
else
{
return COASTING_STATE;
return POWERED_FLIGHT_STATE;
}
}

// Deploys parachute if we moved down 20 metres below apogee
// Deploys parachute if we moved down below the set threshold for below apogee displacement
int deployChute(float altitude)
{
float displacement = MAX_ALTITUDE - altitude;
if (displacement > BELOW_APOGEE_LEVEL_DISPLACEMENT)
{
// Fires ejection charge
// the ejection function is called which fires the ejection charge
ejection();
return CHUTE_DESCENT_STATE;
}
Expand Down Expand Up @@ -90,27 +78,24 @@ int checkGround(float altitude)

// 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);
case PRE_FLIGHT_GROUND_STATE:
return checkInPoweredFlight(currentAltitude);
case POWERED_FLIGHT_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);
}
}
}
2 changes: 1 addition & 1 deletion src/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const BaseType_t pro_cpu = 0;
const BaseType_t app_cpu = 1;

WiFiClient espClient;
PubSubClient client(espClient);
PubSubClient client(espClient);
Loading

0 comments on commit 61d237e

Please sign in to comment.