-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
180 lines (143 loc) · 3.87 KB
/
main.c
File metadata and controls
180 lines (143 loc) · 3.87 KB
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
#include <RTIMULib.h>
#include "navigation.h"
#include "control.h"
#include "i2c.h"
#include "pca9685.h"
#define USAGE_STRING "Usage: %s Kp Ki Kd t marks.txt\n"
#define DIRECTION_MID 50
#define DIRECTION_MAX 60
#define DIRECTION_MIN 40
#define DIRECTION_CHANNEL 0
#define NMARKS 3
#define BUFSIZE 32
#define I2C_DEV "/dev/i2c-1"
RTIMU *imu;
i2c bus;
struct pos_t mark[NMARKS];
int actual_mark;
struct gps_data_t gps;
void direction(double input)
{
PCA9685_setDutyCicle(bus, DIRECTION_CHANNEL, input);
}
double get_angle(void)
{
RTIMU_DATA imuData = imu->getIMUData();
return imuData.fusionPose.z();
}
void *gps_monitor(void *args)
{
struct control_args_t *arg = (struct control_args_t *)args;
struct timespec ts;
struct pos_t actual_pos;
clock_gettime(CLOCK_MONOTONIC, &ts);
while(actual_mark < NMARKS) {
while(arg->running) {
/*Wait for next execution. Sleep again if interrupted*/
while(clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL));
/*get time*/
clock_gettime(CLOCK_MONOTONIC, &ts);
/*Read */
if(gps_read(&gps) < 0) {
perror("GPS read fail");
} else {
if(gps.set && gps.status) {
/*Get actual position*/
actual_pos.latitude = gps.fix.latitude;
actual_pos.longitude = gps.fix.longitude;
/*Lock feedback structure*/
pthread_mutex_lock(&arg->feedback.mutex);
/*Set the new reference*/
arg->feedback.ref = azimuth(actual_pos, mark[actual_mark]);
/*Unlock feedback structure*/
pthread_mutex_unlock(&arg->feedback.mutex);
/*TODO: check if it is close enough to start the camera with haversine function*/
} else {
fprintf(stderr, "GPS read fail: no fix yet");
}
}
/*Calculate the time of the next execution*/
ts.tv_nsec += GPS_POLL_TIME;
/*Prevent tv_nsec overflow*/
while(ts.tv_nsec >= 1e9) {
ts.tv_nsec -= 1e9;
ts.tv_sec++;
}
}
actual_mark++;
}
return NULL;
}
int main(int argc, char *argv[])
{
int t, i, ret;
double Kp, Ki, Kd;
struct control_args_t control_args;
char buf[BUFSIZE];
FILE *marks_file;
if(argc < 6) {
fprintf(stderr, USAGE_STRING, argv[0]);
exit(EXIT_FAILURE);
}
sscanf(argv[1], "%lf", &Kp);
sscanf(argv[2], "%lf", &Ki);
sscanf(argv[3], "%lf", &Kd);
sscanf(argv[4], "%d", &t);
/*TODO: check arguments*/
/*Open/parsing marks.txt*/
marks_file = fopen(argv[5], "r");
if(marks_file < 0) {
fprintf(stderr, "Cannot open '%s' for read\n", argv[5]);
exit(EXIT_FAILURE);
}
for(i = 0; i < NMARKS;) {
/*read file until '\n'*/
fgets(buf, BUFSIZE, marks_file);
/*Scan a "median" line*/
ret = sscanf(buf, "Median: %lf,%lf\n", &mark[i].latitude, &mark[i].longitude);
/*If reach the end of the file*/
if(ret == EOF) {
/*Something is wrong*/
fprintf(stderr, "Fail to read %d marks from %s\n", NMARKS, argv[5]);
fclose(marks_file);
exit(EXIT_FAILURE);
}
/*If it have at least one match*/
if(ret > 0) {
/*Advance*/
i++;
}
}
fclose(marks_file);
actual_mark = 0;
/*IMU initialization*/
/*TODO: RTIMULib should use SPI*/
imu = RTIMU::createIMU(new RTIMUSettings("RTIMULib"));
if((imu == NULL) || (imu->IMUType() == RTIMU_TYPE_NULL)) {
fprintf(stderr, "No IMU found\n");
exit(EXIT_FAILURE);
}
imu->IMUInit();
imu->setSlerpPower(0.02);
imu->setGyroEnable(true);
imu->setAccelEnable(true);
imu->setCompassEnable(true);
/*I2C bus initialization*/
bus = i2c_open("/dev/i2c-1");
if(bus == NULL) {
perror("i2c_open fail");
exit(EXIT_FAILURE);
}
/*GPS initialization*/
if(gps_open(GPSD_SHARED_MEMORY, NULL, &gps)) {
perror("Cannot connect to gpsd daemon");
i2c_close(bus);
exit(EXIT_FAILURE);
}
control_args_init(&control_args);
pid_zoh(&control_args, Kp, Ki, Kd, t);
plant_config(&control_args, direction, DIRECTION_MID, DIRECTION_MAX, DIRECTION_MIN);
feedback_config(&control_args, get_angle, imu->IMUGetPollInterval()*1000000);
/*TODO: launch gps thread*/
/*TODO: Fail-safe monitor*/
}