Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <inttypes.h>
52
#include "ubx.h"
53
#include <avr/io.h>
54
 
55
//#include "uart0.h"
56
 
57
// ubx protocol parser state machine
58
#define UBXSTATE_IDLE   0
59
#define UBXSTATE_SYNC1  1
60
#define UBXSTATE_SYNC2  2
61
#define UBXSTATE_CLASS  3
62
#define UBXSTATE_LEN1   4
63
#define UBXSTATE_LEN2   5
64
#define UBXSTATE_DATA   6
65
#define UBXSTATE_CKA    7
66
#define UBXSTATE_CKB    8
67
 
68
// ublox protocoll identifier
69
#define UBX_CLASS_NAV   0x01
70
 
71
#define UBX_ID_POSLLH   0x02
72
#define UBX_ID_SOL              0x06
73
#define UBX_ID_VELNED   0x12
74
 
75
#define UBX_SYNC1_CHAR  0xB5
76
#define UBX_SYNC2_CHAR  0x62
77
 
78
typedef struct {
79
        uint32_t                ITOW;           // ms GPS Millisecond Time of Week
80
        int32_t                 Frac;           // ns remainder of rounded ms above
81
        int16_t                 week;           // GPS week
82
        uint8_t                 GPSfix;         // GPSfix Type, range 0..6
83
        uint8_t                 Flags;          // Navigation Status Flags
84
        int32_t                 ECEF_X;         // cm ECEF X coordinate
85
        int32_t                 ECEF_Y;         // cm ECEF Y coordinate
86
        int32_t                 ECEF_Z;         // cm ECEF Z coordinate
87
        uint32_t                PAcc;           // cm 3D Position Accuracy Estimate
88
        int32_t                 ECEFVX;         // cm/s ECEF X velocity
89
        int32_t                 ECEFVY;         // cm/s ECEF Y velocity
90
        int32_t                 ECEFVZ;         // cm/s ECEF Z velocity
91
        uint32_t                SAcc;           // cm/s Speed Accuracy Estimate
92
        uint16_t                PDOP;           // 0.01 Position DOP
93
        uint8_t                 res1;           // reserved
94
        uint8_t                 numSV;          // Number of SVs used in navigation solution
95
        uint32_t                res2;           // reserved
96
        Status_t                Status;
97
} UBX_SOL_t;
98
 
99
typedef struct {
100
        uint32_t                ITOW;           // ms GPS Millisecond Time of Week
101
        int32_t                 LON;            // 1e-07 deg Longitude
102
        int32_t                 LAT;            // 1e-07 deg Latitude
103
        int32_t                 HEIGHT;         // mm Height above Ellipsoid
104
        int32_t                 HMSL;           // mm Height above mean sea level
105
        uint32_t                Hacc;           // mm Horizontal Accuracy Estimate
106
        uint32_t                Vacc;           // mm Vertical Accuracy Estimate
107
        Status_t                Status;
108
} UBX_POSLLH_t;
109
 
110
typedef struct {
111
        uint32_t                ITOW;           // ms  GPS Millisecond Time of Week
112
        int32_t                 VEL_N;          // cm/s  NED north velocity
113
        int32_t                 VEL_E;          // cm/s  NED east velocity
114
        int32_t                 VEL_D;          // cm/s  NED down velocity
115
        uint32_t                Speed;          // cm/s  Speed (3-D)
116
        uint32_t                GSpeed;         // cm/s  Ground Speed (2-D)
117
        int32_t                 Heading;        // 1e-05 deg  Heading 2-D
118
        uint32_t                SAcc;           // cm/s  Speed Accuracy Estimate
119
        uint32_t                CAcc;           // deg  Course / Heading Accuracy Estimate
120
        Status_t                Status;
121
} UBX_VELNED_t;
122
 
123
UBX_SOL_t               UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
124
UBX_POSLLH_t    UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
125
UBX_VELNED_t    UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
126
GPS_INFO_t      GPSInfo   = {0,0,0,0,0,0,0,0,0,0, INVALID};
127
 
128
volatile uint8_t GPSTimeout = 0;
129
 
