2 ******************************************************************************
4 * @file examplemodperiodic.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 * Threaded periodic 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 object: ExampleSettings
30 * Output object: ExampleObject2
32 * This module will periodically update the value of the ExampleObject object.
33 * The module settings can configure how the ExampleObject is manipulated.
35 * The module executes in its own thread 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 "examplemodperiodic.h"
49 #include "exampleobject2.h" // object that will be updated by the module
50 #include "examplesettings.h" // object holding module settings
53 #define STACK_SIZE configMINIMAL_STACK_SIZE
54 #define TASK_PRIORITY (tskIDLE_PRIORITY + 1)
59 static xTaskHandle taskHandle
;
62 static void exampleTask(void *parameters
);
65 * Initialise the module, called on startup
66 * \returns 0 on success or -1 if initialisation failed
68 int32_t ExampleModPeriodicInitialize()
71 xTaskCreate(exampleTask
, (signed char *)"ExamplePeriodic", STACK_SIZE
, NULL
, TASK_PRIORITY
, &taskHandle
);
77 * Module thread, should not return.
79 static void exampleTask(__attribute__((unused
)) void *parameters
)
81 ExampleSettingsData settings
;
82 ExampleObject2Data data
;
84 portTickType lastSysTime
;
87 lastSysTime
= xTaskGetTickCount();
89 // Update settings with latest value
90 ExampleSettingsGet(&settings
);
92 // Get the object data
93 ExampleObject2Get(&data
);
95 // Determine how to update the data
96 if (settings
.StepDirection
== EXAMPLESETTINGS_STEPDIRECTION_UP
) {
97 step
= settings
.StepSize
;
99 step
= -settings
.StepSize
;
106 data
.Field4
[0] += step
;
107 data
.Field4
[1] += step
;
109 // Update the ExampleObject, after this function is called
110 // notifications to any other modules listening to that object
111 // will be sent and the GCS object will be updated through the
112 // telemetry link. All operations will take place asynchronously
113 // and the following call will return immediately.
114 ExampleObject2Set(&data
);
116 // Since this module executes at fixed time intervals, we need to
117 // block the task until it is time for the next update.
118 // The settings field is in ms, to convert to RTOS ticks we need
119 // to divide by portTICK_RATE_MS.
120 vTaskDelayUntil(&lastSysTime
, settings
.UpdatePeriod
/ portTICK_RATE_MS
);