2 # *****************************************************************************
5 # * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 # * @brief Python OpenPilot library, gives FlightPlan scripts access to
7 # * RTOS and other functions
9 # * @see The GNU Public License (GPL) Version 3
11 # *****************************************************************************
13 # * This program is free software; you can redistribute it and/or modify
14 # * it under the terms of the GNU General Public License as published by
15 # * the Free Software Foundation; either version 3 of the License, or
16 # * (at your option) any later version.
18 # * This program is distributed in the hope that it will be useful, but
19 # * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 # * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 # * You should have received a copy of the GNU General Public License along
24 # * with this program; if not, write to the Free Software Foundation, Inc.,
25 # * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "openpilot.h"
30 #include "flightplanstatus.h"
31 #include "flightplancontrol.h"
34 # Delay (suspend VM thread) for timeToDelayMs ms
35 def delay(timeToDelayMs
):
39 portTickType timeToDelayTicks;
41 // Check number of arguments
42 if (NATIVE_GET_NUM_ARGS() != 1)
44 PM_RAISE(retval, PM_RET_EX_TYPE);
49 pobj = NATIVE_GET_LOCAL(0);
50 if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT )
51 timeToDelayTicks = (portTickType)(((pPmInt_t) pobj)->val) / portTICK_RATE_MS;
52 else if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_FLT )
53 timeToDelayTicks = (portTickType)(((pPmFloat_t) pobj)->val) / portTICK_RATE_MS;
56 PM_RAISE(retval, PM_RET_EX_TYPE);
61 vTaskDelay(timeToDelayTicks);
67 # Same as delay() but will result in more exact periodic execution
68 # lastWakeTimeMs should be initialized with the system time.
70 # timenow = openpilot.time()
72 # timenow = openpilot.delayUntil(timenow, 1000)
74 def delayUntil(lastWakeTimeMs
, timeToDelayMs
):
79 portTickType lastWakeTimeTicks;
80 portTickType timeToDelayTicks;
82 // Check number of arguments
83 if (NATIVE_GET_NUM_ARGS() != 2)
85 PM_RAISE(retval, PM_RET_EX_TYPE);
89 // Get lastWakeTimeMs argument
90 pobj = NATIVE_GET_LOCAL(0);
91 if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT )
92 lastWakeTimeTicks = (portTickType)(((pPmInt_t) pobj)->val) / portTICK_RATE_MS;
93 else if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_FLT )
94 lastWakeTimeTicks = (portTickType)(((pPmFloat_t) pobj)->val) / portTICK_RATE_MS;
97 PM_RAISE(retval, PM_RET_EX_TYPE);
101 // Get timeToDelayMs argument
102 pobj = NATIVE_GET_LOCAL(1);
103 if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT )
104 timeToDelayTicks = (portTickType)(((pPmInt_t) pobj)->val) / portTICK_RATE_MS;
105 else if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_FLT )
106 timeToDelayTicks = (portTickType)(((pPmFloat_t) pobj)->val) / portTICK_RATE_MS;
109 PM_RAISE(retval, PM_RET_EX_TYPE);
114 vTaskDelayUntil(&lastWakeTimeTicks, timeToDelayTicks);
116 // Return an int object with the time value */
117 retval = int_new((int32_t)(lastWakeTimeTicks*portTICK_RATE_MS), &pobjret);
118 NATIVE_SET_TOS(pobjret);
123 # Update FlightPlanStatus debug fields
124 def debug(val1
, val2
):
127 FlightPlanStatusData status;
130 // Check number of arguments
131 if (NATIVE_GET_NUM_ARGS() != 2)
133 PM_RAISE(retval, PM_RET_EX_TYPE);
138 FlightPlanStatusGet(&status);
141 pobj = NATIVE_GET_LOCAL(0);
142 if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT )
143 status.Debug[0] = (float)(((pPmInt_t) pobj)->val);
144 else if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_FLT )
145 status.Debug[0] = (float)(((pPmFloat_t) pobj)->val);
148 PM_RAISE(retval, PM_RET_EX_TYPE);
153 pobj = NATIVE_GET_LOCAL(1);
154 if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT )
155 status.Debug[1] = (float)(((pPmInt_t) pobj)->val);
156 else if ( OBJ_GET_TYPE(pobj) == OBJ_TYPE_FLT )
157 status.Debug[1] = (float)(((pPmFloat_t) pobj)->val);
160 PM_RAISE(retval, PM_RET_EX_TYPE);
164 // Update status object
165 FlightPlanStatusSet(&status);
171 # Returns 1 if a stop request is pending. The script should periodically check
172 # for stop requests and exit using sys.exit() if one is detected.
173 def hasStopRequest():
177 FlightPlanControlData control;
178 uint32_t stopRequest;
180 // Get control object
181 FlightPlanControlGet(&control);
183 // Check if a stop request is pending
184 if (control.Command == FLIGHTPLANCONTROL_COMMAND_STOP)
190 retval = int_new((int32_t)(stopRequest), &pobjret);
191 NATIVE_SET_TOS(pobjret);
196 # TODO: Wait for object updates in the event queue
197 def waitForObjectUpdates(timeoutMs
):