[python] pprz_msg: minor fixes and move_waypoint_example.py

This commit is contained in:
Felix Ruess
2015-09-14 13:52:17 +02:00
parent 858427bb11
commit 53273a05ca
4 changed files with 57 additions and 7 deletions
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env python
from __future__ import print_function
import sys
from os import path, getenv
# if PAPARAZZI_SRC not set, then assume the tree containing this
# file is a reasonable substitute
PPRZ_SRC = getenv("PAPARAZZI_SRC", path.normpath(path.join(path.dirname(path.abspath(__file__)), '../../../../')))
sys.path.append(PPRZ_SRC + "/sw/lib/python")
from ivy_msg_interface import IvyMessagesInterface
from pprz_msg.message import PprzMessage
class WaypointMover(object):
def __init__(self, verbose=False):
self.verbose = verbose
self._interface = IvyMessagesInterface(self.message_recv)
def message_recv(self, ac_id, msg):
if self.verbose:
print("Got msg %s" % msg.name)
def shutdown(self):
print("Shutting down ivy interface...")
self._interface.shutdown()
def __del__(self):
self.shutdown()
def move_waypoint(self, ac_id, wp_id, lat, lon, alt):
msg = PprzMessage("ground", "MOVE_WAYPOINT")
msg['ac_id'] = ac_id
msg['wp_id'] = wp_id
msg['lat'] = lat
msg['long'] = lon
msg['alt'] = alt
print("Sending message: %s" % msg)
self._interface.send(msg)
if __name__ == '__main__':
wm = WaypointMover()
wm.move_waypoint(ac_id=202, wp_id=3, lat=43.563, lon=1.481, alt=172.0)
wm.shutdown()
+3
View File
@@ -13,6 +13,7 @@ PPRZ_SRC = os.getenv("PAPARAZZI_SRC", os.path.normpath(os.path.join(os.path.dirn
sys.path.append(PPRZ_SRC + "/sw/lib/python")
from pprz_msg.message import PprzMessage
from pprz_msg import messages_xml_map
class IvyMessagesInterface(object):
@@ -20,6 +21,8 @@ class IvyMessagesInterface(object):
self.callback = callback
self.ivy_id = 0
self.verbose = verbose
# make sure all messages are parsed before we start creating them in callbacks
messages_xml_map.parse_messages()
self.init_ivy(init, bind_regex)
def stop(self):
+5 -5
View File
@@ -3,12 +3,11 @@ Paparazzi message representation
"""
from __future__ import absolute_import, division, print_function
from __future__ import division, print_function
import sys
import json
import struct
from . import messages_xml_map
# import messages_xml_map
import messages_xml_map
class PprzMessageError(Exception):
@@ -118,11 +117,12 @@ class PprzMessage(object):
self.set_value_by_name(key, value)
def set_values(self, values):
#print("msg %s: %s" % (self.name, ", ".join(self.fieldnames)))
if len(values) == len(self.fieldnames):
self._fieldvalues = values
else:
print("set values %i %i" % (len(values), len(self.fieldnames)))
raise PprzMessageError("Error: fields not matching")
raise PprzMessageError("Error: Msg %s has %d fields, tried to set %i values" %
(self.name, len(self.fieldnames), len(values)))
def set_value_by_name(self, name, value):
# Try to set a value from its name
+3 -2
View File
@@ -31,6 +31,7 @@ def parse_messages(messages_file=''):
messages_file = default_messages_file
if not os.path.isfile(messages_file):
raise MessagesNotFound(messages_file)
#print("Parsing %s" % messages_file)
from lxml import etree
tree = etree.parse(messages_file)
for the_class in tree.xpath("//msg_class[@name]"):
@@ -152,8 +153,8 @@ def test():
parse_messages(args.file)
if args.list_messages:
print("Listing %i messages in '%s' msg_class" % (len(message_dictionary[args.msg_class]), args.msg_class))
for msg in message_dictionary[args.msg_class]:
print(msg)
for msg_name, msg_fields in message_dictionary[args.msg_class].iteritems():
print(msg_name + ": " + ", ".join(msg_fields))
if __name__ == '__main__':
test()