add mqttCmd
parent
831199bf93
commit
5399e5cf7f
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
#coding:utf-8
|
||||
import paho.mqtt.client as mqtt
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from utils.readConfig import Read_CMD_Config
|
||||
from utils.basicMqtt import MQTTClient
|
||||
from utils.protoJson_mqtt_cmd_data_to_ros import Json_msg_to_ros
|
||||
import logging
|
||||
|
||||
from std_msgs.msg import String
|
||||
import rospy
|
||||
|
||||
def init_dataFormat(cfg:Read_CMD_Config):
|
||||
if cfg.msg_format == "Json":
|
||||
# Json_msg_to_ros.rate = rospy.Rate(10)
|
||||
# Json_msg_to_ros.publisher_Flight_Information = rospy.Publisher(cfg.ROStopicName_Cmd_Broadcast_Receiver,String,queue_size=10)
|
||||
client.on_message = Json_msg_to_ros.on_message
|
||||
Json_msg_to_ros.Cmd_Broadcast_topicToMqtt = cfg.Cmd_Broadcast_topicToMqtt
|
||||
elif cfg.msg_format == "Proto":
|
||||
pass
|
||||
else:
|
||||
logging.debug("msg_format not found")
|
||||
|
||||
def on_connect(self, userdata, flags, rc):
|
||||
logger.info("Connected with result code " + str(rc))
|
||||
client.subscribe(cfg.Cmd_Broadcast_topicToMqtt)
|
||||
|
||||
def on_disconnect(client, userdata, rc):
|
||||
logger.info("disconnecting reason " +str(rc))
|
||||
client.connected_flag=False
|
||||
client.disconnect_flag=True
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Read Config
|
||||
FilePath = os.path.join(os.path.dirname(__file__),"utils","mqttConfig_CMD.yml")
|
||||
cfg = Read_CMD_Config(FilePath)
|
||||
client = MQTTClient(cfg.MQTTClientNameCmd)
|
||||
|
||||
# set log
|
||||
log_format = "%(asctime)s - %(levelname)s - %(message)s"
|
||||
formatter = logging.Formatter(log_format)
|
||||
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(formatter)
|
||||
stream_handler.setLevel(logging.DEBUG)
|
||||
|
||||
file_handler = logging.FileHandler(cfg.logFileName)
|
||||
file_handler.setFormatter(formatter)
|
||||
file_handler.setLevel(logging.INFO)
|
||||
|
||||
logger = logging.getLogger("__CMD__")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.addHandler(file_handler)
|
||||
logger.addHandler(stream_handler)
|
||||
logger.info(cfg)
|
||||
|
||||
# Mqtt
|
||||
client.on_connect = on_connect
|
||||
client.on_disconnect = on_disconnect
|
||||
client.connect(host=cfg.host, port=cfg.port, keepalive=cfg.keepalive)
|
||||
client.loop_start()
|
||||
|
||||
# Ros
|
||||
# rospy.init_node(cfg.ROSClientNameCmd)
|
||||
|
||||
# init data format
|
||||
init_dataFormat(cfg)
|
||||
|
||||
try:
|
||||
# rospy.spin()
|
||||
while True:
|
||||
time.sleep(0.25)
|
||||
except BaseException as error:
|
||||
client.disconnect()
|
||||
logger.info("End of program")
|
||||
sys.exit()
|
||||
|
||||
@ -1 +1 @@
|
||||
{"serialize":{"json":0.3499185360000183,"proto":1.0996155560000034},"deserializ":{"json":0.4229183979999789,"proto":0.5481516849999934}}
|
||||
{"serialize":{"json":0.34842266000123345,"proto":1.0934251180005958},"deserializ":{"json":0.42545599799996126,"proto":0.48490119200141635}}
|
||||
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
#coding:utf-8
|
||||
import paho.mqtt.client as mqtt
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import utils
|
||||
import proto.flight_information_pb2 as flight_information_pb2
|
||||
import proto.flyformatioln_pb2 as flyformatioln_pb2
|
||||
import logging
|
||||
|
||||
|
||||
def init_dataFormat(cfg:utils.Read_PUB_Config):
|
||||
# ros_namespace="/drone1"
|
||||
if cfg.msg_format == "Proto":
|
||||
utils.Proto_msg_from_ros.client = client
|
||||
utils.Proto_msg_from_ros.flight_information_msg = flight_information_pb2.flight_information_message()
|
||||
utils.Proto_msg_from_ros.fly_formation_msg = flyformatioln_pb2.fly_formation_message()
|
||||
utils.Proto_msg_from_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
|
||||
utils.Proto_msg_from_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
|
||||
utils.Proto_msg_from_ros.Fly_Formation_topicToMqtt_QOS = cfg.Fly_Formation_topicToMqtt_QOS
|
||||
# rospy.Subscriber(ros_namespace+'/mavros/global_position/global', NavSatFix, utils.Proto_msg_from_ros.callBack_gps)
|
||||
# rospy.Subscriber(ros_namespace+'/mavros/global_position/compass_hdg', Float64, utils.Proto_msg_from_ros.callBack_compass_hdg)
|
||||
elif cfg.msg_format == "Json":
|
||||
utils.Json_msg_from_ros.client = client
|
||||
utils.Json_msg_from_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
|
||||
utils.Json_msg_from_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
|
||||
utils.Json_msg_from_ros.Fly_Formation_topicToMqtt_QOS = cfg.Fly_Formation_topicToMqtt_QOS
|
||||
# rospy.Subscriber(ros_namespace+'/mavros/global_position/global', NavSatFix, utils.Json_msg_from_ros.callBack_gps)
|
||||
# rospy.Subscriber(ros_namespace+'/mavros/global_position/compass_hdg', Float64, utils.Json_msg_from_ros.callBack_compass_hdg)
|
||||
else:
|
||||
logging.debug("msg_format not found")
|
||||
@ -1,7 +1,7 @@
|
||||
from utils.protoJson_mqtt_pub_data_to_ros import Proto_msg_to_ros
|
||||
from utils.protoJson_mqtt_pub_data_to_ros import Json_msg_to_ros
|
||||
from utils.protoJson_mqtt_sub_data_from_ros import Proto_msg_from_ros
|
||||
from utils.protoJson_mqtt_sub_data_from_ros import Json_msg_from_ros
|
||||
from utils.protoJson_mqtt_sub_data_from_ros import Json_msg_from_ros
|
||||
from utils.readConfig import Read_PUB_Config
|
||||
from utils.readConfig import Read_SUB_Config
|
||||
from utils.basicMqtt import MQTTClient
|
||||
@ -0,0 +1,15 @@
|
||||
MQTT:
|
||||
msg_format: Json
|
||||
MQTTClientNameCmd: Drone550Cmd
|
||||
host: 192.168.50.118
|
||||
port: 1883
|
||||
keepalive: 600
|
||||
# Mqtt topic
|
||||
Cmd_Broadcast_topicToMqtt: cmd/broadcast
|
||||
Cmd_Broadcast_topicToMqtt_QOS: 2
|
||||
#ROS
|
||||
ROS:
|
||||
ROSClientNameCmd: Drone550_Cmd
|
||||
ROStopicName_Cmd_Broadcast_Receiver: cmd_receiver
|
||||
LOG:
|
||||
logFileName: cmd.log
|
||||
@ -0,0 +1,32 @@
|
||||
import orjson
|
||||
import time
|
||||
|
||||
import logging
|
||||
|
||||
# TODO: use native ros type instead of json or str
|
||||
logger = logging.getLogger("__CMD__")
|
||||
|
||||
class Proto_msg_to_ros:
|
||||
pass
|
||||
|
||||
class Json_msg_to_ros:
|
||||
rate = None
|
||||
# Ros publisher
|
||||
publisher_Cmd_Broadcast = None
|
||||
|
||||
Cmd_Broadcast_topicToMqtt = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def ros_pub(cls, dataJson):
|
||||
if dataJson.topic == cls.Cmd_Broadcast_topicToMqtt:
|
||||
logger.debug(dataJson.payload.decode("UTF-8"))
|
||||
# cls.publisher_Cmd_Broadcast.publish(dataJson.payload.decode("UTF-8"))
|
||||
# cls.rate.sleep()
|
||||
else:
|
||||
logger.info("topic not found")
|
||||
|
||||
|
||||
@staticmethod
|
||||
def on_message(client, userdata, msg):
|
||||
Json_msg_to_ros.ros_pub(msg)
|
||||
Loading…
Reference in New Issue