up to date

main
RangeOfGlitching 3 years ago
parent c664674eef
commit c42364c9fa

@ -5,7 +5,7 @@ import os
import sys
import time
import utils
import argparse
import proto.flyformatioln_pb2 as flyformatioln_pb2
import logging
from utils.protoJson_delayClientA_PUB import Json_msg
@ -41,9 +41,22 @@ def on_publish(self, userdata, mid):
if __name__ == '__main__':
time.sleep(0.25)
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--qos", type=int, help="qos")
parser.add_argument("-p", "--port", type=int, help="port")
parser.add_argument("-f", "--format", type=str, help="format")
args = parser.parse_args()
# Read Config
FilePath = os.path.join(os.path.dirname(__file__),"utils","mqttConfig_delayA_PUB.yml")
cfg = utils.Read_delayA_PUB_Config(FilePath)
if args.qos != None:
cfg.qos = args.qos
if args.port != None:
cfg.port = args.port
if args.format != None:
cfg.msg_format = args.format
# Mqtt
client = utils.MQTTClient(cfg.MQTTClientNamePub)
pubFun = None

@ -9,7 +9,7 @@ import logging
import utils
from utils.protoJson_delayClientA_SUB import Json_msg
from utils.protoJson_delayClientA_SUB import Proto_msg
import argparse
def init_dataFormat(cfg:utils.Read_delayB_SUB_Config):
if cfg.msg_format == "Proto":
@ -35,11 +35,22 @@ def on_publish(self, userdata, mid):
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--qos", type=int, help="qos")
parser.add_argument("-p", "--port", type=int, help="port")
parser.add_argument("-f", "--format", type=str, help="format")
args = parser.parse_args()
# Read Config
FilePath = os.path.join(os.path.dirname(__file__),"utils","mqttConfig_delayA_SUB.yml")
cfg = utils.Read_delayA_SUB_Config(FilePath)
client = utils.MQTTClient(cfg.MQTTClientNameSub)
if args.qos != None:
cfg.qos = args.qos
if args.port != None:
cfg.port = args.port
if args.format != None:
cfg.msg_format = args.format
init_dataFormat(cfg)
# set log

@ -3,6 +3,7 @@
import paho.mqtt.client as mqtt
import os
import sys
import argparse
import utils
import proto.flight_information_pb2 as flight_information_pb2
import proto.flyformatioln_pb2 as flyformatioln_pb2
@ -44,11 +45,25 @@ def on_publish(self, userdata, mid):
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--qos", type=int, help="qos")
parser.add_argument("-p", "--port", type=int, help="port")
parser.add_argument("-f", "--format", type=str, help="format")
args = parser.parse_args()
# Read Config
FilePath = os.path.join(os.path.dirname(__file__),"utils","mqttConfig_delayB_SUB.yml")
cfg = utils.Read_delayB_SUB_Config(FilePath)
if args.qos != None:
cfg.qos = args.qos
if args.port != None:
cfg.port = args.port
if args.format != None:
cfg.msg_format = args.format
client = utils.MQTTClient(cfg.MQTTClientNameSub)
# Mqtt
client = utils.MQTTClient(cfg.MQTTClientNameSub)
client.on_connect = on_connect

@ -1,13 +0,0 @@
from pathlib import Path
# Get the path to the directory to traverse
directory = Path.cwd()
Protodir = []
Jsondir = []
# Iterate over the files and directories in the directory tree
for path in directory.glob('**/*.log'):
if "Proto" in str(path):
Protodir.append(path)
elif "Json" in str(path):
Jsondir.append(path)
print(Protodir)

