2 ##############################################################################
5 # @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 # @brief Base classes for python UAVObject
8 # @see The GNU Public License (GPL) Version 3
10 #############################################################################/
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 # You should have received a copy of the GNU General Public License along
23 # with this program; if not, write to the Free Software Foundation, Inc.,
24 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "openpilot.h"
36 #define TYPE_FLOAT32 6
41 from list import append
43 class UAVObjectMetadata
:
54 def __init__(self
, objId
):
55 self
.access
= UAVObjectMetadata
.Access
.READWRITE
56 self
.gcsAccess
= UAVObjectMetadata
.Access
.READWRITE
57 self
.telemetryAcked
= False
58 self
.telemetryUpdateMode
= UAVObjectMetadata
.UpdateMode
.MANUAL
59 self
.telemetryUpdatePeriod
= 0
60 self
.gcsTelemetryAcked
= False
61 self
.gcsTelemetryUpdateMode
= UAVObjectMetadata
.UpdateMode
.MANUAL
62 self
.gcsTelemetryUpdatePeriod
= 0
63 self
.loggingUpdateMode
= 0
64 self
.loggingUpdatePeriod
= UAVObjectMetadata
.UpdateMode
.MANUAL
85 def __init__(self
, ftype
, numElements
):
87 self
.numElements
= numElements
88 if ftype
== UAVObjectField
.FType
.FLOAT32
:
93 for n
in range(0, numElements
):
94 append(self
.value
, 0.0)
100 for n
in range(0, numElements
):
101 append(self
.value
, 0)
104 def __init__(self
, objId
):
105 self
.metadata
= UAVObjectMetadata(objId
)
110 def addField(self
, field
):
111 append(self
.fields
, field
)
114 # Support for getName was removed from embedded UAVO database to save RAM + Flash
118 UAVObjHandle objHandle;
127 uint8_t const *tmpStr;
129 // Get dictionary of class attributes
130 self = NATIVE_GET_LOCAL(0);
131 attrs = (pPmObj_t)((pPmInstance_t)self)->cli_attrs;
134 tmpStr = (uint8_t const *)"objId";
135 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
136 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
137 objId = ((pPmInt_t) field)->val;
140 objHandle = UAVObjGetByID(objId);
141 name = UAVObjGetName(objHandle);
143 // Create return object
144 retval = string_new(name, &nameObj); PM_RETURN_IF_ERROR(retval);
145 NATIVE_SET_TOS(nameObj);
154 UAVObjHandle objHandle;
169 uint32_t numElements;
170 uint8_t const *tmpStr;
175 // Get dictionary of class attributes
176 self = NATIVE_GET_LOCAL(0);
177 attrs = (pPmObj_t)((pPmInstance_t)self)->cli_attrs;
180 tmpStr = (uint8_t const *)"objId";
181 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
182 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
183 objId = ((pPmInt_t) field)->val;
185 // Get the instance ID
186 tmpStr = (uint8_t const *)"instId";
187 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
188 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
189 instId = ((pPmInt_t) field)->val;
191 // Get handle and number of bytes in the object
192 objHandle = UAVObjGetByID(objId);
193 numBytes = UAVObjGetNumBytes(objHandle);
194 uint8_t data[numBytes];
197 UAVObjGetInstanceData(objHandle, instId, data);
199 // Get dictionary of fields
200 tmpStr = (uint8_t const *)"fields";
201 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
202 retval = dict_getItem(attrs, fieldName, &fields); PM_RETURN_IF_ERROR(retval);
203 numFields = ((pPmList_t) fields)->length;
205 // Process each field
207 for (fieldIdx = 0; fieldIdx < numFields; ++fieldIdx)
210 retval = list_getItem(fields, fieldIdx, &field); PM_RETURN_IF_ERROR(retval);
211 attrs = (pPmObj_t)((pPmInstance_t)field)->cli_attrs;
213 tmpStr = (uint8_t const *)"ftype";
214 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
215 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
216 type = ((pPmInt_t) field)->val;
217 // Get number of elements
218 tmpStr = (uint8_t const *)"numElements";
219 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
220 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
221 numElements = ((pPmInt_t) field)->val;
223 tmpStr = (uint8_t const *)"value";
224 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
225 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
226 // Set value for each element
227 for (valueIdx = 0; valueIdx < numElements; ++valueIdx)
229 // Update value based on type
235 retval = int_new(data[dataIdx], &value); PM_RETURN_IF_ERROR(retval);
236 dataIdx = dataIdx + 1;
240 tmpInt16 = (int16_t*)(&data[dataIdx]);
241 retval = int_new(*tmpInt16, &value); PM_RETURN_IF_ERROR(retval);
242 dataIdx = dataIdx + 2;
246 tmpInt32 = (int32_t*)(&data[dataIdx]);
247 retval = int_new(*tmpInt32, &value); PM_RETURN_IF_ERROR(retval);
248 dataIdx = dataIdx + 4;
251 tmpFloat = (float*)(&data[dataIdx]);
252 retval = float_new(*tmpFloat, &value); PM_RETURN_IF_ERROR(retval);
253 dataIdx = dataIdx + 4;
257 if ( OBJ_GET_TYPE(field) == OBJ_TYPE_LST )
259 retval = list_setItem(field, valueIdx, value); PM_RETURN_IF_ERROR(retval);
263 tmpStr = (uint8_t const *)"value";
264 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
265 retval = dict_setItem(attrs, fieldName, value); PM_RETURN_IF_ERROR(retval);
278 UAVObjHandle objHandle;
293 uint32_t numElements;
294 uint8_t const *tmpStr;
300 // Get dictionary of class attributes
301 self = NATIVE_GET_LOCAL(0);
302 attrs = (pPmObj_t)((pPmInstance_t)self)->cli_attrs;
305 tmpStr = (uint8_t const *)"objId";
306 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
307 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
308 objId = ((pPmInt_t) field)->val;
310 // Get the instance ID
311 tmpStr = (uint8_t const *)"instId";
312 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
313 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
314 instId = ((pPmInt_t) field)->val;
316 // Get handle and number of bytes in the object
317 objHandle = UAVObjGetByID(objId);
318 numBytes = UAVObjGetNumBytes(objHandle);
319 uint8_t data[numBytes];
321 // Get dictionary of fields
322 tmpStr = (uint8_t const *)"fields";
323 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
324 retval = dict_getItem(attrs, fieldName, &fields); PM_RETURN_IF_ERROR(retval);
325 numFields = ((pPmList_t) fields)->length;
327 // Process each field
329 for (fieldIdx = 0; fieldIdx < numFields; ++fieldIdx)
332 retval = list_getItem(fields, fieldIdx, &field); PM_RETURN_IF_ERROR(retval);
333 attrs = (pPmObj_t)((pPmInstance_t)field)->cli_attrs;
335 tmpStr = (uint8_t const *)"ftype";
336 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
337 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
338 type = ((pPmInt_t) field)->val;
339 // Get number of elements
340 tmpStr = (uint8_t const *)"numElements";
341 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
342 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
343 numElements = ((pPmInt_t) field)->val;
345 tmpStr = (uint8_t const *)"value";
346 retval = string_new(&tmpStr, &fieldName); PM_RETURN_IF_ERROR(retval);
347 retval = dict_getItem(attrs, fieldName, &field); PM_RETURN_IF_ERROR(retval);
348 // Set value for each element
349 for (valueIdx = 0; valueIdx < numElements; ++valueIdx)
352 if ( OBJ_GET_TYPE(field) == OBJ_TYPE_LST )
354 retval = list_getItem(field, valueIdx, &value); PM_RETURN_IF_ERROR(retval);
358 // Update value based on type
364 if ( OBJ_GET_TYPE(value) == OBJ_TYPE_INT )
366 tmpInt8 = (int8_t)((pPmInt_t)value)->val;
368 else if ( OBJ_GET_TYPE(value) == OBJ_TYPE_FLT )
370 tmpInt8 = (int8_t)((pPmFloat_t)value)->val;
372 memcpy( &data[dataIdx], &tmpInt8, 1 );
373 dataIdx = dataIdx + 1;
377 if ( OBJ_GET_TYPE(value) == OBJ_TYPE_INT )
379 tmpInt16 = (int16_t)((pPmInt_t)value)->val;
381 else if ( OBJ_GET_TYPE(value) == OBJ_TYPE_FLT )
383 tmpInt16 = (int16_t)((pPmFloat_t)value)->val;
385 memcpy( &data[dataIdx], &tmpInt16, 2 );
386 dataIdx = dataIdx + 2;
390 if ( OBJ_GET_TYPE(value) == OBJ_TYPE_INT )
392 tmpInt32 = (int32_t)((pPmInt_t)value)->val;
394 else if ( OBJ_GET_TYPE(value) == OBJ_TYPE_FLT )
396 tmpInt32 = (int32_t)((pPmFloat_t)value)->val;
398 memcpy( &data[dataIdx], &tmpInt32, 4 );
399 dataIdx = dataIdx + 4;
402 if ( OBJ_GET_TYPE(value) == OBJ_TYPE_INT )
404 tmpFloat = (float)((pPmInt_t)value)->val;
406 else if ( OBJ_GET_TYPE(value) == OBJ_TYPE_FLT )
408 tmpFloat = (float)((pPmFloat_t)value)->val;
410 memcpy( &data[dataIdx], &tmpFloat, 4 );
411 dataIdx = dataIdx + 4;
418 UAVObjSetInstanceData(objHandle, instId, data);