Skip to content

Commit

Permalink
- added integer unit conversion coef attrib of msg members to pprz_m…
Browse files Browse the repository at this point in the history
…sg python interface
  • Loading branch information
Oswald Berthold committed Sep 10, 2015
1 parent 2a275d3 commit 7026bd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sw/lib/python/pprz_msg/message.py
Expand Up @@ -8,6 +8,7 @@
import json
import struct
from . import messages_xml_map
# import messages_xml_map


class PprzMessageError(Exception):
Expand All @@ -33,6 +34,7 @@ def __init__(self, class_name, msg):
self._id = messages_xml_map.get_msg_id(class_name, msg)
self._fieldnames = messages_xml_map.get_msg_fields(class_name, self._name)
self._fieldtypes = messages_xml_map.get_msg_fieldtypes(class_name, self._id)
self._fieldcoefs = messages_xml_map.get_msg_fieldcoefs(class_name, self._id)
self._fieldvalues = []
# set empty values according to type
for t in self._fieldtypes:
Expand Down Expand Up @@ -73,6 +75,11 @@ def fieldtypes(self):
"""Get list of field types."""
return self._fieldtypes

@property
def fieldcoefs(self):
"""Get list of field coefs."""
return self._fieldcoefs

def fieldbintypes(self, t):
"""Get type and length for binary format"""
data_types = {
Expand Down
20 changes: 20 additions & 0 deletions sw/lib/python/pprz_msg/messages_xml_map.py
Expand Up @@ -13,6 +13,7 @@

message_dictionary = {}
message_dictionary_types = {}
message_dictionary_coefs = {}
message_dictionary_id_name = {}
message_dictionary_name_id = {}

Expand All @@ -39,6 +40,7 @@ def parse_messages(messages_file=''):
message_dictionary_name_id[class_name] = {}
message_dictionary[class_name] = {}
message_dictionary_types[class_name] = {}
message_dictionary_coefs[class_name] = {}
for the_message in the_class.xpath("message[@name]"):
message_name = the_message.attrib['name']
if 'id' in the_message.attrib:
Expand All @@ -56,11 +58,17 @@ def parse_messages(messages_file=''):
# insert this message into our dictionary as a list with room for the fields
message_dictionary[class_name][message_name] = []
message_dictionary_types[class_name][message_id] = []
message_dictionary_coefs[class_name][message_id] = []

for the_field in the_message.xpath('field[@name]'):
# for now, just save the field names -- in the future maybe expand this to save a struct?
message_dictionary[class_name][message_name].append(the_field.attrib['name'])
message_dictionary_types[class_name][message_id].append(the_field.attrib['type'])
try:
message_dictionary_coefs[class_name][message_id].append(float(the_field.attrib['alt_unit_coef']))
except KeyError:
# print("no such key")
message_dictionary_coefs[class_name][message_id].append(1.)


def get_msgs(msg_class):
Expand Down Expand Up @@ -121,6 +129,18 @@ def get_msg_fieldtypes(msg_class, msg_id):
print("Error: msg_class %s not found." % msg_class)
return []

def get_msg_fieldcoefs(msg_class, msg_id):
if not message_dictionary:
parse_messages()
if msg_class in message_dictionary_coefs:
if msg_id in message_dictionary_coefs[msg_class]:
return message_dictionary_coefs[msg_class][msg_id]
else:
print("Error: message with ID %d not found in msg_class %s." % (msg_id, msg_class))
else:
print("Error: msg_class %s not found." % msg_class)
return []


def test():
import argparse
Expand Down

0 comments on commit 7026bd4

Please sign in to comment.