Skip to content

Commit

Permalink
mavlink to REST API, seperated GPS and heading
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-iqt committed Oct 10, 2023
1 parent c581e0b commit c8a3ea4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
14 changes: 12 additions & 2 deletions gamutrf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
101 changes: 59 additions & 42 deletions gamutrf/mqtt_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@


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
self.gps_server = gps_server
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):
Expand All @@ -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(
{
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit c8a3ea4

Please sign in to comment.