Subversion Repositories NaviCtrl

Rev

Rev 700 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
515 killagreg 10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
360 holgerb 11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
41 ingob 13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
515 killagreg 21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
360 holgerb 22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
41 ingob 31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 32
// + Software LICENSING TERMS
41 ingob 33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
515 killagreg 35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
360 holgerb 36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
41 ingob 55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
263 killagreg 56
#include <string.h>
41 ingob 57
#include "91x_lib.h"
700 holgerb 58
#include "uart0.h"
41 ingob 59
#include "uart1.h"
60
#include "ubx.h"
645 holgerb 61
#include "main.h"
41 ingob 62
#include "led.h"
119 killagreg 63
#include "timer1.h"
561 holgerb 64
#include "GPS.h"
41 ingob 65
 
66
// ------------------------------------------------------------------------------------------------
67
// defines
68
 
69
#define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist!
70
#define DAYS_PER_YEAR           365
71
#define DAYS_PER_LEAPYEAR       366
72
#define DAYS_PER_4YEARS         1461    //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years
73
#define DAYS_PER_100YEARS       36524   //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years
74
#define DAYS_PER_400YEARS       146097  //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years
75
#define SECONDS_PER_MINUTE      60
76
#define MINUTES_PER_HOUR        60
77
#define HOURS_PER_DAY           24
78
#define DAYS_PER_WEEK           7
79
#define SECONDS_PER_HOUR        3600    //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
80
#define SECONDS_PER_DAY         86400   //(SECONDS_PER_HOUR * HOURS_PER_DAY)
81
#define SECONDS_PER_WEEK        604800  //(SECONDS_PER_DAY * DAYS_PER_WEEK)
82
 
