Skip to content

Commit

Permalink
Fix Hilsim (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyunlam authored Oct 2, 2024
1 parent 9529f6a commit ef01db3
Show file tree
Hide file tree
Showing 54 changed files with 24,160 additions and 2 deletions.
1 change: 1 addition & 0 deletions MIDAS/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/src/log_checksum.h
**/.DS_Store
*.launch
*.pyc
18,990 changes: 18,990 additions & 0 deletions MIDAS/hilsim/flight_computer.csv

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions MIDAS/hilsim/hilsim_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import serial
import pandas

import glob
import sys

import hilsimpacket_pb2
import os
import time
import rocketstate_pb2

def serial_ports():
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')

for port in ports:
try:
s = serial.Serial(port)
#s.close()
return s
result.append(port)
except (OSError, serial.SerialException):
pass
return None

def csv_to_protobuf(parsed_csv) -> hilsimpacket_pb2.HILSIMPacket:
hilsim_packet = hilsimpacket_pb2.HILSIMPacket()

hilsim_packet.imu_high_ax = parsed_csv['highg_ax']
hilsim_packet.imu_high_ay = parsed_csv["highg_ay"]
hilsim_packet.imu_high_az = parsed_csv["highg_az"]
hilsim_packet.barometer_altitude = parsed_csv["barometer_altitude"]
hilsim_packet.barometer_temperature = parsed_csv["temperature"]
hilsim_packet.barometer_pressure = parsed_csv["pressure"]
hilsim_packet.imu_low_lsm_ax = parsed_csv["ax"]
hilsim_packet.imu_low_lsm_ay = parsed_csv["ay"]
hilsim_packet.imu_low_lsm_az = parsed_csv["az"]
hilsim_packet.imu_low_lsm_gx = parsed_csv["gx"]
hilsim_packet.imu_low_lsm_gy = parsed_csv["gy"]
hilsim_packet.imu_low_lsm_gz = parsed_csv["gz"]
hilsim_packet.mag_x = parsed_csv["mx"]
hilsim_packet.mag_y = parsed_csv["my"]
hilsim_packet.mag_z = parsed_csv["mz"]

hilsim_packet.imu_low_ax = 0
hilsim_packet.imu_low_ay = 0
hilsim_packet.imu_low_az = 0
hilsim_packet.ornt_roll = 0
hilsim_packet.ornt_pitch = 0
hilsim_packet.ornt_yaw = 0
hilsim_packet.ornt_rollv = 0
hilsim_packet.ornt_pitchv = 0
hilsim_packet.ornt_yawv = 0
hilsim_packet.ornt_rolla = 0
hilsim_packet.ornt_pitcha = 0
hilsim_packet.ornt_yawa = 0
hilsim_packet.ornt_ax = 0
hilsim_packet.ornt_ay = 0
hilsim_packet.ornt_az = 0
hilsim_packet.ornt_gx = 0
hilsim_packet.ornt_gy = 0
hilsim_packet.ornt_gz = 0
hilsim_packet.ornt_mx = 0
hilsim_packet.ornt_my = 0
hilsim_packet.ornt_mz = 0
hilsim_packet.ornt_temp = 0
return hilsim_packet


tmp = 0
def new_protobuf() -> hilsimpacket_pb2.HILSIMPacket:
global tmp
hilsim_packet = hilsimpacket_pb2.HILSIMPacket()
tmp = tmp + 1
hilsim_packet.imu_high_ax = tmp
hilsim_packet.imu_high_ay = tmp
hilsim_packet.imu_high_az = tmp
hilsim_packet.barometer_altitude = tmp
hilsim_packet.barometer_temperature = tmp
hilsim_packet.barometer_pressure = tmp
hilsim_packet.imu_low_lsm_ax = tmp
hilsim_packet.imu_low_lsm_ay = tmp
hilsim_packet.imu_low_lsm_az = tmp
hilsim_packet.imu_low_lsm_gx = tmp
hilsim_packet.imu_low_lsm_gy = tmp
hilsim_packet.imu_low_lsm_gz = tmp
hilsim_packet.mag_x = tmp
hilsim_packet.mag_y = tmp
hilsim_packet.mag_z = tmp

hilsim_packet.imu_low_ax = tmp
hilsim_packet.imu_low_ay = tmp
hilsim_packet.imu_low_az = tmp
hilsim_packet.ornt_roll = tmp
hilsim_packet.ornt_pitch = tmp
hilsim_packet.ornt_yaw = tmp
hilsim_packet.ornt_rollv = tmp
hilsim_packet.ornt_pitchv = tmp
hilsim_packet.ornt_yawv = tmp
hilsim_packet.ornt_rolla = tmp
hilsim_packet.ornt_pitcha = tmp
hilsim_packet.ornt_yawa = tmp
hilsim_packet.ornt_ax = tmp
hilsim_packet.ornt_ay = tmp
hilsim_packet.ornt_az = tmp
hilsim_packet.ornt_gx = tmp
hilsim_packet.ornt_gy = tmp
hilsim_packet.ornt_gz = tmp
hilsim_packet.ornt_mx = tmp
hilsim_packet.ornt_my = tmp
hilsim_packet.ornt_mz = tmp
hilsim_packet.ornt_temp = tmp
return hilsim_packet


# Get first serial port...
if __name__ == "__main__":
MIDAS = serial_ports()
if MIDAS == None:
print("You need to connect MIDAS")
exit()

MIDAS.write(b'!')
print("Reading")

# Read input
print(MIDAS.read_until())
print(MIDAS.read_until())
print(MIDAS.read_until())
print(MIDAS.read_until())

# Read the csv
csv = pandas.read_csv(os.path.dirname(os.path.abspath(sys.argv[tmp])) + "/flight_computer.csv", index_col=0)
while True:
packet = new_protobuf()
data = packet.SerializeToString()
# Encode the length of package in 2 bytes and then output the the information

MIDAS.write(len(data).to_bytes(2, "big"))
byte_len = len(data).to_bytes(2, "big")
# print(int.from_bytes(byte_len))
MIDAS.write(data)
#output = MIDAS.read_all()
print(MIDAS.read_all())
# print("Dumping data...")
by = MIDAS.read()
data2 = MIDAS.read(int.from_bytes(by))
state = rocketstate_pb2.RocketState()
state.ParseFromString(data2)
print(state.rocket_state)
#print(len(data).to_bytes(2, "big"))
#print(data)
# print("More stuff")
25 changes: 25 additions & 0 deletions MIDAS/hilsim/hilsimpacket_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions MIDAS/hilsim/rocketstate_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions MIDAS/src/hilsim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/__pycache__/*
*.pyc
11 changes: 10 additions & 1 deletion MIDAS/src/hilsim/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# HILSIM
Protobuf to be added
To communicate with HILSIM server, we use protobuf to go back and forth.
Important links:
- Protobuf: https://protobuf.dev/
- Nanopb: https://github.com/nanopb/nanopb

You can run `generate_protobuf.sh` or `generate_protobuf.bat` to generate the protobuf files.

Do note that you will need the command protoc to run it. It can be found here: <https://github.com/protocolbuffers/protobuf#protobuf-compiler-installation>

Protoc is only used to generate the python files, and the C++ files have to be generated by nanopb.
11 changes: 11 additions & 0 deletions MIDAS/src/hilsim/generate_protobuf.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off
REM Generate hilsim packet
protoc -I=. --python_out=. hilsimpacket.proto
python nanopb_generator/nanopb_generator.py hilsimpacket.proto

python nanopb_generator/nanopb_generator.py rocketstate.proto
protoc -I=. --python_out=. rocketstate.proto

cp hilsimpacket_pb2.py ../../hilsim/hilsimpacket_pb2.py
cp rocketstate_pb2.py ../../hilsim/rocketstate_pb2.py
REM Get the size of the packets and automatically dump it to a header file
Empty file.
24 changes: 24 additions & 0 deletions MIDAS/src/hilsim/generate_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import hilsimpacket_pb2

def main():
hilsim_packet = hilsimpacket_pb2.HILSIMPacket()
hilsim_packet.imu_high_ax = 0
hilsim_packet.imu_high_ay = 0
hilsim_packet.imu_high_az = 0
hilsim_packet.barometer_altitude = 0
hilsim_packet.barometer_temperature = 0
hilsim_packet.barometer_pressure = 0
hilsim_packet.imu_low_ax = 0
hilsim_packet.imu_low_ay = 0
hilsim_packet.imu_low_az = 0
hilsim_packet.imu_low_gx = 0
hilsim_packet.imu_low_gy = 0
hilsim_packet.imu_low_gz = 0
hilsim_packet.mag_x = 0
hilsim_packet.mag_y = 0
hilsim_packet.mag_z = 0
print(vars(hilsimpacket_pb2.HILSIMPacket))
print(len(hilsim_packet.SerializeToString()))

if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions MIDAS/src/hilsim/global_packet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include "hilsim/hilsimpacket.pb.h"
#include "hilsim/rocketstate.pb.h"

extern HILSIMPacket global_packet;
12 changes: 12 additions & 0 deletions MIDAS/src/hilsim/hilsimpacket.pb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.7 */

#include "hilsimpacket.pb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif

PB_BIND(HILSIMPacket, HILSIMPacket, AUTO)



Loading

0 comments on commit ef01db3

Please sign in to comment.