OP-1900 added deceleration check to autotakeoff failsafe
[librepilot.git] / flight / modules / Osd / osdinput / osdinput.c
blobf46c61a577ed6e883d520eb9ed402f4ec4e03eaa
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup osdinputModule osdinput Module
6 * @brief Process osdinput information
7 * @{
9 * @file osdinput.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * @brief osdinput module, handles osdinput stream
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 // ****************
33 #include <openpilot.h>
35 #include "osdinput.h"
37 #include "attitudestate.h"
38 #include "taskinfo.h"
39 #include "flightstatus.h"
41 #include "fifo_buffer.h"
43 // ****************
44 // Private functions
46 static void osdinputTask(void *parameters);
48 // ****************
49 // Private constants
51 #define STACK_SIZE_BYTES 1024
52 #define TASK_PRIORITY (tskIDLE_PRIORITY + 4)
53 #define MAX_PACKET_LENGTH 33
54 // ****************
55 // Private variables
57 static uint32_t oposdPort;
59 static xTaskHandle osdinputTaskHandle;
61 static char *oposd_rx_buffer;
62 t_fifo_buffer rx;
64 enum osd_pkt_type {
65 OSD_PKT_TYPE_MISC = 0, OSD_PKT_TYPE_NAV = 1, OSD_PKT_TYPE_MAINT = 2, OSD_PKT_TYPE_ATT = 3, OSD_PKT_TYPE_MODE = 4,
68 // ****************
69 /**
70 * Initialise the osdinput module
71 * \return -1 if initialisation failed
72 * \return 0 on success
75 int32_t osdinputStart(void)
77 // Start osdinput task
78 xTaskCreate(osdinputTask, "OSDINPUT", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &osdinputTaskHandle);
80 return 0;
82 /**
83 * Initialise the gps module
84 * \return -1 if initialisation failed
85 * \return 0 on success
87 int32_t osdinputInitialize(void)
89 AttitudeStateInitialize();
90 FlightStatusInitialize();
91 // Initialize quaternion
92 AttitudeStateData attitude;
93 AttitudeStateGet(&attitude);
94 attitude.q1 = 1;
95 attitude.q2 = 0;
96 attitude.q3 = 0;
97 attitude.q4 = 0;
98 attitude.Roll = 0;
99 attitude.Pitch = 0;
100 attitude.Yaw = 0;
101 AttitudeStateSet(&attitude);
103 oposdPort = PIOS_COM_OSD;
105 oposd_rx_buffer = pios_malloc(MAX_PACKET_LENGTH);
106 PIOS_Assert(oposd_rx_buffer);
108 return 0;
110 MODULE_INITCALL(osdinputInitialize, osdinputStart);
112 // ****************
114 * Main osdinput task. It does not return.
117 static void osdinputTask(__attribute__((unused)) void *parameters)
119 portTickType xDelay = 100 / portTICK_RATE_MS;
120 portTickType lastSysTime;
122 lastSysTime = xTaskGetTickCount();
124 uint8_t rx_count = 0;
125 bool start_flag = false;
126 int32_t osdRxOverflow = 0;
127 uint8_t c = 0xAA;
128 // Loop forever
129 while (1) {
130 // This blocks the task until there is something on the buffer
131 while (PIOS_COM_ReceiveBuffer(oposdPort, &c, 1, xDelay) > 0) {
132 // detect start while acquiring stream
133 if (!start_flag && ((c == 0xCB) || (c == 0x34))) {
134 start_flag = true;
135 rx_count = 0;
136 } else if (!start_flag) {
137 continue;
140 if (rx_count >= 11) {
141 // Flush the buffer and note the overflow event.
142 osdRxOverflow++;
143 start_flag = false;
144 rx_count = 0;
145 } else {
146 oposd_rx_buffer[rx_count] = c;
147 rx_count++;
149 if (rx_count == 11) {
150 if (oposd_rx_buffer[1] == OSD_PKT_TYPE_ATT) {
151 AttitudeStateData attitude;
152 AttitudeStateGet(&attitude);
153 attitude.q1 = 1;
154 attitude.q2 = 0;
155 attitude.q3 = 0;
156 attitude.q4 = 0;
157 attitude.Roll = (float)((int16_t)(oposd_rx_buffer[3] | oposd_rx_buffer[4] << 8)) / 10.0f;
158 attitude.Pitch = (float)((int16_t)(oposd_rx_buffer[5] | oposd_rx_buffer[6] << 8)) / 10.0f;
159 attitude.Yaw = (float)((int16_t)(oposd_rx_buffer[7] | oposd_rx_buffer[8] << 8)) / 10.0f;
160 AttitudeStateSet(&attitude);
161 } else if (oposd_rx_buffer[1] == OSD_PKT_TYPE_MODE) {
162 FlightStatusData status;
163 FlightStatusGet(&status);
164 status.Armed = oposd_rx_buffer[8];
165 status.FlightMode = oposd_rx_buffer[3];
166 FlightStatusSet(&status);
168 // frame completed
169 start_flag = false;
170 rx_count = 0;
173 vTaskDelayUntil(&lastSysTime, 50 / portTICK_RATE_MS);
177 // ****************
180 * @}
181 * @}