From c8a3ea41830f2640d8652a228ce52370084c01e1 Mon Sep 17 00:00:00 2001 From: lk-iqt <112730501+lk-iqt@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:05:33 -0400 Subject: [PATCH] mavlink to REST API, seperated GPS and heading --- gamutrf/api.py | 14 +++++- gamutrf/mqtt_reporter.py | 101 +++++++++++++++++++++++---------------- 2 files changed, 71 insertions(+), 44 deletions(-) diff --git a/gamutrf/api.py b/gamutrf/api.py index 386f9e4a..e19122e8 100644 --- a/gamutrf/api.py +++ b/gamutrf/api.py @@ -145,10 +145,18 @@ def argument_parser(): action=argparse.BooleanOptionalAction, help="get RSSI values", ) - parser.add_argument( "--use_mavlink_gps", help="Use external Pixhawk/MAVLINK GPS", default=False, type=bool ) + parser.add_argument( + "--use_mavlink_heading", help="Use external Pixhawk/MAVLINK heading", default=False, type=bool + ) + parser.add_argument( + "--mavlink_api_server", + help="server to query for mavlink GPS data", + default="mavlink-api", + type=str, + ) return parser @@ -223,7 +231,9 @@ def __init__(self, arguments): mqtt_server = self.arguments.mqtt_server, gps_server = ORCHESTRATOR, compass = True, - use_mavlink_gps = self.arguments.use_mavlink_gps + use_mavlink_gps = self.arguments.use_mavlink_gps, + use_mavlink_heading = self.arguments.use_mavlink_heading, + mavlink_api_server= self.arguments.mavlink_api_server ) self.q = queue.Queue(self.arguments.qsize) self.sdr_recorder = get_recorder( diff --git a/gamutrf/mqtt_reporter.py b/gamutrf/mqtt_reporter.py index eea247b2..a669b26e 100644 --- a/gamutrf/mqtt_reporter.py +++ b/gamutrf/mqtt_reporter.py @@ -10,7 +10,7 @@ class MQTTReporter: - def __init__(self, name, mqtt_server=None, gps_server=None, compass=False, use_mavlink_gps=False): + def __init__(self, name, mqtt_server=None, gps_server=None, compass=False, use_mavlink_gps=False, use_mavlink_heading=False, mavlink_api_server=None): self.name = name self.mqtt_server = mqtt_server self.compass = compass @@ -18,9 +18,11 @@ def __init__(self, name, mqtt_server=None, gps_server=None, compass=False, use_m self.mqttc = None self.heading = "no heading" self.use_mavlink_gps = use_mavlink_gps - self.mavlink_gps_topic = '/MAVLINK-GPS' + self.use_mavlink_heading = use_mavlink_heading + self.mavlink_api_server = mavlink_api_server self.mavlink_gps_msg = None - self.mavlink_max_wait_time_s = 3 + #self.mavlink_gps_topic = '/MAVLINK-GPS' + #self.mavlink_max_wait_time_s = 3 @staticmethod def log(path, prefix, start_time, record_args): @@ -38,21 +40,30 @@ def connect(self): logging.info(f"connecting to {self.mqtt_server}") self.mqttc = mqtt.Client() self.mqttc.connect(self.mqtt_server) - if self.use_mavlink_gps: - self.mqttc.subscribe(self.mavlink_gps_topic) - self.mqttc.on_message = self.mavlink_gps_msg_callback + #if self.use_mavlink_gps: + # self.mqttc.subscribe(self.mavlink_gps_topic) + # self.mqttc.on_message = self.mavlink_gps_msg_callback self.mqttc.loop_start() def get_heading(self): - try: - self.heading = str( - float(httpx.get(f"http://{self.gps_server}:8000/v1/heading").text) - ) - except Exception as err: - logging.error("could not update heading: %s", err) + if self.use_mavlink_heading: + try: + self.headding=( + float(httpx.get(f"http://{self.mavlink_api_server}:8888/heading").text) + ) + except Exception as err: + logging.error("could not update mavlink heading: %s", err) + else: + try: + self.heading = str( + float(httpx.get(f"http://{self.gps_server}:8000/v1/heading").text) + ) + except Exception as err: + logging.error("could not update heading: %s", err) def add_gps(self, publish_args): - if not self.gps_server: + + if not self.gps_server or not self.use_mavlink_gps: return publish_args publish_args.update( { @@ -64,31 +75,37 @@ def add_gps(self, publish_args): "gps": "no fix", } ) - try: - #Use external MAVLINK GPS - if self.use_mavlink_gps: - self.mqttc.publish(self.mavlink_gps_topic, json.dumps({"msg":"GPS"})) - start_time=time.time() - while self.mavlink_gps_msg == None and time.time() - start_time < self.mavlink_max_wait_time_s: - time.sleep(0.001) - if self.mavlink_gps_msg == None: - return publish_args - else: - publish_args.update( - { - "position": self.mavlink_gps_msg["lat"]+","+self.mavlink_gps_msg["lon"], - "altitude": self.mavlink_gps_msg["alt"], - "gps_time": self.mavlink_gps_msg["time_usec"], - "map_url": None, - "heading": self.mavlink_gps_msg["hdg"], - "gps": "fix", - } - ) - self.heading=self.mavlink_gps_msg["hdg"] - self.mavlink_gps_msg = None + + #Use external MAVLINK GPS + if self.use_mavlink_gps: + try: + #self.mqttc.publish(self.mavlink_gps_topic, json.dumps({"msg":"GPS"})) + #start_time=time.time() + #while self.mavlink_gps_msg == None and time.time() - start_time < self.mavlink_max_wait_time_s: + # time.sleep(0.001) + #if self.mavlink_gps_msg == None: + # return publish_args + #else: + self.mavlink_gps_msg=json.loads(httpx.get(f"http://{self.mavlink_api_server}:8888/gps-data").text) + + publish_args.update( + { + "position": self.mavlink_gps_msg["lat"]+","+self.mavlink_gps_msg["lon"], + "altitude": self.mavlink_gps_msg["alt"], + "gps_time": self.mavlink_gps_msg["time_usec"], + "map_url": None, + "heading": self.heading, + "gps": "fix", + } + ) + #self.heading=self.mavlink_gps_msg["hdg"] + #self.mavlink_gps_msg = None + except Exception as err: + logging.error("could not update with mavlink GPS: %s", err) - #Use internal GPIO GPS - else: + #Use internal GPIO GPS + else: + try: if self.compass: self.get_heading() if gpsd.gpsd_stream is None: @@ -104,13 +121,13 @@ def add_gps(self, publish_args): "gps": "fix", } ) - except (BrokenPipeError, gpsd.NoFixError, AttributeError) as err: - logging.error("could not update with GPS: %s", err) + except (BrokenPipeError, gpsd.NoFixError, AttributeError) as err: + logging.error("could not update with GPS: %s", err) return publish_args - def mavlink_gps_msg_callback(self, client, userdata, msg): - gps_msg=json.loads(msg) - self.mavlink_gps_msg + #def mavlink_gps_msg_callback(self, client, userdata, msg): + # gps_msg=json.loads(msg) + # self.mavlink_gps_msg = gps_msg def publish(self, publish_path, publish_args): if not self.mqtt_server: