174 lines
4.5 KiB
Python
Raw Normal View History

import paho.mqtt.client as mqtt
import json
2023-04-22 12:32:27 +02:00
import threading
import os
import signal
import time
from raspy_sensors import RaspySensors
2023-04-24 13:04:03 +02:00
from software.defines import Topics
#region global Varaibles
2023-04-22 12:32:27 +02:00
#sensors:RaspySensors()
leftRight = 50
2023-04-24 13:04:03 +02:00
plantID = 0
sensorData = {
"Air Temperature [°C]" : 0,
"Air Humidity [%]" : 0,
"Earth Humidity [%]" : 0,
"Brightness [Lux]" : 0,
"Plant ID": 0,
"Action ID": 0
}
gpsPosition = {
"Position": 0,
"Action ID": 0
}
batteryStatus = {
"Battery": 0,
"Action ID": 0
}
2023-04-07 16:05:15 +02:00
#endregion
#region global functions
2023-04-07 16:05:15 +02:00
def measure_send_data(plantID, actionID):
'''Measure data for one plant via sensor class and send via MQTT'''
2023-04-07 16:05:15 +02:00
sensorData = sensors.readSensors()
sensorData["Plant_ID"] = plantID
sensorData["Action_ID"] = actionID
2023-04-24 13:04:03 +02:00
client.publish(Topics["ROBOT_DATA_SENSORDATA"], json.dumps(sensorData, indent=4))
2023-04-22 12:32:27 +02:00
def drive_home():
'''Function to drive robot back to starting position in thread'''
2023-04-22 14:34:46 +02:00
print("Robot driving home")
os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {leftRight}')
print("Robot home")
#TODO decide about robot occupied message
#client.publish("ROBOT/DATA/OCCUPIED", "false")
2023-04-22 12:32:27 +02:00
2023-04-22 14:34:46 +02:00
def drive_plant_thread():
2023-04-22 12:32:27 +02:00
'''Function to drive to plant according to number from MQTT message in thread'''
2023-04-24 13:04:03 +02:00
os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py')
2023-04-22 14:34:46 +02:00
print("Raising Signal to meassure")
2023-04-22 12:32:27 +02:00
signal.raise_signal(signal.SIGUSR1)
2023-04-24 13:04:03 +02:00
#TODO alles hier ohne weitere Threads
2023-04-22 12:32:27 +02:00
def init_mqtt():
'''Initialise MQTT client'''
mqttBroker = "mqtt.eclipseprojects.io"
2023-04-22 12:32:27 +02:00
global client
client = mqtt.Client("Robot")
#Add callbacks
2023-04-22 12:32:27 +02:00
client.message_callback_add("ROBOT/DATA", send_data_json) #Testing
2023-04-24 13:04:03 +02:00
client.message_callback_add(Topics["ROBOT_ACTION_DRIVE"], drive_plant)
client.message_callback_add(Topics["ROBOT_ACTION_GETPOSITION"], get_position)
client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], get_BatteryStatus)
2023-04-22 14:34:46 +02:00
client.connect(mqttBroker) #Has to be before subscribing
#Subscribe to topics
2023-04-22 12:32:27 +02:00
client.subscribe("ROBOT/DATA") #Testing
2023-04-24 13:04:03 +02:00
client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
2023-04-22 14:34:46 +02:00
2023-04-07 16:05:15 +02:00
2023-04-22 14:34:46 +02:00
def signal_measure(signum, frame):
2023-04-22 12:32:27 +02:00
'''Signal function to measure one plant and start driving home'''
print("Measuring Sensors")
#measure_send_data()
print("Driving Home")
drive_home()
#endregion
#region MQTT callbacks
#Testing
2023-04-24 13:04:03 +02:00
def send_data_json(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
strIn = str(message.payload.decode("UTF-8"))
dataDict = json.loads(strIn)
print("Received data: ", json.dumps(dataDict))
2023-04-24 13:04:03 +02:00
def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
2023-04-22 14:34:46 +02:00
'''Function to drive to plant according to request
Starting Drive in Thread'''
2023-04-24 13:04:03 +02:00
global plantID, leftRight
2023-04-22 14:34:46 +02:00
#TODO define MQTT message
2023-04-24 13:04:03 +02:00
plantID = int(message.payload.decode("UTF-8"))
2023-04-22 14:34:46 +02:00
2023-04-24 13:04:03 +02:00
print(f"received drive to plant {plantID}")
2023-04-22 14:34:46 +02:00
2023-04-24 13:04:03 +02:00
if plantID % 2 == 0:
2023-04-22 12:32:27 +02:00
leftRight = -50 #rotating left
else:
leftRight = 50 #rotating right
2023-04-22 14:34:46 +02:00
thread = threading.Thread(target= drive_plant_thread, daemon=True)
2023-04-22 12:32:27 +02:00
thread.start()
2023-04-24 13:04:03 +02:00
def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
'''Callback function for GPS position request
Function to send actual GPS position via MQTT'''
2023-04-24 13:04:03 +02:00
#[ ]TODO Write Sensor Function
2023-04-22 14:34:46 +02:00
client.publish("ROBOT/DATA/POSITION", json.dumps(gpsPosition, indent=4))
2023-04-24 13:04:03 +02:00
def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
"""
Callback function for battery status request
Function to read battery status from ev3 and send via MQTT
2023-04-24 13:04:03 +02:00
Args:
clients (mqtt.Client): _description_
userdata (_type_): _description_
message (mqtt.MQTTMessage): _description_
"""
#[ ]TODO read Battery
2023-04-22 14:34:46 +02:00
client.publish("ROBOT/DATA/BATTERY", json.dumps(batteryStatus, indent=4))
2023-04-07 16:05:15 +02:00
#endregion
def main():
2023-04-24 13:04:03 +02:00
"""
Initialises MQTT and Sensors
Runs forever and controlls all robot functions
"""
2023-04-22 14:34:46 +02:00
2023-04-24 13:04:03 +02:00
#TODO on_connect
init_mqtt()
2023-04-22 12:32:27 +02:00
print("MQTT initialized")
signal.signal(signal.SIGUSR1, signal_measure)
# dataDict = {} #Testing
2023-04-22 14:34:46 +02:00
client.loop_start()
2023-04-22 12:32:27 +02:00
print("Starting Loop")
while True:
2023-04-22 14:34:46 +02:00
# print("Looping")
# time.sleep(1)
pass
if __name__ == "__main__":
main()