Commit 6a7d1d75 authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Adds quit method to stop listening to tcp port and exit OhmPi.py

Showing with 17 additions and 6 deletions
+17 -6
...@@ -15,7 +15,8 @@ OHMPI_CONFIG = { ...@@ -15,7 +15,8 @@ OHMPI_CONFIG = {
'version': 2, 'version': 2,
'max_elec': 64, 'max_elec': 64,
'board_address': {'A': 0x70, 'B': 0x71, 'M': 0x72, 'N': 0x73}, # def. {'A': 0x76, 'B': 0x71, 'M': 0x74, 'N': 0x70} 'board_address': {'A': 0x70, 'B': 0x71, 'M': 0x72, 'N': 0x73}, # def. {'A': 0x76, 'B': 0x71, 'M': 0x74, 'N': 0x70}
} 'settings': 'ohmpi_settings.json'
} # TODO: add a dictionary with INA models and associated gain values
CONTROL_CONFIG = { CONTROL_CONFIG = {
'tcp_port': 5555, 'tcp_port': 5555,
......
...@@ -15,7 +15,7 @@ publisher_config['topic'] = MQTT_CONTROL_CONFIG['ctrl_topic'] ...@@ -15,7 +15,7 @@ publisher_config['topic'] = MQTT_CONTROL_CONFIG['ctrl_topic']
publisher_config.pop('ctrl_topic') publisher_config.pop('ctrl_topic')
settings = { settings = {
'injection_duration': 0.2, 'injection_duration': 0.2,
'nbr_meas': 100, 'nbr_meas': 1,
'sequence_delay': 1, 'sequence_delay': 1,
'nb_stack': 1, 'nb_stack': 1,
'export_path': 'data/measurement.csv' 'export_path': 'data/measurement.csv'
......
...@@ -20,7 +20,7 @@ from datetime import datetime ...@@ -20,7 +20,7 @@ from datetime import datetime
from termcolor import colored from termcolor import colored
import threading import threading
from logging_setup import setup_loggers from logging_setup import setup_loggers
from config import CONTROL_CONFIG from config import CONTROL_CONFIG, OHMPI_CONFIG
from subprocess import Popen from subprocess import Popen
# finish import (done only when class is instantiated as some libs are only available on arm64 platform) # finish import (done only when class is instantiated as some libs are only available on arm64 platform)
...@@ -118,6 +118,7 @@ class OhmPi(object): ...@@ -118,6 +118,7 @@ class OhmPi(object):
self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=0x49) self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=0x49)
# Starts the command processing thread # Starts the command processing thread
self.cmd_listen = True
self.cmd_thread = threading.Thread(target=self.process_commands) self.cmd_thread = threading.Thread(target=self.process_commands)
self.cmd_thread.start() self.cmd_thread.start()
...@@ -650,7 +651,7 @@ class OhmPi(object): ...@@ -650,7 +651,7 @@ class OhmPi(object):
print(colored(f'Listening to commands on tcp port {tcp_port}.' print(colored(f'Listening to commands on tcp port {tcp_port}.'
f' Make sure your client interface is running and bound to this port...', 'blue')) f' Make sure your client interface is running and bound to this port...', 'blue'))
self.exec_logger.debug(f'Start listening for commands on port {tcp_port}') self.exec_logger.debug(f'Start listening for commands on port {tcp_port}')
while True: while self.cmd_listen:
message = socket.recv() message = socket.recv()
self.exec_logger.debug(f'Received command: {message}') self.exec_logger.debug(f'Received command: {message}')
e = None e = None
...@@ -780,6 +781,15 @@ class OhmPi(object): ...@@ -780,6 +781,15 @@ class OhmPi(object):
if self.thread is not None: if self.thread is not None:
self.thread.join() self.thread.join()
self.exec_logger.debug(f'Status: {self.status}') self.exec_logger.debug(f'Status: {self.status}')
def quit(self):
"""Quit OhmPi.
"""
self.cmd_listen = False
if self.cmd_thread is not None:
self.cmd_thread.join()
self.exec_logger.debug(f'Stopped listening to tcp port.')
exit()
...@@ -812,6 +822,6 @@ if __name__ == "__main__": ...@@ -812,6 +822,6 @@ if __name__ == "__main__":
# start interface # start interface
Popen(['python', CONTROL_CONFIG['interface']]) Popen(['python', CONTROL_CONFIG['interface']])
ohmpi = OhmPi(settings='ohmpi_settings.json') ohmpi = OhmPi(settings=OHMPI_CONFIG['settings'])
{ {
"nb_electrodes": 64, "nb_electrodes": 64,
"injection_duration":0.2, "injection_duration":0.2,
"nbr_meas": 100, "nbr_meas": 1,
"sequence_delay": 1, "sequence_delay": 1,
"nb_stack": 1, "nb_stack": 1,
"export_path": "data/measurement.csv" "export_path": "data/measurement.csv"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment