Skip to content
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
4 changes: 2 additions & 2 deletions MIDAS/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.pio
.vscode
/src/log_checksum.h
**/.DS_Store
*.launch
*.pyc
*.pyc
struct_sizes.json
5 changes: 4 additions & 1 deletion MIDAS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ Note that if you never directly installed platformio and instead are just using
the VSCode extension, these commands won't work until you install platformio
manually.

To run SILSIM from the command line, use `pio run -e mcu_silsim`.
To run SILSIM from the command line, use `pio run -e mcu_silsim`.

### HILSIM
To start a basic hilsim run, you must first run the `struct_sizes` target, then you can upload the hilsim data and run `hilsimstreamer.py`
103 changes: 103 additions & 0 deletions MIDAS/hilsim/hilsimstreamer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import serial
import serial.tools
import serial.tools.list_ports
import time
import json

import csv
import os

def write_to_csv(filename, data):
file_exists = os.path.exists(filename)

with open(filename, 'a', newline='') as csvfile:
writer = csv.writer(csvfile)

if not file_exists:
writer.writerow(['Timestamp', 'fsmstate', 'global_armed', 'a_armed', 'a_firing', 'b_armed', 'b_firing', 'c_armed', 'c_firing', 'd_armed', 'd_firing'])

writer.writerow(data)

device = None
# Look for midas comport
for comport in serial.tools.list_ports.comports():
if comport.vid == 0x303a:
# This is an espressif device
print(comport.name, "is an Espressif device")
device = comport
break

print(device.device)

if not device:
print("MIDAS is not connected!")
exit()

# make this a command line argument
file = open(r"data43.launch", "rb")

# Read the json file
SIZES = { int(k): v for k, v in json.load(open("../struct_sizes.json", 'r')).items() }
print(SIZES)

test_list=file.read(4)
print("Checksum", hex(int.from_bytes(test_list, byteorder='little')))
ser = serial.Serial(
port=comport.device,
baudrate=115200,
timeout=None
)
print(ser.write('!'.encode('ascii')))
print("Magic", ser.read_until('\n'.encode('ascii'))) # Should be our magic
print("Checksum", hex(int(ser.read_until('\n'.encode('ascii'))))) # Should be our magic
print("Garbage", ser.read_until('\n'.encode('ascii')))
print("Garbage", ser.read_until('\n'.encode('ascii')))
#print("Garbage", ser.read_until('\n'.encode('ascii')))
#print("Garbage", ser.read_until('\n'.encode('ascii')))
#print("Garbage", ser.read_until('\n'.encode('ascii')))

counter = 0


start_time = time.perf_counter()
prev = None
while True:
tag = file.read(4)
if not tag:
break

tag = int.from_bytes(tag, byteorder='little')
timestamp = file.read(4)
timestamp = int.from_bytes(timestamp, byteorder='little')
# print(tag, int.from_bytes(timestamp, byteorder='little'))

if tag in SIZES:
size = SIZES[tag]
# print(size)

data = file.read(size)
# print(data)

ser.write(tag.to_bytes(1, byteorder='little'))
# ser.write(size.to_bytes(4, byteorder='little'))
ser.write(data)
content = (ser.read())
# data = bytes.decode(content, encoding="ascii")
# if len(content) != 0:
# # print(content)
# if ("Error") in (data):
# print((content))
if content != prev:
prev = content
print(counter, file.tell(), int.from_bytes(content))

previous_pyro_array = [timestamp, int.from_bytes(content), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read()), int.from_bytes(ser.read())]

write_to_csv("pyro_data.csv", previous_pyro_array)
else:
raise ValueError(f"Unknown tag: {tag}")
counter += 1

ser.close()
end_time = time.perf_counter()
print("Done in ", end_time - start_time)
44 changes: 44 additions & 0 deletions MIDAS/hilsim/pyro_data_plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from matplotlib import pyplot as plt
import pandas as pd

df = pd.read_csv('pyro_data.csv')

# STATE_IDLE = 0
# STATE_FIRST_BOOST = 1
# STATE_BURNOUT = 2
# STATE_COAST = 3
# STATE_SUSTAINER_IGNITION = 4
# STATE_SECOND_BOOST = 5
# STATE_FIRST_SEPARATION = 6
# STATE_APOGEE = 7
# STATE_DROGUE_DEPLOY = 8
# STATE_DROGUE = 9
# STATE_MAIN_DEPLOY = 10
# STATE_MAIN = 11
# STATE_LANDED = 12
# FSM_STATE_COUNT = 13

y_labs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
y_labs_map = ['STATE_IDLE', 'STATE_FIRST_BOOST', 'STATE_BURNOUT', 'STATE_COAST', 'STATE_SUSTAINER_IGNITION', 'STATE_SECOND_BOOST', 'STATE_FIRST_SEPARATION', 'STATE_APOGEE', 'STATE_DROGUE_DEPLOY', 'STATE_DROGUE', 'STATE_MAIN_DEPLOY', 'STATE_MAIN', 'STATE_LANDED', 'FSM_STATE_COUNT']

for index, row in df.iterrows():
if (index > 0 and df.loc[df.index[index]]["a_armed"] != df.loc[df.index[index-1]]["a_armed"] or
df.loc[df.index[index]]["b_armed"] != df.loc[df.index[index-1]]["b_armed"] or
df.loc[df.index[index]]["c_armed"] != df.loc[df.index[index-1]]["c_armed"] or
df.loc[df.index[index]]["d_armed"] != df.loc[df.index[index-1]]["d_armed"] or
df.loc[df.index[index]]["a_firing"] != df.loc[df.index[index-1]]["a_firing"] or
df.loc[df.index[index]]["b_firing"] != df.loc[df.index[index-1]]["b_firing"] or
df.loc[df.index[index]]["c_firing"] != df.loc[df.index[index-1]]["c_firing"] or
df.loc[df.index[index]]["d_firing"] != df.loc[df.index[index-1]]["d_firing"] or
df.loc[df.index[index]]["global_armed"] != df.loc[df.index[index-1]]["global_armed"]
):

plt.axvline(x = df.loc[df.index[index]]["Timestamp"], color = 'red')


plt.yticks(y_labs, y_labs_map)

plt.ylim(0, 15)

plt.plot(df['Timestamp'], df['fsmstate'])
plt.show()
19 changes: 17 additions & 2 deletions MIDAS/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ build_flags =
-DARDUINO_USB_CDC_ON_BOOT=1
-DCONFIG_DISABLE_HAL_LOCKS=1
-std=gnu++2a
-DHILSIM=1
-DIS_BOOSTER
build_unflags =
-std=gnu++11
Expand All @@ -47,9 +48,12 @@ build_flags =
-DHILSIM=1
-DIS_SUSTAINER
-std=gnu++2a
build_src_filter = +<*> -<silsim/> -<hardware/> +<hilsim>
build_src_filter = +<*> -<silsim/> -<hardware/> +<hilsim> -<hilsim/StructSizes.cpp>
build_unflags =
-std=gnu++11
lib_deps =
adafruit/Adafruit LIS3MDL@^1.2.1 ; Magnetometer driver
stevemarple/MicroNMEA@^2.0.6 ; NMEA Parsing library (for GPS messages)

[env:mcu_hilsim_booster]
platform = espressif32
Expand All @@ -62,9 +66,12 @@ build_flags =
-DHILSIM=1
-DIS_BOOSTER
-std=gnu++2a
build_src_filter = +<*> -<silsim/> -<hardware/> +<hilsim>
build_src_filter = +<*> -<silsim/> -<hardware/> +<hilsim> -<hilsim/StructSizes.cpp>
build_unflags =
-std=gnu++11
lib_deps =
adafruit/Adafruit LIS3MDL@^1.2.1 ; Magnetometer driver
stevemarple/MicroNMEA@^2.0.6 ; NMEA Parsing library (for GPS messages)


[env:mcu_silsim_sustainer]
Expand Down Expand Up @@ -98,3 +105,11 @@ lib_deps =
Eigen
lib_ignore =
TCAL9539