83
// days per month in normal and leap years
84
const u32       Leap[ 13 ]      = { 0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
85
const u32 Normal[ 13 ]  = { 0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
86
 
427 killagreg 87
#define LEAP_SECONDS_FROM_1980  16
41 ingob 88
 
89
// message sync bytes
90
#define UBX_SYNC1_CHAR  0xB5
91
#define UBX_SYNC2_CHAR  0x62
245 killagreg 92
// protocoll identifiers
93
// navigation class
41 ingob 94
#define UBX_CLASS_NAV   0x01
700 holgerb 95
#define UBX_CLASS_MON   0x0A
96
 
41 ingob 97
#define UBX_ID_POSLLH   0x02
98
#define UBX_ID_SOL              0x06
99
#define UBX_ID_VELNED   0x12
247 killagreg 100
 
700 holgerb 101
#define UBX_ID_MON_VER  0x04
102
 
41 ingob 103
// ------------------------------------------------------------------------------------------------
104
// typedefs
105
 
106
 
107
// ubx parser state
108
typedef enum
109
{
110
        UBXSTATE_IDLE,
111
        UBXSTATE_SYNC1,
112
        UBXSTATE_SYNC2,
113
        UBXSTATE_CLASS,
114
        UBXSTATE_LEN1,
115
        UBXSTATE_LEN2,
116
        UBXSTATE_DATA,
117
        UBXSTATE_CKA,
118
        UBXSTATE_CKB
119
} ubxState_t;
120
 
121
typedef struct
122
{
123
        u32                     itow;           // ms GPS Millisecond Time of Week
124
        s32                     frac;           // ns remainder of rounded ms above
125
        s16                     week;           // GPS week
126
        u8                      GPSfix;         // GPSfix Type, range 0..6
127
        u8                      Flags;          // Navigation Status Flags
128
        s32                     ECEF_X;         // cm ECEF X coordinate
129
        s32                     ECEF_Y;         // cm ECEF Y coordinate
130
        s32                     ECEF_Z;         // cm ECEF Z coordinate
131
        u32                     PAcc;           // cm 3D Position Accuracy Estimate
132
        s32                     ECEFVX;         // cm/s ECEF X velocity
133
        s32                     ECEFVY;         // cm/s ECEF Y velocity
134
        s32                     ECEFVZ;         // cm/s ECEF Z velocity
135
        u32                     SAcc;           // cm/s Speed Accuracy Estimate
136
        u16                     PDOP;           // 0.01 Position DOP
137
        u8                      res1;           // reserved
138
        u8                      numSV;          // Number of SVs used in navigation solution
139
        u32                     res2;           // reserved
52 killagreg 140
        u8                      Status;     // invalid/newdata/processed
41 ingob 141
} __attribute__((packed)) ubx_nav_sol_t;
142
 
143
 
144
typedef struct
145
{
146
        u32                     itow;           // ms  GPS Millisecond Time of Week
147
        s32                     VEL_N;          // cm/s  NED north velocity
148
        s32                     VEL_E;          // cm/s  NED east velocity
149
        s32                     VEL_D;          // cm/s  NED down velocity
150
        u32                     Speed;          // cm/s  Speed (3-D)
151
        u32                     GSpeed;         // cm/s  Ground Speed (2-D)
152
        s32                     Heading;        // 1e-05 deg  Heading 2-D
153
        u32                     SAcc;           // cm/s  Speed Accuracy Estimate
154
        u32                     CAcc;           // deg  Course / Heading Accuracy Estimate
52 killagreg 155
        u8                      Status;         // invalid/newdata/processed
41 ingob 156
} __attribute__((packed)) ubx_nav_velned_t;
157
 
158
typedef struct
159
{
160
        u32                     itow;           // ms GPS Millisecond Time of Week
161
        s32                     LON;            // 1e-07 deg Longitude
162
        s32                     LAT;            // 1e-07 deg Latitude
163
        s32                     HEIGHT;         // mm Height above Ellipsoid
164
        s32                     HMSL;           // mm Height above mean sea level
165
        u32                     Hacc;           // mm Horizontal Accuracy Estimate
166
        u32                     Vacc;           // mm Vertical Accuracy Estimate
52 killagreg 167
        u8                      Status;         // invalid/newdata/processed
41 ingob 168
} __attribute__((packed)) ubx_nav_posllh_t;
169
 
170
//------------------------------------------------------------------------------------
171
// global variables
172
 
173
// local buffers for the incomming ubx messages
263 killagreg 174
ubx_nav_sol_t           UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
175
ubx_nav_posllh_t        UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
176
ubx_nav_velned_t        UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
177
ubxmsg_t                        UbxMsg;
41 ingob 178
 
179
// shared buffer
244 killagreg 180
gps_data_t              GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
41 ingob 181
DateTime_t                      GPSDateTime = {0,0,0,0,0,0,0, INVALID};
182
 
146 killagreg 183
#define UBX_TIMEOUT 500 // 500 ms
184
u32 UBX_Timeout = 0;
41 ingob 185
 
186
//------------------------------------------------------------------------------------
187
// functions
188
 
189
u8 IsLeapYear(u16 year)
190
{
191
        if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
192
        else return 0;
193
}
194
/********************************************************/
195
/*  Calculates the UTC Time from the GPS week and tow   */
196
/********************************************************/
197
void SetGPSTime(DateTime_t * pTimeStruct)
198
{
199
        u32 Days, Seconds, Week;
200
        u16 YearPart;
201
        u32 * MonthDayTab = 0;
202
        u8 i;
203
 
204
 
205
        // if GPS data show valid time data
206
        if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
207
        {
208
                Seconds = UbxSol.itow / 1000L;
209
                Week = (u32)UbxSol.week;
210
                // correct leap seconds since 1980
211
                if(Seconds < LEAP_SECONDS_FROM_1980)
212
                {
213
                        Week--;
214
                        Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
215
                }
216
                else Seconds -= LEAP_SECONDS_FROM_1980;
217
 
515 killagreg 218
                Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
41 ingob 219
                Days += (Week * DAYS_PER_WEEK);
220
                Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
221
 
222
                pTimeStruct->Year = 1;
223
                YearPart = (u16)(Days / DAYS_PER_400YEARS);
224
                pTimeStruct->Year += YearPart * 400;
225
                Days = Days % DAYS_PER_400YEARS;
226
                YearPart = (u16)(Days / DAYS_PER_100YEARS);
227
                pTimeStruct->Year += YearPart * 100;
228
                Days = Days % DAYS_PER_100YEARS;
229
                YearPart = (u16)(Days / DAYS_PER_4YEARS);
230
                pTimeStruct->Year += YearPart * 4;
231
                Days = Days % DAYS_PER_4YEARS;
232
                if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR);
233
                else YearPart = 3;
234
                pTimeStruct->Year += YearPart;
235
                // calculate remaining days of year
236
                Days -= (u32)(YearPart *  DAYS_PER_YEAR);
237
                Days += 1;
238
                // check if current year is a leap year
239
                if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap;
240
                else MonthDayTab = (u32*)Normal;
515 killagreg 241
                // seperate month and day from days of year
41 ingob 242
                for ( i = 0; i < 12; i++ )
243
                {
244
                        if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
245
                        {
246
                                pTimeStruct->Month = i+1;
247
                                pTimeStruct->Day = Days - MonthDayTab[i];
248
                                i = 12;
249
                        }
250
                }
251
                Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
252
                pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR);
253
                Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
254
                pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE);
255
                Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
256
                pTimeStruct->Sec = (u8)(Seconds);
257
                pTimeStruct->mSec  = (u16)(UbxSol.itow % 1000L);
258
                pTimeStruct->Valid = 1;
259
        }
