2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
5 * @addtogroup osdinputModule osdinput Module
6 * @brief Process osdinput information
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
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
33 #include <openpilot.h>
37 #include "attitudestate.h"
39 #include "flightstatus.h"
41 #include "fifo_buffer.h"
46 static void osdinputTask(void *parameters
);
51 #define STACK_SIZE_BYTES 1024
52 #define TASK_PRIORITY (tskIDLE_PRIORITY + 4)
53 #define MAX_PACKET_LENGTH 33
57 static uint32_t oposdPort
;
59 static xTaskHandle osdinputTaskHandle
;
61 static char *oposd_rx_buffer
;
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,
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
);
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
);
101 AttitudeStateSet(&attitude
);
103 oposdPort
= PIOS_COM_OSD
;
105 oposd_rx_buffer
= pios_malloc(MAX_PACKET_LENGTH
);
106 PIOS_Assert(oposd_rx_buffer
);
110 MODULE_INITCALL(osdinputInitialize
, osdinputStart
);
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;
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))) {
136 } else if (!start_flag
) {
140 if (rx_count
>= 11) {
141 // Flush the buffer and note the overflow event.
146 oposd_rx_buffer
[rx_count
] = c
;
149 if (rx_count
== 11) {
150 if (oposd_rx_buffer
[1] == OSD_PKT_TYPE_ATT
) {
151 AttitudeStateData attitude
;
152 AttitudeStateGet(&attitude
);
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
);
173 vTaskDelayUntil(&lastSysTime
, 50 / portTICK_RATE_MS
);