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
Binary file added src/instrumentation/.thermocouple.py.swp
Binary file not shown.
58 changes: 43 additions & 15 deletions src/instrumentation/read_labjack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import u6
import json
from thermocouple import *
import os
import posix
import datetime

# Gains
X1 = 0b00000000
Expand Down Expand Up @@ -131,10 +134,10 @@
######### BEGIN USER ADJUSTABLE #########

# Stream settings
scan_frequency = 25
resolution_index = 2
settling_factor = 2
samples_per_packet = 50
scan_frequency = 500
resolution_index = 4
settling_factor = 4
samples_per_packet = 12
channel_settings = [(86, DIFF | X1000), # L_RUN_TANK (SEEMS GOOD. CHECK CAL)
(87, DIFF | X1000), # L_THRUST (VERY NOISY)

Expand Down Expand Up @@ -195,38 +198,50 @@
SettlingFactor = settling_factor,
SamplesPerPacket = samples_per_packet)

# Get cold junction voltage using LJ internal temp sensor
V_ref = get_ref_voltage(d.getTemperature())
print("T_ref in K: ", d.getTemperature())
print("V_ref in V: ", V_ref)

# Avoid having to power cycle the LJ on restart
try:
d.streamStop()
print("Stream was still running. Stopping...")
except:
pass

# Get cold junction voltage using LJ internal temp sensor
V_ref = get_ref_voltage(d.getTemperature())

# Avoid having to power cycle the LJ on restart

if samples_per_packet < len(channel_settings):
raise ValueError \
("samples_per_packet: (" + str(samples_per_packet) + \
") must be at least the number of channels: (" + \
str(len(channel_settings)) + ")!")

pipe_path = "/home/uvr/Documents/GitHub/PDP-Monitoring-System/src/instrumentation/data"

try:
posix.mkfifo(pipe_path)
print("Named pipe created successfully!")
except FileExistsError:
print("Named pipe already exists!")
except OSError as e:
print(f"Named pipe creation failed: {e}")

# Stream data from the LJ
if d is None:
print("No LabJack device connected. Exiting...")
exit()
else:
print("Starting stream...")
d.streamStart()

try:
with open('instrumentation_data.txt', 'w') as file:
#with open('data_log.txt', 'w') as data_log:
with open(pipe_path, 'w') as file:

# Contains sensor values in SI units
converted = {}

for reading in d.streamData(convert=False):

# Reading is a dict of many things, one of which is the
# 'result' which can be passed to processStreamData() to
# give voltages.
Expand All @@ -253,6 +268,7 @@
SHUNT = values[CHAN_SHUNT]

# Convert voltage to sensor value in SI units and store in dict

converted['P_INJECTOR'] = \
(sum(P_INJECTOR)/len(P_INJECTOR))*GAIN_P_INJECTOR

Expand All @@ -268,6 +284,12 @@
converted['P_RUN_TANK'] = \
(sum(P_RUN_TANK)/len(P_RUN_TANK))*GAIN_P_RUN_TANK

converted['L_RUN_TANK'] = \
(sum(L_RUN_TANK)/len(L_RUN_TANK))*GAIN_L_RUN_TANK+OFFSET_L_RUN_TANK

converted['L_THRUST'] = \
(sum(L_THRUST)/len(L_THRUST))*GAIN_L_THRUST+OFFSET_L_THRUST

# Thermocouples
converted['T_RUN_TANK'] = \
V_to_K((sum(T_RUN_TANK)/len(T_RUN_TANK)), V_ref)
Expand All @@ -281,15 +303,21 @@
converted['T_POST_COMB'] = \
V_to_K((sum(T_POST_COMB)/len(T_POST_COMB)), V_ref)

converted['timestamp'] = str(datetime.datetime.now().time())

# Write to file so websocket can send to ground support
file.write(f'{json.dumps(converted)}\n')
with open('tmp.txt', 'w') as tmp:
tmp.write(f'{json.dumps(converted)}')
tmp.write('\n!')
file.flush()

# Log to PDP for later analysis
#data_log.write(f'{json.dumps(converted)}\n')

except:
print("Interrupt signal received!")
finally:
d.streamStop()
print("Stream stopped.\n")
d.close()
os.remove(pipe_path)


10 changes: 5 additions & 5 deletions src/instrumentation/thermocouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def get_ref_voltage(T_cold_junction_K):

# For 0C to 1372C at reduced accuracy
c0 = -1.7600413686 * 10**-2
c1 = 3.8921204975 * 10**-2
c2 = 1.8558770032 * 10**-5
c1 = 3.8921204975 * 10**-2
c2 = 1.8558770032 * 10**-5
c3 = -9.9457592874 * 10**-8
c4 = 3.1840945719 * 10**-10
c4 = 3.1840945719 * 10**-10
c5 = -5.6072844889 * 10**-13
c6 = 5.6075059059 * 10**-16
c6 = 5.6075059059 * 10**-16
c7 = -3.2020720003 * 10**-19
c8 = 9.7151147152 * 10**-23
c8 = 9.7151147152 * 10**-23
c9 = -1.2104721275 * 10**-26

mV = c0 + \
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def main() -> None:
exit(1)

