From 504a68244a1691361314f52dd9d2a933a5e38474 Mon Sep 17 00:00:00 2001 From: klemie Date: Sun, 20 Oct 2024 00:17:35 -0700 Subject: [PATCH 1/6] serail mocks working --- .../testModeUtils/instrumentationMock.py | 23 ++++++++ src/server/testModeUtils/serailMock.py | 21 +++++++ ...oint.py => wsInstrumentationEntrypoint.py} | 2 +- src/server/wsTestServerEntryPoint.py | 33 +++++++++++ src/server/wss.py | 57 ++++++++++++++++--- 5 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 src/server/testModeUtils/instrumentationMock.py create mode 100644 src/server/testModeUtils/serailMock.py rename src/server/{wsEntrypoint.py => wsInstrumentationEntrypoint.py} (81%) create mode 100644 src/server/wsTestServerEntryPoint.py diff --git a/src/server/testModeUtils/instrumentationMock.py b/src/server/testModeUtils/instrumentationMock.py new file mode 100644 index 0000000..b507f60 --- /dev/null +++ b/src/server/testModeUtils/instrumentationMock.py @@ -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.random.choice(PRESSURE_SENSOR_RANGE), + 'P_COMB_CHMBR': math.random.choice(PRESSURE_SENSOR_RANGE), + 'P_N2O_FLOW': math.random.choice(PRESSURE_SENSOR_RANGE), + 'P_N2_FLOW': math.random.choice(PRESSURE_SENSOR_RANGE), + 'P_RUN_TANK': math.random.choice(PRESSURE_SENSOR_RANGE), + 'L_RUN_TANK': math.random.choice(LOAD_SENSOR_RANGE), + 'L_THRUST': math.random.choice(LOAD_SENSOR_RANGE), + 'T_RUN_RANK': math.random.choice(TEMPERATURE_SENSOR_RANGE), + 'T_INJECTOR': math.random.choice(TEMPERATURE_SENSOR_RANGE), + 'T_COMB_CHMBR': math.random.choice(TEMPERATURE_SENSOR_RANGE), + 'T_POST_COMB': math.random.choice(TEMPERATURE_SENSOR_RANGE), + 'SHUNT': math.random.choice(TEMPERATURE_SENSOR_RANGE), + } \ No newline at end of file diff --git a/src/server/testModeUtils/serailMock.py b/src/server/testModeUtils/serailMock.py new file mode 100644 index 0000000..33ff79f --- /dev/null +++ b/src/server/testModeUtils/serailMock.py @@ -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 + } \ No newline at end of file diff --git a/src/server/wsEntrypoint.py b/src/server/wsInstrumentationEntrypoint.py similarity index 81% rename from src/server/wsEntrypoint.py rename to src/server/wsInstrumentationEntrypoint.py index 6d66550..e8e85e1 100644 --- a/src/server/wsEntrypoint.py +++ b/src/server/wsInstrumentationEntrypoint.py @@ -5,5 +5,5 @@ if __name__ == "__main__" or __name__ == "LJWebsocket": print("Entered Instrumentation") - ws = WebSocketServer("instrumentation") + ws = WebSocketServer("INSTRUMENTATION_WS") asyncio.run(ws.start_instrumentation()) diff --git a/src/server/wsTestServerEntryPoint.py b/src/server/wsTestServerEntryPoint.py new file mode 100644 index 0000000..b04b393 --- /dev/null +++ b/src/server/wsTestServerEntryPoint.py @@ -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() diff --git a/src/server/wss.py b/src/server/wss.py index 5095dbb..7f7718e 100644 --- a/src/server/wss.py +++ b/src/server/wss.py @@ -1,19 +1,27 @@ import asyncio +import time import websockets import json import logging import platform +from testModeUtils.instrumentationMock import labjack_mock as lj_mock +from testModeUtils.serailMock import serial_feedback_mock as serial_mock __name__ = "WebSocketServer" OS = platform.system() -HOST = "192.168.0.1" if OS == "Linux" else "localhost" +HOST_PRODUCTION = "192.168.0.1" +HOST_TEST = "localhost" + PORT_SERIAL = 8080 PORT_INSTRUMENTATION = 8888 INSTRUMENTATION_FILE_DATA_PATH = 'tmp.txt' +INSTRUMENTATION_WS_TYPE = "INSTRUMENTATION_WS" +SERIAL_WS_TYPE = "SERIAL_WS" + class Command: def __init__(self, command, valve, action): self.command = command @@ -27,16 +35,21 @@ def __init__(self, valve, action): self.action = action class WebSocketServer: - def __init__(self, ws_type: str): + def __init__(self, ws_type: str, test_mode: bool = False): self.__ws_type = ws_type - if self.__ws_type == "instrumentation": + self.__test_mode = test_mode + self.__host = HOST_PRODUCTION + + if test_mode: + self.__test_mode = True + self.__host = HOST_TEST + + if self.__ws_type == INSTRUMENTATION_WS_TYPE: self.__port = PORT_INSTRUMENTATION else: self.__port = PORT_SERIAL - print(f'type:{self.__ws_type}, port:{self.__port}') - - self.__host = HOST + print(f'type:{self.__ws_type}, port:{self.__port}, host:{self.__host}') self.__wss_instance = None @@ -104,6 +117,31 @@ async def __instrumentation_handler(self, websocket): await asyncio.sleep(0.1) + async def __test_instrumentation__handler(self, websocket): + print("Test Instrumentation Handler") + while True: + packet = lj_mock() + await websocket.send(json.dumps({ + "identifier": "INSTRUMENTATION", + "data": packet + })) + await asyncio.sleep(0) + + + async def __test_serial_handler(self, websocket): + print("Test Serial Handler") + while True: + async for message in websocket: + packet = json.loads(message) + await websocket.send(json.dumps(serial_mock(valve=packet['valve'], action='TRANSIT'))) + print(f"Sent: {packet['valve']} TRANSIT") + time.sleep(3) + feedback = serial_mock(valve=packet['valve'], action=packet['action']) + print(f"Sent: {feedback}") + await websocket.send(json.dumps(feedback)) + await asyncio.sleep(0) + + async def wss_reception_handler(self, queue): while True: message = await self.__incoming_queue.get() @@ -160,7 +198,8 @@ async def start_serial(self): Desc: Starts the websocket server ''' - async with websockets.serve(self.__serial_handler, self.__host, self.__port): + handler = self.__serial_handler if not self.__test_mode else self.__test_serial_handler + async with websockets.serve(handler, self.__host, self.__port): await asyncio.Future() async def start_instrumentation(self): @@ -170,6 +209,8 @@ async def start_instrumentation(self): Desc: Starts the websocket server ''' - async with websockets.serve(self.__instrumentation_handler, self.__host, self.__port): + handler = self.__instrumentation_handler if not self.__test_mode else self.__test_instrumentation__handler + async with websockets.serve(handler, self.__host, self.__port): await asyncio.Future() + \ No newline at end of file From 312ed79e5041bac804093a04238a6559ff0d3f36 Mon Sep 17 00:00:00 2001 From: jjder Date: Sun, 20 Oct 2024 13:59:58 -0700 Subject: [PATCH 2/6] Add mock instrumentation websocket --- .../testModeUtils/instrumentationMock.py | 23 -------- .../testModeUtils/mock_instrumentation.py | 57 +++++++++++++++++++ 2 files changed, 57 insertions(+), 23 deletions(-) delete mode 100644 src/server/testModeUtils/instrumentationMock.py create mode 100644 src/server/testModeUtils/mock_instrumentation.py diff --git a/src/server/testModeUtils/instrumentationMock.py b/src/server/testModeUtils/instrumentationMock.py deleted file mode 100644 index b507f60..0000000 --- a/src/server/testModeUtils/instrumentationMock.py +++ /dev/null @@ -1,23 +0,0 @@ -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.random.choice(PRESSURE_SENSOR_RANGE), - 'P_COMB_CHMBR': math.random.choice(PRESSURE_SENSOR_RANGE), - 'P_N2O_FLOW': math.random.choice(PRESSURE_SENSOR_RANGE), - 'P_N2_FLOW': math.random.choice(PRESSURE_SENSOR_RANGE), - 'P_RUN_TANK': math.random.choice(PRESSURE_SENSOR_RANGE), - 'L_RUN_TANK': math.random.choice(LOAD_SENSOR_RANGE), - 'L_THRUST': math.random.choice(LOAD_SENSOR_RANGE), - 'T_RUN_RANK': math.random.choice(TEMPERATURE_SENSOR_RANGE), - 'T_INJECTOR': math.random.choice(TEMPERATURE_SENSOR_RANGE), - 'T_COMB_CHMBR': math.random.choice(TEMPERATURE_SENSOR_RANGE), - 'T_POST_COMB': math.random.choice(TEMPERATURE_SENSOR_RANGE), - 'SHUNT': math.random.choice(TEMPERATURE_SENSOR_RANGE), - } \ No newline at end of file diff --git a/src/server/testModeUtils/mock_instrumentation.py b/src/server/testModeUtils/mock_instrumentation.py new file mode 100644 index 0000000..177adab --- /dev/null +++ b/src/server/testModeUtils/mock_instrumentation.py @@ -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 + From be90f1a347cadbfd1c9824363bb85046b64b8f40 Mon Sep 17 00:00:00 2001 From: klemie Date: Sun, 20 Oct 2024 14:46:50 -0700 Subject: [PATCH 3/6] in room changes --- src/instrumentation/read_labjack.py | 10 +++++++-- src/main.py | 2 +- src/server/instrumentationMock.py | 23 ++++++++++++++++++++ src/server/{testModeUtils => }/serailMock.py | 0 src/server/testModeUtils/___init__.py | 0 src/server/wss.py | 13 +++++------ 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/server/instrumentationMock.py rename src/server/{testModeUtils => }/serailMock.py (100%) create mode 100644 src/server/testModeUtils/___init__.py diff --git a/src/instrumentation/read_labjack.py b/src/instrumentation/read_labjack.py index 8d06faa..10b9ec1 100644 --- a/src/instrumentation/read_labjack.py +++ b/src/instrumentation/read_labjack.py @@ -131,10 +131,10 @@ ######### BEGIN USER ADJUSTABLE ######### # Stream settings -scan_frequency = 25 +scan_frequency = 1000 resolution_index = 2 settling_factor = 2 -samples_per_packet = 50 +samples_per_packet = 12 channel_settings = [(86, DIFF | X1000), # L_RUN_TANK (SEEMS GOOD. CHECK CAL) (87, DIFF | X1000), # L_THRUST (VERY NOISY) @@ -253,6 +253,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 @@ -268,6 +269,11 @@ 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) diff --git a/src/main.py b/src/main.py index b603bb7..d83fdc9 100644 --- a/src/main.py +++ b/src/main.py @@ -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) diff --git a/src/server/instrumentationMock.py b/src/server/instrumentationMock.py new file mode 100644 index 0000000..204ad4d --- /dev/null +++ b/src/server/instrumentationMock.py @@ -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), + } \ No newline at end of file diff --git a/src/server/testModeUtils/serailMock.py b/src/server/serailMock.py similarity index 100% rename from src/server/testModeUtils/serailMock.py rename to src/server/serailMock.py diff --git a/src/server/testModeUtils/___init__.py b/src/server/testModeUtils/___init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/server/wss.py b/src/server/wss.py index 7f7718e..c40c5db 100644 --- a/src/server/wss.py +++ b/src/server/wss.py @@ -1,11 +1,12 @@ +import os import asyncio import time import websockets import json import logging import platform -from testModeUtils.instrumentationMock import labjack_mock as lj_mock -from testModeUtils.serailMock import serial_feedback_mock as serial_mock +# from .instrumentationMock import labjack_mock as lj_mock +# from .serailMock import serial_feedback_mock as serial_mock __name__ = "WebSocketServer" @@ -17,7 +18,7 @@ PORT_SERIAL = 8080 PORT_INSTRUMENTATION = 8888 -INSTRUMENTATION_FILE_DATA_PATH = 'tmp.txt' +INSTRUMENTATION_FILE_DATA_PATH = '/home/uvr/Documents/GitHub/PDP-Monitoring-System/src/instrumentation/tmp.txt' INSTRUMENTATION_WS_TYPE = "INSTRUMENTATION_WS" SERIAL_WS_TYPE = "SERIAL_WS" @@ -107,14 +108,14 @@ async def __instrumentation_handler(self, websocket): ''' print("instrumentation handler") while True: - with open('../instrumentation/tmp.txt', 'r') as file: + with open(INSTRUMENTATION_FILE_DATA_PATH, 'r') as file: lines = file.readlines() if len(lines) > 1: await websocket.send(json.dumps({ "identifier": "INSTRUMENTATION", "data": json.loads(lines[0]) })) - await asyncio.sleep(0.1) + await asyncio.sleep(0.001) async def __test_instrumentation__handler(self, websocket): @@ -212,5 +213,3 @@ async def start_instrumentation(self): handler = self.__instrumentation_handler if not self.__test_mode else self.__test_instrumentation__handler async with websockets.serve(handler, self.__host, self.__port): await asyncio.Future() - - \ No newline at end of file From fc1e2845a0755b91a897de0e1ed98c22fa6255e8 Mon Sep 17 00:00:00 2001 From: klemie Date: Sun, 20 Oct 2024 18:10:45 -0700 Subject: [PATCH 4/6] Posix pipe lj -> ws --- src/instrumentation/read_labjack.py | 25 ++++++++++++++++++++----- src/server/testPipes.py | 5 +++++ src/server/wss.py | 20 ++++++++++---------- 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 src/server/testPipes.py diff --git a/src/instrumentation/read_labjack.py b/src/instrumentation/read_labjack.py index 10b9ec1..8de3d69 100644 --- a/src/instrumentation/read_labjack.py +++ b/src/instrumentation/read_labjack.py @@ -1,6 +1,8 @@ import u6 import json from thermocouple import * +import os +import posix # Gains X1 = 0b00000000 @@ -131,7 +133,7 @@ ######### BEGIN USER ADJUSTABLE ######### # Stream settings -scan_frequency = 1000 +scan_frequency = 500 resolution_index = 2 settling_factor = 2 samples_per_packet = 12 @@ -212,6 +214,16 @@ ") 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...") @@ -220,7 +232,7 @@ d.streamStart() try: - with open('instrumentation_data.txt', 'w') as file: + with open(pipe_path, 'w') as file: # Contains sensor values in SI units converted = {} @@ -289,13 +301,16 @@ # 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() + # with open('tmp.txt', 'w') as tmp: + # tmp.write(f'{json.dumps(converted)}') + # tmp.write('\n!') except: print("Interrupt signal received!") finally: d.streamStop() print("Stream stopped.\n") d.close() + os.remove(pipe_path) + diff --git a/src/server/testPipes.py b/src/server/testPipes.py new file mode 100644 index 0000000..dedd31c --- /dev/null +++ b/src/server/testPipes.py @@ -0,0 +1,5 @@ + +while True: + with open("../instrumentation/data") as pipe: + line = pipe.readline() + print(line) diff --git a/src/server/wss.py b/src/server/wss.py index c40c5db..cf0db97 100644 --- a/src/server/wss.py +++ b/src/server/wss.py @@ -18,7 +18,8 @@ PORT_SERIAL = 8080 PORT_INSTRUMENTATION = 8888 -INSTRUMENTATION_FILE_DATA_PATH = '/home/uvr/Documents/GitHub/PDP-Monitoring-System/src/instrumentation/tmp.txt' +INSTRUMENTATION_FILE_DATA_PATH = "/home/uvr/Documents/GitHub/PDP-Monitoring-System/src/instrumentation/data" + INSTRUMENTATION_WS_TYPE = "INSTRUMENTATION_WS" SERIAL_WS_TYPE = "SERIAL_WS" @@ -107,15 +108,14 @@ async def __instrumentation_handler(self, websocket): Handles the websocket requests and serial feedback to send over the websocket ''' print("instrumentation handler") - while True: - with open(INSTRUMENTATION_FILE_DATA_PATH, 'r') as file: - lines = file.readlines() - if len(lines) > 1: - await websocket.send(json.dumps({ - "identifier": "INSTRUMENTATION", - "data": json.loads(lines[0]) - })) - await asyncio.sleep(0.001) + with open(INSTRUMENTATION_FILE_DATA_PATH, 'r') as file: + while True: + lines = file.readline() + await websocket.send(json.dumps({ + "identifier": "INSTRUMENTATION", + "data": json.loads(lines) + })) + await asyncio.sleep(0.001) async def __test_instrumentation__handler(self, websocket): From 56c4258627726f5acce254a8176678049cc2f46c Mon Sep 17 00:00:00 2001 From: klemie Date: Thu, 7 Nov 2024 19:20:38 -0800 Subject: [PATCH 5/6] Fixed Websocket crashing issue --- src/instrumentation/.thermocouple.py.swp | Bin 0 -> 12288 bytes src/instrumentation/read_labjack.py | 8 +++----- src/instrumentation/thermocouple.py | 10 +++++----- src/server/wsInstrumentationEntrypoint.py | 1 + src/server/wss.py | 15 ++++++++++----- 5 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 src/instrumentation/.thermocouple.py.swp diff --git a/src/instrumentation/.thermocouple.py.swp b/src/instrumentation/.thermocouple.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..8cd281a666d179a250624d5e6f4393fe7d6f9df3 GIT binary patch literal 12288 zcmeI2%ZnUE9LFn)(HP?+5f9>_bQO|Grl+d=#HnVqU%J^C}h z`pt&)I_o@nl1fVuTz3-kYd|*+-S;fH%m|rn$GY{p*V)eL>&EDS7cgP#c%)|IaFR@0 zCx*!rv-Q@gOyTUNVd(t>i)&k0TY$wnm|~ zb@3mqX@ZOaV*mzb$)G9s5LAry_?GzFb0ePW8hyhKyk%kGK(j4 zKb|(*YSV*M__oE}0|Z@~`PB{@<4$)w8YR7ru+a#}yazwD#Jm(h!{DlC2Ilx;8 z@RkGIg#dRsz*z`zmILgC0DCz=ECh(<0A2~0_h#mf(Keemm8R%1nmeNn${Ay+mKEBL z!ogrl!{@g`mC$h~Z7!7MpGg)Qj;7(^qKCEv%-CcWX3`gDuJB#A?3U=xd(r3nwo~@` ziHFlDqi>CfLE)i*1c=QuS*l?`bnnK73KLQ5$(RVd-i zahV&)vPT=yqon8A!gK7hCpTilt+1<;X(JdS188@h$_`Q`T;HPY*_Ac4k|wvf=LzXo z+H)lwD|n(@_%EHLcjwB0*WLWRl*$gZhsxXH=CdTTIcE~j%hCL5YXf8pZgj2fSn}&TFP11y#rehQi+;BD}>FpM0_ZSMKDyrYXI2;r|A0VV34rr&F zMBUX&%pnxkGUzZ_p^widNatqIPI=+M<^fp-tF#{2mOxGM1#;Lf0(ka?r}W97uH`Xb*9-ia2jokn z)gcWK(m}?R0r?X8fKYtLN{0oG>+>=oJ7FrV7J;QZ-2jEbDVE5xUFoVpk^ZhLeIHw# z-$ZuQls-0CKn762oShOYrndd2+NAw#A{F=cI?I^6a}oAweUbAP6A zQhG|w;-11o=Q^EqDq*s Date: Sun, 24 Nov 2024 11:59:06 -0800 Subject: [PATCH 6/6] Added timestamps to packets --- src/instrumentation/read_labjack.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/instrumentation/read_labjack.py b/src/instrumentation/read_labjack.py index 7d54dd6..57cf36d 100644 --- a/src/instrumentation/read_labjack.py +++ b/src/instrumentation/read_labjack.py @@ -3,6 +3,7 @@ from thermocouple import * import os import posix +import datetime # Gains X1 = 0b00000000 @@ -134,8 +135,8 @@ # Stream settings scan_frequency = 500 -resolution_index = 2 -settling_factor = 2 +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) @@ -197,15 +198,17 @@ SettlingFactor = settling_factor, SamplesPerPacket = samples_per_packet) -# 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 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) + \ @@ -227,9 +230,11 @@ print("No LabJack device connected. Exiting...") exit() else: + print("Starting stream...") d.streamStart() try: + #with open('data_log.txt', 'w') as data_log: with open(pipe_path, 'w') as file: # Contains sensor values in SI units @@ -237,7 +242,6 @@ 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. @@ -299,10 +303,15 @@ 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') file.flush() + # Log to PDP for later analysis + #data_log.write(f'{json.dumps(converted)}\n') + except: print("Interrupt signal received!") finally: