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"
38 #include "flightstatus.h"
40 #include "fifo_buffer.h"
45 static void osdinputTask(void *parameters
);
50 #define STACK_SIZE_BYTES 1024
51 #define TASK_PRIORITY (tskIDLE_PRIORITY + 4)
52 #define MAX_PACKET_LENGTH 33
56 static uint32_t oposdPort
;
58 static xTaskHandle osdinputTaskHandle
;
60 static char *oposd_rx_buffer
;
64 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,
69 * Initialise the osdinput module
70 * \return -1 if initialisation failed
71 * \return 0 on success
74 int32_t osdinputStart(void)
76 // Start osdinput task
77 xTaskCreate(osdinputTask
, "OSDINPUT", STACK_SIZE_BYTES
/ 4, NULL
, TASK_PRIORITY
, &osdinputTaskHandle
);
82 * Initialise the gps module
83 * \return -1 if initialisation failed
84 * \return 0 on success
86 int32_t osdinputInitialize(void)
88 AttitudeStateInitialize();
89 FlightStatusInitialize();
90 // Initialize quaternion
91 AttitudeStateData attitude
;
92 AttitudeStateGet(&attitude
);
100 AttitudeStateSet(&attitude
);
102 oposdPort
= PIOS_COM_OSD
;
104 oposd_rx_buffer
= pios_malloc(MAX_PACKET_LENGTH
);
105 PIOS_Assert(oposd_rx_buffer
);
109 MODULE_INITCALL(osdinputInitialize
, osdinputStart
);
113 * Main osdinput task. It does not return.
116 static void osdinputTask(__attribute__((unused
)) void *parameters
)
118 portTickType xDelay
= 100 / portTICK_RATE_MS
;
119 portTickType lastSysTime
;
121 lastSysTime
= xTaskGetTickCount();
123 uint8_t rx_count
= 0;
124 bool start_flag
= false;
125 int32_t osdRxOverflow
= 0;
129 // This blocks the task until there is something on the buffer
130 while (PIOS_COM_ReceiveBuffer(oposdPort
, &c
, 1, xDelay
) > 0) {
131 // detect start while acquiring stream
132 if (!start_flag
&& ((c
== 0xCB) || (c
== 0x34))) {
135 } else if (!start_flag
) {
139 if (rx_count
>= 11) {
140 // Flush the buffer and note the overflow event.
145 oposd_rx_buffer
[rx_count
] = c
;
148 if (rx_count
== 11) {
149 if (oposd_rx_buffer
[1] == OSD_PKT_TYPE_ATT
) {
150 AttitudeStateData attitude
;
151 AttitudeStateGet(&attitude
);
156 attitude
.Roll
= (float)((int16_t)(oposd_rx_buffer
[3] | oposd_rx_buffer
[4] << 8)) / 10.0f
;
157 attitude
.Pitch
= (float)((int16_t)(oposd_rx_buffer
[5] | oposd_rx_buffer
[6] << 8)) / 10.0f
;
158 attitude
.Yaw
= (float)((int16_t)(oposd_rx_buffer
[7] | oposd_rx_buffer
[8] << 8)) / 10.0f
;
159 AttitudeStateSet(&attitude
);
160 } else if (oposd_rx_buffer
[1] == OSD_PKT_TYPE_MODE
) {
161 FlightStatusData status
;
162 FlightStatusGet(&status
);
163 status
.Armed
= oposd_rx_buffer
[8];
164 status
.FlightMode
= oposd_rx_buffer
[3];
165 FlightStatusSet(&status
);
172 vTaskDelayUntil(&lastSysTime
, 50 / portTICK_RATE_MS
);