130
void UpdateGPSInfo (void)
131
{
132
 
133
        if ((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
134
        {
135
                RED_FLASH;
136
                if(GPSInfo.status != NEWDATA)
137
                {
138
                        GPSInfo.status = INVALID;
139
                        // NAV SOL
140
                        GPSInfo.flags = UbxSol.Flags;
141
                        GPSInfo.satfix = UbxSol.GPSfix;
142
                        GPSInfo.satnum = UbxSol.numSV;
143
                        GPSInfo.PAcc = UbxSol.PAcc;
144
                        GPSInfo.VAcc = UbxSol.SAcc;
145
                        // NAV POSLLH
146
                        GPSInfo.longitude = UbxPosLlh.LON;
147
                        GPSInfo.latitude = UbxPosLlh.LAT;
148
                        GPSInfo.altitude = UbxPosLlh.HEIGHT;
149
 
150
                        GPSInfo.veleast = UbxVelNed.VEL_E;
151
                        GPSInfo.velnorth = UbxVelNed.VEL_N;
152
                        GPSInfo.veltop = -UbxVelNed.VEL_D;
153
                        GPSInfo.velground = UbxVelNed.GSpeed;
154
 
155
                        GPSInfo.status = NEWDATA;
156
 
157
                }
158
                // set state to collect new data
159
                UbxSol.Status = PROCESSED;                      // never update old data
160
                UbxPosLlh.Status = PROCESSED;           // never update old data
161
                UbxVelNed.Status = PROCESSED;           // never update old data
162
        }
163
 
164
 
165
}
166
 
167
 
168
// this function should be called within the UART RX ISR
169
void ubx_parser(uint8_t c)
170
{
171
        static uint8_t ubxstate = UBXSTATE_IDLE;
172
        static uint8_t cka, ckb;
173
        static uint16_t msglen;
174
        static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
175
 
176
        switch(ubxstate)
177
        {
178
                case UBXSTATE_IDLE: // check 1st sync byte
179
                        if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1;
180
                        else ubxstate = UBXSTATE_IDLE; // out of synchronization
181
                        break;
182
 
183
                case UBXSTATE_SYNC1: // check 2nd sync byte
184
                        if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2;
185
                        else ubxstate = UBXSTATE_IDLE; // out of synchronization
186
                        break;
187
 
188
                case UBXSTATE_SYNC2: // check msg class to be NAV
189
                        if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS;
190
                        else ubxstate = UBXSTATE_IDLE; // unsupported message class
191
                        break;
192
 
193
                case UBXSTATE_CLASS: // check message identifier
194
                        switch(c)
195
                        {
196
                                case UBX_ID_POSLLH: // geodetic position
197
                                        ubxP =  (int8_t *)&UbxPosLlh; // data start pointer
198
                                        ubxEp = (int8_t *)(&UbxPosLlh + 1); // data end pointer
199
                                        ubxSp = (int8_t *)&UbxPosLlh.Status; // status pointer
200
                                        break;
201
 
202
                                case UBX_ID_SOL: // navigation solution
203
                                        ubxP =  (int8_t *)&UbxSol; // data start pointer
204
                                        ubxEp = (int8_t *)(&UbxSol + 1); // data end pointer
205
                                        ubxSp = (int8_t *)&UbxSol.Status; // status pointer
206
                                        break;
207
 
208
                                case UBX_ID_VELNED: // velocity vector in tangent plane
209
                                        ubxP =  (int8_t *)&UbxVelNed; // data start pointer
210
                                        ubxEp = (int8_t *)(&UbxVelNed + 1); // data end pointer
211
                                        ubxSp = (int8_t *)&UbxVelNed.Status; // status pointer
212
                                        break;
213
 
214
                                default:                        // unsupported identifier
215
                                        ubxstate = UBXSTATE_IDLE;
216
                                        break;
217
                        }
218
                        if (ubxstate != UBXSTATE_IDLE)
219
                        {
220
                                ubxstate = UBXSTATE_LEN1;
221
                                cka = UBX_CLASS_NAV + c;
222
                                ckb = UBX_CLASS_NAV + cka;
223
                        }
224
                        break;
225
 
226
                case UBXSTATE_LEN1: // 1st message length byte
227
                        msglen = c;
228
                        cka += c;
229
                        ckb += cka;
230
                        ubxstate = UBXSTATE_LEN2;
231
                        break;
232
 
233
                case UBXSTATE_LEN2: // 2nd message length byte
234
                        msglen += ((uint16_t)c)<<8;
235
                        cka += c;
236
                        ckb += cka;
237
                        // if the old data are not processed so far then break parsing now
238
                        // to avoid writing new data in ISR during reading by another function
239
                        if ( *ubxSp == NEWDATA )
240
                        {
241
                                UpdateGPSInfo(); //update GPS info respectively
242
                                ubxstate = UBXSTATE_IDLE;
243
                        }
244
                        else // data invalid or allready processd
245
                        {
246
                                *ubxSp = INVALID;
247
                                ubxstate = UBXSTATE_DATA;
248
                        }
249
                        break;
250
 
251
                case UBXSTATE_DATA:
252
                        if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left
253
                        cka += c;
254
                        ckb += cka;
255
                        if (--msglen == 0)      ubxstate = UBXSTATE_CKA; // switch to next state if all data was read
256
                        break;
257
 
258
                case UBXSTATE_CKA:
259
                        if (c == cka) ubxstate = UBXSTATE_CKB;
260
                        else
261
                        {
262
                                *ubxSp = INVALID;
263
                                ubxstate = UBXSTATE_IDLE;
264
                        }
265
                        break;
266
 
267
                case UBXSTATE_CKB:
268
                        if (c == ckb)
269
                        {
270
                                *ubxSp = NEWDATA; // new data are valid
271
                                UpdateGPSInfo(); //update GPS info respectively
272
                                GPSTimeout = 255;
273
                        }
274
                        else
275
                        {       // if checksum not fit then set data invalid
276
                                *ubxSp = INVALID;
277
                        }
278
                        ubxstate = UBXSTATE_IDLE; // ready to parse new data
279
                        break;
280
 
281
                default: // unknown ubx state
282
                        ubxstate = UBXSTATE_IDLE;
283
                        break;
284
        }
285
 
286
}
287
 
288