-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps.c
276 lines (235 loc) · 6.31 KB
/
gps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* gps.c
*
* Created: 31-03-2012 15:30:41
* Author: Hussain
*/
/**
* @file gps.c
* @brief Read the GPS data using a UART interrupt
*/
#include "common.h"
#include "peripherals.h"
#include "propagator.h"
#include "gps.h"
///4-byte buffer for the GPS reading
volatile static uint32_t buffer = 0;
///Position variables for the data in GPS structure
volatile static uint8_t pos = 0xFF,vel = 0xFF,dop = 0xFF,geo = 0xFF, time = 0xFF;
///Variables to check whether the message has ended
volatile static uint8_t last_byte, message_end;
//volatile static uint16_t pdop = 0xFFFF;
///Temporary GPS reading
volatile struct GPS_reading gps;
char arrayx[40];
char arrayy[40];
char arrayz[40];
void init_UART_GPS(void)
{
UCSR0A = 0;
UCSR0B = 0;
UCSR0C = 0;
///Double Baud Rate
UCSR0A |= _BV(U2X0);
///Enable Reception
UCSR0B |= _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
///8-bit Data Byte, 2 Stop bits
UCSR0C |= _BV(USBS0) | _BV(UCSZ01) | _BV(UCSZ00);
///Set Baud Rate to 9600
UBRR0L = 103;
UBRR0H = 0;
}
/** @brief Interrupt on receiving a byte through UART GPS
*/
ISR(USART0_RX_vect)
{
PORTA ^= 0xf0;
///Buffer the Received Byte
last_byte = UDR0;
///Put the received byte in the last 4-bytes buffer
buffer = buffer << 8;
buffer &= 0xFFFFFF00;
buffer |= (uint32_t) last_byte;
///Check if the last byte was for position
if(pos < 12)
{
if(pos<4)
{
Current_state.gps.x = Current_state.gps.x >> 8;
Current_state.gps.x &= 0x00FFFFFF;
Current_state.gps.x |= ((uint32_t) last_byte)<<24;
//sprintf(arrayx,"%x %x",last_byte);transmit_string_UART0(arrayx);
//transmit_UART0('\r');*/
}
else if (pos<8)
{
Current_state.gps.y = Current_state.gps.y >> 8;
Current_state.gps.y &= 0x00FFFFFF;
Current_state.gps.y |= ((uint32_t) last_byte)<<24;
//sprintf(arrayy,"%x %x",last_byte);transmit_string_UART0(arrayy);
//transmit_UART0('\r');*/
}
else if(pos<12)
{
Current_state.gps.z = Current_state.gps.z >> 8;
Current_state.gps.z &= 0x00FFFFFF;
Current_state.gps.z |= ((uint32_t) last_byte)<<24;
//sprintf(arrayz,"%x %x",last_byte);transmit_string_UART0(arrayz);
//transmit_UART0('\r');*/
}
///* Increment position and terminate it if full
pos++;
if(pos == 12)
{
pos = 0xFF;
//Current_state.gps.x = convert_uint32_to_int32((int32_t)Current_state.gps.x1);
//Current_state.gps.y = convert_uint32_to_int32(Current_state.gps.y1);
//Current_state.gps.z = convert_uint32_to_int32(Current_state.gps.z1);
}
}
///Check if the last byte was for velocity
if(vel < 12)
{
if(vel < 4)
{
Current_state.gps.v_x = Current_state.gps.v_x >> 8;
Current_state.gps.v_x &= 0x00FFFFFF;
Current_state.gps.v_x |= ((uint32_t) last_byte)<<24;
}
else if(vel < 8)
{
Current_state.gps.v_y = Current_state.gps.v_y >> 8;
Current_state.gps.v_y &= 0x00FFFFFF;
Current_state.gps.v_y |= ((uint32_t) last_byte)<<24;
}
else if(vel < 12)
{
Current_state.gps.v_z = Current_state.gps.v_z >> 8;
Current_state.gps.v_z &= 0x00FFFFFF;
Current_state.gps.v_z |= ((uint32_t) last_byte)<<24;
}
//*((uint8_t *)&gps.v_x + vel) = last_byte;
vel++;
if(vel == 12)
{
vel = 0xFF;
//Current_state.gps.v_x = convert_uint32_to_int32(Current_state.gps.v_x1);
//Current_state.gps.v_y = convert_uint32_to_int32(Current_state.gps.v_y1);
//Current_state.gps.v_z = convert_uint32_to_int32(Current_state.gps.v_z1);
}
}
///Check if the last byte was for PDOP
if(dop < 4)
{
if(dop >= 2)
{
Current_state.gps.pdop = Current_state.gps.pdop >> 8;
Current_state.gps.pdop &= 0x00FF;
Current_state.gps.pdop |= ((uint16_t) last_byte)<<8;
}
// *((uint8_t *)&pdop + (dop - 2)) = last_byte;
dop++;
if(dop == 4)
dop = 0xFF;
}
///Check if the last byte was for Geodetic position
if(geo < 16)
{
if(geo < 4);
else if(geo < 8)
{
Current_state.gps.lat = Current_state.gps.lat >> 8;
Current_state.gps.lat &= 0x00FFFFFF;
Current_state.gps.lat |= ((uint32_t) last_byte)<<24;
}
else if(geo < 12)
{
Current_state.gps.lon = Current_state.gps.lon >> 8;
Current_state.gps.lon &= 0x00FFFFFF;
Current_state.gps.lon |= ((uint32_t) last_byte)<<24;
}
else if(geo < 16 )
{
Current_state.gps.alt = Current_state.gps.alt >> 8;
Current_state.gps.alt &= 0x00FFFFFF;
Current_state.gps.alt |= ((uint32_t) last_byte)<<24;
}
// *((uint8_t *)&gps.lat + (geo - 4)) = last_byte;
geo++;
if(geo == 16)
{
geo = 0xFF;
//Current_state.gps.lat = convert_uint32_to_int32(Current_state.gps.lat1);
//Current_state.gps.lon = convert_uint32_to_int32(Current_state.gps.lon1);
//Current_state.gps.alt = convert_uint32_to_int32(Current_state.gps.alt1);
}
}
///Check if the last byte was for Time
if(time < 7)
{
if(time == 0)
{
Current_state.gps.hours = last_byte;
}
else if(time == 1)
{
Current_state.gps.minutes = last_byte;
}
else if(time == 2)
{
Current_state.gps.seconds = last_byte;
}
else if(time == 3)
{
Current_state.gps.date = last_byte;
}else if(time == 4)
{
Current_state.gps.month = last_byte;
}else if(time > 4)
{
Current_state.gps.year = Current_state.gps.year >> 8;
Current_state.gps.year &= 0x00FF;
Current_state.gps.year |= ((uint16_t) last_byte)<<8;
}
//*((uint8_t *)&gps.hours + time) = last_byte;
time++;
if(time == 7)
{
time = 0xFF;
///* * The Entire message has been read
message_end = 1;
// UCSR0B &= ~(_BV(RXCIE0)); //remember this change iterrupt off
}
}
///Check the buffer against message ID's
///Position
if(buffer == 0x3F3F04AC)
{
message_end = 0;
pos = 0;
}
///Velocity
else if(buffer == 0x3F3F05AC)
vel = 0;
///DOP
else if(buffer == 0x3F3F0BAC)
dop = 0;
///Geodetic System
else if(buffer == 0x3F3F0EAC)
geo = 0;
///Time
else if(buffer == 0x3F3F0FAC)
time = 0;
///Check if PDOP < 500(factor 100), shut off the interrupt and the GPS device
if(( Current_state.gps.pdop < 500) && message_end)//&& ( Current_state.gps.pdop!=0)
{
GPS_done = 1;
///* Switch off the interrupt
UCSR0B &= ~(_BV(RXCIE0));
///* Switch off the GPS device
//power_down_peripheral(PGPS);
Current_state.gps.pdop = 0xFFFF;
message_end = 0;
buffer = 0;
}
}