Subversion Repositories NaviCtrl

Rev

Rev 883 | 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"
881 holgerb 60
#include "uart2.h"
41 ingob 61
#include "ubx.h"
645 holgerb 62
#include "main.h"
41 ingob 63
#include "led.h"
119 killagreg 64
#include "timer1.h"
561 holgerb 65
#include "GPS.h"
883 holgerb 66
#include "spi_slave.h"
41 ingob 67
 
68
// ------------------------------------------------------------------------------------------------
69
// defines
70
 
71
#define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist!
72
#define DAYS_PER_YEAR           365
73
#define DAYS_PER_LEAPYEAR       366
74
#define DAYS_PER_4YEARS         1461    //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years
75
#define DAYS_PER_100YEARS       36524   //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years
76
#define DAYS_PER_400YEARS       146097  //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years
77
#define SECONDS_PER_MINUTE      60
78
#define MINUTES_PER_HOUR        60
79
#define HOURS_PER_DAY           24
80
#define DAYS_PER_WEEK           7
81
#define SECONDS_PER_HOUR        3600    //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
82
#define SECONDS_PER_DAY         86400   //(SECONDS_PER_HOUR * HOURS_PER_DAY)
83
#define SECONDS_PER_WEEK        604800  //(SECONDS_PER_DAY * DAYS_PER_WEEK)
84
 
