Skip to content

Implemented the state machine #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Avionics/Firmware/include/StateMachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef STATEMACHINE_H
#define STATEMACHINE_H

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_ICM20948.h>
#include <FS.h>
#include <SPIFFS.h>

#define BUTTON_PIN 12

// Declare the RocketState enum
enum class RocketState {
IDLE, // Waiting for ignition
IGNITION, // Ignition sequence
ASCENT, // Ascent phase
DESCENT, // Descent phase
LANDING // Landing phase
};

// Declare variables as extern
extern const int PMOS_PIN;
extern const int NMOS_PIN;
extern Adafruit_ICM20948 imu;
extern bool pmosState;
extern bool nmosState;
extern RocketState currentState;

// Declare functions
void setupStateMachine();
void loopStateMachine();
void idleState();
void ignitionState();
void ascentState();
void descentState();
void landingState();
void transitionTo(RocketState newState);
void handleStateChange(RocketState state);
void beginFlight();

#endif // STATEMACHINE_H
124 changes: 124 additions & 0 deletions Avionics/Firmware/src/StateMachine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_ICM20948.h>
#include <FS.h>
#include <SPIFFS.h>
#include "StateMachine.h"

// Define the pins for ignition control
#define BUTTON_PIN 12
const int PMOS_PIN = 26; // Force external linkage
const int NMOS_PIN = 25; // Force external linkage

// Define the RocketState enum only once in the header (do not redefine here)
// Current rocket state
RocketState currentState = RocketState::IDLE;

// Global variables for ignition states
extern bool pmosState;
extern bool nmosState;

void setupStateMachine() {
Serial.begin(115200);
while (!Serial) {}

// Initialize the IMU
if (!imu.begin_I2C()) {
Serial.println("Failed to find IMU");
while (true);
}
Serial.println("IMU found");

// Initialize Pins
pinMode(BUTTON_PIN, INPUT);
pinMode(PMOS_PIN, OUTPUT);
pinMode(NMOS_PIN, OUTPUT);

currentState = RocketState::IDLE;
}

void loopStateMachine() {
switch (currentState) {
case RocketState::IDLE:
idleState();
break;
case RocketState::IGNITION:
ignitionState();
break;
case RocketState::ASCENT:
ascentState();
break;
case RocketState::DESCENT:
descentState();
break;
case RocketState::LANDING:
landingState();
break;
}
}

void idleState() {
Serial.println("In IDLE state.");
if (digitalRead(BUTTON_PIN) == HIGH) {
transitionTo(RocketState::IGNITION);
}
}

void ignitionState() {
Serial.println("In IGNITION state.");
digitalWrite(PMOS_PIN, !pmosState);
digitalWrite(NMOS_PIN, !nmosState);
pmosState = !pmosState;
nmosState = !nmosState;
delay(2000);
transitionTo(RocketState::ASCENT);
}

void ascentState() {
Serial.println("In ASCENT state.");
sensors_event_t accel, gyro;
imu.getEvent(&accel, &gyro, nullptr, nullptr);
Serial.print("Acceleration: ");
Serial.print(accel.acceleration.x);
Serial.print(", ");
Serial.print(accel.acceleration.y);
Serial.print(", ");
Serial.println(accel.acceleration.z);
transitionTo(RocketState::DESCENT);
}

void descentState() {
Serial.println("In DESCENT state.");
delay(5000);
transitionTo(RocketState::LANDING);
}

void landingState() {
Serial.println("In LANDING state.");
transitionTo(RocketState::IDLE);
}

void transitionTo(RocketState newState) {
currentState = newState;
handleStateChange(newState);
}

void handleStateChange(RocketState state) {
switch (state) {
case RocketState::IDLE:
idleState();
break;
case RocketState::IGNITION:
ignitionState();
break;
case RocketState::ASCENT:
ascentState();
break;
case RocketState::DESCENT:
descentState();
break;
case RocketState::LANDING:
landingState();
break;
}
}
9 changes: 0 additions & 9 deletions Avionics/Firmware/src/flightdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,18 @@ void initialize_csv() {
if (!file) {
Serial.println("Failed to open file for initializing. Formatting...");
SPIFFS.format();
<<<<<<< HEAD
if (!SPIFFS.begin()) {
Serial.println("Failed to mount SPIFFS during formatting.");
return;
}

// Try to open the file again after formatting
file = SPIFFS.open("/data.csv", FILE_WRITE);
=======
if (!SPIFFS.begin())
Serial.println("Failed to mount SPIFFS during formatting.");
>>>>>>> e10866b (Axes correctly oriented)
if (!file) {
Serial.println("Failed to open file for initializing. Terminating...");
return;
}
}
<<<<<<< HEAD

=======
>>>>>>> e10866b (Axes correctly oriented)
Serial.println("Opened file for initializing");

file.print("Time (ms)"); file.print(",");
Expand Down