Rev 281 | Rev 360 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 281 | Rev 322 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "uart1.h" |
59 | #include "uart1.h" |
60 | #include "ubx.h" |
60 | #include "ubx.h" |
61 | #include "led.h" |
61 | #include "led.h" |
62 | #include "timer1.h" |
62 | #include "timer1.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 15 |
85 | #define LEAP_SECONDS_FROM_1980 15 |
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 | 283 | ||
284 | // if a new set of ubx messages was collected |
284 | // if a new set of ubx messages was collected |
285 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
285 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
286 | { // and the itow is equal (same time base) |
286 | { // and the itow is equal (same time base) |
287 | if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow)) |
287 | if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow)) |
288 | { |
288 | { |
289 | UBX_Timeout = SetDelay(UBX_TIMEOUT); |
289 | UBX_Timeout = SetDelay(UBX_TIMEOUT); |
290 | DebugOut.Analog[9]++; |
290 | DebugOut.Analog[9]++; |
291 | // update GPS data only if the status is INVALID or PROCESSED |
291 | // update GPS data only if the status is INVALID or PROCESSED |
292 | if(GPSData.Status != NEWDATA) |
292 | if(GPSData.Status != NEWDATA) |
293 | { // wait for new data at all neccesary ubx messages |
293 | { // wait for new data at all neccesary ubx messages |
294 | GPSData.Status = INVALID; |
294 | GPSData.Status = INVALID; |
295 | // update message cycle time |
295 | // update message cycle time |
296 | GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow); |
296 | GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow); |
297 | last_itow = UbxSol.itow; // update last itow |
297 | last_itow = UbxSol.itow; // update last itow |
298 | // DebugOut.Analog[16] = GPSData.MsgCycleTime; |
298 | // DebugOut.Analog[16] = GPSData.MsgCycleTime; |
299 | // NAV SOL |
299 | // NAV SOL |
300 | GPSData.Flags = UbxSol.Flags; |
300 | GPSData.Flags = (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits |
301 | GPSData.NumOfSats = UbxSol.numSV; |
301 | GPSData.NumOfSats = UbxSol.numSV; |
302 | GPSData.SatFix = UbxSol.GPSfix; |
302 | GPSData.SatFix = UbxSol.GPSfix; |
303 | GPSData.Position_Accuracy = UbxSol.PAcc; |
303 | GPSData.Position_Accuracy = UbxSol.PAcc; |
304 | GPSData.Speed_Accuracy = UbxSol.SAcc; |
304 | GPSData.Speed_Accuracy = UbxSol.SAcc; |
305 | SetGPSTime(&SystemTime); // update system time |
305 | SetGPSTime(&SystemTime); // update system time |
306 | // NAV POSLLH |
306 | // NAV POSLLH |
307 | GPSData.Position.Status = INVALID; |
307 | GPSData.Position.Status = INVALID; |
308 | GPSData.Position.Longitude = UbxPosLlh.LON; |
308 | GPSData.Position.Longitude = UbxPosLlh.LON; |
309 | GPSData.Position.Latitude = UbxPosLlh.LAT; |
309 | GPSData.Position.Latitude = UbxPosLlh.LAT; |
310 | GPSData.Position.Altitude = UbxPosLlh.HMSL; |
310 | GPSData.Position.Altitude = UbxPosLlh.HMSL; |
311 | GPSData.Position.Status = NEWDATA; |
311 | GPSData.Position.Status = NEWDATA; |
312 | // NAV VELNED |
312 | // NAV VELNED |
313 | GPSData.Speed_East = UbxVelNed.VEL_E; |
313 | GPSData.Speed_East = UbxVelNed.VEL_E; |
314 | GPSData.Speed_North = UbxVelNed.VEL_N; |
314 | GPSData.Speed_North = UbxVelNed.VEL_N; |
315 | GPSData.Speed_Top = -UbxVelNed.VEL_D; |
315 | GPSData.Speed_Top = -UbxVelNed.VEL_D; |
316 | GPSData.Speed_Ground = UbxVelNed.GSpeed; |
316 | GPSData.Speed_Ground = UbxVelNed.GSpeed; |
317 | GPSData.Heading = UbxVelNed.Heading; |
317 | GPSData.Heading = UbxVelNed.Heading; |
318 | 318 | ||
319 | GPSData.Status = NEWDATA; // new data available |
319 | GPSData.Status = NEWDATA; // new data available |
320 | } // EOF if(GPSData.Status != NEWDATA) |
320 | } // EOF if(GPSData.Status != NEWDATA) |
321 | // set state to collect new data |
321 | // set state to collect new data |
322 | UbxSol.Status = PROCESSED; // ready for new data |
322 | UbxSol.Status = PROCESSED; // ready for new data |
323 | UbxPosLlh.Status = PROCESSED; // ready for new data |
323 | UbxPosLlh.Status = PROCESSED; // ready for new data |
324 | UbxVelNed.Status = PROCESSED; // ready for new data |
324 | UbxVelNed.Status = PROCESSED; // ready for new data |
325 | } // EOF all itow are equal |
325 | } // EOF all itow are equal |
326 | } // EOF all ubx messages received |
326 | } // EOF all ubx messages received |
327 | } |
327 | } |
328 | 328 | ||
329 | 329 | ||
330 | /********************************************************/ |
330 | /********************************************************/ |
331 | /* UBX Parser */ |
331 | /* UBX Parser */ |
332 | /********************************************************/ |
332 | /********************************************************/ |
333 | void UBX_RxParser(u8 c) |
333 | void UBX_RxParser(u8 c) |
334 | { |
334 | { |
335 | static ubxState_t ubxState = UBXSTATE_IDLE; |
335 | static ubxState_t ubxState = UBXSTATE_IDLE; |
336 | static ubxmsghdr_t RxHdr; |
336 | static ubxmsghdr_t RxHdr; |
337 | static u8 RxData[UBX_MSG_DATA_SIZE]; |
337 | static u8 RxData[UBX_MSG_DATA_SIZE]; |
338 | static u16 RxBytes = 0; |
338 | static u16 RxBytes = 0; |
339 | static u8 cka, ckb; |
339 | static u8 cka, ckb; |
340 | 340 | ||
341 | 341 | ||
342 | //state machine |
342 | //state machine |
343 | switch (ubxState) // ubx message parser |
343 | switch (ubxState) // ubx message parser |
344 | { |
344 | { |
345 | case UBXSTATE_IDLE: // check 1st sync byte |
345 | case UBXSTATE_IDLE: // check 1st sync byte |
346 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
346 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
347 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
347 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
348 | break; |
348 | break; |
349 | 349 | ||
350 | case UBXSTATE_SYNC1: // check 2nd sync byte |
350 | case UBXSTATE_SYNC1: // check 2nd sync byte |
351 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
351 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
352 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
352 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
353 | break; |
353 | break; |
354 | 354 | ||
355 | case UBXSTATE_SYNC2: // check msg class to be NAV |
355 | case UBXSTATE_SYNC2: // check msg class to be NAV |
356 | RxHdr.Class = c; |
356 | RxHdr.Class = c; |
357 | ubxState = UBXSTATE_CLASS; |
357 | ubxState = UBXSTATE_CLASS; |
358 | break; |
358 | break; |
359 | 359 | ||
360 | case UBXSTATE_CLASS: // check message identifier |
360 | case UBXSTATE_CLASS: // check message identifier |
361 | RxHdr.Id = c; |
361 | RxHdr.Id = c; |
362 | ubxState = UBXSTATE_LEN1; |
362 | ubxState = UBXSTATE_LEN1; |
363 | cka = RxHdr.Class + RxHdr.Id; |
363 | cka = RxHdr.Class + RxHdr.Id; |
364 | ckb = RxHdr.Class + cka; |
364 | ckb = RxHdr.Class + cka; |
365 | break; |
365 | break; |
366 | 366 | ||
367 | case UBXSTATE_LEN1: // 1st message length byte |
367 | case UBXSTATE_LEN1: // 1st message length byte |
368 | RxHdr.Length = (u16)c; // lowbyte first |
368 | RxHdr.Length = (u16)c; // lowbyte first |
369 | cka += c; |
369 | cka += c; |
370 | ckb += cka; |
370 | ckb += cka; |
371 | ubxState = UBXSTATE_LEN2; |
371 | ubxState = UBXSTATE_LEN2; |
372 | break; |
372 | break; |
373 | 373 | ||
374 | case UBXSTATE_LEN2: // 2nd message length byte |
374 | case UBXSTATE_LEN2: // 2nd message length byte |
375 | RxHdr.Length += ((u16)c)<<8; // high byte last |
375 | RxHdr.Length += ((u16)c)<<8; // high byte last |
376 | if (RxHdr.Length >= UBX_MSG_DATA_SIZE) |
376 | if (RxHdr.Length >= UBX_MSG_DATA_SIZE) |
377 | { |
377 | { |
378 | ubxState = UBXSTATE_IDLE; |
378 | ubxState = UBXSTATE_IDLE; |
379 | } |
379 | } |
380 | else |
380 | else |
381 | { |
381 | { |
382 | cka += c; |
382 | cka += c; |
383 | ckb += cka; |
383 | ckb += cka; |
384 | RxBytes = 0; // reset data byte counter |
384 | RxBytes = 0; // reset data byte counter |
385 | ubxState = UBXSTATE_DATA; |
385 | ubxState = UBXSTATE_DATA; |
386 | } |
386 | } |
387 | break; |
387 | break; |
388 | 388 | ||
389 | case UBXSTATE_DATA: // collecting data |
389 | case UBXSTATE_DATA: // collecting data |
390 | if (RxBytes < UBX_MSG_DATA_SIZE) |
390 | if (RxBytes < UBX_MSG_DATA_SIZE) |
391 | { |
391 | { |
392 | RxData[RxBytes++] = c; // copy curent data byte if any space is left |
392 | RxData[RxBytes++] = c; // copy curent data byte if any space is left |
393 | cka += c; |
393 | cka += c; |
394 | ckb += cka; |
394 | ckb += cka; |
395 | if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received |
395 | if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received |
396 | } |
396 | } |
397 | else // rx buffer overrun |
397 | else // rx buffer overrun |
398 | { |
398 | { |
399 | ubxState = UBXSTATE_IDLE; |
399 | ubxState = UBXSTATE_IDLE; |
400 | } |
400 | } |
401 | break; |
401 | break; |
402 | 402 | ||
403 | case UBXSTATE_CKA: |
403 | case UBXSTATE_CKA: |
404 | if (c == cka) ubxState = UBXSTATE_CKB; |
404 | if (c == cka) ubxState = UBXSTATE_CKB; |
405 | else |
405 | else |
406 | { |
406 | { |
407 | ubxState = UBXSTATE_IDLE; |
407 | ubxState = UBXSTATE_IDLE; |
408 | } |
408 | } |
409 | break; |
409 | break; |
410 | 410 | ||
411 | case UBXSTATE_CKB: |
411 | case UBXSTATE_CKB: |
412 | if (c == ckb) |
412 | if (c == ckb) |
413 | { // checksum is ok |
413 | { // checksum is ok |
414 | 414 | ||
415 | switch(RxHdr.Class) |
415 | switch(RxHdr.Class) |
416 | { |
416 | { |
417 | case UBX_CLASS_NAV: |
417 | case UBX_CLASS_NAV: |
418 | switch(RxHdr.Id) |
418 | switch(RxHdr.Id) |
419 | { |
419 | { |
420 | case UBX_ID_POSLLH: // geodetic position |
420 | case UBX_ID_POSLLH: // geodetic position |
421 | memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length); |
421 | memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length); |
422 | UbxPosLlh.Status = NEWDATA; |
422 | UbxPosLlh.Status = NEWDATA; |
423 | break; |
423 | break; |
424 | 424 | ||
425 | case UBX_ID_VELNED: // velocity vector in tangent plane |
425 | case UBX_ID_VELNED: // velocity vector in tangent plane |
426 | memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length); |
426 | memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length); |
427 | UbxVelNed.Status = NEWDATA; |
427 | UbxVelNed.Status = NEWDATA; |
428 | break; |
428 | break; |
429 | 429 | ||
430 | case UBX_ID_SOL: // navigation solution |
430 | case UBX_ID_SOL: // navigation solution |
431 | memcpy((u8*)&UbxSol, RxData, RxHdr.Length); |
431 | memcpy((u8*)&UbxSol, RxData, RxHdr.Length); |
432 | UbxSol.Status = NEWDATA; |
432 | UbxSol.Status = NEWDATA; |
433 | break; |
433 | break; |
434 | 434 | ||
435 | default: |
435 | default: |
436 | break; |
436 | break; |
437 | } // EOF switch(Id) |
437 | } // EOF switch(Id) |
438 | Update_GPSData(); |
438 | Update_GPSData(); |
439 | break; |
439 | break; |
440 | 440 | ||
441 | default: |
441 | default: |
442 | break; |
442 | break; |
443 | } // EOF switch(class) |
443 | } // EOF switch(class) |
444 | 444 | ||
445 | // check generic msg filter |
445 | // check generic msg filter |
446 | if(UbxMsg.Status != NEWDATA) |
446 | if(UbxMsg.Status != NEWDATA) |
447 | { // msg buffer is free |
447 | { // msg buffer is free |
448 | if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask))) |
448 | if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask))) |
449 | { // msg matches to the filter criteria |
449 | { // msg matches to the filter criteria |
450 | UbxMsg.Status = INVALID; |
450 | UbxMsg.Status = INVALID; |
451 | UbxMsg.Hdr.Class = RxHdr.Class; |
451 | UbxMsg.Hdr.Class = RxHdr.Class; |
452 | UbxMsg.Hdr.Id = RxHdr.Id; |
452 | UbxMsg.Hdr.Id = RxHdr.Id; |
453 | UbxMsg.Hdr.Length = RxHdr.Length; |
453 | UbxMsg.Hdr.Length = RxHdr.Length; |
454 | if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE) |
454 | if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE) |
455 | { // copy data block |
455 | { // copy data block |
456 | memcpy(UbxMsg.Data, RxData, RxHdr.Length); |
456 | memcpy(UbxMsg.Data, RxData, RxHdr.Length); |
457 | UbxMsg.Status = NEWDATA; |
457 | UbxMsg.Status = NEWDATA; |
458 | } |
458 | } |
459 | } // EOF filter matches |
459 | } // EOF filter matches |
460 | } // EOF != INVALID |
460 | } // EOF != INVALID |
461 | }// EOF crc ok |
461 | }// EOF crc ok |
462 | ubxState = UBXSTATE_IDLE; // ready to parse new data |
462 | ubxState = UBXSTATE_IDLE; // ready to parse new data |
463 | break; |
463 | break; |
464 | 464 | ||
465 | default: // unknown ubx state |
465 | default: // unknown ubx state |
466 | ubxState = UBXSTATE_IDLE; |
466 | ubxState = UBXSTATE_IDLE; |
467 | break; |
467 | break; |
468 | 468 | ||
469 | } |
469 | } |
470 | } |
470 | } |
471 | 471 | ||
472 | u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len) |
472 | u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len) |
473 | { |
473 | { |
474 | u16 i; |
474 | u16 i; |
475 | u8 cka = 0, ckb = 0; |
475 | u8 cka = 0, ckb = 0; |
476 | // check if buffer is available |
476 | // check if buffer is available |
477 | if(pBuff->Locked == TRUE) return(0); |
477 | if(pBuff->Locked == TRUE) return(0); |
478 | // check if buffer size is sufficient |
478 | // check if buffer size is sufficient |
479 | if(pBuff->Size < 8 + Len) return(0); |
479 | if(pBuff->Size < 8 + Len) return(0); |
480 | // lock the buffer |
480 | // lock the buffer |
481 | pBuff->Locked = TRUE; |
481 | pBuff->Locked = TRUE; |
482 | // start at begin |
482 | // start at begin |
483 | pBuff->Position = 0; |
483 | pBuff->Position = 0; |
484 | pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR; |
484 | pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR; |
485 | pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR; |
485 | pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR; |
486 | for(i=0;i<Len;i++) |
486 | for(i=0;i<Len;i++) |
487 | { |
487 | { |
488 | pBuff->pData[pBuff->Position++] = pData[i]; |
488 | pBuff->pData[pBuff->Position++] = pData[i]; |
489 | } |
489 | } |
490 | // calculate checksum |
490 | // calculate checksum |
491 | for(i=2;i<pBuff->Position;i++) |
491 | for(i=2;i<pBuff->Position;i++) |
492 | { |
492 | { |
493 | cka += pBuff->pData[i]; |
493 | cka += pBuff->pData[i]; |
494 | ckb += cka; |
494 | ckb += cka; |
495 | } |
495 | } |
496 | pBuff->pData[pBuff->Position++] = cka; |
496 | pBuff->pData[pBuff->Position++] = cka; |
497 | pBuff->pData[pBuff->Position++] = ckb; |
497 | pBuff->pData[pBuff->Position++] = ckb; |
498 | pBuff->DataBytes = pBuff->Position; |
498 | pBuff->DataBytes = pBuff->Position; |
499 | pBuff->Position = 0; // reset buffer position for transmision |
499 | pBuff->Position = 0; // reset buffer position for transmision |
500 | return(1); |
500 | return(1); |
501 | } |
501 | } |
502 | /* |
502 | /* |
503 | switch(ubxclass) |
503 | switch(ubxclass) |
504 | { |
504 | { |
505 | case UBX_CLASS_NAV: |
505 | case UBX_CLASS_NAV: |
506 | switch(ubxid) |
506 | switch(ubxid) |
507 | { |
507 | { |
508 | case UBX_ID_POSLLH: // geodetic position |
508 | case UBX_ID_POSLLH: // geodetic position |
509 | ubxSp = (u8 *)&UbxPosLlh; // data start pointer |
509 | ubxSp = (u8 *)&UbxPosLlh; // data start pointer |
510 | ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer |
510 | ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer |
511 | ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer |
511 | ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer |
512 | break; |
512 | break; |
513 | |
513 | |
514 | case UBX_ID_SOL: // navigation solution |
514 | case UBX_ID_SOL: // navigation solution |
515 | ubxSp = (u8 *)&UbxSol; // data start pointer |
515 | ubxSp = (u8 *)&UbxSol; // data start pointer |
516 | ubxEp = (u8 *)(&UbxSol + 1); // data end pointer |
516 | ubxEp = (u8 *)(&UbxSol + 1); // data end pointer |
517 | ubxStP = (u8 *)&UbxSol.Status; // status pointer |
517 | ubxStP = (u8 *)&UbxSol.Status; // status pointer |
518 | break; |
518 | break; |
519 | |
519 | |
520 | case UBX_ID_VELNED: // velocity vector in tangent plane |
520 | case UBX_ID_VELNED: // velocity vector in tangent plane |
521 | ubxSp = (u8 *)&UbxVelNed; // data start pointer |
521 | ubxSp = (u8 *)&UbxVelNed; // data start pointer |
522 | ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer |
522 | ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer |
523 | ubxStP = (u8 *)&UbxVelNed.Status; // status pointer |
523 | ubxStP = (u8 *)&UbxVelNed.Status; // status pointer |
524 | break; |
524 | break; |
525 | |
525 | |
526 | default: // unsupported identifier |
526 | default: // unsupported identifier |
527 | ubxState = UBXSTATE_IDLE; |
527 | ubxState = UBXSTATE_IDLE; |
528 | return; |
528 | return; |
529 | } |
529 | } |
530 | break; |
530 | break; |
531 | 531 | ||
532 | default: // other classes |
532 | default: // other classes |
533 | if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE; |
533 | if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE; |
534 | else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask))) |
534 | else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask))) |
535 | { // buffer is free and message matches to filter criteria |
535 | { // buffer is free and message matches to filter criteria |
536 | UbxMsg.Status = INVALID; |
536 | UbxMsg.Status = INVALID; |
537 | UbxMsg.Hdr.Class = ubxclass; |
537 | UbxMsg.Hdr.Class = ubxclass; |
538 | UbxMsg.Hdr.Id = ubxid; |
538 | UbxMsg.Hdr.Id = ubxid; |
539 | UbxMsg.Hdr.Length = msglen; |
539 | UbxMsg.Hdr.Length = msglen; |
540 | ubxSp = (u8 *)&(UbxMsg.Data); // data start pointer |
540 | ubxSp = (u8 *)&(UbxMsg.Data); // data start pointer |
541 | ubxEp = (u8 *)(&UbxMsg + 1); // data end pointer |
541 | ubxEp = (u8 *)(&UbxMsg + 1); // data end pointer |
542 | ubxStP = (u8 *)&UbxMsg.Status; // status pointer |
542 | ubxStP = (u8 *)&UbxMsg.Status; // status pointer |
543 | } |
543 | } |
544 | else ubxState = UBXSTATE_IDLE; |
544 | else ubxState = UBXSTATE_IDLE; |
545 | break; |
545 | break; |
546 | } |
546 | } |
547 | */ |
547 | */ |
548 | 548 | ||
549 | 549 |