85
// days per month in normal and leap years
86
const u32       Leap[ 13 ]      = { 0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
87
const u32 Normal[ 13 ]  = { 0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
88
 
835 holgerb 89
#define LEAP_SECONDS_FROM_1980  18
41 ingob 90
 
91
// message sync bytes
92
#define UBX_SYNC1_CHAR  0xB5
93
#define UBX_SYNC2_CHAR  0x62
245 killagreg 94
// protocoll identifiers
95
// navigation class
41 ingob 96
#define UBX_CLASS_NAV   0x01
700 holgerb 97
#define UBX_CLASS_MON   0x0A
98
 
41 ingob 99
#define UBX_ID_POSLLH   0x02
100
#define UBX_ID_SOL              0x06
101
#define UBX_ID_VELNED   0x12
247 killagreg 102
 
700 holgerb 103
#define UBX_ID_MON_VER  0x04
104
 
41 ingob 105
// ------------------------------------------------------------------------------------------------
106
// typedefs
107
 
108
 
109
// ubx parser state
110
typedef enum
111
{
112
        UBXSTATE_IDLE,
113
        UBXSTATE_SYNC1,
114
        UBXSTATE_SYNC2,
115
        UBXSTATE_CLASS,
116
        UBXSTATE_LEN1,
117
        UBXSTATE_LEN2,
118
        UBXSTATE_DATA,
119
        UBXSTATE_CKA,
120
        UBXSTATE_CKB
121
} ubxState_t;
122
 
123
typedef struct
124
{
125
        u32                     itow;           // ms GPS Millisecond Time of Week
126
        s32                     frac;           // ns remainder of rounded ms above
127
        s16                     week;           // GPS week
128
        u8                      GPSfix;         // GPSfix Type, range 0..6
129
        u8                      Flags;          // Navigation Status Flags
130
        s32                     ECEF_X;         // cm ECEF X coordinate
131
        s32                     ECEF_Y;         // cm ECEF Y coordinate
132
        s32                     ECEF_Z;         // cm ECEF Z coordinate
133
        u32                     PAcc;           // cm 3D Position Accuracy Estimate
134
        s32                     ECEFVX;         // cm/s ECEF X velocity
135
        s32                     ECEFVY;         // cm/s ECEF Y velocity
136
        s32                     ECEFVZ;         // cm/s ECEF Z velocity
137
        u32                     SAcc;           // cm/s Speed Accuracy Estimate
138
        u16                     PDOP;           // 0.01 Position DOP
139
        u8                      res1;           // reserved
140
        u8                      numSV;          // Number of SVs used in navigation solution
141
        u32                     res2;           // reserved
52 killagreg 142
        u8                      Status;     // invalid/newdata/processed
41 ingob 143
} __attribute__((packed)) ubx_nav_sol_t;
144
 
145
 
146
typedef struct
147
{
148
        u32                     itow;           // ms  GPS Millisecond Time of Week
149
        s32                     VEL_N;          // cm/s  NED north velocity
150
        s32                     VEL_E;          // cm/s  NED east velocity
151
        s32                     VEL_D;          // cm/s  NED down velocity
152
        u32                     Speed;          // cm/s  Speed (3-D)
153
        u32                     GSpeed;         // cm/s  Ground Speed (2-D)
154
        s32                     Heading;        // 1e-05 deg  Heading 2-D
155
        u32                     SAcc;           // cm/s  Speed Accuracy Estimate
156
        u32                     CAcc;           // deg  Course / Heading Accuracy Estimate
52 killagreg 157
        u8                      Status;         // invalid/newdata/processed
41 ingob 158
} __attribute__((packed)) ubx_nav_velned_t;
159
 
160
typedef struct
161
{
162
        u32                     itow;           // ms GPS Millisecond Time of Week
163
        s32                     LON;            // 1e-07 deg Longitude
164
        s32                     LAT;            // 1e-07 deg Latitude
165
        s32                     HEIGHT;         // mm Height above Ellipsoid
166
        s32                     HMSL;           // mm Height above mean sea level
167
        u32                     Hacc;           // mm Horizontal Accuracy Estimate
168
        u32                     Vacc;           // mm Vertical Accuracy Estimate
52 killagreg 169
        u8                      Status;         // invalid/newdata/processed
41 ingob 170
} __attribute__((packed)) ubx_nav_posllh_t;
171
 
172
//------------------------------------------------------------------------------------
173
// global variables
174
 
175
// local buffers for the incomming ubx messages
263 killagreg 176
ubx_nav_sol_t           UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
177
ubx_nav_posllh_t        UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
178
ubx_nav_velned_t        UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
179
ubxmsg_t                        UbxMsg;
41 ingob 180
 
181
// shared buffer
244 killagreg 182
gps_data_t              GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
41 ingob 183
DateTime_t                      GPSDateTime = {0,0,0,0,0,0,0, INVALID};
184
 
146 killagreg 185
#define UBX_TIMEOUT 500 // 500 ms
186
u32 UBX_Timeout = 0;
41 ingob 187
 
188
//------------------------------------------------------------------------------------
189
// functions
190
 
191
u8 IsLeapYear(u16 year)
192
{
193
        if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
194
        else return 0;
195
}
196
/********************************************************/
197
/*  Calculates the UTC Time from the GPS week and tow   */
198
/********************************************************/
199
void SetGPSTime(DateTime_t * pTimeStruct)
200
{
201
        u32 Days, Seconds, Week;
202
        u16 YearPart;
203
        u32 * MonthDayTab = 0;
204
        u8 i;
205
 
206
 
207
        // if GPS data show valid time data
208
        if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
209
        {
210
                Seconds = UbxSol.itow / 1000L;
211
                Week = (u32)UbxSol.week;
212
                // correct leap seconds since 1980
213
                if(Seconds < LEAP_SECONDS_FROM_1980)
214
                {
215
                        Week--;
216
                        Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
217
                }
218
                else Seconds -= LEAP_SECONDS_FROM_1980;
219
 
515 killagreg 220
                Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
41 ingob 221
                Days += (Week * DAYS_PER_WEEK);
222
                Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
223
 
224
                pTimeStruct->Year = 1;
225
                YearPart = (u16)(Days / DAYS_PER_400YEARS);
226
                pTimeStruct->Year += YearPart * 400;
227
                Days = Days % DAYS_PER_400YEARS;
228
                YearPart = (u16)(Days / DAYS_PER_100YEARS);
229
                pTimeStruct->Year += YearPart * 100;
230
                Days = Days % DAYS_PER_100YEARS;
231
                YearPart = (u16)(Days / DAYS_PER_4YEARS);
232
                pTimeStruct->Year += YearPart * 4;
233
                Days = Days % DAYS_PER_4YEARS;
234
                if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR);
235
                else YearPart = 3;
236
                pTimeStruct->Year += YearPart;
237
                // calculate remaining days of year
238
                Days -= (u32)(YearPart *  DAYS_PER_YEAR);
239
                Days += 1;
240
                // check if current year is a leap year
241
                if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap;
242
                else MonthDayTab = (u32*)Normal;
515 killagreg 243
                // seperate month and day from days of year
41 ingob 244
                for ( i = 0; i < 12; i++ )
245
                {
246
                        if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
247
                        {
248
                                pTimeStruct->Month = i+1;
249
                                pTimeStruct->Day = Days - MonthDayTab[i];
250
                                i = 12;
251
                        }
252
                }
253
                Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
254
                pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR);
255
                Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
256
                pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE);
257
                Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
258
                pTimeStruct->Sec = (u8)(Seconds);
259
                pTimeStruct->mSec  = (u16)(UbxSol.itow % 1000L);
260
                pTimeStruct->Valid = 1;
261
        }