@ -15,8 +15,9 @@ def checkNumberofLines(df):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--qos", type=int, default=0, help="qos", required=True)
parser.add_argument("-b", "--broker", type=str, default="{}", help="Broker", required=True)
parser.add_argument("-b", "--broker", type=str, default="HiveMQ", help="Broker", required=True)
args = parser.parse_args()
# read in the log file
df_Proto_delayA_PUB = pd.read_csv(f"{args.broker}/qos{args.qos}/ProtodelayA_PUB.log", delim_whitespace=True, header=None, names=["count", "time"])
@ -47,8 +48,8 @@ if __name__ == "__main__":
avg_Json = np.average(array_Json)
fig = plt.figure()
plt.plot(array_Proto, color="blue", label=f"Proto: {avg_Proto}")
plt.plot(array_Json, color="red", label=f"Json: {avg_Json}")
# plt.plot(array_Proto, color="blue", label=f"Proto: {avg_Proto}")
plt.plot(array_Json, color="red", label=f"qos0: {avg_Json}")
plt.legend()
plt.xlabel("Number of messages")
plt.ylabel("Delay (s)")

@ -0,0 +1,3 @@
python delayClientA_PUB.py -q 0 -p 1884 -f Json

@ -1,7 +1,7 @@
MQTT:
msg_format: Proto
# msg_format: Json
qos: 1
# msg_format: Proto
msg_format: Json
qos: 0
MQTTClientNamePub: delayA_PUB
host: 192.168.50.117
port: 1883

@ -1,7 +1,7 @@
MQTT:
msg_format: Proto
# msg_format: Json
qos: 1
# msg_format: Proto
msg_format: Json
qos: 0
MQTTClientNameSub: delayA_SUB
host: 192.168.50.117
port: 1883

@ -1,7 +1,7 @@
MQTT:
msg_format: Proto
# msg_format: Json
qos: 1
# msg_format: Proto
msg_format: Json
qos: 0
MQTTClientNameSub: delayB_SUB
host: 192.168.50.117
port: 1883

@ -25,9 +25,9 @@ import random
class fakeGps():
def __init__(self):
self.latitude = 0.0
self.longitude = 20
self.altitude = 30
self.latitude = 8.0
self.longitude = 8.1
self.altitude = 8.88
class fake_hdg():
def __init__(self):
@ -99,7 +99,7 @@ if __name__ == '__main__':
file_handler.setLevel(logging.INFO)
logger = logging.getLogger("__PUB__")
logger.setLevel(logging.CRITICAL)
logger.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
@ -128,10 +128,10 @@ if __name__ == '__main__':
# formation = fake_formation()
while True:
# test proto
gps.latitude = random.uniform(0, 10)
gps.longitude = random.uniform(0, 10)
gps.altitude = random.uniform(0, 10)
hdg.data = random.uniform(0, 10)
# gps.latitude = random.uniform(10, 100)
# gps.longitude = random.uniform(10, 100)
# gps.altitude = random.uniform(10, 100)
# hdg.data = random.uniform(0, 10)
# formation.velocity = random.uniform(0, 10)
try:

@ -20,7 +20,8 @@ def init_dataFormat(cfg:utils.Read_SUB_Config):
utils.Proto_msg_to_ros.flight_information_msg = flight_information_pb2.flight_information_message()
utils.Proto_msg_to_ros.fly_formation_msg = flyformatioln_pb2.fly_formation_message()
client.on_message = utils.Proto_msg_to_ros.on_message
client.message_callback_add(cfg.Flight_Information_topicToMqtt, utils.Proto_msg_to_ros.on_message_Flight_Information)
client.message_callback_add(cfg.Fly_Formation_topicToMqtt, utils.Proto_msg_to_ros.on_message_Fly_Formation)
utils.Proto_msg_to_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
utils.Proto_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
@ -28,20 +29,21 @@ def init_dataFormat(cfg:utils.Read_SUB_Config):
# utils.Json_msg_to_ros.rate = rospy.Rate(10)
# utils.Json_msg_to_ros.publisher_Flight_Information = rospy.Publisher(cfg.ROStopicName_Flight_Information,String,queue_size=10)
# utils.Json_msg_to_ros.publisher_Fly_Formation = rospy.Publisher(cfg.ROStopicName_Fly_Formation,String,queue_size=10)
client.message_callback_add(cfg.Flight_Information_topicToMqtt, utils.Json_msg_to_ros.on_message_Flight_Information)
client.message_callback_add(cfg.Fly_Formation_topicToMqtt, utils.Json_msg_to_ros.on_message_Fly_Formation)
client.on_message = utils.Json_msg_to_ros.on_message
utils.Json_msg_to_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
utils.Json_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
else:
logging.debug("msg_format not found")
# def on_message(client, userdata, msg):
# utils.Json_msg_to_ros.ros_pub(msg)
def on_connect(self, userdata, flags, rc):
logger.info("Connected with result code " + str(rc))
client.subscribe(cfg.Flight_Information_topicToMqtt)
client.subscribe(cfg.Fly_Formation_topicToMqtt)
def on_disconnect(client, userdata, rc):
# logger.info("disconnecting reason " +str(rc))

