-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive-test.cpp
436 lines (357 loc) · 8.47 KB
/
drive-test.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
*Copyright (c) 2014 - Franz Detro
*
* Some real world test program for motor control
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ev3dev.h"
#include <thread>
#include <chrono>
#include <iostream>
#include <fstream>
#ifndef NO_LINUX_HEADERS
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#define KEY_RELEASE 0
#define KEY_PRESS 1
#define KEY_REPEAT 2
#endif
using namespace std;
using namespace ev3dev;
ofstream sensorlog;
class control
{
public:
control();
~control();
void drive(int speed, int time=0);
void turn(int direction);
void stop();
void reset();
bool initialized() const;
void terminate_on_key();
void panic_if_touched();
void remote_loop();
void drive_autonomously();
//void drive_straight();
void terminate() { _terminate = true; }
protected:
large_motor _motor_left;
large_motor _motor_right;
//infrared_sensor _sensor_ir;
touch_sensor _sensor_touch;
//ultrasonic_sensor _sensor_ultrasonic;
//gyro_sensor _sensor_gyro;
lcd _lcd;
enum state
{
state_idle,
state_driving,
state_turning
};
state _state;
bool _terminate;
};
control::control() :
_motor_left(OUTPUT_B),
_motor_right(OUTPUT_C),
_state(state_idle),
_terminate(false)
{
}
control::~control()
{
reset();
}
void control::drive(int speed, int time)
{
if (time > 0)
{
_motor_left .set_run_mode(motor::run_mode_time);
_motor_right.set_run_mode(motor::run_mode_time);
_motor_left .set_time_setpoint(time);
_motor_right.set_time_setpoint(time);
}
else
{
_motor_left .set_run_mode(motor::run_mode_forever);
_motor_right.set_run_mode(motor::run_mode_forever);
}
_motor_left.set_duty_cycle_setpoint(-speed);
_motor_right.set_duty_cycle_setpoint(-speed);
_state = state_driving;
_motor_left .run();
_motor_right.run();
if (time > 0)
{
while (_motor_left.running() || _motor_right.running())
this_thread::sleep_for(chrono::milliseconds(10));
_state = state_idle;
}
}
void control::turn(int direction)
{
if (_state != state_idle)
stop();
if (direction == 0)
return;
_sensor_gyro.set_mode(gyro_sensor::mode_angle);
_sensor_ultrasonic.set_mode(ultrasonic_sensor::mode_dist_cm);
int angle = _sensor_gyro.value();
int distance = _sensor_ultrasonic.value();
_motor_left.set_run_mode(motor::run_mode_position);
//_motor_left.set_position_mode(motor::position_mode_relative);
_motor_left.set_position(0);
_motor_left .set_position_setpoint(direction);
//_motor_left.set_regulation_mode(motor::mode_on);
_motor_left.set_duty_cycle_setpoint(50);
_motor_right.set_run_mode(motor::run_mode_position);
//_motor_right.set_position_mode(motor::position_mode_relative);
_motor_right.set_position(0);
_motor_right.set_position_setpoint(-direction);
//_motor_right.set_regulation_mode(motor::mode_on);
_motor_right.set_duty_cycle_setpoint(50);
_state = state_turning;
_motor_left .run();
_motor_right.run();
while (_motor_left.running() || _motor_right.running())
{
angle = _sensor_gyro.value();
distance = _sensor_ultrasonic.value();
this_thread::sleep_for(chrono::milliseconds(10));
sensorlog << angle << "," << distance << "\n";
}
_state = state_idle;
}
void control::stop()
{
_motor_left .stop();
_motor_right.stop();
_state = state_idle;
}
void control::reset()
{
if (_motor_left.connected())
_motor_left .reset();
if (_motor_right.connected())
_motor_right.reset();
_state = state_idle;
}
bool control::initialized() const
{
return (_motor_left .connected() &&
_motor_right.connected());
}
void control::terminate_on_key()
{
#ifndef NO_LINUX_HEADERS
thread t([&] () {
int fd = open("/dev/input/by-path/platform-gpio-keys.0-event", O_RDONLY);
if (fd < 0)
{
cout << "Couldn't open platform-gpio-keys device!" << endl;
return;
}
input_event ev;
while (true)
{
size_t rb = read(fd, &ev, sizeof(ev));
if (rb < sizeof(input_event))
continue;
if ((ev.type == EV_KEY) /*&& (ev.value == KEY_PRESS)*/)
{
terminate();
return;
}
}
});
t.detach();
#endif
}
void control::panic_if_touched()
{
if (!_sensor_touch.connected())
{
cout << "no touch sensor found!" << endl;
return;
}
thread t([&] () {
while (!_terminate) {
if (_sensor_touch.value())
{
terminate();
reset();
break;
}
this_thread::sleep_for(chrono::milliseconds(100));
}
});
t.detach();
}
void control::remote_loop()
{
remote_control r(_sensor_ir);
if (!r.connected())
{
cout << "no infrared sensor found!" << endl;
return;
}
const int speed = 70;
const int ninety_degrees = 260;
r.on_red_up = [&] (bool state)
{
if (state)
{
if (_state == state_idle)
drive(speed);
}
else
stop();
};
r.on_red_down = [&] (bool state)
{
if (state)
{
if (_state == state_idle)
drive(-speed);
}
else
stop();
};
r.on_blue_up = [&] (bool state)
{
if (state)
{
if (_state == state_idle)
turn(-ninety_degrees);
}
};
r.on_blue_down = [&] (bool state)
{
if (state)
{
if (_state == state_idle)
turn(ninety_degrees);
}
};
r.on_beacon = [&] (bool state)
{
if (state)
terminate();
};
while (!_terminate)
{
if (!r.process())
{
this_thread::sleep_for(chrono::milliseconds(10));
}
}
reset();
}
void control::drive_autonomously()
{
if (!_sensor_ultrasonic.connected())
{
cout << "no ultrasonic found!" << endl;
return;
}
_sensor_ultrasonic.set_mode(ultrasonic_sensor::mode_dist_cm);
_sensor_gyro.set_mode(gyro_sensor::mode_angle);
while (!_terminate)
{
int distance = _sensor_ultrasonic.value();
int angle = _sensor_gyro.value();
printf("Gyro sensor value: %d \n",angle);
if (distance <= 0)
{
// panic
terminate();
reset();
cout << "PANIC!" << endl << endl;
break;
}
else if (distance >= 100)
{
if (_state != state_driving)
drive(75);
this_thread::sleep_for(chrono::milliseconds(10));
}
else
{
cout << "GOING IN THE LOOP!" << endl << endl;
stop();
int direction = 100;
int start_distance = distance;
while (distance <= 150)
{
turn(direction);
distance = _sensor_ultrasonic.value();
if (distance < start_distance)
{
if (direction < 0)
{
drive(-70, 1000);
}
else
{
direction = -200;
}
}
}
}
}
}
int main()
{
control c;
sensorlog.open ("sensorlog.csv");
if (c.initialized())
{
c.terminate_on_key(); // we terminate if a button is pressed
c.panic_if_touched(); // we panic if the touch sensor is triggered
// change mode to 1 to get IR remote mode
// change mode to 3 for wooot?
int mode = 3;
if (mode == 1)
{
cout << "ensure that channel 1 is selected on your remote control." << endl << endl
<< "upper red button - forward" << endl
<< "lower red button - backward" << endl
<< "upper blue button - left" << endl
<< "lower blue button - right" << endl
<< "middle button - exit" << endl << endl;
c.remote_loop();
}
else if (mode == 2)
{
cout << "touch the sensor or press a button to stop." << endl << endl;
c.drive_autonomously();
}
else if (mode == 3)
{
c.turn(10*360);
c.stop();
}
}
else
{
cout << "you need to connect an infrared sensor and large motors to ports B and C!" << endl;
return 1;
}
sensorlog.close();
return 0;
}