-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
175 lines (151 loc) · 4.63 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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h> //for SIGINT and SIGTERM
#include <wiringPi.h>
#include <softPwm.h>
#include <math.h>
#include <pthread.h> //threads
#include <unistd.h> //usleep()
#include <string.h> //for strcopy stuff
#include <errno.h>
#include <time.h>
#include "imu.h"
#include "gps.h"
#include "mc.h"
volatile int keepRunning = 1;
double* latArr;//main, create, loop
double* lonArr;//main, create, loop
void intHandler(int dummy) {
keepRunning = 0;
}
void createDestinationArray(){
FILE* fd;
char buffer[24];
int i = 0;
latArr = malloc(sizeof(double));
lonArr = malloc(sizeof(double));
if( (fd = fopen("ddCoords.txt", "r")) );
else{
fprintf(stderr, "COORDINATES FILE FOPEN() ERROR: %s\n", strerror(errno));//*
exit(1);
}
while(fgets(buffer, sizeof(buffer), fd) != NULL){
latArr[i] = strtod(strtok(strtok(buffer, "\n"), ","), NULL);
lonArr[i] = strtod(strtok(NULL, ","), NULL);
printf("%.4f %.4f,",latArr[i], lonArr[i]);//
i++;
latArr = realloc(latArr, (i+1)*sizeof(double));
lonArr = realloc(lonArr, (i+1)*sizeof(double));
}
printf("\n");//
fclose(fd);
}
void* strobe(){/*
int modDir = 0;
int counter = 0;
while(keepRunning){
softPwmWrite(GPSFIX_LED, counter);
if(modDir == 0){
if(counter == 100){
modDir = 1;
counter--;
}
else
counter++;
}
else{
if(counter == 0){
modDir = 0;
counter++;
}
else
counter--;
}
usleep(10000);
}
}*/
pthread_exit(NULL);
}
void* loop(){
time_t tStart, tNow;
int current_dest = 0;
double sGPSLat = 0;
double sGPSLon = 0;
double sGPSAlt = 0;
double sGPSSpeed = 0;
double sGPSHeading = 0;
double sIMUHeading = 0;
double sIMUPitch = 0;
double sIMURoll = 0;
double* PsGPSLat = &sGPSLat;
double* PsGPSLon = &sGPSLon;
double* PsGPSAlt = &sGPSAlt;
double* PsGPSSpeed = &sGPSSpeed;
double* PsGPSHeading = &sGPSHeading;
double* PsIMUHeading = &sIMUHeading;
double* PsIMUPitch = &sIMUPitch;
double* PsIMURoll = &sIMURoll;
time(&tStart);
while(keepRunning){
getGPSdata(PsGPSLat, PsGPSLon, PsGPSAlt, PsGPSSpeed, PsGPSHeading);
double bearing_to_dest = atan2(lonArr[current_dest]-sGPSLon, latArr[current_dest]-sGPSLat)*180/M_PI;
// if(bearing_to_dest < 0)
// bearing_to_dest += 360;
getIMUdata(PsIMUHeading, PsIMUPitch, PsIMURoll);
// headingControl(0);
headingControl(bearing_to_dest - sIMUHeading);
if(current_dest < sizeof(latArr)){
if((latArr[current_dest]-.0002 <= sGPSLat && sGPSLat <= latArr[current_dest]+.0002) &&
(lonArr[current_dest]-.0003 <= sGPSLon && sGPSLon <= lonArr[current_dest]+.0003)){
current_dest++;
}
}
else{
keepRunning = 1;
}
//keeprunning, gpsfix, currentdest, lat, lon, alt, speed, heading, bearing, heading, pitch, roll, flighttime
time(&tNow);
printf("%d,%d,%d,%.4f,%.4f,%.1f,%.2f,%.1f,%.1f,%.1f,%.1f,%.1f,%ld\n",keepRunning,no_gps_fix,current_dest,sGPSLat,sGPSLon,sGPSAlt,sGPSSpeed,sGPSHeading
,bearing_to_dest,sIMUHeading,sIMUPitch,sIMURoll,tNow - tStart);
}
pthread_exit(NULL);
}
int main(int argc, char* argv[]){
pthread_t threads[2];
pthread_attr_t attr;
int rc;
void* status;
signal(SIGINT, intHandler);
signal(SIGTERM, intHandler);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
wiringPiSetupGpio();
startServo();
createDestinationArray();
openSerialPort();
enableIMU();
rc = pthread_create(&threads[0], NULL, strobe, NULL);
if(rc){
fprintf(stderr, "ERROR: RETURN CODE FROM PTHREAD_CREATE() IS %s\n", strerror(errno));//*
exit(-1);
}
rc = pthread_create(&threads[1], NULL, loop, NULL);
if(rc){
fprintf(stderr, "ERROR: RETURN CODE FROM PTHREAD_CREATE() IS %s\n", strerror(errno));//*
exit(-1);
}
//this waits for threads to end before proceeding
for(int t=0; t<2; t++){
rc = pthread_join(threads[t], &status);
if(rc){
fprintf(stderr, "ERROR: RETURN CODE FROM PTHREAD_JOIN() IS %s\n", strerror(errno));//*
exit(-1);
}
}
stopServo();
free(latArr);
free(lonArr);
closeSerialPort();
printf("Done\n");
return 0;
}