Skip to content
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

New branch #4

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
60 changes: 60 additions & 0 deletions data-logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from serial import Serial

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np
import csv

import atexit


port = "COM4"
baud_rate = 115200

@atexit.register
def plot_data():
# plot the data once the is done running

if(os.path.exists("sensor-data.csv")):
print("File found")
else:
print("File does not exist")

# read file
arr = np.loadtxt("sensor-data.csv", delimiter=",", unpack=True)

plt.plot(arr[0], arr[1])
plt.title("Unfiltered x acceleration")
plt.xlabel("Time (ms)")
plt.ylabel("X acceleration (m/s^2)")

plt.show()


def read_serial_data():

# create Serial object
serial_port = Serial(port, baud_rate)

# csv writer object
output_file = open("sensor-data.csv", "w", newline="")
output_writer = csv.writer(output_file)

# register function to run when the function exits
# atexit.register(plot_data)

while(True):
now = datetime.now()
current_time = now.timestamp()

# read x acceleration into a csv file
data = serial_port.readline()

if data:
data = float(data)

output_writer.writerow([current_time, data])


read_serial_data()
24 changes: 24 additions & 0 deletions graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np
import os

def plot_data():
# plot the data once the is done running

if(os.path.exists("sensor-data.csv")):
print("File found")
else:
print("File does not exist")

# read file
arr = np.loadtxt("sensor-data.csv", delimiter=",", unpack=True)

plt.plot(arr[0], arr[1])
plt.title("Unfiltered x acceleration")
plt.xlabel("Time (ms)")
plt.ylabel("X acceleration (m/s^2)")

plt.show()

plot_data()
45 changes: 45 additions & 0 deletions include/defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef DEFS_H
#define DEFS_H

/* debug enable for use during testing */
#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

/* timing constant */
#define SETUP_DELAY 300
#define TASK_DELAY 10

/* flight constants */
#define EJECTION_HEIGHT 1000 // eject at 1000m AGL
#define SEA_LEVEL_PRESSURE 101325 // Assume the sea level pressure is 101325 Pascals - this can change with weather
#define ALTITUDE_OFFSET 1500 /* this value is the altitude at rocket launch site */

/* tasks constants */
#define STACK_SIZE 2048
#define ALTIMETER_QUEUE_LENGTH 10 // todo: change to 2 items
#define GYROSCOPE_QUEUE_LENGTH 10
#define FILTERED_DATA_QUEUE_LENGTH 10

/* MQTT constants */
const char* MQTT_SERVER = "192.168.1.100";
#define MQTT_BUFFER_SIZE 300
#define MQTT_PORT 1883

/* WIFI credentials */
const char* SSID = "onboard";
const char* PASSWORD = "123456789";

#endif
79 changes: 79 additions & 0 deletions include/kalman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <BasicLinearAlgebra.h>
#include "defs.h"
#include "kalman.h"

float q = 0.0001;
float T = 0.1;

// The system dynamics
BLA::Matrix<3, 3> A = {1.0, 0.1, 0.005,
0, 1.0, 0.1,
0, 0, 1.0};

// Relationship between measurement and states
BLA::Matrix<2, 3> H = {1.0, 0, 0,
0, 0, 1.0};

// Initial posteriori estimate error covariance
BLA::Matrix<3, 3> P = {1, 0, 0,
0, 1, 0,
0, 0, 1};

// Measurement error covariance
BLA::Matrix<2, 2> R = {0.25, 0,
0, 0.75};

// Process noise covariance
BLA::Matrix<3, 3> Q = {q, q, q,
q, q, q,
q, q, q};

// Identity Matrix
BLA::Matrix<3, 3> I = {1, 0, 0,
0, 1, 0,
0, 0, 1};

BLA::Matrix<3, 1> x_hat = {1500.0,
0.0,
0.0};

BLA::Matrix<2, 1> Y = {0.0,
0.0};


/* This filters our altitude and acceleration values */
struct Filtered_Data filterData(float x_acceleration){
/* this struct will store the filtered data values */
struct Filtered_Data filtered_values;

// Measurement matrix
BLA::Matrix<2, 1> Z = {0, x_acceleration};

// Predicted state estimate
BLA::Matrix<3, 1> x_hat_minus = A * x_hat;

// Predicted estimate covariance
BLA::Matrix<3, 3> P_minus = A * P * (~A) + Q;

// Kalman gain
BLA::Matrix<2, 2> con = (H * P_minus * (~H) + R);
BLA::Matrix<3, 2> K = P_minus * (~H) * Invert(con);

// Measurement residual
Y = Z - (H * x_hat_minus);

// Updated state estimate
x_hat = x_hat_minus + K * Y;

// Updated estimate covariance
P = (I - K * H) * P_minus;
Y = Z - (H * x_hat_minus);

filtered_values.x_acceleration = x_hat(2);

// return_val.displacement = x_hat(0);
// return_val.velocity = x_hat(1);
// return_val.acceleration = x_hat(2);

return filtered_values;
}
12 changes: 12 additions & 0 deletions include/kalman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#pragma once

/* define struct to hold filtered data*/
struct Filtered_Data{
float x_acceleration;
};


/* this function returns Kalman-filtered data */
struct Filtered_Data filterData(float);

4 changes: 4 additions & 0 deletions include/sensors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
// Created by Edwin Mwiti on 3/6/2023.
//

11 changes: 11 additions & 0 deletions include/sensors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Created by Edwin Miwiri on 3/6/2023.
//

#ifndef SENSORS_H
#define SENSORS_H

void initialize_gyroscope();
void initialize_altimeter();

#endif
9 changes: 9 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@
[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
monitor_speed=115200
framework = arduino
; upload_port = COM4
lib_deps =
adafruit/Adafruit MPU6050@^2.2.4
adafruit/Adafruit Unified Sensor@^1.1.7
adafruit/Adafruit BMP085 Library @ ^1.2.2
tomstewart89/BasicLinearAlgebra@^3.7
Knolleary/PubSubClient@^2.8

Loading