262
        else
263
        {
264
                pTimeStruct->Valid = 0;
265
        }
266
}
267
 
268
 
269
 
270
/********************************************************/
271
/*                  Initialize UBX Parser               */
272
/********************************************************/
273
void UBX_Init(void)
274
{
275
        // mark msg buffers invalid
276
        UbxSol.Status = INVALID;
277
        UbxPosLlh.Status = INVALID;
278
        UbxVelNed.Status = INVALID;
247 killagreg 279
        UbxMsg.Status = INVALID;
41 ingob 280
        GPSData.Status = INVALID;
806 holgerb 281
        UBX_Timeout = SetDelay(5000);
41 ingob 282
}
283
 
284
/********************************************************/
285
/*            Upate GPS data stcructure                 */
286
/********************************************************/
263 killagreg 287
void Update_GPSData(void)
41 ingob 288
{
263 killagreg 289
        static u32 last_itow = 0;
569 holgerb 290
        static u32 milliseconds = 0;
41 ingob 291
 
292
        // if a new set of ubx messages was collected
263 killagreg 293
        if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
294
        {       // and the itow is equal (same time base)
515 killagreg 295
                UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT;
263 killagreg 296
                if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow))
41 ingob 297
                {
146 killagreg 298
                        UBX_Timeout = SetDelay(UBX_TIMEOUT);
700 holgerb 299
                        DebugOut.Analog[9]++;  
263 killagreg 300
                        // update GPS data only if the status is INVALID or PROCESSED
515 killagreg 301
                        if(GPSData.Status != NEWDATA)
41 ingob 302
                        { // wait for new data at all neccesary ubx messages
659 holgerb 303
                                CountNewGpsDataIn5Sec++;
41 ingob 304
                                GPSData.Status = INVALID;
244 killagreg 305
                                // update message cycle time
264 killagreg 306
                                GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow);
569 holgerb 307
                                milliseconds += GPSData.MsgCycleTime;
264 killagreg 308
                                last_itow = UbxSol.itow; // update last itow
41 ingob 309
                                // NAV SOL
322 holgerb 310
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
41 ingob 311
                                GPSData.NumOfSats =                     UbxSol.numSV;
312
                                GPSData.SatFix =                                UbxSol.GPSfix;
645 holgerb 313
//if(Parameter.User3 > 100) { GPSData.NumOfSats = 0; GPSData.SatFix = 0;}
314
//if(Parameter.User3 > 100) { GPSData.SatFix = 0;}
780 holgerb 315
//DebugOut.Analog[] = UbxPosLlh.Hacc;
316
//DebugOut.Analog[] = UbxPosLlh.Vacc;
492 killagreg 317
                                GPSData.Position_Accuracy =             UbxSol.PAcc;    // in steps of 1cm
318
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;    // in steps of 1cm/s
41 ingob 319
                                SetGPSTime(&SystemTime); // update system time
320
                                // NAV VELNED
492 killagreg 321
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;        // in steps of 1cm/s
322
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;    // in steps of 1cm/s
323
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;   // in steps of 1cm/s
324
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;       // in steps of 1cm/s
325
                                GPSData.Heading =                               UbxVelNed.Heading; //in steps of 1E-5 deg
569 holgerb 326
                                // NAV POSLLH
327
                                GPSData.Position.Status =               INVALID;
328
                                if(!(SimulationFlags & SIMULATION_ACTIVE))
329
                                {
883 holgerb 330
 
331
if((NMEA_Position.Status != INVALID) && (PPM_In[EE_Parameter.ExternalGpsChannel] > 50))
881 holgerb 332
 {
333
                                        GPSData.Position.Longitude = NMEA_Position.Longitude;
334
                                        GPSData.Position.Latitude  = NMEA_Position.Latitude;
335
                                        GPSData.Position.Altitude  = NMEA_Position.Altitude;
336
                                        if(CheckAscii(NMEA_In_Fix_Txt[3]))
337
                                           GPSData.NumOfSats = 10 * AsciiToNum(NMEA_In_Fix_Txt[2]) + AsciiToNum(NMEA_In_Fix_Txt[3]);
338
                                         else
339
                                        if(CheckAscii(NMEA_In_Fix_Txt[2])) GPSData.NumOfSats = AsciiToNum(NMEA_In_Fix_Txt[2]); // nur eine Ziffer? (weiss nicht, ob das vorkommt..)
340
                                         else GPSData.NumOfSats = 0;
883 holgerb 341
                                        ExternalGpsInUse = 1;
881 holgerb 342
 }                                       
343
 else
344
 {
345
 
569 holgerb 346
                                        GPSData.Position.Longitude =    UbxPosLlh.LON;  // in steps of 1E-7 deg
347
                                        GPSData.Position.Latitude =     UbxPosLlh.LAT;  // in steps of 1E-7 deg
348
                                        GPSData.Position.Altitude =     UbxPosLlh.HMSL; // in steps of 1 mm
883 holgerb 349
                                        ExternalGpsInUse = 0;
881 holgerb 350
 }
569 holgerb 351
                                }
352
                                else // simulation active
353
                                {
574 holgerb 354
                                 if(GPSData.SatFix != SATFIX_3D || GPSData.NumOfSats < 6)  // simulate satfix
569 holgerb 355
                                 {
645 holgerb 356
                                   GPSData.SatFix = SATFIX_3D;          // Simulation
357
                                   GPSData.Flags |= FLAG_GPSFIXOK;      // Simulation
358
                                   GPSData.NumOfSats = 8;                       // Simulation
569 holgerb 359
                                   if(!SystemTime.Valid)
360
                                   {
361
                                        UbxSol.Status = 1;
362
                                        UbxSol.Flags |= FLAG_WKNSET;
363
                                        UbxSol.Flags |= FLAG_TOWSET;
364
                                        UbxSol.week = 1043;      // starts in year 2000
365
                                        UbxSol.itow = milliseconds;
366
                                        SetGPSTime(&SystemTime); // update system time
367
                                        SystemTime.Valid = 1; // use the time that is given by the GPS-Module
368
                                   }
369
                                 }
370
                                }
371
                                GPSData.Position.Status =               NEWDATA;
515 killagreg 372
 
41 ingob 373
                                GPSData.Status = NEWDATA; // new data available
374
                        } // EOF if(GPSData.Status != NEWDATA)
