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

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