Go to the documentation of this file.00001
00002
00003 import struct
00004 import rospy
00005 import vigir_footstep_planning_msgs.msg
00006
00007 from vigir_generic_params.msg import ParameterMsg
00008 from vigir_footstep_planning_msgs.serialization import *
00009
00010 class Parameter():
00011
00012 TYPE_INVALID = 0
00013 TYPE_BOOLEAN = 1
00014 TYPE_INT = 2
00015 TYPE_DOUBLE = 3
00016 TYPE_STRING = 4
00017 TYPE_DATE_TIME = 5
00018 TYPE_BASE64 = 6
00019 TYPE_ARRAY = 7
00020 TYPE_STRUCT = 8
00021
00022 _type = int()
00023 _name = str()
00024 _value = None
00025
00026 def __init__(self, _type = TYPE_INVALID, name = str(), value = None, msg = None):
00027 if msg is not None:
00028 self.from_msg(msg)
00029 elif (_type != self.TYPE_INVALID):
00030 self.set_type(_type)
00031 self.set_name(name)
00032 self.set_value(value)
00033
00034 def __str__(self):
00035 return self._name + " (" + str(self.get_type_as_text()) + "): " + str(self._value)
00036
00037 def __repr__(self):
00038 return self.__str__()
00039
00040 def set_name(self, name):
00041 self._name = name
00042
00043 def get_name(self):
00044 return self._name
00045
00046 def set_type(self, t):
00047 self._type = t
00048
00049 def get_type(self):
00050 return self._type
00051
00052 def get_type_as_text(self):
00053 if self.get_type() == self.TYPE_BOOLEAN:
00054 return "bool"
00055 elif self.get_type() == self.TYPE_INT:
00056 return "int"
00057 elif (self.get_type() == self.TYPE_DOUBLE):
00058 return "double"
00059 elif (self.get_type() == self.TYPE_STRING):
00060 return "string"
00061 elif (self.get_type() == self.TYPE_DATE_TIME):
00062 return "date_time"
00063 elif (self.get_type() == self.TYPE_BASE64):
00064 return "base64"
00065 elif (self.get_type() == self.TYPE_ARRAY):
00066 return "array"
00067 elif (self.get_type() == self.TYPE_STRUCT):
00068 return "struct"
00069 else:
00070 return "unknown"
00071
00072 def set_value(self, value):
00073 try:
00074 if self.get_type() == self.TYPE_BOOLEAN:
00075 if (value.lower() == "true"):
00076 self._value = True
00077 if (value.lower() == "false"):
00078 self._value = False
00079 elif self.get_type() == self.TYPE_INT:
00080 self._value = int(value)
00081 elif (self.get_type() == self.TYPE_DOUBLE):
00082 self._value = float(value)
00083 elif (self.get_type() == self.TYPE_STRING):
00084 self._value = str(value)
00085 elif (self.get_type() == self.TYPE_ARRAY):
00086 if (len(self._value) == 0):
00087 return False
00088 _type = self._value[0].get_type()
00089 _name = self._value[0].get_name()
00090 _value = []
00091 for val in value.split():
00092 val = val.strip('[],')
00093 p = Parameter(_type, _name, val)
00094 if p.get_value() is not None:
00095 _value.append(p)
00096 else:
00097 return False
00098 self._value = _value
00099 else:
00100 rospy.logerr("set_value called on parameter with incompatible type!")
00101 return False
00102 except ValueError:
00103 return False
00104 return True
00105
00106 def get_value(self):
00107 if (self.get_type() == self.TYPE_ARRAY):
00108 val = []
00109 for p in self._value:
00110 val.append(p.get_value())
00111 return val
00112 else:
00113 return self._value
00114
00115 def to_msg(self):
00116 msg = vigir_footstep_planning_msgs.msg.ParameterMsg()
00117 msg.key.data = self.get_name()
00118
00119
00120 msg.data += pack_uint8(self.get_type())
00121
00122 if self.get_type() == self.TYPE_BOOLEAN:
00123 msg.data += pack_bool(self._value)
00124 elif self.get_type() == self.TYPE_INT:
00125 msg.data += pack_int32(self._value)
00126 elif (self.get_type() == self.TYPE_DOUBLE):
00127 msg.data += pack_double(self._value)
00128 elif (self.get_type() == self.TYPE_STRING):
00129 msg.data += pack_string(self._value)
00130 elif (self.get_type() == self.TYPE_ARRAY):
00131 msg.data += pack_uint32(len(self._value))
00132 for p in self._value:
00133 msg.data += p.to_msg().data
00134 elif (self.get_type() == self.TYPE_STRUCT):
00135 msg.data += pack_uint32(len(self._value))
00136 for p in self._value:
00137 msg.data += pack_string(p.get_name()) + p.to_msg().data
00138 else:
00139 print "ERROR: Unsupported type (" + str(self.get_type()) + ")!"
00140
00141 return msg
00142
00143 def from_msg(self, msg, offset = 0):
00144
00145 self._name = msg.key.data
00146
00147
00148 self._type, offset = unpack_uint8(msg.data, offset)
00149
00150 if self.get_type() == self.TYPE_BOOLEAN:
00151 self._value, offset = unpack_bool(msg.data, offset)
00152 elif self.get_type() == self.TYPE_INT:
00153 self._value, offset = unpack_int32(msg.data, offset)
00154 elif (self.get_type() == self.TYPE_DOUBLE):
00155 self._value, offset = unpack_double(msg.data, offset)
00156 elif (self.get_type() == self.TYPE_STRING):
00157 self._value, offset = unpack_string(msg.data, offset)
00158 elif (self.get_type() == self.TYPE_ARRAY):
00159 size, offset = unpack_uint32(msg.data, offset)
00160 self._value = []
00161 for i in range(size):
00162 p = Parameter()
00163 offset = p.from_msg(msg, offset)
00164 self._value.append(p)
00165 elif (self.get_type() == self.TYPE_STRUCT):
00166 size, offset = unpack_uint32(msg.data, offset)
00167 self._value = []
00168 for i in range(size):
00169
00170 name, offset = unpack_string(msg.data, offset)
00171
00172 p = Parameter()
00173 offset = p.from_msg(msg, offset)
00174 p.set_name(name)
00175 self._value.append(p)
00176 else:
00177 print "ERROR: Unsupported type (" + str(self.get_type()) + ")!"
00178
00179 return offset
00180