2 ******************************************************************************
4 * @file examplemodcallback.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief Example module to be used as a template for actual modules.
7 * Event callback version.
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 * Input objects: ExampleObject1, ExampleSettings
30 * Output object: ExampleObject2
32 * This module executes in response to ExampleObject1 updates. When the
33 * module is triggered it will update the data of ExampleObject2.
35 * No threads are used in this example.
37 * UAVObjects are automatically generated by the UAVObjectGenerator from
38 * the object definition XML file.
40 * Modules have no API, all communication to other modules is done through UAVObjects.
41 * However modules may use the API exposed by shared libraries.
42 * See the OpenPilot wiki for more details.
43 * http://www.openpilot.org/OpenPilot_Application_Architecture
47 #include "openpilot.h"
48 #include "callbackinfo.h" // object needed for callback id macro CALLBACKINFO_RUNNING_<MODULENAME>
49 #include "exampleobject1.h" // object the module will listen for updates (input)
50 #include "exampleobject2.h" // object the module will update (output)
51 #include "examplesettings.h" // object holding module settings (input)
54 #define STACK_SIZE configMINIMAL_STACK_SIZE
55 #define CALLBACK_PRIORITY CALLBACK_PRIORITY_LOW
56 #define CBTASK_PRIORITY CALLBACK_TASKPRIORITY_AUXILIARY
60 static DelayedCallbackInfo
*cbinfo
;
63 static void ObjectUpdatedCb(UAVObjEvent
*ev
);
65 static void DelayedCb();
67 * Initialise the module, called on startup.
68 * \returns 0 on success or -1 if initialisation failed
70 int32_t ExampleModCallbackInitialize()
72 // Listen for ExampleObject1 updates, connect a callback function
73 ExampleObject1ConnectCallback(&ObjectUpdatedCb
);
75 cbinfo
= PIOS_CALLBACKSCHEDULER_Create(&DelayedCb
, CALLBACK_PRIORITY
, CBTASK_PRIORITY
, CALLBACKINFO_RUNNING_EXAMPLE
, STACK_SIZE
);
81 * This function is called each time ExampleObject1 is updated, this could be
82 * a local update or a remote update from the GCS. In this example the module
83 * does not have its own thread, the callbacks are executed from within the
84 * event thread. Because of that the callback execution time must be kept to
87 static void ObjectUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
89 PIOS_CALLBACKSCHEDULER_Dispatch(cbinfo
);
93 * This function is called by the PIOS_CALLBACKSCHEDULER_Scheduler when its execution
94 * has been requested. Callbacks scheduled for execution are executed in the
95 * same thread in a round robin fashion. The Dispatch function to reschedule
96 * execution can be called from within the Callback itself, in which case the
97 * re-run will be scheduled after all other callback with equal or higher
98 * priority have been executed. Like event callbacks, delayed callbacks are
99 * executed in the same thread context one at a time, therefore blocking IO
100 * functions or very long lasting calculations are prohibited. Unlike Event
101 * callbacks these callbacks run with a standard (IDLE+1) thread priority and
102 * do not block regular threads. They are therefore saver to use.
104 static void DelayedCb();
105 ExampleSettingsData settings
;
106 ExampleObject1Data data1
;
107 ExampleObject2Data data2
;
110 // Update settings with latest value
111 ExampleSettingsGet(&settings
);
113 // Get the input object
114 ExampleObject1Get(&data1
);
116 // Determine how to update the output object
117 if (settings
.StepDirection
== EXAMPLESETTINGS_STEPDIRECTION_UP
) {
118 step
= settings
.StepSize
;
120 step
= -settings
.StepSize
;
124 data2
.Field1
= data1
.Field1
+ step
;
125 data2
.Field2
= data1
.Field2
+ step
;
126 data2
.Field3
= data1
.Field3
+ step
;
127 data2
.Field4
[0] = data1
.Field4
[0] + step
;
128 data2
.Field4
[1] = data1
.Field4
[1] + step
;
130 // Update the ExampleObject2, after this function is called
131 // notifications to any other modules listening to that object
132 // will be sent and the GCS object will be updated through the
133 // telemetry link. All operations will take place asynchronously
134 // and the following call will return immediately.
135 ExampleObject2Set(&data2
);
137 // call the module again 10 seconds later,
138 // even if the exampleobject has not been updated
139 PIOS_CALLBACKSCHEDULER_Schedule(cbinfo
, 10 * 1000, CALLBACK_UPDATEMODE_NONE
);