260
        else
261
        {
262
                pTimeStruct->Valid = 0;
263
        }
264
}
265
 
266
 
267
 
268
/********************************************************/
269
/*                  Initialize UBX Parser               */
270
/********************************************************/
271
void UBX_Init(void)
272
{
273
        // mark msg buffers invalid
274
        UbxSol.Status = INVALID;
275
        UbxPosLlh.Status = INVALID;
276
        UbxVelNed.Status = INVALID;
247 killagreg 277
        UbxMsg.Status = INVALID;
41 ingob 278
        GPSData.Status = INVALID;
146 killagreg 279
 
280
        UBX_Timeout = SetDelay(2 * UBX_Timeout);
41 ingob 281
}
282
 
283
/********************************************************/
284
/*            Upate GPS data stcructure                 */
285
/********************************************************/
263 killagreg 286
void Update_GPSData(void)
41 ingob 287
{
263 killagreg 288
        static u32 last_itow = 0;
569 holgerb 289
        static u32 milliseconds = 0;
41 ingob 290
 
291
        // if a new set of ubx messages was collected
263 killagreg 292
        if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
293
        {       // and the itow is equal (same time base)
515 killagreg 294
                UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT;
263 killagreg 295
                if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow))
41 ingob 296
                {
146 killagreg 297
                        UBX_Timeout = SetDelay(UBX_TIMEOUT);
700 holgerb 298
                        DebugOut.Analog[9]++;  
263 killagreg 299
                        // update GPS data only if the status is INVALID or PROCESSED
515 killagreg 300
                        if(GPSData.Status != NEWDATA)
41 ingob 301
                        { // wait for new data at all neccesary ubx messages
659 holgerb 302
                                CountNewGpsDataIn5Sec++;
41 ingob 303
                                GPSData.Status = INVALID;
244 killagreg 304
                                // update message cycle time
264 killagreg 305
                                GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow);
569 holgerb 306
                                milliseconds += GPSData.MsgCycleTime;
264 killagreg 307
                                last_itow = UbxSol.itow; // update last itow
41 ingob 308
                                // NAV SOL
322 holgerb 309
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
41 ingob 310
                                GPSData.NumOfSats =                     UbxSol.numSV;
311
                                GPSData.SatFix =                                UbxSol.GPSfix;
645 holgerb 312
//if(Parameter.User3 > 100) { GPSData.NumOfSats = 0; GPSData.SatFix = 0;}
313
//if(Parameter.User3 > 100) { GPSData.SatFix = 0;}
492 killagreg 314
                                GPSData.Position_Accuracy =             UbxSol.PAcc;    // in steps of 1cm
315
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;    // in steps of 1cm/s
41 ingob 316
                                SetGPSTime(&SystemTime); // update system time
317
                                // NAV VELNED
492 killagreg 318
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;        // in steps of 1cm/s
319
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;    // in steps of 1cm/s
320
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;   // in steps of 1cm/s
321
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;       // in steps of 1cm/s
322
                                GPSData.Heading =                               UbxVelNed.Heading; //in steps of 1E-5 deg
569 holgerb 323
                                // NAV POSLLH
324
                                GPSData.Position.Status =               INVALID;
325
                                if(!(SimulationFlags & SIMULATION_ACTIVE))
326
                                {
327
                                        GPSData.Position.Longitude =    UbxPosLlh.LON;  // in steps of 1E-7 deg
328
                                        GPSData.Position.Latitude =     UbxPosLlh.LAT;  // in steps of 1E-7 deg
329
                                        GPSData.Position.Altitude =     UbxPosLlh.HMSL; // in steps of 1 mm
330
                                }
331
                                else // simulation active
332
                                {
574 holgerb 333
                                 if(GPSData.SatFix != SATFIX_3D || GPSData.NumOfSats < 6)  // simulate satfix
569 holgerb 334
                                 {
645 holgerb 335
                                   GPSData.SatFix = SATFIX_3D;          // Simulation
336
                                   GPSData.Flags |= FLAG_GPSFIXOK;      // Simulation
337
                                   GPSData.NumOfSats = 8;                       // Simulation
569 holgerb 338
                                   if(!SystemTime.Valid)
339
                                   {
340
                                        UbxSol.Status = 1;
341
                                        UbxSol.Flags |= FLAG_WKNSET;
342
                                        UbxSol.Flags |= FLAG_TOWSET;
343
                                        UbxSol.week = 1043;      // starts in year 2000
344
                                        UbxSol.itow = milliseconds;
345
                                        SetGPSTime(&SystemTime); // update system time
346
                                        SystemTime.Valid = 1; // use the time that is given by the GPS-Module
347
                                   }
348
                                 }
349
                                }
350
                                GPSData.Position.Status =               NEWDATA;
515 killagreg 351
 
41 ingob 352
                                GPSData.Status = NEWDATA; // new data available
353
                        } // EOF if(GPSData.Status != NEWDATA)
