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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import paho.mqtt.client as mqtt
  2. import json
  3. import threading
  4. import os
  5. import raspy_sensors as RaspySensors
  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 = 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. os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py')
  31. print("Measuring Sensors")
  32. #FIXME Sensor not working
  33. # measure_send_data(plantID, actionID, client)
  34. print("Robot driving home")
  35. if plantID % 2 == 0:
  36. leftRight = -50 #rotating left
  37. else:
  38. leftRight = 50 #rotating right
  39. os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {leftRight}')
  40. print("Robot home")
  41. #TODO decide about robot occupied message
  42. #client.publish("ROBOT/DATA/OCCUPIED", "false")
  43. #region MQTT callbacks
  44. #Testing
  45. def send_data_json(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  46. strIn = str(message.payload.decode("UTF-8"))
  47. dataDict = json.loads(strIn)
  48. print("Received data: ", json.dumps(dataDict))
  49. def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  50. """
  51. Function to drive to plant according to request
  52. Starting Drive in Thread
  53. Args:
  54. clients (mqtt.Client): current mqtt client
  55. userdata (_type_): _description_
  56. message (mqtt.MQTTMessage): received message
  57. """
  58. dictMessage = json.loads(str(message.payload.decode("UTF-8")))
  59. plantID = dictMessage["PlantID"]
  60. actionID = dictMessage["ActionID"]
  61. print(f"received drive to plant {plantID}")
  62. thread = threading.Thread(target= drive_plant_thread, args=(plantID, actionID, clients), daemon=True)
  63. thread.start()
  64. def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  65. """
  66. Callback function for GPS position request
  67. Function to send actual GPS position via MQTT
  68. Args:
  69. clients (mqtt.Client): current mqtt client
  70. userdata (_type_): _description_
  71. message (mqtt.MQTTMessage): received message
  72. """
  73. # TODO Write GPS Sensor Function
  74. position = {
  75. "Position": ""
  76. }
  77. clients.publish(Topics["ROBOT_DATA_POSITION"], json.dumps(position, indent=4))
  78. def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
  79. """
  80. Callback function for battery status request
  81. Function to read battery status from ev3 and send via MQTT
  82. Args:
  83. clients (mqtt.Client): current mqtt client
  84. userdata (_type_): _description_
  85. message (mqtt.MQTTMessage): received message
  86. """
  87. battery = {
  88. "Battery": 0.0
  89. }
  90. #TODO read Battery
  91. clients.publish(Topics["ROBOT_DATA_BATTERY"], json.dumps(battery, indent=4))
  92. #endregion