-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_module.c
329 lines (290 loc) · 8 KB
/
base_module.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "mikes.h"
#include "base_module.h"
#include "mikes_logs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <math.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#define MAX_PACKET_LENGTH 200
static int current_azimuth;
static pthread_mutex_t base_module_lock;
static int fdR[2];
static int fdW[2];
static pid_t plink_child;
static volatile unsigned char new_base_data_arrived;
static unsigned char base_initialized;
void connect_base_module()
{
if (pipe(fdR) < 0)
{
mikes_log(ML_ERR, "base: pipe2()");
base_initialized = 0;
return;
}
if (!pipe(fdW) < 0)
{
mikes_log(ML_ERR, "base: pipe2()");
base_initialized = 0;
return;
}
if ((plink_child = fork()) == 0)
{
/* child */
close(0);
close(1);
dup2(fdR[0], 0);
dup2(fdW[1], 1);
close(fdR[0]);
close(fdR[1]);
close(fdW[0]);
close(fdW[1]);
if (execl("/usr/bin/plink", "/usr/bin/plink", "/dev/ttyUSB0",
"-serial", "-sercfg", "115200,N,n,8,1", NULL) < 0)
{
mikes_log(ML_ERR, "base: child execl()");
base_initialized = 0;
return;
}
}
if (plink_child < 0)
{
mikes_log(ML_ERR, "base: child execl()");
base_initialized = 0;
return;
}
close(fdR[0]);
close(fdW[1]);
if (fcntl( fdW[0], F_SETFL, fcntl(fdW[0], F_GETFL) | O_NONBLOCK) < 0)
{
mikes_log(ML_ERR, "base: setting nonblock on read pipe end");
base_initialized = 0;
}
mikes_log(ML_INFO, "base module connected");
current_azimuth = NO_AZIMUTH;
base_initialized = 1;
}
base_data_type local_data;
void read_base_packet()
{
unsigned char ch;
int numRead;
do {
if ((numRead = read(fdW[0], &ch, 1)) < 0)
{
if (errno != EAGAIN)
{
perror("read()");
exit(-1);
}
else usleep(1);
}
} while (program_runs && (ch != '$'));
unsigned char more_packets_in_queue = 0;
char line[1024];
do {
int lnptr = 0;
do {
if ((numRead = read(fdW[0], line + lnptr, 1)) < 0)
{
if (errno != EAGAIN)
{
perror("read()");
exit(-1);
}
else { usleep(1); continue; }
}
lnptr += numRead;
if (lnptr > 1023) break;
} while (program_runs && (line[lnptr - 1] != '\n'));
if (lnptr > 0) line[lnptr - 1] = 0;
more_packets_in_queue = 0;
while (program_runs)
{
if (read(fdW[0], &ch, 1) < 0)
{
if (errno == EAGAIN) break;
else
{
perror("read()");
exit(-1);
}
}
if (ch == '$')
{
more_packets_in_queue = 1;
break;
}
}
} while (program_runs && more_packets_in_queue);
pthread_mutex_lock(&base_module_lock);
sscanf(line, "%ld%ld%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd",
&(local_data.counterA), &(local_data.counterB), &(local_data.velocityA),
&(local_data.velocityB), &(local_data.dist1), &(local_data.dist2),
&(local_data.dist3), &(local_data.cube), &(local_data.heading),
&(local_data.ax), &(local_data.ay), &(local_data.az),
&(local_data.gx), &(local_data.gy), &(local_data.gz));
new_base_data_arrived = 1;
//printf("CA: %ld, CB: %ld\n", local_data.counterA, local_data.counterB);
pthread_mutex_unlock(&base_module_lock);
//mikes_log(ML_INFO, line);
}
void wait_for_new_base_data()
{
new_base_data_arrived = 0;
while (!new_base_data_arrived) usleep(1000);
}
void set_motor_speeds(int left_motor, int right_motor)
{
char cmd[40];
int lm = abs(left_motor);
int rm = abs(right_motor);
sprintf(cmd, "@M%c%1d%1d%c%1d%1d", ((left_motor > 0)?' ':'-'),
((lm / 10) % 10),
(lm % 10),
((right_motor > 0)?' ':'-'),
(rm / 10) % 10,
(rm % 10));
if (write(fdR[1], cmd, strlen(cmd)) < strlen(cmd))
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not send command");
}
}
void stop_now()
{
if (write(fdR[1], "@S", 2) < 2)
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not send command");
}
current_azimuth = NO_AZIMUTH;
}
void follow_azimuth(int azimuth)
{
char cmd[20];
current_azimuth = azimuth;
sprintf(cmd, "@A%d%d%d", azimuth / 100, (azimuth % 100) / 10, azimuth % 10);
if (write(fdR[1], cmd, strlen(cmd)) < strlen(cmd))
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not send azimuth");
}
}
int get_current_azimuth()
{
return current_azimuth;
}
void reset_counters()
{
if (write(fdR[1], "@R", 2) < 2)
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not reset counters");
}
wait_for_new_base_data();
wait_for_new_base_data();
}
void regulated_speed(int left_motor, int right_motor)
{
char cmd[40];
int lm = abs(left_motor);
int rm = abs(right_motor);
sprintf(cmd, "@V%c%d%d%d%c%d%d%d", ((left_motor > 0)?' ':'-'),
(lm / 100) % 10,
(lm / 10) % 10,
(lm % 10),
(right_motor > 0)?' ':'-',
(rm / 100) % 10,
(rm / 10) % 10,
(rm % 10));
if (write(fdR[1], cmd, strlen(cmd)) < strlen(cmd))
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not send regulated speed");
}
current_azimuth = NO_AZIMUTH;
}
void *base_module_thread(void *args)
{
while (program_runs)
{
read_base_packet();
usleep(10000);
}
mikes_log(ML_INFO, "base quits.");
stop_now();
usleep(100000);
kill(plink_child, SIGTERM);
threads_running_add(-1);
return 0;
}
void init_base_module()
{
pthread_t t;
base_initialized = 0;
connect_base_module();
if (!base_initialized) return;
pthread_mutex_init(&base_module_lock, 0);
if (pthread_create(&t, 0, base_module_thread, 0) != 0)
{
perror("mikes:base");
mikes_log(ML_ERR, "creating thread for base module");
base_initialized = 0;
}
else threads_running_add(1);
}
void get_base_data(base_data_type* buffer)
{
pthread_mutex_lock(&base_module_lock);
memcpy(buffer, &local_data, sizeof(base_data_type));
pthread_mutex_unlock(&base_module_lock);
}
short angle_difference(short alpha, short beta)
{
short diff = beta - alpha;
if (diff > 180) return diff - 360;
else if (diff < -180) return diff + 360;
return diff;
}
void cancel_azimuth_mode()
{
if (write(fdR[1], "@X", 2) < 2)
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not cancel azimuth mode");
}
current_azimuth = NO_AZIMUTH;
}
void pause_status_reporting()
{
if (write(fdR[1], "@-", 2) < 2)
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not pause reporting");
}
}
void resume_status_reporting()
{
if (write(fdR[1], "@+", 2) < 2)
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not resume reporting");
}
}
void set_laziness(unsigned char laziness)
{
char cmd[10];
sprintf(cmd, "@L %d%d", laziness / 10, laziness % 10);
if (write(fdR[1], cmd, strlen(cmd)) < strlen(cmd))
{
perror("mikes:base");
mikes_log(ML_ERR, "base: could not set laziness");
}
}