263 killagreg 354
                        // set state to collect new data
355
                        UbxSol.Status =                                 PROCESSED;      // ready for new data
356
                        UbxPosLlh.Status =                              PROCESSED;      // ready for new data
357
                        UbxVelNed.Status =                              PROCESSED;      // ready for new data
358
                } // EOF all itow are equal
359
        } // EOF all ubx messages received
503 holgerb 360
 
361
 
362
//++++++++++++++++++++++++++++++++
363
// Please do not delete
515 killagreg 364
// This helps me for testing
503 holgerb 365
//++++++++++++++++++++++++++++++++
366
//GPSData.Position.Longitude = 1517409123L;  // Hamilton, Australia
367
//GPSData.Position.Latitude = -329294773L;       // Hamilton, Australia
368
//++++++++
369
//GPSData.Position.Longitude =-1556010020L;  // Alaska
370
//GPSData.Position.Latitude = 629581270L;        // Alaska
371
//++++++++
372
//GPSData.Position.Longitude =-584343419L;   // Buenos aires
373
//GPSData.Position.Latitude = -345464421L;   // Buenos aires
374
//++++++++
375
//GPSData.Position.Longitude =1683362691L;   // Neuseeland
376
//GPSData.Position.Latitude = -465945926L;   // Neuseeland
504 holgerb 377
//++++++++
507 holgerb 378
//GPSData.Position.Longitude = 194140605L;   // Afrika
379
//GPSData.Position.Latitude = -345384656L;   // Afrika
380
//++++++++
504 holgerb 381
//GPSData.Position.Longitude =-740443840L;  // Liberty Staue davor
515 killagreg 382
//GPSData.Position.Latitude = 406888880L;        // Liberty Staue
504 holgerb 383
 
384
//GPSData.Position.Longitude =-740451660L;  // Liberty Staue daneben
515 killagreg 385
//GPSData.Position.Latitude = 406891880L;        // Liberty Staue
504 holgerb 386
 
