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.

mainProg.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import paho.mqtt.client as mqtt
  2. import json
  3. import threading
  4. import os
  5. import signal
  6. import time
  7. from raspy_sensors import RaspySensors
  8. #region global Varaibles
  9. #sensors:RaspySensors()
  10. leftRight = 50
  11. plantNumber = 0
  12. sensorData = {
  13. "Air Temperature [°C]" : 0,
  14. "Air Humidity [%]" : 0,
  15. "Earth Humidity [%]" : 0,
  16. "Brightness [Lux]" : 0,
  17. "Plant ID": 0,
  18. "Action ID": 0
  19. }
  20. gpsPosition = {
  21. "Position": 0,
  22. "Action ID": 0
  23. }
  24. batteryStatus = {
  25. "Battery": 0,
  26. "Action ID": 0
  27. }
  28. #endregion
  29. #region global functions
  30. def measure_send_data(plantID, actionID):
  31. '''Measure data for one plant via sensor class and send via MQTT'''
  32. sensorData = sensors.readSensors()
  33. sensorData["Plant_ID"] = plantID
  34. sensorData["Action_ID"] = actionID
  35. client.publish("ROBOT/DATA/SENSORDATA", json.dumps(sensorData, indent=4))
  36. def drive_home():
  37. '''Function to drive robot back to starting position in thread'''
  38. print("Robot driving home")
  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. def drive_plant_thread():
  44. '''Function to drive to plant according to number from MQTT message in thread'''
  45. os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantNumber}.py')
  46. print("Raising Signal to meassure")
  47. signal.raise_signal(signal.SIGUSR1)
  48. def init_mqtt():
  49. '''Initialise MQTT client'''
  50. mqttBroker = "mqtt.eclipseprojects.io"
  51. global client
  52. client = mqtt.Client("Robot")
  53. #Add callbacks
  54. client.message_callback_add("ROBOT/DATA", send_data_json) #Testing
  55. client.message_callback_add("ROBOT/ACTION/DRIVE", drive_plant)
  56. client.message_callback_add("ROBOT/ACTION/GETPOSITION", get_position)
  57. client.message_callback_add("ROBOT/ACTION/GETBATTERY", get_BatteryStatus)
  58. client.connect(mqttBroker) #Has to be before subscribing
  59. #Subscribe to topics
  60. client.subscribe("ROBOT/DATA") #Testing
  61. client.subscribe("ROBOT/ACTION/DRIVE")
  62. client.subscribe("ROBOT/Action/GETPOSITION")
  63. client.subscribe("ROBOT/ACTION/GETBATTERY")
  64. def signal_measure(signum, frame):
  65. '''Signal function to measure one plant and start driving home'''
  66. print("Measuring Sensors")
  67. #measure_send_data()
  68. print("Driving Home")
  69. drive_home()
  70. #endregion
  71. #region MQTT callbacks
  72. #Testing
  73. def send_data_json(client, userdata, message):
  74. strIn = str(message.payload.decode("UTF-8"))
  75. dataDict = json.loads(strIn)
  76. print("Received data: ", json.dumps(dataDict))
  77. def drive_plant(clients, userdata, message):
  78. '''Function to drive to plant according to request
  79. Starting Drive in Thread'''
  80. global plantNumber, leftRight
  81. #TODO define MQTT message
  82. plantNumber = int(message.payload.decode("UTF-8"))
  83. print(f"received drive to plant {plantNumber}")
  84. if plantNumber % 2 == 0:
  85. leftRight = -50 #rotating left
  86. else:
  87. leftRight = 50 #rotating right
  88. thread = threading.Thread(target= drive_plant_thread, daemon=True)
  89. thread.start()
  90. def get_position(clients, userdata, message):
  91. '''Callback function for GPS position request
  92. Function to send actual GPS position via MQTT'''
  93. #[ ]TODO handle MQTT message
  94. gpsPosition["Position"] = sensors.readPosition()
  95. gpsPosition["Action_ID"] = message
  96. client.publish("ROBOT/DATA/POSITION", json.dumps(gpsPosition, indent=4))
  97. def get_BatteryStatus(clients, userdata, message):
  98. '''Callback function for battery status request
  99. Function to read battery status from ev3 and send via MQTT'''
  100. #[ ]TODO handle MQTT message
  101. batteryStatus["Battery"] = sensors.readBattery()
  102. batteryStatus["Action_ID"] = message
  103. client.publish("ROBOT/DATA/BATTERY", json.dumps(batteryStatus, indent=4))
  104. #endregion
  105. def main():
  106. '''Initialises MQTT and Sensors
  107. Runs forever and controlls all robot functions'''
  108. init_mqtt()
  109. print("MQTT initialized")
  110. signal.signal(signal.SIGUSR1, signal_measure)
  111. # dataDict = {} #Testing
  112. client.loop_start()
  113. print("Starting Loop")
  114. while True:
  115. # print("Looping")
  116. # time.sleep(1)
  117. pass
  118. if __name__ == "__main__":
  119. main()