Subversion Repositories FlightCtrl

Rev

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

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