try:
wss = WebSocketServer("serial")
wss = WebSocketServer("SERIAL_WS")
except Exception as e:
print(f"Failed to initialize websocket server: {e}")
exit(1)
Expand Down
23 changes: 23 additions & 0 deletions src/server/instrumentationMock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import math
import time

TEMPERATURE_SENSOR_RANGE = range(273, 300)
PRESSURE_SENSOR_RANGE = range(1, 100)
LOAD_SENSOR_RANGE = range(0, 15)

def labjack_mock():
time.sleep(0.001)
return {
'P_INJECTOR': math.randint(PRESSURE_SENSOR_RANGE),
'P_COMB_CHMBR': math.randint(PRESSURE_SENSOR_RANGE),
'P_N2O_FLOW': math.randint(PRESSURE_SENSOR_RANGE),
'P_N2_FLOW': math.randint(PRESSURE_SENSOR_RANGE),
'P_RUN_TANK': math.randint(PRESSURE_SENSOR_RANGE),
'L_RUN_TANK': math.randint(LOAD_SENSOR_RANGE),
'L_THRUST': math.randint(LOAD_SENSOR_RANGE),
'T_RUN_RANK': math.randint(TEMPERATURE_SENSOR_RANGE),
'T_INJECTOR': math.randint(TEMPERATURE_SENSOR_RANGE),
'T_COMB_CHMBR': math.randint(TEMPERATURE_SENSOR_RANGE),
'T_POST_COMB': math.randint(TEMPERATURE_SENSOR_RANGE),
'SHUNT': math.randint(TEMPERATURE_SENSOR_RANGE),
}
21 changes: 21 additions & 0 deletions src/server/serailMock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time

def serial_feedback_mock(
valve: str,
action: str
) -> dict:
'''
Name:
serial_feedback(command: string, valve: string, action: string) -> dict
Args:
valve: the valve that the command is for
action: the action to take on the valve
Desc:
Creates a dictionary to send over the websocket
'''
action = action if action == "OPEN" or action == "CLOSE" else "TRANSIT"
return {
'identifier': 'FEEDBACK',
'valve': valve,
'action': action
}
Empty file.
57 changes: 57 additions & 0 deletions src/server/testModeUtils/mock_instrumentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json
import random
import time
import tornado.ioloop
import tornado.web
import tornado.websocket

clients = set() # Keep track of connected clients

# Function to generate a dictionary with fixed keys and random values
def generate_data():
return {
"P_INJECTOR": random.randint(0, 1000000),
"P_COMB_CHMBR": random.randint(0, 500000),
"P_N2O_FLOW": random.randint(0, 1000000),
"P_N2_FLOW": random.randint(0, 1000000),
"P_RUN_TANK": random.randint(0, 1000000),

"T_RUN_TANK": random.randint(200, 300),
"T_INJECTOR": random.randint(200, 300),
"T_COMB_CHMBR": random.randint(300, 1500),
"T_POST_COMB": random.randint(300, 1500),

"L_RUN_TANK": random.randint(0, 20),
"L_THRUST": random.randint(0, 1200)
}

class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
clients.add(self)
print("New client connected.")

def on_close(self):
clients.remove(self)
print("Client disconnected.")

# Function to send data to clients
def send_data():
data = generate_data()
json_data = json.dumps({'data':data})
for client in clients:
client.write_message(json_data) # Send to each connected client
#print("Sent data:", json_data)
tornado.ioloop.IOLoop.current().call_later(0.001, send_data) # Call again in 1 second

def make_app():
return tornado.web.Application([
(r'/websocket', WebSocketHandler),
])

if __name__ == "__main__":
app = make_app()
app.listen(8888) # Choose an appropriate port
print("Starting WebSocket server on port 8888...")
send_data() # Start sending data
tornado.ioloop.IOLoop.current().start() # Start the Tornado I/O loop

5 changes: 5 additions & 0 deletions src/server/testPipes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

while True:
with open("../instrumentation/data") as pipe:
line = pipe.readline()
print(line)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

if __name__ == "__main__" or __name__ == "LJWebsocket":
print("Entered Instrumentation")
ws = WebSocketServer("instrumentation")
ws = WebSocketServer("INSTRUMENTATION_WS")
asyncio.run(ws.start_instrumentation())
print("yes")
33 changes: 33 additions & 0 deletions src/server/wsTestServerEntryPoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import asyncio
from wss import WebSocketServer

__name__ = "TestServer"

def main() -> None:
serial_wss = None
instrumentation_wss = None

try:
serial_wss = WebSocketServer(ws_type="SERIAL_WS", test_mode=True)
instrumentation_wss = WebSocketServer(ws_type="INSTRUMENTATION_WS", test_mode=True)
except Exception as e:
print(f"Failed to initialize websocket server: {e}")
exit(1)

event_loop = asyncio.get_event_loop()
try:
event_loop.create_task(serial_wss.start_serial())
event_loop.create_task(instrumentation_wss.start_instrumentation())
except Exception as e:
print(f"Failed to start websocket server: {e}")
exit(1)

try:
event_loop.run_forever()
except KeyboardInterrupt:
event_loop.close()
print("Test Server terminated by user")
exit(0)

if __name__ == "TestServer" or __name__ == "__main__":
main()
Loading