Subversion Repositories FlightCtrl

Rev

Rev 724 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
684 killagreg 1
#include <inttypes.h>
2
#include "ubx.h"
702 killagreg 3
#include "main.h"
4
#include <avr/io.h>
684 killagreg 5
 
722 killagreg 6
#include "uart.h"
7
 
684 killagreg 8
// ubx protocol parser state machine
9
#define UBXSTATE_IDLE   0
10
#define UBXSTATE_SYNC1  1
11
#define UBXSTATE_SYNC2  2
12
#define UBXSTATE_CLASS  3
13
#define UBXSTATE_LEN1   4
14
#define UBXSTATE_LEN2   5
15
#define UBXSTATE_DATA   6
16
#define UBXSTATE_CKA    7
17
#define UBXSTATE_CKB    8
18
 
19
// ublox protocoll identifier
20
#define UBX_CLASS_NAV   0x01
21
#define UBX_ID_POSLLH   0x02
22
#define UBX_ID_POSUTM   0x08
23
#define UBX_ID_STATUS   0x03
24
#define UBX_ID_VELNED   0x12
25
#define UBX_SYNC1_CHAR  0xB5
26
#define UBX_SYNC2_CHAR  0x62
27
 
28
typedef struct {
29
        uint32_t                ITOW;           // ms GPS Millisecond Time of Week
30
        uint8_t                 GPSfix;         // GPSfix Type, range 0..6
31
        uint8_t                 Flags;          // Navigation Status Flags
32
        uint8_t                 DiffS;          // Differential Status
33
        uint8_t                 res;            // reserved
34
        uint32_t                TTFF;           // Time to first fix (millisecond time tag)
35
        uint32_t                MSSS;           // Milliseconds since Startup / Reset
702 killagreg 36
        uint8_t                 Status;
684 killagreg 37
} GPS_STATUS_t;
38
 
39
typedef struct {
40
        uint32_t                ITOW;           // ms GPS Millisecond Time of Week
41
        int32_t                 LON;            // 1e-07 deg Longitude
42
        int32_t                 LAT;            // 1e-07 deg Latitude
43
        int32_t                 HEIGHT;         // mm Height above Ellipsoid
44
        int32_t                 HMSL;           // mm Height above mean sea level
45
        uint32_t                Hacc;           // mm Horizontal Accuracy Estimate
46
        uint32_t                Vacc;           // mm Vertical Accuracy Estimate
702 killagreg 47
        uint8_t                 Status;
684 killagreg 48
} GPS_POSLLH_t;
49
 
50
typedef struct {
51
        uint32_t                ITOW;           // ms GPS Millisecond Time of Week
52
        int32_t                 EAST;           // cm  UTM Easting
53
        int32_t                 NORTH;          // cm  UTM Nording
54
        int32_t                 ALT;            // cm  altitude
55
        uint8_t                 ZONE;           // UTM zone number
56
        uint8_t                 HEM;            // Hemisphere Indicator (0=North, 1=South)
702 killagreg 57
        uint8_t                 Status;
684 killagreg 58
} GPS_POSUTM_t;
59
 
60
typedef struct {
61
        uint32_t                ITOW;           // ms  GPS Millisecond Time of Week
62
        int32_t                 VEL_N;          // cm/s  NED north velocity
63
        int32_t                 VEL_E;          // cm/s  NED east velocity
64
        int32_t                 VEL_D;          // cm/s  NED down velocity
65
        uint32_t                Speed;          // cm/s  Speed (3-D)
66
        uint32_t                GSpeed;         // cm/s  Ground Speed (2-D)
67
        int32_t                 Heading;        // 1e-05 deg  Heading 2-D
68
        uint32_t                SAcc;           // cm/s  Speed Accuracy Estimate
69
        uint32_t                CAcc;           // deg  Course / Heading Accuracy Estimate
702 killagreg 70
        uint8_t                 Status;
684 killagreg 71
} GPS_VELNED_t;
72
 
73
 
726 killagreg 74
GPS_STATUS_t    GpsStatus = {0,0,0,0,0,0,0, INVALID};
75
GPS_POSLLH_t    GpsPosLlh = {0,0,0,0,0,0,0, INVALID};
76
GPS_POSUTM_t    GpsPosUtm = {0,0,0,0,0,0, INVALID};
77
GPS_VELNED_t    GpsVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
78
GPS_INFO_t      GPSInfo   = {0,0,0,0,0,0,0,0,0,0, INVALID};
684 killagreg 79
 
726 killagreg 80
volatile uint8_t GPSTimeout = 0;
684 killagreg 81
 
82
void UpdateGPSInfo (void)
83
{
702 killagreg 84
        if (GpsStatus.Status == VALID)                  // valid packet
684 killagreg 85
        {
702 killagreg 86
                GPSInfo.satfix = GpsStatus.GPSfix;
726 killagreg 87
                GpsStatus.Status = PROCESSED;           // never update old data
684 killagreg 88
        }
702 killagreg 89
        if (GpsPosLlh.Status == VALID)                  // valid packet
684 killagreg 90
        {
702 killagreg 91
                GPSInfo.longitude = GpsPosLlh.LON;
92
                GPSInfo.latitude = GpsPosLlh.LAT;
93
                GPSInfo.altitude = GpsPosLlh.HEIGHT;
726 killagreg 94
                GpsPosLlh.Status = PROCESSED;           // never update old data
684 killagreg 95
        }
702 killagreg 96
        if (GpsPosUtm.Status == VALID)                  // valid packet
684 killagreg 97
        {
702 killagreg 98
                GPSInfo.utmeast = GpsPosUtm.EAST;
99
                GPSInfo.utmnorth = GpsPosUtm.NORTH;
100
                GPSInfo.utmalt = GpsPosUtm.ALT;
726 killagreg 101
                GpsPosUtm.Status = PROCESSED;           // never update old data
684 killagreg 102
        }
702 killagreg 103
        if (GpsVelNed.Status == VALID)                  // valid packet
684 killagreg 104
        {
702 killagreg 105
                GPSInfo.veleast = GpsVelNed.VEL_E;
106
                GPSInfo.velnorth = GpsVelNed.VEL_N;
107
                GPSInfo.veltop = -GpsVelNed.VEL_D;
726 killagreg 108
                GpsVelNed.Status = PROCESSED;           // never update old data
684 killagreg 109
        }
722 killagreg 110
        if (GpsStatus.Status != INVALID)
702 killagreg 111
        {
726 killagreg 112
                GPSInfo.status = VALID;             // set valid if data are updated
702 killagreg 113
        }
684 killagreg 114
}
115
 
724 killagreg 116
 
