Autostart added, EV3 error handling improved

This commit is contained in:
Luis Waldhauser 2023-06-04 10:37:24 +02:00
parent a368637346
commit 5e21e4b2c5
2 changed files with 46 additions and 17 deletions

View File

@ -0,0 +1,9 @@
#!/bin/bash
# Script to automatically start the robot operating programm
# Has to be included to autostart of the RaspberryPi
cd ~/Documents/GIT/projektarbeit_duelger_waldhauser_caliskan/
source .venv/bin/activate
cd software/roboter/raspy
python3 mainProg.py

View File

@ -6,7 +6,6 @@ import logging
import raspy_sensors as Sensors import raspy_sensors as Sensors
from defines import Topics, SENSORDATA, ALLSENSORDATA from defines import Topics, SENSORDATA, ALLSENSORDATA
import time
def measure_send_data(plantID, actionID, client: mqtt.Client): def measure_send_data(plantID, actionID, client: mqtt.Client):
""" """
@ -47,9 +46,13 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client):
# errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}') # errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}')
if errorCode != 0: if errorCode != 0:
logging.error(f"Robot Error {errorCode} occurred") if errorCode == 65280:
logging.info("Drive Plant aborted, Robot at starting position") errorMessage = "EV3 not connected"
client.publish(Topics["ROBOT_DATA_ERROR"], "Drive Plant aborted, Robot at starting position", qos=1) else:
errorMessage = "Motor or Sensor unplugged"
logging.info(f"{errorMessage}, Drive Plant aborted, Robot at starting position")
client.publish(Topics["ROBOT_DATA_ERROR"], f"{errorMessage}, Drive Plant aborted, Robot at starting position", qos=1)
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1)
return return
@ -65,9 +68,13 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client):
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}') errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}')
if errorCode != 0: if errorCode != 0:
logging.error(f"Robot Error {errorCode} occurred") if errorCode == 65280:
logging.info(f"Drive Home aborted, Robot at plant {plantID}") errorMessage = "EV3 not connected"
client.publish(Topics["ROBOT_DATA_ERROR"], f"Drive Home aborted, Robot at plant {plantID}", qos=1) else:
errorMessage = "Motor or Sensor unplugged"
logging.info(f"{errorMessage}, Drive Plant aborted, Robot at starting position")
client.publish(Topics["ROBOT_DATA_ERROR"], f"{errorMessage}, Drive Plant aborted, Robot at plant {plantID}", qos=1)
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1)
return return
@ -98,10 +105,15 @@ def drive_plant_all_thread(plantIDs: list, actionID, client: mqtt.Client):
# FIXME Only possible with color codes # FIXME Only possible with color codes
logging.info(f"Robot Driving to plant {plant}") logging.info(f"Robot Driving to plant {plant}")
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plant}') errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plant}')
if errorCode != 0: if errorCode != 0:
logging.error(f"Robot Error {errorCode} occurred") if errorCode == 65280:
logging.info("Drive Plant aborted, Robot at starting position") errorMessage = "EV3 not connected"
client.publish(Topics["ROBOT_DATA_ERROR"], "Drive Plant aborted, Robot at starting position", qos=1) else:
errorMessage = "Motor or Sensor unplugged"
logging.info(f"{errorMessage}, Drive Plant aborted, Robot at starting position")
client.publish(Topics["ROBOT_DATA_ERROR"], f"{errorMessage}, Drive Plant aborted, Robot at starting position", qos=1)
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1)
return return
@ -125,9 +137,13 @@ def drive_plant_all_thread(plantIDs: list, actionID, client: mqtt.Client):
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_arm.py {plant}') errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_arm.py {plant}')
if errorCode != 0: if errorCode != 0:
logging.error(f"Robot Error {errorCode} occurred") if errorCode == 65280:
logging.info("Drive Plant aborted, Robot at starting position") errorMessage = "EV3 not connected"
client.publish(Topics["ROBOT_DATA_ERROR"], f"Rotate arm aborted, Robot at plant {plant}", qos=1) else:
errorMessage = "Motor or Sensor unplugged"
logging.info(f"{errorMessage}, Drive Plant aborted, Robot at starting position")
client.publish(Topics["ROBOT_DATA_ERROR"], f"{errorMessage}, Drive Plant aborted, Robot at plant {plant}", qos=1)
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1)
return return
@ -138,9 +154,13 @@ def drive_plant_all_thread(plantIDs: list, actionID, client: mqtt.Client):
logging.info("Robot driving home") logging.info("Robot driving home")
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back_straight.py') errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back_straight.py')
if errorCode != 0: if errorCode != 0:
logging.error(f"Robot Error {errorCode} occurred") if errorCode == 65280:
logging.info(f"Drive Home aborted, Robot at plant {plant}") errorMessage = "EV3 not connected"
client.publish(Topics["ROBOT_DATA_ERROR"], f"Drive Home aborted, Robot at plant {plant}", qos=1) else:
errorMessage = "Motor or Sensor unplugged"
logging.info(f"{errorMessage}, Drive Plant aborted, Robot at starting position")
client.publish(Topics["ROBOT_DATA_ERROR"], f"{errorMessage}, Drive Plant aborted, Robot at plant {plant}", qos=1)
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1)
return return
@ -231,7 +251,7 @@ def get_batteryStatus(client: mqtt.Client, userdata, message: mqtt.MQTTMessage):
try: try:
batteryLevel = int(os.popen('sshpass -p maker ssh robot@ev3dev.local cat /sys/devices/platform/battery/power_supply/lego-ev3-battery/voltage_now').read()) batteryLevel = int(os.popen('sshpass -p maker ssh robot@ev3dev.local cat /sys/devices/platform/battery/power_supply/lego-ev3-battery/voltage_now').read())
except: except:
logging.error("Robot not connected") logging.error("EV3 not connected")
client.publish(Topics["ROBOT_DATA_ERROR"], "Robot not connected", qos=1) client.publish(Topics["ROBOT_DATA_ERROR"], "Robot not connected", qos=1)
return return