repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import paho.mqtt.client as mqtt
  2. import json
  3. import threading
  4. import os
  5. import raspy_sensors as Sensors
  6. from defines import Topics, SENSORDATA
  7. def measure_send_data(plantID, actionID, client: mqtt.Client):
  8. """
  9. Reads all sensors and publishes data via MQTT in form of SENSORDATA
  10. Args:
  11. plantID (_type_): plant to measure
  12. actionID (_type_): current ID of driving action
  13. client (mqtt.Client): current mqtt client for publishing
  14. """
  15. sensorData = SENSORDATA
  16. try:
  17. sensorData = Sensors.readSensors()
  18. except Exception as e:
  19. print(str(e))
  20. # TODO Error message MQTT
  21. sensorData["PlantID"] = plantID
  22. sensorData["ActionID"] = actionID
  23. client.publish(Topics["ROBOT_DATA_SENSORDATA"], json.dumps(sensorData, indent=4))
  24. def drive_plant_thread(plantID, actionID, client: mqtt.Client):
  25. """
  26. Function to drive to plant according to number from MQTT message in thread
  27. Meassure and publish data via MQTT
  28. Drive home to starting point
  29. Args:
  30. plantID (_type_): plant to measure
  31. actionID (_type_): current ID of driving action
  32. client (mqtt.Client): current mqtt client for publishing
  33. """
  34. # FIXME Change to color code driving
  35. errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py')
  36. # errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}')
  37. # TODO Error Message
  38. if errorCode != 0:
  39. print(f"Robot Error {errorCode} occurred")
  40. print("Drive Plant aborted, Robot at starting position")
  41. return
  42. print("Measuring Sensors")
  43. measure_send_data(plantID, actionID, client)
  44. print("Robot driving home")
  45. errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}')
  46. # TODO Error Message
  47. if errorCode != 0:
  48. print(f"Robot Error {errorCode} occurred")
  49. print(f"Drive Home aborted, Robot at plant {plantID}")
  50. return
  51. print("Robot home")
  52. #TODO decide about robot occupied message
  53. #client.publish("ROBOT/DATA/OCCUPIED", "false")
  54. #region MQTT callbacks
  55. def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  56. """
  57. Function to drive to plant according to request
  58. Starting Drive in Thread to not block main programm
  59. Args:
  60. clients (mqtt.Client): current mqtt client
  61. userdata (_type_): _description_
  62. message (mqtt.MQTTMessage): received DRIVE message with PlantID and ActionID
  63. """
  64. dictMessage = json.loads(str(message.payload.decode("UTF-8")))
  65. plantID = dictMessage["PlantID"]
  66. actionID = dictMessage["ActionID"]
  67. print(f"Received Drive-request to plant {plantID}, ActionID: {actionID}")
  68. thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, clients), daemon=True)
  69. thread.start()
  70. def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  71. """
  72. Callback function for GPS position request
  73. Function to send actual GPS position via MQTT in form of POSITION
  74. Args:
  75. clients (mqtt.Client): current mqtt client
  76. userdata (_type_): _description_
  77. message (mqtt.MQTTMessage): received message
  78. """
  79. # TODO Write GPS Sensor Function
  80. position = {
  81. "Position": ""
  82. }
  83. clients.publish(Topics["ROBOT_DATA_POSITION"], json.dumps(position, indent=4))
  84. def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  85. """
  86. Callback function for battery status request
  87. Function to read battery status from ev3 and send via MQTT in form of BATTERY
  88. 8,5V -> 100%
  89. 5V -> 0%
  90. Args:
  91. clients (mqtt.Client): current mqtt client
  92. userdata (_type_): _description_
  93. message (mqtt.MQTTMessage): received message
  94. """
  95. print("Received battery status request")
  96. try:
  97. batteryLevel = int(os.popen('sshpass -p maker ssh robot@ev3dev.local cat /sys/devices/platform/battery/power_supply/lego-ev3-battery/voltage_now').read())
  98. except:
  99. print("Robot not connected")
  100. #TODO Error Message
  101. return
  102. batteryLevel = round(batteryLevel / 1000000, 2) # Voltage
  103. batteryLevel = batteryLevel - 5
  104. batteryLevel = round(batteryLevel / 3.5, 3) *100 # Percentage
  105. battery = {
  106. "Battery": batteryLevel
  107. }
  108. clients.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4))
  109. #endregion