Rev 561 | Rev 574 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 561 | Rev 569 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | // + Software Nutzungsbedingungen (english version: see below) |
7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
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 |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
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. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
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. |
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 |
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. |
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 |
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. |
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 |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
22 | // + des Mitverschuldens offen. |
22 | // + des Mitverschuldens offen. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
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. |
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. |
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 |
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. |
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. |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
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. |
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. |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Software LICENSING TERMS |
32 | // + Software LICENSING TERMS |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
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 |
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 |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
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. |
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 |
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 |
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. |
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 |
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. |
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 |
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 |
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 |
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 |
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. |
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 |
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. |
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 |
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. |
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. |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
53 | // + #### END OF LICENSING TERMS #### |
53 | // + #### END OF LICENSING TERMS #### |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <string.h> |
56 | #include <string.h> |
57 | #include "91x_lib.h" |
57 | #include "91x_lib.h" |
58 | #include "uart1.h" |
58 | #include "uart1.h" |
59 | #include "ubx.h" |
59 | #include "ubx.h" |
60 | #include "led.h" |
60 | #include "led.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | #include "GPS.h" |
62 | #include "GPS.h" |
63 | 63 | ||
64 | // ------------------------------------------------------------------------------------------------ |
64 | // ------------------------------------------------------------------------------------------------ |
65 | // defines |
65 | // defines |
66 | 66 | ||
67 | #define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist! |
67 | #define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist! |
68 | #define DAYS_PER_YEAR 365 |
68 | #define DAYS_PER_YEAR 365 |
69 | #define DAYS_PER_LEAPYEAR 366 |
69 | #define DAYS_PER_LEAPYEAR 366 |
70 | #define DAYS_PER_4YEARS 1461 //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years |
70 | #define DAYS_PER_4YEARS 1461 //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years |
71 | #define DAYS_PER_100YEARS 36524 //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years |
71 | #define DAYS_PER_100YEARS 36524 //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years |
72 | #define DAYS_PER_400YEARS 146097 //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years |
72 | #define DAYS_PER_400YEARS 146097 //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years |
73 | #define SECONDS_PER_MINUTE 60 |
73 | #define SECONDS_PER_MINUTE 60 |
74 | #define MINUTES_PER_HOUR 60 |
74 | #define MINUTES_PER_HOUR 60 |
75 | #define HOURS_PER_DAY 24 |
75 | #define HOURS_PER_DAY 24 |
76 | #define DAYS_PER_WEEK 7 |
76 | #define DAYS_PER_WEEK 7 |
77 | #define SECONDS_PER_HOUR 3600 //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR) |
77 | #define SECONDS_PER_HOUR 3600 //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR) |
78 | #define SECONDS_PER_DAY 86400 //(SECONDS_PER_HOUR * HOURS_PER_DAY) |
78 | #define SECONDS_PER_DAY 86400 //(SECONDS_PER_HOUR * HOURS_PER_DAY) |
79 | #define SECONDS_PER_WEEK 604800 //(SECONDS_PER_DAY * DAYS_PER_WEEK) |
79 | #define SECONDS_PER_WEEK 604800 //(SECONDS_PER_DAY * DAYS_PER_WEEK) |
80 | 80 | ||
81 | // days per month in normal and leap years |
81 | // days per month in normal and leap years |
82 | const u32 Leap[ 13 ] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; |
82 | const u32 Leap[ 13 ] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; |
83 | const u32 Normal[ 13 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; |
83 | const u32 Normal[ 13 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; |
84 | 84 | ||
85 | #define LEAP_SECONDS_FROM_1980 16 |
85 | #define LEAP_SECONDS_FROM_1980 16 |
86 | 86 | ||
87 | // message sync bytes |
87 | // message sync bytes |
88 | #define UBX_SYNC1_CHAR 0xB5 |
88 | #define UBX_SYNC1_CHAR 0xB5 |
89 | #define UBX_SYNC2_CHAR 0x62 |
89 | #define UBX_SYNC2_CHAR 0x62 |
90 | // protocoll identifiers |
90 | // protocoll identifiers |
91 | // navigation class |
91 | // navigation class |
92 | #define UBX_CLASS_NAV 0x01 |
92 | #define UBX_CLASS_NAV 0x01 |
93 | #define UBX_ID_POSLLH 0x02 |
93 | #define UBX_ID_POSLLH 0x02 |
94 | #define UBX_ID_SOL 0x06 |
94 | #define UBX_ID_SOL 0x06 |
95 | #define UBX_ID_VELNED 0x12 |
95 | #define UBX_ID_VELNED 0x12 |
96 | 96 | ||
97 | // ------------------------------------------------------------------------------------------------ |
97 | // ------------------------------------------------------------------------------------------------ |
98 | // typedefs |
98 | // typedefs |
99 | 99 | ||
100 | 100 | ||
101 | // ubx parser state |
101 | // ubx parser state |
102 | typedef enum |
102 | typedef enum |
103 | { |
103 | { |
104 | UBXSTATE_IDLE, |
104 | UBXSTATE_IDLE, |
105 | UBXSTATE_SYNC1, |
105 | UBXSTATE_SYNC1, |
106 | UBXSTATE_SYNC2, |
106 | UBXSTATE_SYNC2, |
107 | UBXSTATE_CLASS, |
107 | UBXSTATE_CLASS, |
108 | UBXSTATE_LEN1, |
108 | UBXSTATE_LEN1, |
109 | UBXSTATE_LEN2, |
109 | UBXSTATE_LEN2, |
110 | UBXSTATE_DATA, |
110 | UBXSTATE_DATA, |
111 | UBXSTATE_CKA, |
111 | UBXSTATE_CKA, |
112 | UBXSTATE_CKB |
112 | UBXSTATE_CKB |
113 | } ubxState_t; |
113 | } ubxState_t; |
114 | 114 | ||
115 | typedef struct |
115 | typedef struct |
116 | { |
116 | { |
117 | u32 itow; // ms GPS Millisecond Time of Week |
117 | u32 itow; // ms GPS Millisecond Time of Week |
118 | s32 frac; // ns remainder of rounded ms above |
118 | s32 frac; // ns remainder of rounded ms above |
119 | s16 week; // GPS week |
119 | s16 week; // GPS week |
120 | u8 GPSfix; // GPSfix Type, range 0..6 |
120 | u8 GPSfix; // GPSfix Type, range 0..6 |
121 | u8 Flags; // Navigation Status Flags |
121 | u8 Flags; // Navigation Status Flags |
122 | s32 ECEF_X; // cm ECEF X coordinate |
122 | s32 ECEF_X; // cm ECEF X coordinate |
123 | s32 ECEF_Y; // cm ECEF Y coordinate |
123 | s32 ECEF_Y; // cm ECEF Y coordinate |
124 | s32 ECEF_Z; // cm ECEF Z coordinate |
124 | s32 ECEF_Z; // cm ECEF Z coordinate |
125 | u32 PAcc; // cm 3D Position Accuracy Estimate |
125 | u32 PAcc; // cm 3D Position Accuracy Estimate |
126 | s32 ECEFVX; // cm/s ECEF X velocity |
126 | s32 ECEFVX; // cm/s ECEF X velocity |
127 | s32 ECEFVY; // cm/s ECEF Y velocity |
127 | s32 ECEFVY; // cm/s ECEF Y velocity |
128 | s32 ECEFVZ; // cm/s ECEF Z velocity |
128 | s32 ECEFVZ; // cm/s ECEF Z velocity |
129 | u32 SAcc; // cm/s Speed Accuracy Estimate |
129 | u32 SAcc; // cm/s Speed Accuracy Estimate |
130 | u16 PDOP; // 0.01 Position DOP |
130 | u16 PDOP; // 0.01 Position DOP |
131 | u8 res1; // reserved |
131 | u8 res1; // reserved |
132 | u8 numSV; // Number of SVs used in navigation solution |
132 | u8 numSV; // Number of SVs used in navigation solution |
133 | u32 res2; // reserved |
133 | u32 res2; // reserved |
134 | u8 Status; // invalid/newdata/processed |
134 | u8 Status; // invalid/newdata/processed |
135 | } __attribute__((packed)) ubx_nav_sol_t; |
135 | } __attribute__((packed)) ubx_nav_sol_t; |
136 | 136 | ||
137 | 137 | ||
138 | typedef struct |
138 | typedef struct |
139 | { |
139 | { |
140 | u32 itow; // ms GPS Millisecond Time of Week |
140 | u32 itow; // ms GPS Millisecond Time of Week |
141 | s32 VEL_N; // cm/s NED north velocity |
141 | s32 VEL_N; // cm/s NED north velocity |
142 | s32 VEL_E; // cm/s NED east velocity |
142 | s32 VEL_E; // cm/s NED east velocity |
143 | s32 VEL_D; // cm/s NED down velocity |
143 | s32 VEL_D; // cm/s NED down velocity |
144 | u32 Speed; // cm/s Speed (3-D) |
144 | u32 Speed; // cm/s Speed (3-D) |
145 | u32 GSpeed; // cm/s Ground Speed (2-D) |
145 | u32 GSpeed; // cm/s Ground Speed (2-D) |
146 | s32 Heading; // 1e-05 deg Heading 2-D |
146 | s32 Heading; // 1e-05 deg Heading 2-D |
147 | u32 SAcc; // cm/s Speed Accuracy Estimate |
147 | u32 SAcc; // cm/s Speed Accuracy Estimate |
148 | u32 CAcc; // deg Course / Heading Accuracy Estimate |
148 | u32 CAcc; // deg Course / Heading Accuracy Estimate |
149 | u8 Status; // invalid/newdata/processed |
149 | u8 Status; // invalid/newdata/processed |
150 | } __attribute__((packed)) ubx_nav_velned_t; |
150 | } __attribute__((packed)) ubx_nav_velned_t; |
151 | 151 | ||
152 | typedef struct |
152 | typedef struct |
153 | { |
153 | { |
154 | u32 itow; // ms GPS Millisecond Time of Week |
154 | u32 itow; // ms GPS Millisecond Time of Week |
155 | s32 LON; // 1e-07 deg Longitude |
155 | s32 LON; // 1e-07 deg Longitude |
156 | s32 LAT; // 1e-07 deg Latitude |
156 | s32 LAT; // 1e-07 deg Latitude |
157 | s32 HEIGHT; // mm Height above Ellipsoid |
157 | s32 HEIGHT; // mm Height above Ellipsoid |
158 | s32 HMSL; // mm Height above mean sea level |
158 | s32 HMSL; // mm Height above mean sea level |
159 | u32 Hacc; // mm Horizontal Accuracy Estimate |
159 | u32 Hacc; // mm Horizontal Accuracy Estimate |
160 | u32 Vacc; // mm Vertical Accuracy Estimate |
160 | u32 Vacc; // mm Vertical Accuracy Estimate |
161 | u8 Status; // invalid/newdata/processed |
161 | u8 Status; // invalid/newdata/processed |
162 | } __attribute__((packed)) ubx_nav_posllh_t; |
162 | } __attribute__((packed)) ubx_nav_posllh_t; |
163 | 163 | ||
164 | //------------------------------------------------------------------------------------ |
164 | //------------------------------------------------------------------------------------ |
165 | // global variables |
165 | // global variables |
166 | 166 | ||
167 | // local buffers for the incomming ubx messages |
167 | // local buffers for the incomming ubx messages |
168 | ubx_nav_sol_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID}; |
168 | ubx_nav_sol_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID}; |
169 | ubx_nav_posllh_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID}; |
169 | ubx_nav_posllh_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID}; |
170 | ubx_nav_velned_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
170 | ubx_nav_velned_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
171 | ubxmsg_t UbxMsg; |
171 | ubxmsg_t UbxMsg; |
172 | 172 | ||
173 | // shared buffer |
173 | // shared buffer |
174 | gps_data_t GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID}; |
174 | gps_data_t GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID}; |
175 | DateTime_t GPSDateTime = {0,0,0,0,0,0,0, INVALID}; |
175 | DateTime_t GPSDateTime = {0,0,0,0,0,0,0, INVALID}; |
176 | 176 | ||
177 | #define UBX_TIMEOUT 500 // 500 ms |
177 | #define UBX_TIMEOUT 500 // 500 ms |
178 | u32 UBX_Timeout = 0; |
178 | u32 UBX_Timeout = 0; |
179 | 179 | ||
180 | //------------------------------------------------------------------------------------ |
180 | //------------------------------------------------------------------------------------ |
181 | // functions |
181 | // functions |
182 | 182 | ||
183 | u8 IsLeapYear(u16 year) |
183 | u8 IsLeapYear(u16 year) |
184 | { |
184 | { |
185 | if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1; |
185 | if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1; |
186 | else return 0; |
186 | else return 0; |
187 | } |
187 | } |
188 | /********************************************************/ |
188 | /********************************************************/ |
189 | /* Calculates the UTC Time from the GPS week and tow */ |
189 | /* Calculates the UTC Time from the GPS week and tow */ |
190 | /********************************************************/ |
190 | /********************************************************/ |
191 | void SetGPSTime(DateTime_t * pTimeStruct) |
191 | void SetGPSTime(DateTime_t * pTimeStruct) |
192 | { |
192 | { |
193 | u32 Days, Seconds, Week; |
193 | u32 Days, Seconds, Week; |
194 | u16 YearPart; |
194 | u16 YearPart; |
195 | u32 * MonthDayTab = 0; |
195 | u32 * MonthDayTab = 0; |
196 | u8 i; |
196 | u8 i; |
197 | 197 | ||
198 | 198 | ||
199 | // if GPS data show valid time data |
199 | // if GPS data show valid time data |
200 | if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) ) |
200 | if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) ) |
201 | { |
201 | { |
202 | Seconds = UbxSol.itow / 1000L; |
202 | Seconds = UbxSol.itow / 1000L; |
203 | Week = (u32)UbxSol.week; |
203 | Week = (u32)UbxSol.week; |
204 | // correct leap seconds since 1980 |
204 | // correct leap seconds since 1980 |
205 | if(Seconds < LEAP_SECONDS_FROM_1980) |
205 | if(Seconds < LEAP_SECONDS_FROM_1980) |
206 | { |
206 | { |
207 | Week--; |
207 | Week--; |
208 | Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds; |
208 | Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds; |
209 | } |
209 | } |
210 | else Seconds -= LEAP_SECONDS_FROM_1980; |
210 | else Seconds -= LEAP_SECONDS_FROM_1980; |
211 | 211 | ||
212 | Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980; |
212 | Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980; |
213 | Days += (Week * DAYS_PER_WEEK); |
213 | Days += (Week * DAYS_PER_WEEK); |
214 | Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week |
214 | Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week |
215 | 215 | ||
216 | pTimeStruct->Year = 1; |
216 | pTimeStruct->Year = 1; |
217 | YearPart = (u16)(Days / DAYS_PER_400YEARS); |
217 | YearPart = (u16)(Days / DAYS_PER_400YEARS); |
218 | pTimeStruct->Year += YearPart * 400; |
218 | pTimeStruct->Year += YearPart * 400; |
219 | Days = Days % DAYS_PER_400YEARS; |
219 | Days = Days % DAYS_PER_400YEARS; |
220 | YearPart = (u16)(Days / DAYS_PER_100YEARS); |
220 | YearPart = (u16)(Days / DAYS_PER_100YEARS); |
221 | pTimeStruct->Year += YearPart * 100; |
221 | pTimeStruct->Year += YearPart * 100; |
222 | Days = Days % DAYS_PER_100YEARS; |
222 | Days = Days % DAYS_PER_100YEARS; |
223 | YearPart = (u16)(Days / DAYS_PER_4YEARS); |
223 | YearPart = (u16)(Days / DAYS_PER_4YEARS); |
224 | pTimeStruct->Year += YearPart * 4; |
224 | pTimeStruct->Year += YearPart * 4; |
225 | Days = Days % DAYS_PER_4YEARS; |
225 | Days = Days % DAYS_PER_4YEARS; |
226 | if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR); |
226 | if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR); |
227 | else YearPart = 3; |
227 | else YearPart = 3; |
228 | pTimeStruct->Year += YearPart; |
228 | pTimeStruct->Year += YearPart; |
229 | // calculate remaining days of year |
229 | // calculate remaining days of year |
230 | Days -= (u32)(YearPart * DAYS_PER_YEAR); |
230 | Days -= (u32)(YearPart * DAYS_PER_YEAR); |
231 | Days += 1; |
231 | Days += 1; |
232 | // check if current year is a leap year |
232 | // check if current year is a leap year |
233 | if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap; |
233 | if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap; |
234 | else MonthDayTab = (u32*)Normal; |
234 | else MonthDayTab = (u32*)Normal; |
235 | // seperate month and day from days of year |
235 | // seperate month and day from days of year |
236 | for ( i = 0; i < 12; i++ ) |
236 | for ( i = 0; i < 12; i++ ) |
237 | { |
237 | { |
238 | if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) ) |
238 | if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) ) |
239 | { |
239 | { |
240 | pTimeStruct->Month = i+1; |
240 | pTimeStruct->Month = i+1; |
241 | pTimeStruct->Day = Days - MonthDayTab[i]; |
241 | pTimeStruct->Day = Days - MonthDayTab[i]; |
242 | i = 12; |
242 | i = 12; |
243 | } |
243 | } |
244 | } |
244 | } |
245 | Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day |
245 | Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day |
246 | pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR); |
246 | pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR); |
247 | Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour |
247 | Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour |
248 | pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE); |
248 | pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE); |
249 | Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute |
249 | Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute |
250 | pTimeStruct->Sec = (u8)(Seconds); |
250 | pTimeStruct->Sec = (u8)(Seconds); |
251 | pTimeStruct->mSec = (u16)(UbxSol.itow % 1000L); |
251 | pTimeStruct->mSec = (u16)(UbxSol.itow % 1000L); |
252 | pTimeStruct->Valid = 1; |
252 | pTimeStruct->Valid = 1; |
253 | } |
253 | } |
254 | else |
254 | else |
255 | { |
255 | { |
256 | pTimeStruct->Valid = 0; |
256 | pTimeStruct->Valid = 0; |
257 | } |
257 | } |
258 | } |
258 | } |
259 | 259 | ||
260 | 260 | ||
261 | 261 | ||
262 | /********************************************************/ |
262 | /********************************************************/ |
263 | /* Initialize UBX Parser */ |
263 | /* Initialize UBX Parser */ |
264 | /********************************************************/ |
264 | /********************************************************/ |
265 | void UBX_Init(void) |
265 | void UBX_Init(void) |
266 | { |
266 | { |
267 | // mark msg buffers invalid |
267 | // mark msg buffers invalid |
268 | UbxSol.Status = INVALID; |
268 | UbxSol.Status = INVALID; |
269 | UbxPosLlh.Status = INVALID; |
269 | UbxPosLlh.Status = INVALID; |
270 | UbxVelNed.Status = INVALID; |
270 | UbxVelNed.Status = INVALID; |
271 | UbxMsg.Status = INVALID; |
271 | UbxMsg.Status = INVALID; |
272 | GPSData.Status = INVALID; |
272 | GPSData.Status = INVALID; |
273 | 273 | ||
274 | UBX_Timeout = SetDelay(2 * UBX_Timeout); |
274 | UBX_Timeout = SetDelay(2 * UBX_Timeout); |
275 | } |
275 | } |
276 | 276 | ||
277 | /********************************************************/ |
277 | /********************************************************/ |
278 | /* Upate GPS data stcructure */ |
278 | /* Upate GPS data stcructure */ |
279 | /********************************************************/ |
279 | /********************************************************/ |
280 | void Update_GPSData(void) |
280 | void Update_GPSData(void) |
281 | { |
281 | { |
282 | static u32 last_itow = 0; |
282 | static u32 last_itow = 0; |
- | 283 | static u32 milliseconds = 0; |
|
283 | 284 | ||
284 | // if a new set of ubx messages was collected |
285 | // if a new set of ubx messages was collected |
285 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
286 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
286 | { // and the itow is equal (same time base) |
287 | { // and the itow is equal (same time base) |
287 | UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT; |
288 | UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT; |
288 | if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow)) |
289 | if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow)) |
289 | { |
290 | { |
290 | UBX_Timeout = SetDelay(UBX_TIMEOUT); |
291 | UBX_Timeout = SetDelay(UBX_TIMEOUT); |
291 | DebugOut.Analog[9]++; |
292 | DebugOut.Analog[9]++; |
292 | // update GPS data only if the status is INVALID or PROCESSED |
293 | // update GPS data only if the status is INVALID or PROCESSED |
293 | if(GPSData.Status != NEWDATA) |
294 | if(GPSData.Status != NEWDATA) |
294 | { // wait for new data at all neccesary ubx messages |
295 | { // wait for new data at all neccesary ubx messages |
295 | GPSData.Status = INVALID; |
296 | GPSData.Status = INVALID; |
296 | // update message cycle time |
297 | // update message cycle time |
297 | GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow); |
298 | GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow); |
- | 299 | milliseconds += GPSData.MsgCycleTime; |
|
298 | last_itow = UbxSol.itow; // update last itow |
300 | last_itow = UbxSol.itow; // update last itow |
299 | // NAV SOL |
301 | // NAV SOL |
300 | GPSData.Flags = (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits |
302 | GPSData.Flags = (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits |
301 | GPSData.NumOfSats = UbxSol.numSV; |
303 | GPSData.NumOfSats = UbxSol.numSV; |
302 | GPSData.SatFix = UbxSol.GPSfix; |
304 | GPSData.SatFix = UbxSol.GPSfix; |
303 | GPSData.Position_Accuracy = UbxSol.PAcc; // in steps of 1cm |
305 | GPSData.Position_Accuracy = UbxSol.PAcc; // in steps of 1cm |
304 | GPSData.Speed_Accuracy = UbxSol.SAcc; // in steps of 1cm/s |
306 | GPSData.Speed_Accuracy = UbxSol.SAcc; // in steps of 1cm/s |
305 | SetGPSTime(&SystemTime); // update system time |
307 | SetGPSTime(&SystemTime); // update system time |
306 | // NAV POSLLH |
- | |
307 | GPSData.Position.Status = INVALID; |
- | |
308 | if(!(SimulationFlags & SIMULATION_ACTIVE)) |
- | |
309 | { |
- | |
310 | GPSData.Position.Longitude = UbxPosLlh.LON; // in steps of 1E-7 deg |
- | |
311 | GPSData.Position.Latitude = UbxPosLlh.LAT; // in steps of 1E-7 deg |
- | |
312 | } |
- | |
313 | GPSData.Position.Altitude = UbxPosLlh.HMSL; // in steps of 1 mm |
- | |
314 | GPSData.Position.Status = NEWDATA; |
- | |
315 | // NAV VELNED |
308 | // NAV VELNED |
316 | GPSData.Speed_East = UbxVelNed.VEL_E; // in steps of 1cm/s |
309 | GPSData.Speed_East = UbxVelNed.VEL_E; // in steps of 1cm/s |
317 | GPSData.Speed_North = UbxVelNed.VEL_N; // in steps of 1cm/s |
310 | GPSData.Speed_North = UbxVelNed.VEL_N; // in steps of 1cm/s |
318 | GPSData.Speed_Top = -UbxVelNed.VEL_D; // in steps of 1cm/s |
311 | GPSData.Speed_Top = -UbxVelNed.VEL_D; // in steps of 1cm/s |
319 | GPSData.Speed_Ground = UbxVelNed.GSpeed; // in steps of 1cm/s |
312 | GPSData.Speed_Ground = UbxVelNed.GSpeed; // in steps of 1cm/s |
320 | GPSData.Heading = UbxVelNed.Heading; //in steps of 1E-5 deg |
313 | GPSData.Heading = UbxVelNed.Heading; //in steps of 1E-5 deg |
- | 314 | // NAV POSLLH |
|
- | 315 | GPSData.Position.Status = INVALID; |
|
- | 316 | if(!(SimulationFlags & SIMULATION_ACTIVE)) |
|
- | 317 | { |
|
- | 318 | GPSData.Position.Longitude = UbxPosLlh.LON; // in steps of 1E-7 deg |
|
- | 319 | GPSData.Position.Latitude = UbxPosLlh.LAT; // in steps of 1E-7 deg |
|
- | 320 | GPSData.Position.Altitude = UbxPosLlh.HMSL; // in steps of 1 mm |
|
- | 321 | } |
|
- | 322 | else // simulation active |
|
- | 323 | { |
|
- | 324 | if(GPSData.SatFix != SATFIX_3D) // simulate satfix |
|
- | 325 | { |
|
- | 326 | GPSData.SatFix = SATFIX_3D; |
|
- | 327 | GPSData.Flags |= FLAG_GPSFIXOK; |
|
- | 328 | GPSData.NumOfSats = 8; |
|
- | 329 | if(!SystemTime.Valid) |
|
- | 330 | { |
|
- | 331 | UbxSol.Status = 1; |
|
- | 332 | UbxSol.Flags |= FLAG_WKNSET; |
|
- | 333 | UbxSol.Flags |= FLAG_TOWSET; |
|
- | 334 | UbxSol.week = 1043; // starts in year 2000 |
|
- | 335 | UbxSol.itow = milliseconds; |
|
- | 336 | SetGPSTime(&SystemTime); // update system time |
|
- | 337 | SystemTime.Valid = 1; // use the time that is given by the GPS-Module |
|
- | 338 | } |
|
- | 339 | } |
|
- | 340 | } |
|
- | 341 | GPSData.Position.Status = NEWDATA; |
|
321 | 342 | ||
322 | GPSData.Status = NEWDATA; // new data available |
343 | GPSData.Status = NEWDATA; // new data available |
323 | } // EOF if(GPSData.Status != NEWDATA) |
344 | } // EOF if(GPSData.Status != NEWDATA) |
324 | // set state to collect new data |
345 | // set state to collect new data |
325 | UbxSol.Status = PROCESSED; // ready for new data |
346 | UbxSol.Status = PROCESSED; // ready for new data |
326 | UbxPosLlh.Status = PROCESSED; // ready for new data |
347 | UbxPosLlh.Status = PROCESSED; // ready for new data |
327 | UbxVelNed.Status = PROCESSED; // ready for new data |
348 | UbxVelNed.Status = PROCESSED; // ready for new data |
328 | } // EOF all itow are equal |
349 | } // EOF all itow are equal |
329 | } // EOF all ubx messages received |
350 | } // EOF all ubx messages received |
330 | 351 | ||
331 | 352 | ||
332 | //++++++++++++++++++++++++++++++++ |
353 | //++++++++++++++++++++++++++++++++ |
333 | // Please do not delete |
354 | // Please do not delete |
334 | // This helps me for testing |
355 | // This helps me for testing |
335 | //++++++++++++++++++++++++++++++++ |
356 | //++++++++++++++++++++++++++++++++ |
336 | //GPSData.Position.Longitude = 1517409123L; // Hamilton, Australia |
357 | //GPSData.Position.Longitude = 1517409123L; // Hamilton, Australia |
337 | //GPSData.Position.Latitude = -329294773L; // Hamilton, Australia |
358 | //GPSData.Position.Latitude = -329294773L; // Hamilton, Australia |
338 | //++++++++ |
359 | //++++++++ |
339 | //GPSData.Position.Longitude =-1556010020L; // Alaska |
360 | //GPSData.Position.Longitude =-1556010020L; // Alaska |
340 | //GPSData.Position.Latitude = 629581270L; // Alaska |
361 | //GPSData.Position.Latitude = 629581270L; // Alaska |
341 | //++++++++ |
362 | //++++++++ |
342 | //GPSData.Position.Longitude =-584343419L; // Buenos aires |
363 | //GPSData.Position.Longitude =-584343419L; // Buenos aires |
343 | //GPSData.Position.Latitude = -345464421L; // Buenos aires |
364 | //GPSData.Position.Latitude = -345464421L; // Buenos aires |
344 | //++++++++ |
365 | //++++++++ |
345 | //GPSData.Position.Longitude =1683362691L; // Neuseeland |
366 | //GPSData.Position.Longitude =1683362691L; // Neuseeland |
346 | //GPSData.Position.Latitude = -465945926L; // Neuseeland |
367 | //GPSData.Position.Latitude = -465945926L; // Neuseeland |
347 | //++++++++ |
368 | //++++++++ |
348 | //GPSData.Position.Longitude = 194140605L; // Afrika |
369 | //GPSData.Position.Longitude = 194140605L; // Afrika |
349 | //GPSData.Position.Latitude = -345384656L; // Afrika |
370 | //GPSData.Position.Latitude = -345384656L; // Afrika |
350 | //++++++++ |
371 | //++++++++ |
351 | //GPSData.Position.Longitude =-740443840L; // Liberty Staue davor |
372 | //GPSData.Position.Longitude =-740443840L; // Liberty Staue davor |
352 | //GPSData.Position.Latitude = 406888880L; // Liberty Staue |
373 | //GPSData.Position.Latitude = 406888880L; // Liberty Staue |
353 | 374 | ||
354 | //GPSData.Position.Longitude =-740451660L; // Liberty Staue daneben |
375 | //GPSData.Position.Longitude =-740451660L; // Liberty Staue daneben |
355 | //GPSData.Position.Latitude = 406891880L; // Liberty Staue |
376 | //GPSData.Position.Latitude = 406891880L; // Liberty Staue |
356 | 377 | ||
357 | //GPSData.Position.Longitude =-740446540L; // Liberty Staue direkt |
378 | //GPSData.Position.Longitude =-740446540L; // Liberty Staue direkt |
358 | //GPSData.Position.Latitude = 406891590L; // Liberty Staue 1 |
379 | //GPSData.Position.Latitude = 406891590L; // Liberty Staue 1 |
359 | 380 | ||
360 | //GPSData.Position.Longitude =-1142878694L; // Flori |
381 | //GPSData.Position.Longitude =-1142878694L; // Flori |
361 | //GPSData.Position.Latitude = 483712102L; // |
382 | //GPSData.Position.Latitude = 483712102L; // |
362 | 383 | ||
363 | //GPSData.Position.Longitude =1251674613L; // Bubble |
384 | //GPSData.Position.Longitude =1251674613L; // Bubble |
364 | //GPSData.Position.Latitude = 466058365L; // |
385 | //GPSData.Position.Latitude = 466058365L; // |
365 | 386 | ||
366 | } |
387 | } |
367 | 388 | ||
368 | 389 | ||
369 | /********************************************************/ |
390 | /********************************************************/ |
370 | /* UBX Parser */ |
391 | /* UBX Parser */ |
371 | /********************************************************/ |
392 | /********************************************************/ |
372 | void UBX_RxParser(u8 c) |
393 | void UBX_RxParser(u8 c) |
373 | { |
394 | { |
374 | static ubxState_t ubxState = UBXSTATE_IDLE; |
395 | static ubxState_t ubxState = UBXSTATE_IDLE; |
375 | static ubxmsghdr_t RxHdr; |
396 | static ubxmsghdr_t RxHdr; |
376 | static u8 RxData[UBX_MSG_DATA_SIZE]; |
397 | static u8 RxData[UBX_MSG_DATA_SIZE]; |
377 | static u16 RxBytes = 0; |
398 | static u16 RxBytes = 0; |
378 | static u8 cka, ckb; |
399 | static u8 cka, ckb; |
379 | 400 | ||
380 | 401 | ||
381 | //state machine |
402 | //state machine |
382 | switch (ubxState) // ubx message parser |
403 | switch (ubxState) // ubx message parser |
383 | { |
404 | { |
384 | case UBXSTATE_IDLE: // check 1st sync byte |
405 | case UBXSTATE_IDLE: // check 1st sync byte |
385 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
406 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
386 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
407 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
387 | break; |
408 | break; |
388 | 409 | ||
389 | case UBXSTATE_SYNC1: // check 2nd sync byte |
410 | case UBXSTATE_SYNC1: // check 2nd sync byte |
390 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
411 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
391 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
412 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
392 | break; |
413 | break; |
393 | 414 | ||
394 | case UBXSTATE_SYNC2: // check msg class to be NAV |
415 | case UBXSTATE_SYNC2: // check msg class to be NAV |
395 | RxHdr.Class = c; |
416 | RxHdr.Class = c; |
396 | ubxState = UBXSTATE_CLASS; |
417 | ubxState = UBXSTATE_CLASS; |
397 | break; |
418 | break; |
398 | 419 | ||
399 | case UBXSTATE_CLASS: // check message identifier |
420 | case UBXSTATE_CLASS: // check message identifier |
400 | RxHdr.Id = c; |
421 | RxHdr.Id = c; |
401 | ubxState = UBXSTATE_LEN1; |
422 | ubxState = UBXSTATE_LEN1; |
402 | cka = RxHdr.Class + RxHdr.Id; |
423 | cka = RxHdr.Class + RxHdr.Id; |
403 | ckb = RxHdr.Class + cka; |
424 | ckb = RxHdr.Class + cka; |
404 | break; |
425 | break; |
405 | 426 | ||
406 | case UBXSTATE_LEN1: // 1st message length byte |
427 | case UBXSTATE_LEN1: // 1st message length byte |
407 | RxHdr.Length = (u16)c; // lowbyte first |
428 | RxHdr.Length = (u16)c; // lowbyte first |
408 | cka += c; |
429 | cka += c; |
409 | ckb += cka; |
430 | ckb += cka; |
410 | ubxState = UBXSTATE_LEN2; |
431 | ubxState = UBXSTATE_LEN2; |
411 | break; |
432 | break; |
412 | 433 | ||
413 | case UBXSTATE_LEN2: // 2nd message length byte |
434 | case UBXSTATE_LEN2: // 2nd message length byte |
414 | RxHdr.Length += ((u16)c)<<8; // high byte last |
435 | RxHdr.Length += ((u16)c)<<8; // high byte last |
415 | if (RxHdr.Length >= UBX_MSG_DATA_SIZE) |
436 | if (RxHdr.Length >= UBX_MSG_DATA_SIZE) |
416 | { |
437 | { |
417 | ubxState = UBXSTATE_IDLE; |
438 | ubxState = UBXSTATE_IDLE; |
418 | } |
439 | } |
419 | else |
440 | else |
420 | { |
441 | { |
421 | cka += c; |
442 | cka += c; |
422 | ckb += cka; |
443 | ckb += cka; |
423 | RxBytes = 0; // reset data byte counter |
444 | RxBytes = 0; // reset data byte counter |
424 | ubxState = UBXSTATE_DATA; |
445 | ubxState = UBXSTATE_DATA; |
425 | } |
446 | } |
426 | break; |
447 | break; |
427 | 448 | ||
428 | case UBXSTATE_DATA: // collecting data |
449 | case UBXSTATE_DATA: // collecting data |
429 | if (RxBytes < UBX_MSG_DATA_SIZE) |
450 | if (RxBytes < UBX_MSG_DATA_SIZE) |
430 | { |
451 | { |
431 | RxData[RxBytes++] = c; // copy curent data byte if any space is left |
452 | RxData[RxBytes++] = c; // copy curent data byte if any space is left |
432 | cka += c; |
453 | cka += c; |
433 | ckb += cka; |
454 | ckb += cka; |
434 | if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received |
455 | if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received |
435 | } |
456 | } |
436 | else // rx buffer overrun |
457 | else // rx buffer overrun |
437 | { |
458 | { |
438 | ubxState = UBXSTATE_IDLE; |
459 | ubxState = UBXSTATE_IDLE; |
439 | } |
460 | } |
440 | break; |
461 | break; |
441 | 462 | ||
442 | case UBXSTATE_CKA: |
463 | case UBXSTATE_CKA: |
443 | if (c == cka) ubxState = UBXSTATE_CKB; |
464 | if (c == cka) ubxState = UBXSTATE_CKB; |
444 | else |
465 | else |
445 | { |
466 | { |
446 | ubxState = UBXSTATE_IDLE; |
467 | ubxState = UBXSTATE_IDLE; |
447 | } |
468 | } |
448 | break; |
469 | break; |
449 | 470 | ||
450 | case UBXSTATE_CKB: |
471 | case UBXSTATE_CKB: |
451 | if (c == ckb) |
472 | if (c == ckb) |
452 | { // checksum is ok |
473 | { // checksum is ok |
453 | 474 | ||
454 | switch(RxHdr.Class) |
475 | switch(RxHdr.Class) |
455 | { |
476 | { |
456 | case UBX_CLASS_NAV: |
477 | case UBX_CLASS_NAV: |
457 | switch(RxHdr.Id) |
478 | switch(RxHdr.Id) |
458 | { |
479 | { |
459 | case UBX_ID_POSLLH: // geodetic position |
480 | case UBX_ID_POSLLH: // geodetic position |
460 | memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length); |
481 | memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length); |
461 | UbxPosLlh.Status = NEWDATA; |
482 | UbxPosLlh.Status = NEWDATA; |
462 | break; |
483 | break; |
463 | 484 | ||
464 | case UBX_ID_VELNED: // velocity vector in tangent plane |
485 | case UBX_ID_VELNED: // velocity vector in tangent plane |
465 | memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length); |
486 | memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length); |
466 | UbxVelNed.Status = NEWDATA; |
487 | UbxVelNed.Status = NEWDATA; |
467 | break; |
488 | break; |
468 | 489 | ||
469 | case UBX_ID_SOL: // navigation solution |
490 | case UBX_ID_SOL: // navigation solution |
470 | memcpy((u8*)&UbxSol, RxData, RxHdr.Length); |
491 | memcpy((u8*)&UbxSol, RxData, RxHdr.Length); |
471 | UbxSol.Status = NEWDATA; |
492 | UbxSol.Status = NEWDATA; |
472 | break; |
493 | break; |
473 | 494 | ||
474 | default: |
495 | default: |
475 | break; |
496 | break; |
476 | } // EOF switch(Id) |
497 | } // EOF switch(Id) |
477 | Update_GPSData(); |
498 | Update_GPSData(); |
478 | break; |
499 | break; |
479 | 500 | ||
480 | default: |
501 | default: |
481 | break; |
502 | break; |
482 | } // EOF switch(class) |
503 | } // EOF switch(class) |
483 | 504 | ||
484 | // check generic msg filter |
505 | // check generic msg filter |
485 | if(UbxMsg.Status != NEWDATA) |
506 | if(UbxMsg.Status != NEWDATA) |
486 | { // msg buffer is free |
507 | { // msg buffer is free |
487 | if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask))) |
508 | if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask))) |
488 | { // msg matches to the filter criteria |
509 | { // msg matches to the filter criteria |
489 | UbxMsg.Status = INVALID; |
510 | UbxMsg.Status = INVALID; |
490 | UbxMsg.Hdr.Class = RxHdr.Class; |
511 | UbxMsg.Hdr.Class = RxHdr.Class; |
491 | UbxMsg.Hdr.Id = RxHdr.Id; |
512 | UbxMsg.Hdr.Id = RxHdr.Id; |
492 | UbxMsg.Hdr.Length = RxHdr.Length; |
513 | UbxMsg.Hdr.Length = RxHdr.Length; |
493 | if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE) |
514 | if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE) |
494 | { // copy data block |
515 | { // copy data block |
495 | memcpy(UbxMsg.Data, RxData, RxHdr.Length); |
516 | memcpy(UbxMsg.Data, RxData, RxHdr.Length); |
496 | UbxMsg.Status = NEWDATA; |
517 | UbxMsg.Status = NEWDATA; |
497 | } |
518 | } |
498 | } // EOF filter matches |
519 | } // EOF filter matches |
499 | } // EOF != INVALID |
520 | } // EOF != INVALID |
500 | }// EOF crc ok |
521 | }// EOF crc ok |
501 | ubxState = UBXSTATE_IDLE; // ready to parse new data |
522 | ubxState = UBXSTATE_IDLE; // ready to parse new data |
502 | break; |
523 | break; |
503 | 524 | ||
504 | default: // unknown ubx state |
525 | default: // unknown ubx state |
505 | ubxState = UBXSTATE_IDLE; |
526 | ubxState = UBXSTATE_IDLE; |
506 | break; |
527 | break; |
507 | 528 | ||
508 | } |
529 | } |
509 | } |
530 | } |
510 | 531 | ||
511 | u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len) |
532 | u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len) |
512 | { |
533 | { |
513 | u16 i; |
534 | u16 i; |
514 | u8 cka = 0, ckb = 0; |
535 | u8 cka = 0, ckb = 0; |
515 | // check if buffer is available |
536 | // check if buffer is available |
516 | if(pBuff->Locked == TRUE) return(0); |
537 | if(pBuff->Locked == TRUE) return(0); |
517 | // check if buffer size is sufficient |
538 | // check if buffer size is sufficient |
518 | if(pBuff->Size < 8 + Len) return(0); |
539 | if(pBuff->Size < 8 + Len) return(0); |
519 | // lock the buffer |
540 | // lock the buffer |
520 | pBuff->Locked = TRUE; |
541 | pBuff->Locked = TRUE; |
521 | // start at begin |
542 | // start at begin |
522 | pBuff->Position = 0; |
543 | pBuff->Position = 0; |
523 | pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR; |
544 | pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR; |
524 | pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR; |
545 | pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR; |
525 | for(i=0;i<Len;i++) |
546 | for(i=0;i<Len;i++) |
526 | { |
547 | { |
527 | pBuff->pData[pBuff->Position++] = pData[i]; |
548 | pBuff->pData[pBuff->Position++] = pData[i]; |
528 | } |
549 | } |
529 | // calculate checksum |
550 | // calculate checksum |
530 | for(i=2;i<pBuff->Position;i++) |
551 | for(i=2;i<pBuff->Position;i++) |
531 | { |
552 | { |
532 | cka += pBuff->pData[i]; |
553 | cka += pBuff->pData[i]; |
533 | ckb += cka; |
554 | ckb += cka; |
534 | } |
555 | } |
535 | pBuff->pData[pBuff->Position++] = cka; |
556 | pBuff->pData[pBuff->Position++] = cka; |
536 | pBuff->pData[pBuff->Position++] = ckb; |
557 | pBuff->pData[pBuff->Position++] = ckb; |
537 | pBuff->DataBytes = pBuff->Position; |
558 | pBuff->DataBytes = pBuff->Position; |
538 | pBuff->Position = 0; // reset buffer position for transmision |
559 | pBuff->Position = 0; // reset buffer position for transmision |
539 | return(1); |
560 | return(1); |
540 | } |
561 | } |
541 | /* |
562 | /* |
542 | switch(ubxclass) |
563 | switch(ubxclass) |
543 | { |
564 | { |
544 | case UBX_CLASS_NAV: |
565 | case UBX_CLASS_NAV: |
545 | switch(ubxid) |
566 | switch(ubxid) |
546 | { |
567 | { |
547 | case UBX_ID_POSLLH: // geodetic position |
568 | case UBX_ID_POSLLH: // geodetic position |
548 | ubxSp = (u8 *)&UbxPosLlh; // data start pointer |
569 | ubxSp = (u8 *)&UbxPosLlh; // data start pointer |
549 | ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer |
570 | ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer |
550 | ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer |
571 | ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer |
551 | break; |
572 | break; |
552 | 573 | ||
553 | case UBX_ID_SOL: // navigation solution |
574 | case UBX_ID_SOL: // navigation solution |
554 | ubxSp = (u8 *)&UbxSol; // data start pointer |
575 | ubxSp = (u8 *)&UbxSol; // data start pointer |
555 | ubxEp = (u8 *)(&UbxSol + 1); // data end pointer |
576 | ubxEp = (u8 *)(&UbxSol + 1); // data end pointer |
556 | ubxStP = (u8 *)&UbxSol.Status; // status pointer |
577 | ubxStP = (u8 *)&UbxSol.Status; // status pointer |
557 | break; |
578 | break; |
558 | 579 | ||
559 | case UBX_ID_VELNED: // velocity vector in tangent plane |
580 | case UBX_ID_VELNED: // velocity vector in tangent plane |
560 | ubxSp = (u8 *)&UbxVelNed; // data start pointer |
581 | ubxSp = (u8 *)&UbxVelNed; // data start pointer |
561 | ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer |
582 | ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer |
562 | ubxStP = (u8 *)&UbxVelNed.Status; // status pointer |
583 | ubxStP = (u8 *)&UbxVelNed.Status; // status pointer |
563 | break; |
584 | break; |
564 | 585 | ||
565 | default: // unsupported identifier |
586 | default: // unsupported identifier |
566 | ubxState = UBXSTATE_IDLE; |
587 | ubxState = UBXSTATE_IDLE; |
567 | return; |
588 | return; |
568 | } |
589 | } |
569 | break; |
590 | break; |
570 | 591 | ||
571 | default: // other classes |
592 | default: // other classes |
572 | if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE; |
593 | if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE; |
573 | else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask))) |
594 | else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask))) |
574 | { // buffer is free and message matches to filter criteria |
595 | { // buffer is free and message matches to filter criteria |
575 | UbxMsg.Status = INVALID; |
596 | UbxMsg.Status = INVALID; |
576 | UbxMsg.Hdr.Class = ubxclass; |
597 | UbxMsg.Hdr.Class = ubxclass; |
577 | UbxMsg.Hdr.Id = ubxid; |
598 | UbxMsg.Hdr.Id = ubxid; |
578 | UbxMsg.Hdr.Length = msglen; |
599 | UbxMsg.Hdr.Length = msglen; |
579 | ubxSp = (u8 *)&(UbxMsg.Data); // data start pointer |
600 | ubxSp = (u8 *)&(UbxMsg.Data); // data start pointer |
580 | ubxEp = (u8 *)(&UbxMsg + 1); // data end pointer |
601 | ubxEp = (u8 *)(&UbxMsg + 1); // data end pointer |
581 | ubxStP = (u8 *)&UbxMsg.Status; // status pointer |
602 | ubxStP = (u8 *)&UbxMsg.Status; // status pointer |
582 | } |
603 | } |
583 | else ubxState = UBXSTATE_IDLE; |
604 | else ubxState = UBXSTATE_IDLE; |
584 | break; |
605 | break; |
585 | } |
606 | } |
586 | */ |
607 | */ |
587 | 608 | ||
588 | 609 |