update credits
[librepilot.git] / flight / modules / Example / examplemodthread.c
blobd0f49dfd64af19b3b90f65499132e06f3c063ddf
1 /**
2 ******************************************************************************
4 * @file examplemodthread.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 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
21 * for more details.
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
28 /**
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 * 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
46 #include "openpilot.h"
47 #include "examplemodthread.h"
48 #include "exampleobject1.h" // object the module will listen for updates (input)
49 #include "exampleobject2.h" // object the module will update (output)
50 #include "examplesettings.h" // object holding module settings (input)
52 // Private constants
53 #define MAX_QUEUE_SIZE 20
54 #define STACK_SIZE configMINIMAL_STACK_SIZE
55 #define TASK_PRIORITY (tskIDLE_PRIORITY + 1)
57 // Private types
59 // Private variables
60 static xQueueHandle queue;
61 static xTaskHandle taskHandle;
63 // Private functions
64 static void exampleTask(void *parameters);
66 /**
67 * Initialise the module, called on startup
68 * \returns 0 on success or -1 if initialisation failed
70 int32_t ExampleModThreadInitialize()
72 // Create object queue
73 queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
75 // Listen for ExampleObject1 updates
76 ExampleObject1ConnectQueue(queue);
78 // Start main task
79 xTaskCreate(exampleTask, (signed char *)"ExampleThread", STACK_SIZE, NULL, TASK_PRIORITY, &taskHandle);
81 return 0;
84 /**
85 * Module thread, should not return.
87 static void exampleTask(__attribute__((unused)) void *parameters)
89 UAVObjEvent ev;
90 ExampleSettingsData settings;
91 ExampleObject1Data data1;
92 ExampleObject1Data data2;
93 int32_t step;
95 // Main task loop
96 while (1) {
97 // Check the event queue for any object updates. In this case
98 // we are only listening for the ExampleSettings object. However
99 // the module could listen to multiple objects.
100 // Since this object only executes on object updates, the task
101 // will block until an object event is pushed in the queue.
102 while (xQueueReceive(queue, &ev, portMAX_DELAY) != pdTRUE) {
106 // Make sure that the object update is for ExampleObject1
107 // This is redundant in this example since we have only
108 // registered a single object in the queue, however
109 // in most practical cases there will be more than one
110 // object connected in the queue.
111 if (ev.obj == ExampleObject1Handle()) {
112 // Update settings with latest value
113 ExampleSettingsGet(&settings);
115 // Get the input object
116 ExampleObject1Get(&data1);
118 // Determine how to update the output object
119 if (settings.StepDirection == EXAMPLESETTINGS_STEPDIRECTION_UP) {
120 step = settings.StepSize;
121 } else {
122 step = -settings.StepSize;
125 // Update data
126 data2.Field1 = data1.Field1 + step;
127 data2.Field2 = data1.Field2 + step;
128 data2.Field3 = data1.Field3 + step;
129 data2.Field4[0] = data1.Field4[0] + step;
130 data2.Field4[1] = data1.Field4[1] + step;
132 // Update the ExampleObject2, after this function is called
133 // notifications to any other modules listening to that object
134 // will be sent and the GCS object will be updated through the
135 // telemetry link. All operations will take place asynchronously
136 // and the following call will return immediately.
137 ExampleObject2Set(&data2);