[env:struct_sizes]
platform=native
build_type = release
build_unflags =
-std=gnu++11
build_src_filter = +<hilsim/StructSizes.cpp>
6 changes: 3 additions & 3 deletions MIDAS/src/Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Mutex {
*/
T read() {
if (!mutex_handle || mutex_handle != check) {
Serial.println("Aw shucks");
Serial.println("Aw shucks read");
Serial.flush();
}
xSemaphoreTake(mutex_handle, portMAX_DELAY);
Expand All @@ -77,7 +77,7 @@ struct Mutex {
*/
void read2(T* ptr) {
if (!mutex_handle || mutex_handle != check) {
Serial.println("Aw shucks");
Serial.println("Aw shucks read2");
Serial.flush();
}
xSemaphoreTake(mutex_handle, portMAX_DELAY);
Expand All @@ -93,7 +93,7 @@ struct Mutex {
*/
void write(T value) {
if (!mutex_handle || mutex_handle != check) {
Serial.println("Aw shucks");
Serial.println("Aw shucks write");
Serial.flush();
}

Expand Down
8 changes: 6 additions & 2 deletions MIDAS/src/hardware/Pyro.cpp → MIDAS/src/Pyro.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <cmath>

#include "sensors.h"
#include "pins.h"
#ifdef HILSIM
#include "hilsim/sensors.h"
#else
#include "hardware/sensors.h"
#endif
#include "hardware/pins.h"

#include "TCAL9539.h"

Expand Down
1 change: 1 addition & 0 deletions MIDAS/src/hardware/SDLog.cpp → MIDAS/src/SDLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SPI.h>
#include <SD_MMC.h>

#include "hardware/pins.h"
#include "SDLog.h"

/**
Expand Down
6 changes: 5 additions & 1 deletion MIDAS/src/hardware/SDLog.h → MIDAS/src/SDLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#include <FS.h>

#include <SD.h>
#include "sensors.h"
#ifdef HILSIM
#include "hilsim/sensors.h"
#else
#include "hardware/sensors.h"
#endif
#include "data_logging.h"

/**
Expand Down
2 changes: 1 addition & 1 deletion MIDAS/src/hardware/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "systems.h"
#include "hardware/pins.h"
#include "hardware/Emmc.h"
#include "hardware/SDLog.h"
#include "SDLog.h"
#include "sensor_data.h"

/**
Expand Down
2 changes: 1 addition & 1 deletion MIDAS/src/hardware/sensors.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "errors.h"
#include "../errors.h"
#include "sensor_data.h"
#include "hardware/pins.h"

Expand Down
33 changes: 33 additions & 0 deletions MIDAS/src/hilsim/Barometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "sensors.h"
#include <MS5611.h>

MS5611 MS(MS5611_CS); //singleton object for the MS sensor

/**
* @brief Initializes barometer, returns NoError
*
* @return Error code
*/
ErrorCode BarometerSensor::init() {
MS.init();

return ErrorCode::NoError;
}

/**
* @brief Reads the pressure and temperature from the MS5611
*
* @return Barometer data packet
*/
Barometer BarometerSensor::read() {
MS.read(12);

/*
* TODO: Switch to latest version of library (0.3.9) when we get hardware to verify
*/
float pressure = static_cast<float>(MS.getPressure() * 0.01 + 26.03);
float temperature = static_cast<float>(MS.getTemperature() * 0.01);
float altitude = static_cast<float>(-log(pressure * 0.000987) * (temperature + 273.15) * 29.254);

return barometer;
}
33 changes: 33 additions & 0 deletions MIDAS/src/hilsim/Continuity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "sensors.h"
#include "ads7138-q1.h"
#include <hal.h>

#define PYRO_VOLTAGE_DIVIDER (5.0 / (5.0 + 20.0)) //voltage divider for pyro batt voltage, check hardware schematic
#define CONT_VOLTAGE_DIVIDER (5.0 / (5.0 + 20.0)) //voltage divider for continuity voltage, check hardware schematic

/**
* @brief Initializes ADC, returns NoError
*
* @return Error code
*/
ErrorCode ContinuitySensor::init() {
ADS7138Init(); // Ask ADS to init the pins, we still need to get the device to actually read

return ErrorCode::NoError;
}

/**
* @brief Reads the value of the ADC
*
* @return Continuity data packet
*/
Continuity ContinuitySensor::read() {
Continuity continuity;
//ADC reference voltage is 3.3, returns 12 bit value
continuity.sense_pyro = adcAnalogRead(ADCAddress{SENSE_PYRO}).value * 3.3f / 4096.f / PYRO_VOLTAGE_DIVIDER;
continuity.pins[0] = adcAnalogRead(ADCAddress{SENSE_MOTOR}).value * 3.3f / 4096.f / CONT_VOLTAGE_DIVIDER;
continuity.pins[1] = adcAnalogRead(ADCAddress{SENSE_MAIN}).value * 3.3f / 4096.f / CONT_VOLTAGE_DIVIDER;
continuity.pins[2] = adcAnalogRead(ADCAddress{SENSE_APOGEE}).value * 3.3f / 4096.f / CONT_VOLTAGE_DIVIDER;
continuity.pins[3] = adcAnalogRead(ADCAddress{SENSE_AUX}).value * 3.3f / 4096.f / CONT_VOLTAGE_DIVIDER;
return continuity;
}
Loading