263 killagreg 375
                        // set state to collect new data
376
                        UbxSol.Status =                                 PROCESSED;      // ready for new data
377
                        UbxPosLlh.Status =                              PROCESSED;      // ready for new data
378
                        UbxVelNed.Status =                              PROCESSED;      // ready for new data
379
                } // EOF all itow are equal
380
        } // EOF all ubx messages received
503 holgerb 381
 
382
 
383
//++++++++++++++++++++++++++++++++
384
// Please do not delete
515 killagreg 385
// This helps me for testing
503 holgerb 386
//++++++++++++++++++++++++++++++++
387
//GPSData.Position.Longitude = 1517409123L;  // Hamilton, Australia
388
//GPSData.Position.Latitude = -329294773L;       // Hamilton, Australia
389
//++++++++
390
//GPSData.Position.Longitude =-1556010020L;  // Alaska
391
//GPSData.Position.Latitude = 629581270L;        // Alaska
392
//++++++++
393
//GPSData.Position.Longitude =-584343419L;   // Buenos aires
394
//GPSData.Position.Latitude = -345464421L;   // Buenos aires
395
//++++++++
396
//GPSData.Position.Longitude =1683362691L;   // Neuseeland
397
//GPSData.Position.Latitude = -465945926L;   // Neuseeland
504 holgerb 398
//++++++++
507 holgerb 399
//GPSData.Position.Longitude = 194140605L;   // Afrika
400
//GPSData.Position.Latitude = -345384656L;   // Afrika
401
//++++++++
504 holgerb 402
//GPSData.Position.Longitude =-740443840L;  // Liberty Staue davor
515 killagreg 403
//GPSData.Position.Latitude = 406888880L;        // Liberty Staue
504 holgerb 404
 
