-
Notifications
You must be signed in to change notification settings - Fork 6
/
tracker.c
267 lines (206 loc) · 5.03 KB
/
tracker.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
// tracker.c (c) 2017 by Rodizio. Very dirty code, just a quick proof-of-concept. Licensed under GPL2
// drives a stepper motor through a stepper motor driver connected to the GPIO pins
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <resolv.h>
#include <string.h>
#include <utime.h>
#include <unistd.h>
#include <getopt.h>
#include <pcap.h>
#include <endian.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "lib.h"
#include <wiringPi.h>
wifibroadcast_rx_status_t *status_memory_open(char* shm_file, char* line) {
int fd;
for(;;) {
fd = shm_open(shm_file, O_RDWR, S_IRUSR | S_IWUSR);
if(fd > 0) {
break;
}
// Goto line/row 1/1
printf("\033[%s;1H",line);
printf("Waiting for rx to be started ...\n");
usleep(100000);
}
if (ftruncate(fd, sizeof(wifibroadcast_rx_status_t)) == -1) {
perror("ftruncate");
exit(1);
}
void *retval = mmap(NULL, sizeof(wifibroadcast_rx_status_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (retval == MAP_FAILED) {
perror("mmap");
exit(1);
}
return (wifibroadcast_rx_status_t*)retval;
}
void turnleft() {
digitalWrite (23, LOW);
usleep(2);
digitalWrite (17, HIGH);
usleep(3);
digitalWrite (27, HIGH);
usleep(3);
digitalWrite (27, LOW);
// usleep(10000);
// digitalWrite (23, HIGH);
usleep(3);
}
void turnleftbrake() {
digitalWrite (23, LOW);
usleep(2);
digitalWrite (17, HIGH);
usleep(3);
digitalWrite (27, HIGH);
usleep(3);
digitalWrite (27, LOW);
usleep(10000);
//digitalWrite (23, HIGH);
usleep(2);
}
void turnright() {
digitalWrite (23, LOW);
usleep(2);
digitalWrite (17, LOW);
usleep(3);
digitalWrite (27, HIGH);
usleep(3);
digitalWrite (27, LOW);
// usleep(10000);
// digitalWrite (23, HIGH);
usleep(3);
}
void turnrightbrake() {
digitalWrite (23, LOW);
usleep(2);
digitalWrite (17, LOW);
usleep(3);
digitalWrite (27, HIGH);
usleep(3);
digitalWrite (27, LOW);
usleep(10000);
// digitalWrite (23, HIGH);
usleep(2);
}
int main(int argc, char *argv[]) {
wiringPiSetupGpio();
pinMode(17, OUTPUT); // DIR
pinMode(27, OUTPUT); // STEP
pinMode(22, OUTPUT); // RESET?
pinMode(4, OUTPUT); // SLEEP?
pinMode(23, OUTPUT); // ENABLE
// set reset and sleep to high to enable DRV
digitalWrite (22, HIGH);
usleep(2);
digitalWrite (4, HIGH);
usleep(2);
// set enable to high to disable FETs
digitalWrite (23, HIGH);
usleep(3);
wifibroadcast_rx_status_t *t = status_memory_open(argv[1],argv[2]);
int left = 0;
int right = 0;
int center= 0;
int diff = 0;
int lastmove = 0;
int move = 0;
int samedircount = 0;
int idlecount = 0;
for(;;) {
left = t->adapter[2].current_signal_dbm;
right = t->adapter[1].current_signal_dbm;
center = t->adapter[0].current_signal_dbm;
lastmove = move;
move = 0;
diff = left - right;
printf("left: %d center: %d right:%d diff:%d samecnt:%d ", left, center, right, diff, samedircount);
if (left < right) {
printf("left < right ");
}
if ((left > center) && (center > right)) {
printf("left better center better right - ");
//turnleft();
move = 1;
}
if ((right > center) && (center > left)) {
printf("right worse center worse left - ");
//turnright();
move = 2;
}
if ((center < right) || (center < left)) { // in case left and right is the same but center is worse
printf("center worse right or left ");
// check which is better
if (right > left) { // if right signal is better
printf("rightbetter - ");
//turnright();
move = 2;
}
if (left > right) { // if left signal is better
printf("leftbetter - ");
//turnleft();
move = 1;
}
if (left == right) { // if left and right are the same
printf("left/right the same - ");
//turnleft();
move = 0;
}
}
if ((diff < -2) || (diff > 2)) {
printf("diff over 2 ");
if (diff < -2) {
//turnright();
move = 2;
printf("rightbetter - ");
} else {
//turnleft();
move = 1;
printf("lefttbetter - ");
}
}
if ((move != 0) && ((diff < -2) || (diff > 2))) {
idlecount = 0;
if ((move == 1) && (lastmove != 2)) {
if (samedircount < 5) {
printf(" turn left");
turnleft();
} else {
printf(" turn left samedir");
turnleft();
}
samedircount++;
}
if ((move == 2) && (lastmove != 1)) {
if (samedircount < 5) {
printf(" turn right");
turnright();
} else {
printf(" turn right samedir");
turnright();
}
samedircount++;
}
if ((move == 1) && (lastmove == 2)) {
printf(" doing nothing, last dir was different");
samedircount = 0;
}
if ((move == 2) && (lastmove == 1)) {
printf(" doing nothing, last dir was different");
samedircount = 0;
}
} else {
idlecount++;
if (idlecount > 10) {
printf("idlecount > 10 switching FETs off");
digitalWrite (23, HIGH);
}
samedircount = 0;
}
printf("\n");
usleep(15000);
}
return 0;
}