@ -0,0 +1,109 @@
#!/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 rospy
from std_msgs.msg import String
import logging
def init_dataFormat(cfg:utils.Read_SUB_Config):
if cfg.msg_format == "Proto":
# utils.Proto_msg_to_ros.rate = rospy.Rate(10)
# utils.Proto_msg_to_ros.publisher_Flight_Information = rospy.Publisher(cfg.ROStopicName_Flight_Information,String,queue_size=10)
# utils.Proto_msg_to_ros.publisher_Fly_Formation = rospy.Publisher(cfg.ROStopicName_Fly_Formation,String,queue_size=10)
utils.Proto_msg_to_ros.flight_information_msg = flight_information_pb2.flight_information_message()
utils.Proto_msg_to_ros.fly_formation_msg = flyformatioln_pb2.fly_formation_message()
client.on_message = utils.Proto_msg_to_ros.on_message
utils.Proto_msg_to_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
utils.Proto_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
elif cfg.msg_format == "Json":
# utils.Json_msg_to_ros.rate = rospy.Rate(10)
# utils.Json_msg_to_ros.publisher_Flight_Information = rospy.Publisher(cfg.ROStopicName_Flight_Information,String,queue_size=10)
# utils.Json_msg_to_ros.publisher_Fly_Formation = rospy.Publisher(cfg.ROStopicName_Fly_Formation,String,queue_size=10)
client.on_message = utils.Json_msg_to_ros.on_message
utils.Json_msg_to_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
utils.Json_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
else:
logging.debug("msg_format not found")
# def on_message(client, userdata, msg):
# utils.Json_msg_to_ros.ros_pub(msg)
def on_connect(self, userdata, flags, rc):
logger.info("Connected with result code " + str(rc))
client.subscribe(cfg.Flight_Information_topicToMqtt)
# client.subscribe(cfg.Fly_Formation_topicToMqtt)
client.subscribe("cmd/Change_Leader")
client.message_callback_add("cmd/Change_Leader", on_message_ChangLeader)
def on_message_ChangLeader(client, userdata, msg):
client.unsubscribe(cfg.Flight_Information_topicToMqtt)
cfg.Flight_Information_topicToMqtt = "Drone650/Flight_Information"
client.subscribe(cfg.Flight_Information_topicToMqtt)
def on_disconnect(client, userdata, rc):
# logger.info("disconnecting reason " +str(rc))
client.connected_flag=False
client.disconnect_flag=True
def on_publish(self, userdata, mid):
pass
if __name__ == '__main__':
# Read Config
FilePath = os.path.join(os.path.dirname(__file__),"utils","mqttConfig_SUB.yml")
cfg = utils.Read_SUB_Config(FilePath)
client = utils.MQTTClient(cfg.MQTTClientNameSub)
# 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("__SUB__")
logger.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
logger.info(cfg)
# Mqtt
client = utils.MQTTClient(cfg.MQTTClientNameSub)
client.on_connect = on_connect
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.connect(host=cfg.host, port=cfg.port, keepalive=cfg.keepalive)
# Ros
# rospy.init_node(cfg.ROSClientNameSub)
# initialize
init_dataFormat(cfg)
try:
client.loop_forever()
# rospy.spin()
except KeyboardInterrupt as e:
client.loop_stop()
client.disconnect()
logger.info("End of program")
sys.exit()

