forked from naderessam10/GPS_TRACKING_FINAL_PROJECT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
183 lines (107 loc) · 3 KB
/
main.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
#include "lcd.h"
#include "button_led.h"
#include "gps.h"
#include "uart.h"
#include "math.h"
void SystemInit(){}
volatile uint8_t button_flag=0;
volatile float start_latitude=0.0;
volatile float start_longitude=0.0;
volatile float current_latitude=0.0;
volatile float current_longitude=0.0;
float distance=0;
char distance_str[4];
void modify_lat_long(volatile float*latitude, volatile float*longitude )
{
float seconds=0.0,result=0.0,minutes=0.0;
sint32_t degrees;
//latitude calculation
degrees=(*latitude)/100;
minutes=(*latitude)-(float)(degrees*100);
seconds=minutes/60.00;
result=degrees+seconds;
*latitude= result;
//longitude calculation
degrees=(*longitude)/100;
minutes=(*longitude)-(float)(degrees*100);
seconds=minutes/60.00;
result=degrees+seconds;
*longitude= result;
}
float degtorad(float deg)
{
return deg * ((22/7)/180);
}
float getDistanceFromLatLonInKm(volatile float *lat1,volatile float *lon1,volatile float *lat2,volatile float *lon2)
{
if(*lat1==*lat2 &&*lon1==*lon2){return 0;}
else
{
float R = 6371; // Radius of the earth in km
float dLat = degtorad((*lat2)-(*lat1)); // deg2rad below
float dLon = degtorad((*lon2)-(*lon1));
float a = (sin(dLat/2) * sin(dLat/2)) +(cos(degtorad((*lat1))) *cos(degtorad(*(lat2))) * sin(dLon/2) * sin(dLon/2));
float c = 2 * atan2(sqrt(a),sqrt(1-a));
float d = R * c; // Distance in km
d=d*1000; // distance in meter
return d;
}
}
void itoa(char c[],int a,int base)
{
int i=0;
int str_length=0;
while(a!=0)
{int temp;
temp=a%base;
c[i]=temp+48;
i++;
a=a/base;
}
i=0;
while(*(c+i)!='\0')
{
i++;
}
str_length=i;
i=0;
for(i=0;i<str_length/2;i++)
{int temp;
temp=c[i];
c[i]=c[str_length-i-1];
c[str_length-i-1]=temp;
}
}
int main()
{
LCD_init();
UART1_init();
button_led_init();
readGPSModule(&start_latitude,&start_longitude);
modify_lat_long(&start_latitude,&start_longitude);
delay_ms(200);
while(1)
{
if(button_is_pressed()){button_flag=1;led_on(1);}
if(button_flag==1)
{
readGPSModule(¤t_latitude,¤t_longitude);
modify_lat_long(¤t_latitude,¤t_longitude);
distance=getDistanceFromLatLonInKm(&start_latitude,&start_longitude,¤t_latitude,¤t_longitude);
if(!distance)
{
LCD_Write_Char ('0');
delay_ms(500);
LCD_Cmd(0x01);
}
else
{
itoa(distance_str,distance,10);
delay_ms(500);
LCD_String (distance_str);
delay_ms(500);
LCD_Cmd(0x01);
}
}
}
}