Subversion Repositories FlightCtrl

Rev

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

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