405
//GPSData.Position.Longitude =-740451660L;  // Liberty Staue daneben
515 killagreg 406
//GPSData.Position.Latitude = 406891880L;        // Liberty Staue
504 holgerb 407
 
408
//GPSData.Position.Longitude =-740446540L;  // Liberty Staue direkt
409
//GPSData.Position.Latitude = 406891590L;        // Liberty Staue 1
410
 
514 holgerb 411
//GPSData.Position.Longitude =-1142878694L;  // Flori
515 killagreg 412
//GPSData.Position.Latitude = 483712102L;        //
514 holgerb 413
 
414
//GPSData.Position.Longitude =1251674613L;  // Bubble
515 killagreg 415
//GPSData.Position.Latitude = 466058365L;        //
416
 
41 ingob 417
}
418
 
700 holgerb 419
u8 UbxVersionParser(void)
420
{
421
                        // HW:00000040: Antaris
422
                        // HW:00040001: Antaris-4
423
                        // HW:80040001: Antaris-4
803 holgerb 424
                        // HW:00040005: u-blox 5 (nicht von uns verbaut)
700 holgerb 425
                        // HW:00040006: u-blox 6
426
                        // HW:00040007: u-blox 6
803 holgerb 427
                        // HW:00070000: u-blox 7 (nicht von uns verbaut)
700 holgerb 428
                        // HW:00080000: u-blox M8
41 ingob 429
 
803 holgerb 430
           // MKGPS V10 -> 1500 -> LEA-4H-0-000 -> ubxsw == 5.00 HW:00040001 
431
           // MKGPS V20 -> 2602 -> LEA-6S-0-000 -> ubxsw == 6.02 HW:00040007 
432
           // MKGPS V20 -> 2703 -> LEA-6S-0-001 -> ubxsw == 7.03 HW:00040007
433
                   // MKGPS V30 -> 3101 -> NEO-M8N-0-01 -> ubxsw == 2.01 HW:00080000 (Flash- Variante) kann nur 5Hz bei MAX-Sat:15
434
                   // MKGPS V31 -> 3201 -> NEO-M8Q-0-00 -> ubxsw == 2.01 HW:00080000 (ROM Variante)
435
                   // MKGPS V40 -> 4301 -> NEO-M8Q-0-10 -> ubxsw == 3.01 HW:00080000 (Message:"ROM CORE 3.01 (107888))
436
 
700 holgerb 437
u8 retval = 0xFF;
438
          if(UbxMsg.Data[33] == '4' && UbxMsg.Data[37] == '1') // LEA-4 
439
           {
440
        GPS_Version = 1000 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
441
                retval = 10; // MKGPS V1
442
           }
443
           else
444
          if(UbxMsg.Data[33] == '4' && UbxMsg.Data[37] == '7') // LEA-6 
445
           {
446
        GPS_Version = 2000 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
447
                retval = 20; // MKGPS V2
448
           }
449
           else
450
          if(UbxMsg.Data[33] == '8' && UbxMsg.Data[37] == '0') // NEO-8 
451
           {
452
                retval = 30; // MKGPS V3
803 holgerb 453
                if(UbxMsg.Data[100]     == 'F') // Message[100]:"FIS 0xEF4015 (73171)" bei der Flash-Variante
454
                 {
455
           GPS_Version = 3000 - 100 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
456
                   retval = 30; // MKGPS V3     Flash
457
                 }
458
                 else
459
                if(UbxMsg.Data[0] >= '0' && UbxMsg.Data[0] <= '9') // Steht da gleich zu Anfang eine Zahl?
460
                 {
461
           GPS_Version = 3000 + (UbxMsg.Data[0] - '0') * 100 + (UbxMsg.Data[2] - '0')  * 10 + (UbxMsg.Data[3] - '0');
462
                   retval = 31; // MKGPS V3     ROM
463
                 }
464
                 else
465
                if(UbxMsg.Data[9] >= '0' && UbxMsg.Data[9] <= '9') // Message:"ROM CORE 3.01 (107888)" 
466
                 {
467
           GPS_Version = 4000 + (UbxMsg.Data[9] - '0') * 100 + (UbxMsg.Data[11] - '0')  * 10 + (UbxMsg.Data[12] - '0');
468
               retval = 40; // MKGPS V4 (Galileo)
469
                 }
700 holgerb 470
           }
471
return(retval);
472
}
473
 
