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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  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. #FIXME Sensor not working
  44. measure_send_data(plantID, actionID, client)
  45. print("Robot driving home")
  46. errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}')
  47. # TODO Error Message
  48. if errorCode != 0:
  49. print(f"Robot Error {errorCode} occurred")
  50. print(f"Drive Home aborted, Robot at plant {plantID}")
  51. return
  52. print("Robot home")
  53. #TODO decide about robot occupied message
  54. #client.publish("ROBOT/DATA/OCCUPIED", "false")
  55. #region MQTT callbacks
  56. def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  57. """
  58. Function to drive to plant according to request
  59. Starting Drive in Thread
  60. Args:
  61. clients (mqtt.Client): current mqtt client
  62. userdata (_type_): _description_
  63. message (mqtt.MQTTMessage): received message
  64. """
  65. dictMessage = json.loads(str(message.payload.decode("UTF-8")))
  66. plantID = dictMessage["PlantID"]
  67. actionID = dictMessage["ActionID"]
  68. print(f"Received Drive-request to plant {plantID}, ActionID: {actionID}")
  69. thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, clients), daemon=True)
  70. thread.start()
  71. def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  72. """
  73. Callback function for GPS position request
  74. Function to send actual GPS position via MQTT
  75. Args:
  76. clients (mqtt.Client): current mqtt client
  77. userdata (_type_): _description_
  78. message (mqtt.MQTTMessage): received message
  79. """
  80. # TODO Write GPS Sensor Function
  81. position = {
  82. "Position": ""
  83. }
  84. clients.publish(Topics["ROBOT_DATA_POSITION"], json.dumps(position, indent=4))
  85. def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  86. """
  87. Callback function for battery status request
  88. Function to read battery status from ev3 and send via MQTT
  89. 8,5V -> 100%
  90. 5V -> 0%
  91. Args:
  92. clients (mqtt.Client): current mqtt client
  93. userdata (_type_): _description_
  94. message (mqtt.MQTTMessage): received message
  95. """
  96. print("Received battery status request")
  97. try:
  98. batteryLevel = int(os.popen('sshpass -p maker ssh robot@ev3dev.local cat /sys/devices/platform/battery/power_supply/lego-ev3-battery/voltage_now').read())
  99. except:
  100. print("Robot not connected")
  101. #TODO Error Message
  102. return
  103. batteryLevel = round(batteryLevel / 1000000, 2) # Voltage
  104. batteryLevel = batteryLevel - 5
  105. batteryLevel = round(batteryLevel / 3.5, 3) *100 # Percentage
  106. battery = {
  107. "Battery": batteryLevel
  108. }
  109. clients.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4))
  110. #endregion