387
//GPSData.Position.Longitude =-740446540L;  // Liberty Staue direkt
388
//GPSData.Position.Latitude = 406891590L;        // Liberty Staue 1
389
 
514 holgerb 390
//GPSData.Position.Longitude =-1142878694L;  // Flori
515 killagreg 391
//GPSData.Position.Latitude = 483712102L;        //
514 holgerb 392
 
393
//GPSData.Position.Longitude =1251674613L;  // Bubble
515 killagreg 394
//GPSData.Position.Latitude = 466058365L;        //
395
 
41 ingob 396
}
397
 
700 holgerb 398
u8 UbxVersionParser(void)
399
{
400
                        // HW:00000040: Antaris
401
                        // HW:00040001: Antaris-4
402
                        // HW:80040001: Antaris-4
403
                        // HW:00040005: u-blox 5
404
                        // HW:00040006: u-blox 6
405
                        // HW:00040007: u-blox 6
406
                        // HW:00070000: u-blox 7
407
                        // HW:00080000: u-blox M8
408
           // MKGPS V1 -> 1500 -> LEA-4H-0-000 -> ubxsw == 5.00 HW:00040001 
409
           // MKGPS V2 -> 2602 -> LEA-6S-0-000 -> ubxsw == 6.02 HW:00040007 
410
           // MKGPS V2 -> 2703 -> LEA-6S-0-001 -> ubxsw == 7.03 HW:00040007
411
                   // MKGPS V3 -> 3201 -> NEO-M8Q-0-00 -> ubxsw == 2.00 HW:00080000 (Flash-oder ROM Variante)
41 ingob 412
 
700 holgerb 413
u8 retval = 0xFF;
414
          if(UbxMsg.Data[33] == '4' && UbxMsg.Data[37] == '1') // LEA-4 
415
           {
416
        GPS_Version = 1000 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
417
                retval = 10; // MKGPS V1
418
           }
419
           else
420
          if(UbxMsg.Data[33] == '4' && UbxMsg.Data[37] == '7') // LEA-6 
421
           {
422
        GPS_Version = 2000 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
423
                retval = 20; // MKGPS V2
424
           }
425
           else
426
          if(UbxMsg.Data[33] == '8' && UbxMsg.Data[37] == '0') // NEO-8 
427
           {
428
        GPS_Version = 3000 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
429
                retval = 30; // MKGPS V3
430
           }
431
return(retval);
432
}
433
 
