|
|
@@ -20,12 +20,12 @@ def measure_send_data(plantID, actionID, client: mqtt.Client): |
|
|
|
try: |
|
|
|
sensorData = Sensors.readSensors() |
|
|
|
except Exception as e: |
|
|
|
print(str(e)) |
|
|
|
# TODO Error message MQTT |
|
|
|
logging.error(str(e)) |
|
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], str(e), qos=1) |
|
|
|
|
|
|
|
sensorData["PlantID"] = plantID |
|
|
|
sensorData["ActionID"] = actionID |
|
|
|
client.publish(Topics["ROBOT_DATA_SENSORDATA"], json.dumps(sensorData, indent=4)) |
|
|
|
client.publish(Topics["ROBOT_DATA_SENSORDATA"], json.dumps(sensorData, indent=4), qos=1) |
|
|
|
|
|
|
|
|
|
|
|
def drive_plant_thread(plantID, actionID, client: mqtt.Client): |
|
|
@@ -43,49 +43,56 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client): |
|
|
|
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py') |
|
|
|
# errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}') |
|
|
|
|
|
|
|
# TODO Error Message |
|
|
|
if errorCode != 0: |
|
|
|
logging.error(f"Robot Error {errorCode} occurred") |
|
|
|
logging.error("Drive Plant aborted, Robot at starting position") |
|
|
|
logging.info("Drive Plant aborted, Robot at starting position") |
|
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], "Drive Plant aborted, Robot at starting position", qos=1) |
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
logging.info("Measuring Sensors") |
|
|
|
measure_send_data(plantID, actionID, client) |
|
|
|
|
|
|
|
logging.info("Taking Picture") |
|
|
|
sendPicture(client) |
|
|
|
|
|
|
|
logging.info("Robot driving home") |
|
|
|
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}') |
|
|
|
|
|
|
|
# TODO Error Message |
|
|
|
if errorCode != 0: |
|
|
|
logging.error(f"Robot Error {errorCode} occurred") |
|
|
|
logging.error(f"Drive Home aborted, Robot at plant {plantID}") |
|
|
|
logging.info(f"Drive Home aborted, Robot at plant {plantID}") |
|
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], f"Drive Home aborted, Robot at plant {plantID}", qos=1) |
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
|
return |
|
|
|
|
|
|
|
logging.info("Robot home") |
|
|
|
|
|
|
|
#TODO decide about robot occupied message |
|
|
|
#client.publish("ROBOT/DATA/OCCUPIED", "false") |
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
|
|
|
|
|
|
|
|
|
#region MQTT callbacks |
|
|
|
|
|
|
|
def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
|
def drive_plant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
|
""" |
|
|
|
Function to drive to plant according to request |
|
|
|
Starting Drive in Thread to not block main programm |
|
|
|
|
|
|
|
Args: |
|
|
|
clients (mqtt.Client): current mqtt client |
|
|
|
client (mqtt.Client): current mqtt client |
|
|
|
userdata (_type_): _description_ |
|
|
|
message (mqtt.MQTTMessage): received DRIVE message with PlantID and ActionID |
|
|
|
""" |
|
|
|
dictMessage = json.loads(str(message.payload.decode("UTF-8"))) |
|
|
|
|
|
|
|
plantID = dictMessage["PlantID"] |
|
|
|
actionID = dictMessage["ActionID"] |
|
|
|
|
|
|
|
logging.info(f"Received Drive-request to plant {plantID}, ActionID: {actionID}") |
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "False", qos=1) |
|
|
|
|
|
|
|
thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, clients), daemon=True) |
|
|
|
thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, client), daemon=True) |
|
|
|
thread.start() |
|
|
|
|
|
|
|
|
|
|
@@ -95,19 +102,21 @@ def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
|
Function to send actual GPS position via MQTT in form of POSITION |
|
|
|
|
|
|
|
Args: |
|
|
|
clients (mqtt.Client): current mqtt client |
|
|
|
client (mqtt.Client): current mqtt client |
|
|
|
userdata (_type_): _description_ |
|
|
|
message (mqtt.MQTTMessage): received message |
|
|
|
""" |
|
|
|
logging.info("Received position request") |
|
|
|
|
|
|
|
# TODO Write GPS Sensor Function |
|
|
|
position = { |
|
|
|
"Position": "" |
|
|
|
} |
|
|
|
|
|
|
|
clients.publish(Topics["ROBOT_DATA_POSITION"], json.dumps(position, indent=4)) |
|
|
|
clients.publish(Topics["ROBOT_DATA_POSITION"], json.dumps(position, indent=4), qos=1) |
|
|
|
|
|
|
|
|
|
|
|
def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
|
def get_BatteryStatus(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
|
""" |
|
|
|
Callback function for battery status request |
|
|
|
Function to read battery status from ev3 and send via MQTT in form of BATTERY |
|
|
@@ -115,7 +124,7 @@ def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage) |
|
|
|
5V -> 0% |
|
|
|
|
|
|
|
Args: |
|
|
|
clients (mqtt.Client): current mqtt client |
|
|
|
client (mqtt.Client): current mqtt client |
|
|
|
userdata (_type_): _description_ |
|
|
|
message (mqtt.MQTTMessage): received message |
|
|
|
""" |
|
|
@@ -125,7 +134,7 @@ def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage) |
|
|
|
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: |
|
|
|
logging.error("Robot not connected") |
|
|
|
#TODO Error Message |
|
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], "Robot not connected", qos=1) |
|
|
|
return |
|
|
|
|
|
|
|
batteryLevel = round(batteryLevel / 1000000, 2) # Voltage |
|
|
@@ -135,6 +144,28 @@ def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage) |
|
|
|
battery = { |
|
|
|
"Battery": batteryLevel |
|
|
|
} |
|
|
|
clients.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4)) |
|
|
|
client.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4), qos=1) |
|
|
|
logging.info(f"Battery done {batteryLevel}") |
|
|
|
|
|
|
|
|
|
|
|
def sendPicture(client: mqtt.Client): |
|
|
|
""" |
|
|
|
Takes picture and publish via MQTT |
|
|
|
|
|
|
|
Args: |
|
|
|
client (mqtt.Client): current mqtt client |
|
|
|
""" |
|
|
|
try: |
|
|
|
Sensors.takePicture() |
|
|
|
except Exception as e: |
|
|
|
logging.error(str(e)) |
|
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], str(e), qos=1) |
|
|
|
return |
|
|
|
|
|
|
|
with open("picture.png", "rb") as f: |
|
|
|
file = f.read() |
|
|
|
byteArr = bytearray(file) |
|
|
|
client.publish(Topics["ROBOT_DATA_PICTURE"], byteArr) |
|
|
|
logging.info("Picture Published") |
|
|
|
|
|
|
|
#endregion |