Skip to content

Commit 4e5cd02

Browse files
committed
evm_2.2.4: improve arduino server, also possible to use arduino zero, fix requirements
1 parent cf5c472 commit 4e5cd02

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed
File renamed without changes.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ pyserial
44
intelhex
55
matplotlib
66
numpy
7-
pyzmq==25.1.2
7+
pyzmq
88
pyinstaller

tmf8829/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Version Changelog
22
1.0 first released version
3+
2.0 arduino zmq server improved

tmf8829/zeromq/tmf8829_zeromq_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class ZeroMqClient:
3232
"""ZeroMQ client"""
3333

34-
VERSION = 0x0003
34+
VERSION = 0x0004
3535
"""Version
3636
- 1 First zeromq client release version
3737
- 2 Second logger versions
@@ -43,6 +43,7 @@ class ZeroMqClient:
4343
- 4 option to save previous data also uncompressed (configuration changed at measurement)
4444
fix, check if filename exists also for gz files
4545
for storage use os pathname
46+
store 3d point cloud values and distance
4647
"""
4748

4849
def __init__(self) -> None:

tmf8829/zeromq/tmf8829_zeromq_server_arduino.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from time import sleep
1818

1919
### DRIVER ATTRIBUTES ###
20-
#BAUDRATE = 115200
21-
BAUDRATE = 2000000
2220

2321
class CommandError(Exception):
2422
"""Command error."""
@@ -75,10 +73,10 @@ class ZeroMqArduinoServer(ZeroMqServer):
7573
APPLICATION_ID = 0x01
7674
BOOTLOADER_ID = 0x80
7775

78-
def __init__(self, port: str, cmd_poll_interval=1.0) -> None:
76+
def __init__(self, port: str, cmd_poll_interval=1.0, baudrate=2000000) -> None:
7977
super().__init__(cmd_poll_interval=cmd_poll_interval)
8078

81-
self._com = Serial(baudrate=BAUDRATE, timeout=0.1)
79+
self._com = Serial(baudrate=baudrate, timeout=0.1)
8280
self._com.port = port
8381
self._cmd_resp_lines = Queue()
8482
self._read_thread: Optional[Thread] = None
@@ -87,16 +85,35 @@ def __init__(self, port: str, cmd_poll_interval=1.0) -> None:
8785
self.rawHistograms = 0
8886
self.hostType = TMF8829_ZEROMQ_HOST_ARDUINO_BOARD
8987
self._result_object = bytearray()
90-
88+
9189
@staticmethod
92-
def device_connected() -> List[str]:
90+
def print_connected_com_ports():
91+
"""
92+
print connected com ports
9393
"""
94-
device connected
94+
ports = list_ports.comports()
95+
for port in ports:
96+
print(f"Com Port Device: {port.device}, Description: {port.description}, HWID: {port.hwid}")
97+
return
9598

99+
@staticmethod
100+
def device_connected(dev="Uno") -> List[str]:
101+
"""
102+
Return a list of devices that are connected.
103+
Args:
104+
dev: "Uno" for Arduino Uno,"Zero" for Arduino Zero; other devices not implemented
96105
Returns:
97106
List[str]: list ports
98107
"""
99-
return [e.name for e in list_ports.grep(r"USB VID:PID=2341.*")]
108+
dl = []
109+
if dev == "Uno":
110+
dl = [e.name for e in list_ports.grep(r"USB VID:PID=2341.*")]
111+
elif dev == "Zero":
112+
dl = [e.name for e in list_ports.grep(r"USB VID:PID=03EB:2157*")]
113+
else:
114+
logging.info("device search not implemented")
115+
116+
return dl
100117

101118
def _clear_queue(self, queue: Queue) -> None:
102119
"""
@@ -266,7 +283,7 @@ def _reopen_communication_to_device(self) -> None:
266283
"""
267284
logger.info("--XXX Reopen device connection. XXX--")
268285

269-
self._close_communication_to_device()
286+
self._com.close()
270287
sleep(0.1)
271288
self._open_communication_to_device()
272289

@@ -310,16 +327,6 @@ def stop_measurement(self) -> bool:
310327
state = line.split(b"=")[1]
311328
except Exception as exc:
312329
state = FwStates.UNKNOWN.value
313-
314-
315-
if state != FwStates.STOPPED.value:
316-
logger.info("Could not stop the measurement. Try a second time!!!")
317-
try: # Try once more
318-
for line in self._send_command(Commands.STOP_MEAS):
319-
if line.startswith(b"state"):
320-
state = line.split(b"=")[1]
321-
except Exception as exc:
322-
state = FwStates.UNKNOWN.value
323330

324331
self._meas_running = False
325332
self._nr_subframes = 0
@@ -330,7 +337,7 @@ def stop_measurement(self) -> bool:
330337
logger.info("{} Result processing stopped.".format(time.time()))
331338
logger.info("Number lost result frames are at least {}".format(self.lost_results))
332339
else:
333-
logger.info("Stop the measurement FAILED. Try a second time!!!")
340+
logger.info("Stop the measurement FAILED. Re-open com!!!")
334341
self._reopen_communication_to_device
335342

336343
return self._meas_running
@@ -430,17 +437,24 @@ def set_pre_config_cmd(self, cmd:bytes) -> None:
430437
#####################################################################################
431438

432439
if __name__ == "__main__":
433-
import pathlib
440+
441+
BAUDRATE=2000000
434442

435443
logging.basicConfig(level=logging.DEBUG,format='%(levelname)s %(name)s.%(funcName)s:%(lineno)d %(message)s')
436-
444+
ZeroMqArduinoServer.print_connected_com_ports()
445+
437446
com_port = ZeroMqArduinoServer.device_connected()
438447

439448
if len(com_port) >= 1:
440-
logging.info("Board found Com {}".format(com_port[0]))
441-
server = ZeroMqArduinoServer(port= com_port[0], cmd_poll_interval = 0.01)
442-
server.start(cmd_addr= TMF8829_ZEROMQ_CMD_SERVER_ADDR, result_addr=TMF8829_ZEROMQ_RESULT_SERVER_ADDR)
449+
logging.info("Arduino Uno Board found Com {}".format(com_port[0]))
450+
else:
451+
logging.info("No Arduino Uno Board found")
452+
com_port = ZeroMqArduinoServer.device_connected("Zero")
453+
BAUDRATE=1000000
443454

455+
if len(com_port) >= 1:
456+
server = ZeroMqArduinoServer(port= com_port[0], cmd_poll_interval = 0.01, baudrate=BAUDRATE)
457+
server.start(cmd_addr= TMF8829_ZEROMQ_CMD_SERVER_ADDR, result_addr=TMF8829_ZEROMQ_RESULT_SERVER_ADDR)
444458
try:
445459
while True:
446460
server.process()

0 commit comments

Comments
 (0)