684 killagreg 117
// this function should be called within the UART RX ISR
118
void ubx_parser(uint8_t c)
119
{
120
        static uint8_t ubxstate = UBXSTATE_IDLE;
121
        static uint8_t cka, ckb;
122
        static uint16_t msglen;
123
        static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
124
 
125
        switch(ubxstate)
126
        {
127
                case UBXSTATE_IDLE: // check 1st sync byte
128
                        if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1;
129
                        else ubxstate = UBXSTATE_IDLE; // out of synchronization
130
                        break;
131
 
132
                case UBXSTATE_SYNC1: // check 2nd sync byte
133
                        if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2;
134
                        else ubxstate = UBXSTATE_IDLE; // out of synchronization
135
                        break;
136
 
137
                case UBXSTATE_SYNC2: // check msg class to be NAV
138
                        if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS;
139
                        else ubxstate = UBXSTATE_IDLE; // unsupported message class
140
                        break;
141
 
142
                case UBXSTATE_CLASS: // check message identifier
143
                        switch(c)
144
                        {
145
                                case UBX_ID_POSUTM: // utm position
146
                                        ubxP =  (int8_t *)&GpsPosUtm; // data start pointer
147
                                        ubxEp = (int8_t *)(&GpsPosUtm + sizeof(GPS_POSUTM_t)); // data end pointer
702 killagreg 148
                                        ubxSp = (int8_t *)&GpsPosUtm.Status; // status pointer
684 killagreg 149
                                        break;
150
 
151
                                case UBX_ID_POSLLH: // geodetic position
722 killagreg 152
                                        ubxP =  (int8_t *)&GpsPosLlh; // data start pointer
153
                                        ubxEp = (int8_t *)(&GpsPosLlh + sizeof(GPS_POSLLH_t)); // data end pointer
154
                                        ubxSp = (int8_t *)&GpsPosLlh.Status; // status pointer
684 killagreg 155
                                        break;
156
 
157
                                case UBX_ID_STATUS: // receiver status
158
                                        ubxP =  (int8_t *)&GpsStatus; // data start pointer
159
                                        ubxEp = (int8_t *)(&GpsStatus + sizeof(GPS_STATUS_t)); // data end pointer
702 killagreg 160
                                        ubxSp = (int8_t *)&GpsStatus.Status; // status pointer
684 killagreg 161
                                        break;
162
 
163
                                case UBX_ID_VELNED: // velocity vector in tangent plane
164
                                        ubxP =  (int8_t *)&GpsVelNed; // data start pointer
165
                                        ubxEp = (int8_t *)(&GpsVelNed + sizeof(GPS_VELNED_t)); // data end pointer
702 killagreg 166
                                        ubxSp = (int8_t *)&GpsVelNed.Status; // status pointer
684 killagreg 167
                                        break;
168
 
169
                                default:                        // unsupported identifier
170
                                        ubxstate = UBXSTATE_IDLE;
171
                                        break;
172
                        }
173
                        if (ubxstate != UBXSTATE_IDLE)
174
                        {
175
                                ubxstate = UBXSTATE_LEN1;
176
                                cka = UBX_CLASS_NAV + c;
177
                                ckb = UBX_CLASS_NAV + cka;
178
                        }
179
                        break;
180
 
181
                case UBXSTATE_LEN1: // 1st message length byte
182
                        msglen = c;
183
                        cka += c;
184
                        ckb += cka;
185
                        ubxstate = UBXSTATE_LEN2;
186
                        break;
187
 
188
                case UBXSTATE_LEN2: // 2nd message length byte
189
                        msglen += ((uint16_t)c)<<8;
190
                        cka += c;
191
                        ckb += cka;
192
                        // if the old data are not processed so far then break parsing now
193
                        // to avoid writing new data in ISR during reading by another function
194
                        if ( *ubxSp == VALID ) ubxstate = UBXSTATE_IDLE;
195
                        else // data invalid or allready processd
196
                        {
197
                                *ubxSp = INVALID;
198
                                ubxstate = UBXSTATE_DATA;
199
                        }
200
                        break;
201
 
202
                case UBXSTATE_DATA:
203
                        if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left
204
                        cka += c;
205
                        ckb += cka;
206
                        if (--msglen == 0)      ubxstate = UBXSTATE_CKA; // switch to next state if all data was read
207
                        break;
208
 
209
                case UBXSTATE_CKA:
210
                        if (c == cka) ubxstate = UBXSTATE_CKB;
722 killagreg 211
                        else
212
                        {
213
                                *ubxSp = INVALID;
214
                                ubxstate = UBXSTATE_IDLE;
215
                        }
684 killagreg 216
                        break;
217
 
218
                case UBXSTATE_CKB:
219
                        if (c == ckb)
220
                        {
722 killagreg 221
                                *ubxSp = VALID; // new data are valid
222
                                ROT_FLASH;
684 killagreg 223
                                UpdateGPSInfo(); //update GPS info respectively
726 killagreg 224
                                GPSTimeout = 255;
684 killagreg 225
                        }
722 killagreg 226
                        else
227
                        {       // if checksum not fit then set data invalid
228
                                *ubxSp = INVALID;
229
                        }
684 killagreg 230
                        ubxstate = UBXSTATE_IDLE; // ready to parse new data
231
                        break;
232
 
233
                default: // unknown ubx state
234
                        ubxstate = UBXSTATE_IDLE;
235
                        break;
236
        }
237
 
238
}
239
 
724 killagreg 240