From c3c5694cac5f5f8011667c275b4b79d393dcf15e Mon Sep 17 00:00:00 2001 From: IceCreamMilkyTea Date: Thu, 2 Jan 2025 18:27:32 +0800 Subject: [PATCH 1/5] monitorSensors --- OpenCatPythonAPI/PetoiRobot/ardSerial.py | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/OpenCatPythonAPI/PetoiRobot/ardSerial.py b/OpenCatPythonAPI/PetoiRobot/ardSerial.py index 38e9466..485ad22 100644 --- a/OpenCatPythonAPI/PetoiRobot/ardSerial.py +++ b/OpenCatPythonAPI/PetoiRobot/ardSerial.py @@ -859,6 +859,34 @@ def refreshBox(ls): tk.messagebox.showwarning(title=txt('Warning'), message=txt('Manual mode')) window.mainloop() +def monitorSensors(ports, pinDict): + result = {} + + if 'voltage' in pinDict: + voltage = send(ports, ["R", [97, pinDict['voltage']], 0]) + result['voltage'] = voltage + + if 'angle' in pinDict: + if pinDict['angle'] == 'all': + angleList = send(ports, ["j", 0]) + result['angle'] = angleList + else: + angle = send(ports, ["j", [pinDict['angle']], 0]) + result['angle'] = angle + + if 'distance' in pinDict: + distance = send(ports, ["XU", pinDict['distance'], 0]) + result['distance'] = distance + + return result + + +def keepMonitoring(ports, pinDict): + while True and len(ports): + time.sleep(0.05) + result = monitorSensors(ports, pinDict) + print(result) + #if need to open serial port, use objects goodPorts goodPorts = {} # goodPorts is a dictionary, the structure is {SerialPort Object(): portName(string), ...} @@ -870,11 +898,17 @@ def refreshBox(ls): returnValue = '' timePassed = 0 +# pinDict = {'voltage': 0xA7, 'angle': 'all', 'distance': [16, 17]} +# pinDict = {'voltage': YOUR_VOLATGE_PIN, 'angle': 'all', 'distance': [YOUR_triggerPin, YOUR_echoPin]} +# pinDict = {'angle': ANGLE_INDEX} # monitoring a single angle + if __name__ == '__main__': try: connectPort(goodPorts) t = threading.Thread(target=keepCheckingPort, args=(goodPorts,), daemon=True) t.start() + # t_monitor = threading.Thread(target=keepMonitoring, args=(goodPorts, pinDict), daemon=True) + # t_monitor.start() if len(sys.argv) >= 2: if len(sys.argv) == 2: cmd = sys.argv[1] From 9000d860677e715f0ca84bb38eec50bff77e4000 Mon Sep 17 00:00:00 2001 From: IceCreamMilkyTea Date: Thu, 2 Jan 2025 20:59:54 +0800 Subject: [PATCH 2/5] Split sensor monitors and add callback usage --- OpenCatPythonAPI/PetoiRobot/ardSerial.py | 72 +++++++++++++++--------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/OpenCatPythonAPI/PetoiRobot/ardSerial.py b/OpenCatPythonAPI/PetoiRobot/ardSerial.py index 485ad22..debccd2 100644 --- a/OpenCatPythonAPI/PetoiRobot/ardSerial.py +++ b/OpenCatPythonAPI/PetoiRobot/ardSerial.py @@ -859,33 +859,35 @@ def refreshBox(ls): tk.messagebox.showwarning(title=txt('Warning'), message=txt('Manual mode')) window.mainloop() -def monitorSensors(ports, pinDict): - result = {} - - if 'voltage' in pinDict: - voltage = send(ports, ["R", [97, pinDict['voltage']], 0]) - result['voltage'] = voltage - - if 'angle' in pinDict: - if pinDict['angle'] == 'all': - angleList = send(ports, ["j", 0]) - result['angle'] = angleList +def monitoringVoltage(ports, VoltagePin, timer, callback): + while True and len(ports): + time.sleep(timer) + voltage = send(ports, ["R", [97, VoltagePin], 0]) + if callback is not None: + callback(voltage) else: - angle = send(ports, ["j", [pinDict['angle']], 0]) - result['angle'] = angle - - if 'distance' in pinDict: - distance = send(ports, ["XU", pinDict['distance'], 0]) - result['distance'] = distance - - return result + print("Current Voltage:" + str(voltage)) +def monitoringDistance(ports, trigerPin, echoPin, timer, callback): + while True and len(ports): + time.sleep(timer) + distance = send(ports, ["XU", [trigerPin, echoPin], 0]) + if callback is not None: + callback(distance) + else: + print("Current Distance:" + str(distance)) -def keepMonitoring(ports, pinDict): +def monitoringJoint(ports, jointIndex, timer, callback): while True and len(ports): - time.sleep(0.05) - result = monitorSensors(ports, pinDict) - print(result) + time.sleep(timer) + if jointIndex == 0: + angel = send(ports, ["j", jointIndex]) + else: + angel = send(ports, ["j", [jointIndex], 0]) + if callback is not None: + callback(angel) + else: + print("Current Angel:" + str(angel)) #if need to open serial port, use objects goodPorts goodPorts = {} # goodPorts is a dictionary, the structure is {SerialPort Object(): portName(string), ...} @@ -898,17 +900,31 @@ def keepMonitoring(ports, pinDict): returnValue = '' timePassed = 0 -# pinDict = {'voltage': 0xA7, 'angle': 'all', 'distance': [16, 17]} -# pinDict = {'voltage': YOUR_VOLATGE_PIN, 'angle': 'all', 'distance': [YOUR_triggerPin, YOUR_echoPin]} -# pinDict = {'angle': ANGLE_INDEX} # monitoring a single angle +''' +# Monitor callback usage sample +def voltageHanle(voltage): + if voltage <0.5: + print("Low Power Warning") # do something to handle low power + +def distanceHanle(distance): + if distance <0.5: + print("Small Distance Warning") # do something to handle small distance +''' + if __name__ == '__main__': try: connectPort(goodPorts) t = threading.Thread(target=keepCheckingPort, args=(goodPorts,), daemon=True) t.start() - # t_monitor = threading.Thread(target=keepMonitoring, args=(goodPorts, pinDict), daemon=True) - # t_monitor.start() + ### Monitor Threads + # t_monitor_voltage = threading.Thread(target=monitoringVoltage, args=(goodPorts, 0xA7, 60, voltageHanle), daemon=True) + # t_monitor_voltage.start() + # t_monitor_distance = threading.Thread(target=monitoringDistance, args=(goodPorts, 16, 17, 0.5, distanceHanle), daemon=True) + # t_monitor_distance.start() + # t_monitor_joint = threading.Thread(target=monitoringJoint, args=(goodPorts, 0 , 0.5, None), daemon=True) + # t_monitor_joint.start() + if len(sys.argv) >= 2: if len(sys.argv) == 2: cmd = sys.argv[1] From facce3842519a4dc7f05e51b06273dc399175ce9 Mon Sep 17 00:00:00 2001 From: IceCreamMilkyTea Date: Fri, 3 Jan 2025 23:53:08 +0800 Subject: [PATCH 3/5] Add read_MCU_loop --- OpenCatPythonAPI/PetoiRobot/ardSerial.py | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/OpenCatPythonAPI/PetoiRobot/ardSerial.py b/OpenCatPythonAPI/PetoiRobot/ardSerial.py index debccd2..94f7161 100644 --- a/OpenCatPythonAPI/PetoiRobot/ardSerial.py +++ b/OpenCatPythonAPI/PetoiRobot/ardSerial.py @@ -157,7 +157,7 @@ def serialWriteByte(port, var=None): var.insert(1, var[0][1:]) var[1:] = list(map(int, var[1:])) in_str = token.encode() + struct.pack('b' * (len(var) - 1), *var[1:]) + '~'.encode() - elif token == 'w' or token == 'k' or token == 'X': + elif token == 'w' or token == 'k' or token == 'X' or token == 'g': in_str = var[0] + '\n' else: in_str = token + '\n' @@ -888,7 +888,30 @@ def monitoringJoint(ports, jointIndex, timer, callback): callback(angel) else: print("Current Angel:" + str(angel)) - + +def read_MCU_loop(PortList, callback=None): + result = send(PortList, ['gP', 0]) + print("send results " + str(result)) + p = list(PortList.keys()) + serialObject = p[0] + while True: + try: + if PortList: + data = serialObject.main_engine.readline() + if data: + try: + decoded_data = data.decode('ISO-8859-1').strip() + if callback is not None: + callback(decoded_data) + else: + print(str(decoded_data)) + except Exception as e: + logger.error(f"Error decoding serial port data: {e}") + time.sleep(0.005) # avoid high CPU usage + except Exception as e: + logger.error(f"Error reading serial port data: {e}") + break + #if need to open serial port, use objects goodPorts goodPorts = {} # goodPorts is a dictionary, the structure is {SerialPort Object(): portName(string), ...} @@ -917,6 +940,8 @@ def distanceHanle(distance): connectPort(goodPorts) t = threading.Thread(target=keepCheckingPort, args=(goodPorts,), daemon=True) t.start() + t1=threading.Thread(target=read_MCU_loop, args=(goodPorts, None)) + t1.start() ### Monitor Threads # t_monitor_voltage = threading.Thread(target=monitoringVoltage, args=(goodPorts, 0xA7, 60, voltageHanle), daemon=True) # t_monitor_voltage.start() From ff946c4c972d2225773944f0e0a930df1423977a Mon Sep 17 00:00:00 2001 From: IceCreamMilkyTea Date: Fri, 3 Jan 2025 23:59:46 +0800 Subject: [PATCH 4/5] Update ardSerial.py in serialMaster --- serialMaster/ardSerial.py | 77 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/serialMaster/ardSerial.py b/serialMaster/ardSerial.py index e2f2a2e..5ea364f 100644 --- a/serialMaster/ardSerial.py +++ b/serialMaster/ardSerial.py @@ -917,6 +917,59 @@ def refreshBox(ls): tk.messagebox.showwarning(title=txt('Warning'), message=txt('Manual mode')) window.mainloop() +def monitoringVoltage(ports, VoltagePin, timer, callback): + while True and len(ports): + time.sleep(timer) + voltage = send(ports, ["R", [97, VoltagePin], 0]) + if callback is not None: + callback(voltage) + else: + print("Current Voltage:" + str(voltage)) + +def monitoringDistance(ports, trigerPin, echoPin, timer, callback): + while True and len(ports): + time.sleep(timer) + distance = send(ports, ["XU", [trigerPin, echoPin], 0]) + if callback is not None: + callback(distance) + else: + print("Current Distance:" + str(distance)) + +def monitoringJoint(ports, jointIndex, timer, callback): + while True and len(ports): + time.sleep(timer) + if jointIndex == 0: + angel = send(ports, ["j", jointIndex]) + else: + angel = send(ports, ["j", [jointIndex], 0]) + if callback is not None: + callback(angel) + else: + print("Current Angel:" + str(angel)) + +def read_MCU_loop(PortList, callback=None): + result = send(PortList, ['gP', 0]) + print("send results " + str(result)) + p = list(PortList.keys()) + serialObject = p[0] + while True: + try: + if PortList: + data = serialObject.main_engine.readline() + if data: + try: + decoded_data = data.decode('ISO-8859-1').strip() + if callback is not None: + callback(decoded_data) + else: + print(str(decoded_data)) + except Exception as e: + logger.error(f"Error decoding serial port data: {e}") + time.sleep(0.005) # avoid high CPU usage + except Exception as e: + logger.error(f"Error reading serial port data: {e}") + break + #if need to open serial port, use objects goodPorts goodPorts = {} # goodPorts is a dictionary, the structure is {SerialPort Object(): portName(string), ...} @@ -928,11 +981,33 @@ def refreshBox(ls): returnValue = '' timePassed = 0 +''' +# Monitor callback usage sample +def voltageHanle(voltage): + if voltage <0.5: + print("Low Power Warning") # do something to handle low power + +def distanceHanle(distance): + if distance <0.5: + print("Small Distance Warning") # do something to handle small distance +''' + + if __name__ == '__main__': try: connectPort(goodPorts) - t = threading.Thread(target=keepCheckingPort, args=(goodPorts,)) + t = threading.Thread(target=keepCheckingPort, args=(goodPorts,), daemon=True) t.start() + t1=threading.Thread(target=read_MCU_loop, args=(goodPorts, None), daemon=True) + t1.start() + ### Monitor Threads + # t_monitor_voltage = threading.Thread(target=monitoringVoltage, args=(goodPorts, 0xA7, 60, voltageHanle), daemon=True) + # t_monitor_voltage.start() + # t_monitor_distance = threading.Thread(target=monitoringDistance, args=(goodPorts, 16, 17, 0.5, distanceHanle), daemon=True) + # t_monitor_distance.start() + # t_monitor_joint = threading.Thread(target=monitoringJoint, args=(goodPorts, 0 , 0.5, None), daemon=True) + # t_monitor_joint.start() + if len(sys.argv) >= 2: if len(sys.argv) == 2: cmd = sys.argv[1] From 0ed2655babde385b627432232537405b98d99116 Mon Sep 17 00:00:00 2001 From: IceCreamMilkyTea Date: Sat, 4 Jan 2025 00:46:14 +0800 Subject: [PATCH 5/5] fix format --- serialMaster/ardSerial.py | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/serialMaster/ardSerial.py b/serialMaster/ardSerial.py index 5ea364f..a29dfc5 100644 --- a/serialMaster/ardSerial.py +++ b/serialMaster/ardSerial.py @@ -948,27 +948,27 @@ def monitoringJoint(ports, jointIndex, timer, callback): print("Current Angel:" + str(angel)) def read_MCU_loop(PortList, callback=None): - result = send(PortList, ['gP', 0]) - print("send results " + str(result)) - p = list(PortList.keys()) - serialObject = p[0] - while True: - try: - if PortList: - data = serialObject.main_engine.readline() - if data: - try: - decoded_data = data.decode('ISO-8859-1').strip() - if callback is not None: - callback(decoded_data) - else: - print(str(decoded_data)) - except Exception as e: - logger.error(f"Error decoding serial port data: {e}") - time.sleep(0.005) # avoid high CPU usage - except Exception as e: - logger.error(f"Error reading serial port data: {e}") - break + result = send(PortList, ['gP', 0]) + print("send results " + str(result)) + p = list(PortList.keys()) + serialObject = p[0] + while True: + try: + if PortList: + data = serialObject.main_engine.readline() + if data: + try: + decoded_data = data.decode('ISO-8859-1').strip() + if callback is not None: + callback(decoded_data) + else: + print(str(decoded_data)) + except Exception as e: + logger.error(f"Error decoding serial port data: {e}") + time.sleep(0.005) # avoid high CPU usage + except Exception as e: + logger.error(f"Error reading serial port data: {e}") + break #if need to open serial port, use objects goodPorts goodPorts = {} # goodPorts is a dictionary, the structure is {SerialPort Object(): portName(string), ...}