41 ingob 474
/********************************************************/
475
/*                   UBX Parser                         */
476
/********************************************************/
245 killagreg 477
void UBX_RxParser(u8 c)
41 ingob 478
{
479
        static ubxState_t ubxState = UBXSTATE_IDLE;
263 killagreg 480
        static ubxmsghdr_t RxHdr;
481
        static u8 RxData[UBX_MSG_DATA_SIZE];
482
        static u16 RxBytes = 0;
41 ingob 483
        static u8 cka, ckb;
484
 
485
 
486
        //state machine
487
        switch (ubxState)       // ubx message parser
488
        {
489
                case UBXSTATE_IDLE: // check 1st sync byte
490
                        if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
491
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
492
                        break;
493
 
494
                case UBXSTATE_SYNC1: // check 2nd sync byte
495
                        if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
496
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
497
                        break;
498
 
499
                case UBXSTATE_SYNC2: // check msg class to be NAV
263 killagreg 500
                        RxHdr.Class = c;
245 killagreg 501
                        ubxState = UBXSTATE_CLASS;
41 ingob 502
                        break;
503
 
504
                case UBXSTATE_CLASS: // check message identifier
263 killagreg 505
                        RxHdr.Id = c;
247 killagreg 506
                        ubxState = UBXSTATE_LEN1;
263 killagreg 507
                        cka = RxHdr.Class + RxHdr.Id;
508
                        ckb = RxHdr.Class + cka;
247 killagreg 509
                        break;
510
 
511
                case UBXSTATE_LEN1: // 1st message length byte
263 killagreg 512
                        RxHdr.Length = (u16)c; // lowbyte first
247 killagreg 513
                        cka += c;
514
                        ckb += cka;
515
                        ubxState = UBXSTATE_LEN2;
516
                        break;
517
 
518
                case UBXSTATE_LEN2: // 2nd message length byte
263 killagreg 519
                        RxHdr.Length += ((u16)c)<<8; // high byte last
520
                        if (RxHdr.Length >= UBX_MSG_DATA_SIZE)
41 ingob 521
                        {
515 killagreg 522
                                ubxState = UBXSTATE_IDLE;
41 ingob 523
                        }
263 killagreg 524
                        else
41 ingob 525
                        {
263 killagreg 526
                                cka += c;
527
                                ckb += cka;
528
                                RxBytes = 0;    // reset data byte counter
529
                                ubxState = UBXSTATE_DATA;
41 ingob 530
                        }
531
                        break;
532
 
533
                case UBXSTATE_DATA: // collecting data
263 killagreg 534
                        if (RxBytes < UBX_MSG_DATA_SIZE)
41 ingob 535
                        {
263 killagreg 536
                                RxData[RxBytes++] = c; // copy curent data byte if any space is left
41 ingob 537
                                cka += c;
538
                                ckb += cka;
263 killagreg 539
                                if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received
41 ingob 540
                        }
541
                        else // rx buffer overrun
542
                        {
543
                                ubxState = UBXSTATE_IDLE;
544
                        }
545
                        break;
546
 
547
                case UBXSTATE_CKA:
548
                        if (c == cka) ubxState = UBXSTATE_CKB;
549
                        else
550
                        {
551
                                ubxState = UBXSTATE_IDLE;
552
                        }
553
                        break;
554
 
555
                case UBXSTATE_CKB:
556
                        if (c == ckb)
263 killagreg 557
                        {       // checksum is ok
558
 
559
                                switch(RxHdr.Class)
560
                                {
561
                                        case UBX_CLASS_NAV:
562
                                                switch(RxHdr.Id)
563
                                                {
564
                                                        case UBX_ID_POSLLH: // geodetic position
565
                                                                memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length);
566
                                                                UbxPosLlh.Status = NEWDATA;
567
                                                                break;
568
 
515 killagreg 569
                                                        case UBX_ID_VELNED: // velocity vector in tangent plane
263 killagreg 570
                                                                memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length);
571
                                                                UbxVelNed.Status = NEWDATA;
572
                                                                break;
573
 
574
                                                        case UBX_ID_SOL: // navigation solution
575
                                                                memcpy((u8*)&UbxSol, RxData, RxHdr.Length);
576
                                                                UbxSol.Status = NEWDATA;
577
                                                                break;
578
 
579
                                                        default:
580
                                                                break;
581
                                                } // EOF switch(Id)
582
                                                Update_GPSData();
583
                                                break;
700 holgerb 584
                                        case UBX_CLASS_MON: // version
585
                                                switch(RxHdr.Id)
586
                                                {
587
                                                 case UBX_ID_MON_VER:
588
                                                        if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE) memcpy(UbxMsg.Data, RxData, RxHdr.Length);
589
                                                        UbxVersionParser();
590
                                                        break;
515 killagreg 591
 
700 holgerb 592
                                                        default:
593
                                                                break;
594
                                                }
263 killagreg 595
                                        default:
596
                                                break;
597
                                } // EOF switch(class)