@ -1 +1 @@
{"flight_info_message":{"json":85,"proto":22},"fly_format_message":{"json":55,"proto":7},"imu_info_message":{"json":130,"proto":34},"odom_info_message":{"json":59,"proto":17},"flymode_info_message":{"json":38,"proto":2},"duration_info_message":{"json":8,"proto":4},"timestamp_info_message":{"json":29,"proto":11},"Total":{"json":404,"proto":97}}
{"flight_info_message":{"json":85,"proto":22},"fly_format_message":{"json":55,"proto":7},"imu_info_message":{"json":130,"proto":34},"odom_info_message":{"json":59,"proto":17},"flymode_info_message":{"json":38,"proto":2},"duration_info_message":{"json":8,"proto":4},"timestamp_info_message":{"json":29,"proto":12},"Total":{"json":404,"proto":98}}

@ -1 +1 @@
"2022-12-08T16:44:17.067479Z"
"2023-02-20T17:27:58.498239Z"

@ -1,7 +1,7 @@
MQTT:
msg_format: Json
MQTTClientNamePub: Drone550Pub
host: 192.168.50.118
host: 192.168.50.117
port: 1883
keepalive: 60
willTopic: CheckDoneConnect

@ -1,7 +1,7 @@
MQTT:
msg_format: Json
MQTTClientNameSub: Drone550Sub
host: 192.168.50.118
host: 192.168.50.117
port: 1883
keepalive: 60
willTopic: CheckDoneConnect

@ -24,29 +24,23 @@ class Proto_msg_to_ros:
Fly_Formation_topicToMqtt = None
#Proto
@classmethod
def ros_pub(cls, dataProto):
if dataProto.topic == cls.Flight_Information_topicToMqtt:
proto_msg = cls.flight_information_msg.FromString(dataProto.payload)
protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True)
logger.info(protoTOJson_msg)
# cls.publisher_Flight_Information.publish(protoTOJson_msg)
# cls.rate.sleep()
elif dataProto.topic == cls.Fly_Formation_topicToMqtt:
proto_msg = cls.fly_formation_msg.FromString(dataProto.payload)
protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True)
# protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=True)
logger.info(protoTOJson_msg)
# cls.publisher_Fly_Formation.publish(protoTOJson_msg)
# cls.rate.sleep()
else:
logger.info("topic not found")
@staticmethod
def on_message(client, userdata, msg):
Proto_msg_to_ros.ros_pub(msg)
def on_message_Flight_Information(cls, client, userdata, msg):
proto_msg = cls.flight_information_msg.FromString(msg.payload)
protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True)
logger.info(protoTOJson_msg)
# cls.publisher_Flight_Information.publish(protoTOJson_msg)
# cls.rate.sleep()
@classmethod
def on_message_Fly_Formation(cls, client, userdata, msg):
proto_msg = cls.fly_formation_msg.FromString(msg.payload)
protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True)
# protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=True)
logger.info(protoTOJson_msg)
# cls.publisher_Fly_Formation.publish(protoTOJson_msg)
# cls.rate.sleep()
class Json_msg_to_ros:
@ -59,19 +53,17 @@ class Json_msg_to_ros:
Fly_Formation_topicToMqtt = None
@classmethod
def ros_pub(cls, dataJson):
if dataJson.topic == cls.Flight_Information_topicToMqtt:
logger.info(dataJson.payload.decode("UTF-8"))
# cls.publisher_Flight_Information.publish(dataJson.payload.decode("UTF-8"))
# cls.rate.sleep()
elif dataJson.topic == cls.Fly_Formation_topicToMqtt:
logger.info(dataJson.payload.decode("UTF-8"))
# cls.publisher_Fly_Formation.publish(dataJson.payload.decode("UTF-8"))
# cls.rate.sleep()
else:
logger.info("topic not found")
def on_message_Flight_Information(cls, client, userdata, msg):
logger.info(msg.payload.decode("UTF-8"))
# cls.publisher_Flight_Information.publish(dataJson.payload.decode("UTF-8"))
#cls.rate.sleep()
@classmethod
def on_message_Fly_Formation(cls, client, userdata, msg):
logger.info(msg.payload.decode("UTF-8"))
# cls.publisher_Fly_Formation.publish(dataJson.payload.decode("UTF-8"))
# cls.rate.sleep()
@staticmethod
def on_message(client, userdata, msg):
Json_msg_to_ros.ros_pub(msg)
Loading…
Cancel
Save