OP-1900 added deceleration check to autotakeoff failsafe
[librepilot.git] / flight / modules / gpsp / gps9gpshandler.c
blob13f64ede20e99cde092f0508c20471a0ac5dca03
1 /**
2 ******************************************************************************
4 * @file gps9gpshandler.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
6 * @brief handler for GPSV9 onboard ubx gps module.
7 * --
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <openpilot.h>
28 #include <pios_struct_helper.h>
29 #include <pios_helpers.h>
30 #include <ubx_utils.h>
31 #include <pios_ubx_ddc.h>
33 #include "gps9gpshandler.h"
34 #include "gps9protocol.h"
36 uint32_t lastUnsentData = 0;
37 uint8_t buffer[BUFFER_SIZE];
39 void handleGPS()
41 bool completeSentenceSent = false;
42 int8_t maxCount = 2;
44 do {
45 int32_t datacounter = PIOS_UBX_DDC_GetAvailableBytes(PIOS_I2C_GPS);
46 if (datacounter > 0) {
47 uint8_t toRead = (uint32_t)datacounter > BUFFER_SIZE - lastUnsentData ? BUFFER_SIZE - lastUnsentData : (uint8_t)datacounter;
48 uint8_t toSend = toRead;
49 PIOS_UBX_DDC_ReadData(PIOS_I2C_GPS, buffer, toRead);
51 uint8_t *lastSentence;
52 uint16_t lastSentenceLength;
53 completeSentenceSent = ubx_getLastSentence(buffer, toRead, &lastSentence, &lastSentenceLength);
54 if (completeSentenceSent) {
55 toSend = (uint8_t)(lastSentence - buffer + lastSentenceLength);
56 } else {
57 lastUnsentData = 0;
60 PIOS_COM_SendBuffer(pios_com_main_id, buffer, toSend);
62 if (toRead > toSend) {
63 // move unsent data at the beginning of buffer to be sent next time
64 lastUnsentData = toRead - toSend;
65 memcpy(buffer, (buffer + toSend), lastUnsentData);
69 datacounter = PIOS_COM_ReceiveBuffer(pios_com_main_id, buffer, BUFFER_SIZE, 0);
70 if (datacounter > 0) {
71 PIOS_UBX_DDC_WriteData(PIOS_I2C_GPS, buffer, datacounter);
73 if (maxCount) {
74 // Note: this delay is needed as querying too quickly the UBX module's I2C(DDC)
75 // port causes a lot of weird issues (it stops sending nav sentences)
76 vTaskDelay(2 * configTICK_RATE_HZ / 1000);
78 } while (maxCount--);
81 typedef struct {
82 uint8_t size;
83 const uint8_t *sentence;
84 } ubx_init_sentence;
87 void setupGPS()
89 CfgPrtPkt cfgprt;
91 cfgprt.fragments.data.portID = CFG_PRT_DATA_PORTID_DDC;
92 cfgprt.fragments.data.reserved0 = 0;
93 cfgprt.fragments.data.txReady = CFG_PRT_DATA_TXREADI_DISABLED;
94 cfgprt.fragments.data.mode = CFG_PRT_DATA_MODE_ADDR;
95 cfgprt.fragments.data.reserved3 = 0;
96 cfgprt.fragments.data.inProtoMask = CFG_PRT_DATA_PROTO_UBX | CFG_PRT_DATA_PROTO_NMEA | CFG_PRT_DATA_PROTO_RTCM;
97 cfgprt.fragments.data.outProtoMask = CFG_PRT_DATA_PROTO_UBX;
98 cfgprt.fragments.data.flags = 0;
99 cfgprt.fragments.data.reserved5 = 0;
101 ubx_buildPacket(&cfgprt.packet, UBX_CFG_CLASS, UBX_CFG_PRT, sizeof(CfgPrtData));
102 PIOS_UBX_DDC_WriteData(PIOS_I2C_GPS, cfgprt.packet.binarystream, sizeof(CfgPrtPkt));