598
 
515 killagreg 599
                                // check generic msg filter
263 killagreg 600
                                if(UbxMsg.Status != NEWDATA)
601
                                {       // msg buffer is free
602
                                        if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask)))
603
                                        {       // msg matches to the filter criteria
604
                                                UbxMsg.Status = INVALID;
605
                                                UbxMsg.Hdr.Class = RxHdr.Class;
606
                                                UbxMsg.Hdr.Id = RxHdr.Id;
607
                                                UbxMsg.Hdr.Length = RxHdr.Length;
608
                                                if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE)
609
                                                {       // copy data block
610
                                                        memcpy(UbxMsg.Data, RxData, RxHdr.Length);
611
                                                        UbxMsg.Status = NEWDATA;
515 killagreg 612
                                                }
263 killagreg 613
                                        } // EOF filter matches
614
                                } // EOF != INVALID
615
                        }// EOF crc ok
792 holgerb 616
//                      else DebugOut.Analog[13]++; // CRC Error -> since 2.10e (6.2015) -> removed 11.2016 (V2.16)
41 ingob 617
                        ubxState = UBXSTATE_IDLE; // ready to parse new data
618
                        break;
619
 
620
                default: // unknown ubx state
621
                        ubxState = UBXSTATE_IDLE;
622
                        break;