41 ingob 434
/********************************************************/
435
/*                   UBX Parser                         */
436
/********************************************************/
245 killagreg 437
void UBX_RxParser(u8 c)
41 ingob 438
{
439
        static ubxState_t ubxState = UBXSTATE_IDLE;
263 killagreg 440
        static ubxmsghdr_t RxHdr;
441
        static u8 RxData[UBX_MSG_DATA_SIZE];
442
        static u16 RxBytes = 0;
41 ingob 443
        static u8 cka, ckb;
444
 
445
 
446
        //state machine
447
        switch (ubxState)       // ubx message parser
448
        {
449
                case UBXSTATE_IDLE: // check 1st sync byte
450
                        if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
451
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
452
                        break;
453
 
454
                case UBXSTATE_SYNC1: // check 2nd sync byte
455
                        if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
456
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
457
                        break;
458
 
459
                case UBXSTATE_SYNC2: // check msg class to be NAV
263 killagreg 460
                        RxHdr.Class = c;
245 killagreg 461
                        ubxState = UBXSTATE_CLASS;
41 ingob 462
                        break;
463
 
464
                case UBXSTATE_CLASS: // check message identifier
263 killagreg 465
                        RxHdr.Id = c;
247 killagreg 466
                        ubxState = UBXSTATE_LEN1;
263 killagreg 467
                        cka = RxHdr.Class + RxHdr.Id;
468
                        ckb = RxHdr.Class + cka;
247 killagreg 469
                        break;
470
 
471
                case UBXSTATE_LEN1: // 1st message length byte
263 killagreg 472
                        RxHdr.Length = (u16)c; // lowbyte first
247 killagreg 473
                        cka += c;
474
                        ckb += cka;
475
                        ubxState = UBXSTATE_LEN2;
476
                        break;
477
 
478
                case UBXSTATE_LEN2: // 2nd message length byte
263 killagreg 479
                        RxHdr.Length += ((u16)c)<<8; // high byte last
480
                        if (RxHdr.Length >= UBX_MSG_DATA_SIZE)
41 ingob 481
                        {
515 killagreg 482
                                ubxState = UBXSTATE_IDLE;
41 ingob 483
                        }
263 killagreg 484
                        else
41 ingob 485
                        {
263 killagreg 486
                                cka += c;
487
                                ckb += cka;
488
                                RxBytes = 0;    // reset data byte counter
489
                                ubxState = UBXSTATE_DATA;
41 ingob 490
                        }
491
                        break;
492
 
493
                case UBXSTATE_DATA: // collecting data
263 killagreg 494
                        if (RxBytes < UBX_MSG_DATA_SIZE)
41 ingob 495
                        {
263 killagreg 496
                                RxData[RxBytes++] = c; // copy curent data byte if any space is left
41 ingob 497
                                cka += c;
498
                                ckb += cka;
263 killagreg 499
                                if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received
41 ingob 500
                        }
501
                        else // rx buffer overrun
502
                        {
503
                                ubxState = UBXSTATE_IDLE;
504
                        }
505
                        break;
506
 
507
                case UBXSTATE_CKA:
508
                        if (c == cka) ubxState = UBXSTATE_CKB;
509
                        else
510
                        {
511
                                ubxState = UBXSTATE_IDLE;
512
                        }
513
                        break;
514
 
515
                case UBXSTATE_CKB:
516
                        if (c == ckb)
263 killagreg 517
                        {       // checksum is ok
518
 
519
                                switch(RxHdr.Class)
520
                                {
521
                                        case UBX_CLASS_NAV:
522
                                                switch(RxHdr.Id)
523
                                                {
524
                                                        case UBX_ID_POSLLH: // geodetic position
525
                                                                memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length);
526
                                                                UbxPosLlh.Status = NEWDATA;
527
                                                                break;
528
 
515 killagreg 529
                                                        case UBX_ID_VELNED: // velocity vector in tangent plane
263 killagreg 530
                                                                memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length);
531
                                                                UbxVelNed.Status = NEWDATA;
532
                                                                break;
533
 
534
                                                        case UBX_ID_SOL: // navigation solution
535
                                                                memcpy((u8*)&UbxSol, RxData, RxHdr.Length);
536
                                                                UbxSol.Status = NEWDATA;
537
                                                                break;
538
 
539
                                                        default:
540
                                                                break;
541
                                                } // EOF switch(Id)
542
                                                Update_GPSData();
543
                                                break;
700 holgerb 544
                                        case UBX_CLASS_MON: // version
545
                                                switch(RxHdr.Id)
546
                                                {
547
                                                 case UBX_ID_MON_VER:
548
                                                        if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE) memcpy(UbxMsg.Data, RxData, RxHdr.Length);
549
                                                        UbxVersionParser();
550
                                                        break;
515 killagreg 551
 
700 holgerb 552
                                                        default:
553
                                                                break;
554
                                                }
263 killagreg 555
                                        default:
556
                                                break;
557
                                } // EOF switch(class)
558
 
515 killagreg 559
                                // check generic msg filter
263 killagreg 560
                                if(UbxMsg.Status != NEWDATA)
561
                                {       // msg buffer is free
562
                                        if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask)))
563
                                        {       // msg matches to the filter criteria
564
                                                UbxMsg.Status = INVALID;
565
                                                UbxMsg.Hdr.Class = RxHdr.Class;
566
                                                UbxMsg.Hdr.Id = RxHdr.Id;
567
                                                UbxMsg.Hdr.Length = RxHdr.Length;
568
                                                if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE)
569
                                                {       // copy data block
570
                                                        memcpy(UbxMsg.Data, RxData, RxHdr.Length);
571
                                                        UbxMsg.Status = NEWDATA;
515 killagreg 572
                                                }
