Subversion Repositories FlightCtrl

Rev

Rev 935 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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