623
 
624
        }
625
}
245 killagreg 626
 
247 killagreg 627
u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len)
245 killagreg 628
{
629
        u16 i;
630
        u8 cka = 0, ckb = 0;
631
        // check if buffer is available
632
        if(pBuff->Locked == TRUE) return(0);
633
        // check if buffer size is sufficient
634
        if(pBuff->Size < 8 + Len) return(0);
635
        // lock the buffer
636
        pBuff->Locked = TRUE;
515 killagreg 637
        // start at begin
245 killagreg 638
        pBuff->Position = 0;
639
        pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR;
640
        pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR;
247 killagreg 641
        for(i=0;i<Len;i++)
642
        {
643
                pBuff->pData[pBuff->Position++] = pData[i];
644
        }
245 killagreg 645
        // calculate checksum
646
        for(i=2;i<pBuff->Position;i++)
647
        {
515 killagreg 648
                cka += pBuff->pData[i];
245 killagreg 649
                ckb += cka;
650
        }
651
        pBuff->pData[pBuff->Position++] = cka;
652
        pBuff->pData[pBuff->Position++] = ckb;
653
        pBuff->DataBytes = pBuff->Position;
654
        pBuff->Position = 0;  // reset buffer position for transmision
655
        return(1);
656
}
263 killagreg 657
/*
658
switch(ubxclass)
659
                        {
660
                                case UBX_CLASS_NAV:
661
                                        switch(ubxid)
662
                                        {
663
                                                case UBX_ID_POSLLH: // geodetic position
664
                                                        ubxSp =  (u8 *)&UbxPosLlh; // data start pointer
665
                                                        ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer
666
                                                        ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer
667
                                                        break;
515 killagreg 668
 
263 killagreg 669
                                                case UBX_ID_SOL: // navigation solution
670
                                                        ubxSp =  (u8 *)&UbxSol; // data start pointer
671
                                                        ubxEp = (u8 *)(&UbxSol + 1); // data end pointer
672
                                                        ubxStP = (u8 *)&UbxSol.Status; // status pointer
673
                                                        break;
515 killagreg 674
 
263 killagreg 675
                                                case UBX_ID_VELNED: // velocity vector in tangent plane
676
                                                        ubxSp =  (u8 *)&UbxVelNed; // data start pointer
677
                                                        ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer
678
                                                        ubxStP = (u8 *)&UbxVelNed.Status; // status pointer
679
                                                        break;
515 killagreg 680
 
263 killagreg 681
                                                default:                        // unsupported identifier
682
                                                        ubxState = UBXSTATE_IDLE;
683
                                                        return;
684
                                        }
685
                                        break;
686
 
687
                                default: // other  classes
688
                                        if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE;
689
                                        else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask)))
690
                                        {       // buffer is free and message matches to filter criteria
691
                                                UbxMsg.Status = INVALID;
692
                                                UbxMsg.Hdr.Class = ubxclass;
693
                                                UbxMsg.Hdr.Id = ubxid;
694
                                                UbxMsg.Hdr.Length = msglen;
695
                                                ubxSp =  (u8 *)&(UbxMsg.Data);   // data start pointer
696
                                                ubxEp = (u8 *)(&UbxMsg + 1);    // data end pointer
515 killagreg 697
                                                ubxStP = (u8 *)&UbxMsg.Status;  // status pointer
263 killagreg 698
                                        }
515 killagreg 699
                                        else ubxState = UBXSTATE_IDLE;
263 killagreg 700
                                        break;
701
                        }
702
*/
703