263 killagreg 573
                                        } // EOF filter matches
574
                                } // EOF != INVALID
575
                        }// EOF crc ok
645 holgerb 576
                        else DebugOut.Analog[13]++; // CRC Error -> since 2.10e (6.2015)
41 ingob 577
                        ubxState = UBXSTATE_IDLE; // ready to parse new data
578
                        break;
579
 
580
                default: // unknown ubx state
581
                        ubxState = UBXSTATE_IDLE;
582
                        break;
583
 
584
        }
585
}
245 killagreg 586
 
247 killagreg 587
u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len)
245 killagreg 588
{
589
        u16 i;
590
        u8 cka = 0, ckb = 0;
591
        // check if buffer is available
592
        if(pBuff->Locked == TRUE) return(0);
593
        // check if buffer size is sufficient
594
        if(pBuff->Size < 8 + Len) return(0);
595
        // lock the buffer
596
        pBuff->Locked = TRUE;
515 killagreg 597
        // start at begin
245 killagreg 598
        pBuff->Position = 0;
599
        pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR;
600
        pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR;
247 killagreg 601
        for(i=0;i<Len;i++)
602
        {
603
                pBuff->pData[pBuff->Position++] = pData[i];
604
        }
245 killagreg 605
        // calculate checksum
606
        for(i=2;i<pBuff->Position;i++)
607
        {
515 killagreg 608
                cka += pBuff->pData[i];
245 killagreg 609
                ckb += cka;
610
        }
611
        pBuff->pData[pBuff->Position++] = cka;
612
        pBuff->pData[pBuff->Position++] = ckb;
613
        pBuff->DataBytes = pBuff->Position;
614
        pBuff->Position = 0;  // reset buffer position for transmision
615
        return(1);
616
}
263 killagreg 617
/*
618
switch(ubxclass)
619
                        {
620
                                case UBX_CLASS_NAV:
621
                                        switch(ubxid)
622
                                        {
623
                                                case UBX_ID_POSLLH: // geodetic position
624
                                                        ubxSp =  (u8 *)&UbxPosLlh; // data start pointer
625
                                                        ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer
626
                                                        ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer
627
                                                        break;
515 killagreg 628
 
263 killagreg 629
                                                case UBX_ID_SOL: // navigation solution
630
                                                        ubxSp =  (u8 *)&UbxSol; // data start pointer
631
                                                        ubxEp = (u8 *)(&UbxSol + 1); // data end pointer
632
                                                        ubxStP = (u8 *)&UbxSol.Status; // status pointer
633
                                                        break;
515 killagreg 634
 
263 killagreg 635
                                                case UBX_ID_VELNED: // velocity vector in tangent plane
636
                                                        ubxSp =  (u8 *)&UbxVelNed; // data start pointer
637
                                                        ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer
638
                                                        ubxStP = (u8 *)&UbxVelNed.Status; // status pointer
639
                                                        break;
515 killagreg 640
 
263 killagreg 641
                                                default:                        // unsupported identifier
642
                                                        ubxState = UBXSTATE_IDLE;
643
                                                        return;
644
                                        }
645
                                        break;
646
 
647
                                default: // other  classes
648
                                        if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE;
649
                                        else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask)))
650
                                        {       // buffer is free and message matches to filter criteria
651
                                                UbxMsg.Status = INVALID;
652
                                                UbxMsg.Hdr.Class = ubxclass;
653
                                                UbxMsg.Hdr.Id = ubxid;
654
                                                UbxMsg.Hdr.Length = msglen;
655
                                                ubxSp =  (u8 *)&(UbxMsg.Data);   // data start pointer
656
                                                ubxEp = (u8 *)(&UbxMsg + 1);    // data end pointer
515 killagreg 657
                                                ubxStP = (u8 *)&UbxMsg.Status;  // status pointer
263 killagreg 658
                                        }
515 killagreg 659
                                        else ubxState = UBXSTATE_IDLE;
263 killagreg 660
                                        break;
661
                        }
662
*/
663