|
|
|
|
|
|
|
|
sensorData = Sensors.readSensors() |
|
|
sensorData = Sensors.readSensors() |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.error(str(e)) |
|
|
logging.error(str(e)) |
|
|
# TODO Error message MQTT |
|
|
|
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], str(e), qos=1) |
|
|
client.publish(Topics["ROBOT_DATA_ERROR"], str(e), qos=1) |
|
|
|
|
|
|
|
|
sensorData["PlantID"] = plantID |
|
|
sensorData["PlantID"] = plantID |
|
|
|
|
|
|
|
|
logging.error(f"Robot Error {errorCode} occurred") |
|
|
logging.error(f"Robot Error {errorCode} occurred") |
|
|
logging.info("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_ERROR"], "Drive Plant aborted, Robot at starting position", qos=1) |
|
|
#TODO decide about robot occupied message |
|
|
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
|
|
|
|
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
logging.info("Measuring Sensors") |
|
|
logging.info("Measuring Sensors") |
|
|
measure_send_data(plantID, actionID, client) |
|
|
measure_send_data(plantID, actionID, client) |
|
|
|
|
|
|
|
|
|
|
|
# FIXME Send Picture |
|
|
|
|
|
# sendPicture() |
|
|
|
|
|
|
|
|
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.py {plantID}') |
|
|
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.error(f"Robot Error {errorCode} occurred") |
|
|
logging.error(f"Robot Error {errorCode} occurred") |
|
|
logging.info(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_ERROR"], f"Drive Home aborted, Robot at plant {plantID}", qos=1) |
|
|
#TODO decide about robot occupied message |
|
|
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
logging.info("Robot home") |
|
|
logging.info("Robot home") |
|
|
|
|
|
|
|
|
#TODO decide about robot occupied message |
|
|
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "True", qos=1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region MQTT callbacks |
|
|
#region MQTT callbacks |
|
|
|
|
|
|
|
|
|
|
|
# TODO Test with all sensors |
|
|
def drive_plant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
def drive_plant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): |
|
|
""" |
|
|
""" |
|
|
Function to drive to plant according to request |
|
|
Function to drive to plant according to request |
|
|
Starting Drive in Thread to not block main programm |
|
|
Starting Drive in Thread to not block main programm |
|
|
|
|
|
|
|
|
Args: |
|
|
Args: |
|
|
clients (mqtt.Client): current mqtt client |
|
|
|
|
|
|
|
|
client (mqtt.Client): current mqtt client |
|
|
userdata (_type_): _description_ |
|
|
userdata (_type_): _description_ |
|
|
message (mqtt.MQTTMessage): received DRIVE message with PlantID and ActionID |
|
|
message (mqtt.MQTTMessage): received DRIVE message with PlantID and ActionID |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
actionID = dictMessage["ActionID"] |
|
|
actionID = dictMessage["ActionID"] |
|
|
|
|
|
|
|
|
logging.info(f"Received Drive-request to plant {plantID}, ActionID: {actionID}") |
|
|
logging.info(f"Received Drive-request to plant {plantID}, ActionID: {actionID}") |
|
|
|
|
|
|
|
|
#TODO decide about robot occupied message |
|
|
|
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "False", qos=1) |
|
|
client.publish(Topics["ROBOT_DATA_ROBOTREADY"], "False", qos=1) |
|
|
|
|
|
|
|
|
thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, client), daemon=True) |
|
|
thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, client), daemon=True) |
|
|
|
|
|
|
|
|
Function to send actual GPS position via MQTT in form of POSITION |
|
|
Function to send actual GPS position via MQTT in form of POSITION |
|
|
|
|
|
|
|
|
Args: |
|
|
Args: |
|
|
clients (mqtt.Client): current mqtt client |
|
|
|
|
|
|
|
|
client (mqtt.Client): current mqtt client |
|
|
userdata (_type_): _description_ |
|
|
userdata (_type_): _description_ |
|
|
message (mqtt.MQTTMessage): received message |
|
|
message (mqtt.MQTTMessage): received message |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
5V -> 0% |
|
|
5V -> 0% |
|
|
|
|
|
|
|
|
Args: |
|
|
Args: |
|
|
clients (mqtt.Client): current mqtt client |
|
|
|
|
|
|
|
|
client (mqtt.Client): current mqtt client |
|
|
userdata (_type_): _description_ |
|
|
userdata (_type_): _description_ |
|
|
message (mqtt.MQTTMessage): received message |
|
|
message (mqtt.MQTTMessage): received message |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
client.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4), qos=1) |
|
|
client.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4), qos=1) |
|
|
|
|
|
|
|
|
|
|
|
# TODO Test needed |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
with open("picture.txt", "rb") as f: |
|
|
|
|
|
file = f.read() |
|
|
|
|
|
byteArr = bytearray(file) |
|
|
|
|
|
print(byteArr) |
|
|
|
|
|
client.publish(Topics["ROBOT_DATA_IMAGE"], byteArr) |
|
|
|
|
|
print("Published") |
|
|
|
|
|
|
|
|
#endregion |
|
|
#endregion |