Rev 227 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 227 | Rev 389 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CanGPS. |
2 | * CanGPS. |
| 3 | * |
3 | * |
| 4 | * Messages: <byte0><byte1>...<byte7> |
4 | * Messages: <byte0><byte1>...<byte7> |
| 5 | * $GPGGA: <GGA_MSG><UTC><Latitude><Latitude dec.><N/S><Longitude><Longitude dec.><E/W> |
5 | * $GPGGA: <GGA_MSG><UTC><Latitude><Latitude dec.><N/S><Longitude><Longitude dec.><E/W> |
| 6 | * $GPVTG: <VTG_MSG><Course true><Course dec. true><Course magnetic><Course dec. magnetic><Speed km/h><Speed dec. km/h> |
6 | * $GPVTG: <VTG_MSG><Course true><Course dec. true><Course magnetic><Course dec. magnetic><Speed km/h><Speed dec. km/h> |
| 7 | * $GPZDA: <ZDA_MSG><hh><mm><ss><day><month><year> |
7 | * $GPZDA: <ZDA_MSG><hh><mm><ss><day><month><year> |
| 8 | * |
8 | * |
| 9 | * @date 2001-01-09 |
9 | * @date 2001-01-09 |
| 10 | * @author Erik Larsson |
10 | * @author Erik Larsson |
| 11 | */ |
11 | */ |
| 12 | 12 | ||
| 13 | #define DEBUG 0 /* Prints debug messages to uart, only use this function if output is not connected to GPS */ |
13 | #define DEBUG 0 /* Prints debug messages to uart, only use this function if output is not connected to GPS */ |
| 14 | 14 | ||
| 15 | /*----------------------------------------------------------------------------- |
15 | /*----------------------------------------------------------------------------- |
| 16 | * Includes |
16 | * Includes |
| 17 | *---------------------------------------------------------------------------*/ |
17 | *---------------------------------------------------------------------------*/ |
| 18 | /* system files */ |
18 | /* system files */ |
| 19 | #include <avr/io.h> |
19 | #include <avr/io.h> |
| 20 | #include <avr/interrupt.h> |
20 | #include <avr/interrupt.h> |
| 21 | #include <avr/wdt.h> |
21 | #include <avr/wdt.h> |
| 22 | #include <stdio.h> |
22 | #include <stdio.h> |
| 23 | #include <string.h> |
23 | #include <string.h> |
| 24 | /* lib files */ |
24 | /* lib files */ |
| 25 | #include <can.h> |
25 | #include <can.h> |
| 26 | #include <serial.h> |
26 | #include <serial.h> |
| 27 | #include <uart.h> |
27 | #include <uart.h> |
| 28 | #include <timebase.h> |
28 | #include <timebase.h> |
| 29 | 29 | ||
| 30 | /* defines */ |
30 | /* defines */ |
| 31 | #define GPS_CMD_GET 0x1400 |
31 | #define GPS_CMD_GET 0x1400 |
| 32 | #define GPS_CMD_SEND 0x1401 |
32 | #define GPS_CMD_SEND 0x1401 |
| 33 | #define STATUS_SEND_PERIOD 500 /* milliseconds */ |
33 | #define STATUS_SEND_PERIOD 500 /* milliseconds */ |
| 34 | 34 | ||
| 35 | #define GGA_MSG 0x01 |
35 | #define GGA_MSG 0x01 |
| 36 | #define VTG_MSG 0x02 |
36 | #define VTG_MSG 0x02 |
| 37 | #define ZDA_MSG 0x03 |
37 | #define ZDA_MSG 0x03 |
| 38 | #define GGA_MSG_BYTES 8 |
38 | #define GGA_MSG_BYTES 8 |
| 39 | #define VTG_MSG_BYTES 7 |
39 | #define VTG_MSG_BYTES 7 |
| 40 | #define ZDA_MSG_BYTES 7 |
40 | #define ZDA_MSG_BYTES 7 |
| 41 | 41 | ||
| 42 | #define TRUE 1 |
42 | #define TRUE 1 |
| 43 | #define FALSE !TRUE |
43 | #define FALSE !TRUE |
| 44 | #define CR 0x0D |
44 | #define CR 0x0D |
| 45 | #define LF 0x0A |
45 | #define LF 0x0A |
| 46 | #define GPS_MSG_DELIMITER "," |
46 | #define GPS_MSG_DELIMITER "," |
| 47 | 47 | ||
| 48 | /*------------------------------------------------------------------------- |
48 | /*------------------------------------------------------------------------- |
| 49 | * Global variables |
49 | * Global variables |
| 50 | * -----------------------------------------------------------------------*/ |
50 | * -----------------------------------------------------------------------*/ |
| 51 | 51 | ||
| 52 | /*---------------------------------------------------------------------------- |
52 | /*---------------------------------------------------------------------------- |
| 53 | * Functions |
53 | * Functions |
| 54 | * -------------------------------------------------------------------------*/ |
54 | * -------------------------------------------------------------------------*/ |
| 55 | 55 | ||
| 56 | /*---------------------------------------------------------------------------- |
56 | /*---------------------------------------------------------------------------- |
| 57 | * Interrupt routines |
57 | * Interrupt routines |
| 58 | * -------------------------------------------------------------------------*/ |
58 | * -------------------------------------------------------------------------*/ |
| 59 | ISR( PCINT2_vect ) |
59 | ISR( PCINT2_vect ) |
| 60 | { |
60 | { |
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | /*----------------------------------------------------------------------------- |
63 | /*----------------------------------------------------------------------------- |
| 64 | * Main Program |
64 | * Main Program |
| 65 | *---------------------------------------------------------------------------*/ |
65 | *---------------------------------------------------------------------------*/ |
| 66 | int main(void) { |
66 | int main(void) { |
| 67 | uint32_t timeStamp = 0, timeStampTurn = 0; |
67 | uint32_t timeStamp = 0, timeStampTurn = 0; |
| 68 | uint8_t buffering = FALSE, gpsMsg_received = FALSE, rxGps_element; |
68 | uint8_t buffering = FALSE, gpsMsg_received = FALSE, rxGps_element; |
| 69 | char buffer[100], rxGps; |
69 | char buffer[100], rxGps; |
| 70 | /* GPGGA, Global positioning system fix data */ |
70 | /* GPGGA, Global positioning system fix data */ |
| 71 | char gga_utc[7], gga_latitude[9], gga_latitude_NS, gga_longitude[10], gga_longitude_EW; |
71 | char gga_utc[7], gga_latitude[9], gga_latitude_NS, gga_longitude[10], gga_longitude_EW; |
| 72 | /* GPVTG, Course over ground and speed */ |
72 | /* GPVTG, Course over ground and speed */ |
| 73 | char vtg_course_true[6], vtg_course_magnetic[6], vtg_speed_kmh[6]; |
73 | char vtg_course_true[6], vtg_course_magnetic[6], vtg_speed_kmh[6]; |
| 74 | /* GPZDA, Time & Date */ |
74 | /* GPZDA, Time & Date */ |
| 75 | char zda_time[7], zda_day[3], zda_month[3], zda_year[5]; |
75 | char zda_time[7], zda_day[3], zda_month[3], zda_year[5]; |
| 76 | 76 | ||
| 77 | char *pch; |
77 | char *pch; |
| 78 | 78 | ||
| 79 | Timebase_Init(); |
79 | Timebase_Init(); |
| 80 | Serial_Init(); |
80 | Serial_Init(); |
| 81 | 81 | ||
| 82 | sei(); |
82 | sei(); |
| 83 | #if DEBUG |
83 | #if DEBUG |
| 84 | printf("\n------------------------------------------------------------\n"); |
84 | printf("\n------------------------------------------------------------\n"); |
| 85 | printf( " CAN: GPS\n"); |
85 | printf( " CAN: GPS\n"); |
| 86 | printf( "------------------------------------------------------------\n"); |
86 | printf( "------------------------------------------------------------\n"); |
| 87 | 87 | ||
| 88 | printf("CanInit..."); |
88 | printf("CanInit..."); |
| 89 | if (Can_Init() != CAN_OK) { |
89 | if (Can_Init() != CAN_OK) { |
| 90 | printf("FAILED!\n"); |
90 | printf("FAILED!\n"); |
| 91 | }else{ |
91 | }else{ |
| 92 | printf("OK!\n"); |
92 | printf("OK!\n"); |
| 93 | } |
93 | } |
| 94 | #else |
94 | #else |
| 95 | Can_Init(); |
95 | Can_Init(); |
| 96 | #endif |
96 | #endif |
| 97 | 97 | ||
| 98 | Can_Message_t txMsg; |
98 | Can_Message_t txMsg; |
| 99 | Can_Message_t rxMsg; |
99 | Can_Message_t rxMsg; |
| 100 | txMsg.Id = GPS_CMD_SEND; |
100 | txMsg.Id = GPS_CMD_SEND; |
| 101 | txMsg.RemoteFlag = 0; |
101 | txMsg.RemoteFlag = 0; |
| 102 | txMsg.ExtendedFlag = 1; |
102 | txMsg.ExtendedFlag = 1; |
| 103 | txMsg.DataLength = 3; |
103 | txMsg.DataLength = 3; |
| 104 | 104 | ||
| 105 | /* main loop */ |
105 | /* main loop */ |
| 106 | while (1) { |
106 | while (1) { |
| 107 | /* service the CAN routines */ |
107 | /* service the CAN routines */ |
| 108 | Can_Service(); |
108 | Can_Service(); |
| 109 | 109 | ||
| 110 | /* check if any messages have been received */ |
110 | /* check if any messages have been received */ |
| 111 | while (Can_Receive(&rxMsg) == CAN_OK) { |
111 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 112 | /* This node that control a servo is adressed */ |
112 | /* This node that control a servo is adressed */ |
| 113 | if( rxMsg.Id == GPS_CMD_GET ){ |
113 | if( rxMsg.Id == GPS_CMD_GET ){ |
| 114 | 114 | ||
| 115 | } |
115 | } |
| 116 | } |
116 | } |
| 117 | 117 | ||
| 118 | if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){ |
118 | if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){ |
| 119 | timeStamp = Timebase_CurrentTime(); |
119 | timeStamp = Timebase_CurrentTime(); |
| 120 | /* Send blinds status to CAN */ |
120 | /* Send blinds status to CAN */ |
| 121 | 121 | ||
| 122 | } |
122 | } |
| 123 | 123 | ||
| 124 | /* Get data from GPS */ |
124 | /* Get data from GPS */ |
| 125 | rxGps = uart_getc(); |
125 | rxGps = uart_getc(); |
| 126 | while( rxGps != UART_NO_DATA ){ |
126 | while( rxGps != UART_NO_DATA ){ |
| 127 | /* '$' indicates start of message, start buffering */ |
127 | /* '$' indicates start of message, start buffering */ |
| 128 | if( rxGps == '$' ){ |
128 | if( rxGps == '$' ){ |
| 129 | buffering = TRUE; |
129 | buffering = TRUE; |
| 130 | rxGps_element = 0; |
130 | rxGps_element = 0; |
| 131 | }else if( rxGps == CR ){ |
131 | }else if( rxGps == CR ){ |
| 132 | /* End of message */ |
132 | /* End of message */ |
| 133 | buffer[rxGps_element]='\0'; |
133 | buffer[rxGps_element]='\0'; |
| 134 | buffering = FALSE; |
134 | buffering = FALSE; |
| 135 | gpsMsg_received = TRUE; |
135 | gpsMsg_received = TRUE; |
| 136 | } |
136 | } |
| 137 | 137 | ||
| 138 | if( buffering ){ |
138 | if( buffering ){ |
| 139 | buffer[ rxGps_element ] = rxGps; |
139 | buffer[ rxGps_element ] = rxGps; |
| 140 | rxGps_element++; |
140 | rxGps_element++; |
| 141 | } |
141 | } |
| 142 | /* Get next character */ |
142 | /* Get next character */ |
| 143 | rxGps = uart_getc(); |
143 | rxGps = uart_getc(); |
| 144 | } |
144 | } |
| 145 | 145 | ||
| 146 | if( gpsMsg_received ){ |
146 | if( gpsMsg_received ){ |
| 147 | /* One whole msg is completed, start compare and parse */ |
147 | /* One whole msg is completed, start compare and parse */ |
| 148 | if( !strncmp(buffer, "GPGGA", 5) ){ |
148 | if( !strncmp(buffer, "GPGGA", 5) ){ |
| 149 | /* Standard NMEA message, $GPGGA */ |
149 | /* Standard NMEA message, $GPGGA */ |
| 150 | strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPGGA" */ |
150 | strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPGGA" */ |
| 151 | 151 | ||
| 152 | /* Get UTC */ |
152 | /* Get UTC */ |
| 153 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
153 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 154 | strcpy(gga_utc, pch); |
154 | strcpy(gga_utc, pch); |
| 155 | 155 | ||
| 156 | /* Get latitude */ |
156 | /* Get latitude */ |
| 157 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
157 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 158 | strcpy(gga_latitude, pch); |
158 | strcpy(gga_latitude, pch); |
| 159 | 159 | ||
| 160 | /* Get latitude N/S */ |
160 | /* Get latitude N/S */ |
| 161 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
161 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 162 | strcpy(gga_latitude_NS, pch); |
162 | strcpy(gga_latitude_NS, pch); |
| 163 | 163 | ||
| 164 | /* Get longitude */ |
164 | /* Get longitude */ |
| 165 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
165 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 166 | strcpy(gga_longitude, pch); |
166 | strcpy(gga_longitude, pch); |
| 167 | 167 | ||
| 168 | /* Get longitude E/W */ |
168 | /* Get longitude E/W */ |
| 169 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
169 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 170 | strcpy(gga_longitude_EW, pch); |
170 | strcpy(gga_longitude_EW, pch); |
| 171 | 171 | ||
| 172 | }else if( !strncmp(buffer, "GPVTG", 5) ){ |
172 | }else if( !strncmp(buffer, "GPVTG", 5) ){ |
| 173 | /* Standard NMEA message, $GPVTG */ |
173 | /* Standard NMEA message, $GPVTG */ |
| 174 | strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPVTG" */ |
174 | strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPVTG" */ |
| 175 | 175 | ||
| 176 | /* Get course over ground, true degree */ |
176 | /* Get course over ground, true degree */ |
| 177 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
177 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 178 | strcpy(vtg_course_true, pch); |
178 | strcpy(vtg_course_true, pch); |
| 179 | 179 | ||
| 180 | /* Get course over ground, magnetic degree */ |
180 | /* Get course over ground, magnetic degree */ |
| 181 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
181 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 182 | strcpy(vtg_course_magnetic, pch); |
182 | strcpy(vtg_course_magnetic, pch); |
| 183 | 183 | ||
| 184 | /* Get speed over ground, km/h */ |
184 | /* Get speed over ground, km/h */ |
| 185 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
185 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 186 | strcpy(vtg_speed_kmh, pch); |
186 | strcpy(vtg_speed_kmh, pch); |
| 187 | 187 | ||
| 188 | }else if( !strncmp(buffer, "GPZDA", 5) ){ |
188 | }else if( !strncmp(buffer, "GPZDA", 5) ){ |
| 189 | /* Standard NMEA message, $GPZDA */ |
189 | /* Standard NMEA message, $GPZDA */ |
| 190 | strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPZDA" */ |
190 | strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPZDA" */ |
| 191 | 191 | ||
| 192 | /* Get time */ |
192 | /* Get time */ |
| 193 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
193 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 194 | strcpy(zda_time, pch); |
194 | strcpy(zda_time, pch); |
| 195 | 195 | ||
| 196 | /* Get day, 0-31 */ |
196 | /* Get day, 0-31 */ |
| 197 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
197 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 198 | strcpy(zda_day, pch); |
198 | strcpy(zda_day, pch); |
| 199 | 199 | ||
| 200 | /* Get month, 1-12 */ |
200 | /* Get month, 1-12 */ |
| 201 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
201 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 202 | strcpy(zda_month, pch); |
202 | strcpy(zda_month, pch); |
| 203 | 203 | ||
| 204 | /* Get year */ |
204 | /* Get year */ |
| 205 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
205 | pch = strtok(NULL, GPS_MSG_DELIMITER); |
| 206 | strcpy(zda_year, pch); |
206 | strcpy(zda_year, pch); |
| 207 | } |
207 | } |
| 208 | 208 | ||
| 209 | gpsMsg_received = FALSE; /* Get ready for next message */ |
209 | gpsMsg_received = FALSE; /* Get ready for next message */ |
| 210 | 210 | ||
| 211 | } |
211 | } |
| 212 | } |
212 | } |
| 213 | return 0; |
213 | return 0; |
| 214 | } |
214 | } |
| 215 | 215 | ||
| 216 | 216 | ||