-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot2.cpp
More file actions
594 lines (444 loc) · 14.8 KB
/
Copy pathbot2.cpp
File metadata and controls
594 lines (444 loc) · 14.8 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#include "ev3dev.h"
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <memory>
#include <atomic>
#include <csignal>
#include <thread>
#include <chrono>
#include <cassert>
#include "job.h"
#include "buffer.h"
#include "navigator.h"
using namespace ev3dev;
using namespace std::literals::chrono_literals;
std::atomic< bool > killFlag;
constexpr int HISTORY_SIZE = 9;
struct DataPoint {
int pos;
int val;
};
using SwipeData = std::vector<DataPoint>;
// http://www.mstarlabs.com/apeng/techniques/pidsoftw.html
struct PID {
PID( float gain, float ti, float td, float upd, float setpoint = 0 ) :
gain( gain ), ti( ti ), td( td ), upd( upd ), setpoint( setpoint ),
integral( 0 ), derivative( setpoint )
{ }
float operator()( float in ) { return update( in ); }
float update( float in ) {
auto err = in - setpoint;
auto out = err;
out += integral * ti / upd;
integral += err;
out += (err - derivative) * td / upd;
derivative = err;
return - gain * out;
}
private:
// params
const float gain;
const float ti;
const float td;
const float upd;
const float setpoint;
// state
float integral;
float derivative;
};
struct CrossroadAnalyzer {
CrossroadAnalyzer() :
data( std::pair< Buffer< SwipeData >, int >( { HISTORY_SIZE }, 0 ) )
{
_navigator.initialize();
}
void run() {
while ( !killFlag ) {
data.waitAndReadOnce( [&]( std::pair< Buffer< SwipeData >, int > &sensorData ) { process( sensorData ); } );
}
}
job::GuardedVar< std::pair< Buffer< SwipeData >, int > > data;
job::GuardedVar< int > result; // or watever data type is needed here
protected:
// this function will be called every time data are avalibale, it should
// produce result into result variable, it shoud not access data variable
void process( std::pair< Buffer< SwipeData >, int > &sensorData ) {
process( sensorData.first, sensorData.second );
sensorData.first.clear();
}
void process( Buffer< SwipeData > &sensorData, int distance ) {
std::cout << "crossroad analyser" << std::endl;
int low_last = -1000;
int high_last = 1000;
bool left = false;
bool right = false;
int index=0;
for ( const SwipeData &swipe : sensorData ) { // iterate from oldest to data()
int low, high = 0;
for (auto i = swipe.cbegin(); i != swipe.cend(); ++i) {
if ((index % 2) ? (i->val > 0) : (i->val < 0)) {
high = i->pos;
} else if ((index % 2) ? (i->val < 0) : (i->val > 0)) {
low = i->pos;
}
}
int diff_low = std::abs(low_last - low);
int diff_high = std::abs(high_last - high);
int width = std::abs(low - high);
int last_width = std::abs(low_last - high_last);
if (diff_high > 10 && width > last_width * 1.1)
left = true;
if (diff_low > 10 && width > last_width * 1.1)
right = true;
//std::cout << "path(" << low << " " << high << ") diff(" << diff_low << " " << diff_high << ") wider(" << left << " " << right << ")" << std::endl;
low_last = low;
high_last = high;
index++;
}
int direction = 0;
std::cout << "turn(" << left << " " << right << ") distance(" << distance << ")" << std::endl;
if (left && !right) {
_navigator.turnMet(-1, distance);
direction = -1;
} else if (!left && right) {
_navigator.turnMet(1, distance);
direction = 1;
} else if (left && right) {
direction = _navigator.crossroadsMet(distance);
} else {
direction = 0;
}
result.assign( direction );
}
private:
Navigator _navigator;
};
class DriveControl {
static constexpr const int speed = 80;
public:
bool check() {
return _motor_L.connected() && _motor_R.connected();
}
void init() {
init_modes();
}
void stop() {
_motor_L.stop();
_motor_R.stop();
}
void forward() {
_motor_L.set_run_mode( motor::run_mode_forever );
_motor_R.set_run_mode( motor::run_mode_forever );
_motor_L.start();
_motor_R.start();
}
void adjust( int i ) {
_motor_L.set_pulses_per_second_sp( speed - i );
_motor_R.set_pulses_per_second_sp( speed + i );
}
void turn(const int direction) {
std::cout << "turn: " << direction << std::endl;
stop();
_motor_L.set_run_mode( motor::run_mode_position );
_motor_R.set_run_mode( motor::run_mode_position );
_motor_L.set_pulses_per_second_sp( 400 );
_motor_R.set_pulses_per_second_sp( 400 );
_motor_L.set_position_mode( motor::position_mode_relative );
_motor_R.set_position_mode( motor::position_mode_relative );
// reset position
_motor_L.set_position( 0 );
_motor_R.set_position( 0 );
// go forward by 350
_motor_L.set_position_sp( 340 );
_motor_R.set_position_sp( 340 );
_motor_L.start();
_motor_R.start();
// wait till performed
while (_motor_L.running() || _motor_R.running());
// turn
if (direction != 0) {
const int position_sp = 335;
std::cout << "turning" << std::endl;
_motor_L.set_position_sp( direction == -1 ? position_sp : -position_sp );
_motor_R.set_position_sp( direction == -1 ? -position_sp : position_sp );
_motor_L.start();
_motor_R.start();
// wait till performed
while (_motor_L.running() || _motor_R.running());
}
// reset position
_motor_L.set_position( 0 );
_motor_R.set_position( 0 );
// set the default speed
_motor_L.set_pulses_per_second_sp( speed );
_motor_R.set_pulses_per_second_sp( speed );
forward();
}
int position() {
return (_motor_R.position() + _motor_L.position()) / 2;
}
protected:
void init_modes() {
_motor_L.reset();
_motor_R.reset();
_motor_L.set_stop_mode( motor::stop_mode_hold );
_motor_R.set_stop_mode( motor::stop_mode_hold );
_motor_L.set_regulation_mode( motor::mode_on );
_motor_R.set_regulation_mode( motor::mode_on );
_motor_L.set_polarity_mode( dc_motor::polarity_inverted );
_motor_R.set_polarity_mode( dc_motor::polarity_inverted );
_motor_L.set_pulses_per_second_sp( speed );
_motor_R.set_pulses_per_second_sp( speed );
}
private:
large_motor _motor_L = large_motor( OUTPUT_A );
large_motor _motor_R = large_motor( OUTPUT_D );
};
class SwipeAnalyzer {
static constexpr int blur_radius = 2;
public:
SwipeAnalyzer( CrossroadAnalyzer &crossroad, DriveControl &drives ) :
_crossroad( &crossroad ), _drives( &drives )
{ }
int process( SwipeData& swipe ) {
auto cross = _crossroad->result.tryCopyOut();
if ( cross.first ) { // results are valid
// do crossroad
// exit if in target
if (cross.second == -2)
exit(0);
_drives->turn(cross.second);
_oldpos = _drives->position();
return 0;
}
// discard unusable data
if (swipe.size() < 2)
return 0;
const int size = swipe.size();
/*
std::cout << "Raw Value - Position: " << std::endl;
for (const auto& i : swipe) {
std::cout << (i.val ? '#' : '.');
}
std::cout << std::endl;
*/
median_blur(swipe);
gradient(swipe);
_history.first.push_back( swipe );
/*
std::cout << "Processed Value - Position: " << std::endl;
for (const auto& i : swipe) {
std::cout << "(" << i.val << ")";
}
std::cout << std::endl << std::endl;
*/
int min = 0, max = 0, minix = -1, maxix = -1;
for ( int i = 0; i < size; ++i ) {
int v = swipe[i].val;
if ( v < min ) {
min = v;
minix = i;
}
if ( v > max ) {
max = v;
maxix = i;
}
}
if ( minix == -1 && maxix == -1 ) { // lost :-/, just continue staright
// std::cout << "lost" << std::endl;
return 0;
}
int minpos = minix == -1 ? swipe.front().pos : swipe[ minix ].pos;
int maxpos = maxix == -1 ? swipe.back().pos : swipe[ maxix ].pos;
int width = std::abs( maxpos - minpos );
if ( !_was_wider ) {
_was_wider = is_wider( width );
}
if ( _was_wider && is_narrower( width ) ) {
_was_wider = false;
// dispatch a new job for crosroad analysis
int position = _drives->position();
// std::cout << "widening, distance = " << _oldpos - position << std::endl;
int dist = (_oldpos - position);
_history.second = std::ceil(float(dist) / float(320));
_crossroad->data.assign( _history );
_last_width.clear();
}
if (_was_wider)
return 0;
int blackCenter = (minpos + maxpos) / 2;
// std::cout << "bc = diff = " << blackCenter << " (" << minpos << ", " << maxpos << ")" << std::endl;
int c = _linePid.update( blackCenter );
// if ( c )
// std::cout << "c = " << c << std::endl;
return c;
}
protected:
bool is_wider(const int width) {
_last_width.push_back( width );
// for ( auto v : _last_width )
// std::cout << float( v ) / float( width ) << " < ";
// std::cout << std::endl;
if (_last_width.size() != 3)
return false;
auto it2 = _last_width.begin(),
it1 = it2++;
for ( ; it2 != _last_width.end(); ++it1, ++it2 )
if ( *it1 * 1.1 >= *it2 )
return false;
return true;
}
bool is_narrower(const int width) {
_last_width.push_back( width );
if (_last_width.size() != 3)
return false;
auto it2 = _last_width.begin(),
it1 = it2++;
for ( ; it2 != _last_width.end(); ++it1, ++it2 )
if ( *it1 * 1.1 <= *it2 )
return false;
return true;
}
void gradient(SwipeData& swipe) {
for (auto it = swipe.begin(); it != swipe.end() - 1; ++it)
it->val -= (it + 1)->val;
}
void median_blur(SwipeData& swipe) {
std::array< int, 2*blur_radius + 1 > neighbors;
_temp.clear();
for ( const auto& point : swipe )
_temp.push_back(point.val);
int size = int(_temp.size());
for ( int i = 0; i < size; ++i ) {
for ( int j = -blur_radius; j <= blur_radius; j++ )
neighbors[ blur_radius-j ] = ( i+j < 0 || i+j > size ) ? 0 : _temp[i];
std::sort( neighbors.begin(), neighbors.end() );
swipe[i].val = neighbors[blur_radius];
}
}
private:
std::vector<int> _temp;
PID _linePid = PID( 0.5, 10, 15, 100, 0 );
Buffer< int > _last_width = { 3 };
CrossroadAnalyzer *_crossroad = nullptr;
DriveControl *_drives = nullptr;
std::pair< Buffer< SwipeData >, int > _history = { { HISTORY_SIZE }, 0 };
int _oldpos = 0;
bool _was_wider = false;
};
struct KillSwitch {
KillSwitch() : _button( INPUT_4 ) { }
~KillSwitch() {
if ( _thr.joinable() ) {
_thr.join();
_thr = std::thread();
}
}
void run() {
while ( !killFlag ) {
if ( _button.value() > 0 ) {
killFlag = true;
std::cout << "killed" << std::endl;
} else
std::this_thread::sleep_for( 100ms );
}
}
void spawn() {
assert( !_thr.joinable() );
_thr = std::thread( [&] { this->run(); } );
}
private:
ev3dev::touch_sensor _button;
std::thread _thr;
};
class SensorControl {
static constexpr int speed = 900;
public:
bool check() const { return _eye.connected() && _motor.connected(); }
void init() {
_eye.set_mode( "RGB-RAW" );
_motor.reset();
_motor.set_run_mode( motor::run_mode_position );
_motor.set_stop_mode( motor::stop_mode_brake );
_motor.set_regulation_mode( motor::mode_on );
_motor.set_pulses_per_second_sp( speed );
_motor.set_position_mode( motor::position_mode_absolute );
_motor.set_position( 0 );
}
void update(SwipeData& data) {
const int intensity = _eye.value(0) + _eye.value(1) + _eye.value(2);
DataPoint point;
point.pos = _motor.position();
point.val = (intensity < 382) ? 1 : 0;
_data.push_back(point);
if (update_motor()) {
_data.swap(data);
_data.clear();
}
}
protected:
bool update_motor() {
if ( _motor.running() )
return false;
int pos_sp = ( _motor.position_sp() < 0 ) ? _limit_ccw : _limit_cw;
_motor.set_position_sp( pos_sp );
_motor.start();
return true;
}
private:
SwipeData _data;
int _limit_ccw = 80;
int _limit_cw = -80;
color_sensor _eye = color_sensor( INPUT_AUTO );
medium_motor _motor = medium_motor( OUTPUT_AUTO );
};
class MainControl {
public:
bool check() { return _sensors.check() && _drives.check(); }
void run() {
_sensors.init();
_drives.init();
_drives.forward();
_crossroadThr = std::thread( [&] { _crossroad.run(); } );
while ( !killFlag ) {
update();
}
_crossroad.data.cancelWaits();
_crossroad.result.cancelWaits();
_crossroadThr.join();
_drives.stop();
}
protected:
void update() {
_swipe.clear();
_sensors.update(_swipe);
if (!_swipe.empty()) {
int correction = _analyzer.process(_swipe);
_drives.adjust( correction );
}
}
private:
SwipeData _swipe;
CrossroadAnalyzer _crossroad;
std::thread _crossroadThr;
SensorControl _sensors;
DriveControl _drives;
SwipeAnalyzer _analyzer = { _crossroad, _drives };
};
int main() {
// stop control loop on signal
killFlag = false;
std::signal( SIGINT, []( int ) { killFlag = true; } );
MainControl bot;
KillSwitch killSwith;
if ( !bot.check() )
goto error;
killSwith.spawn();
bot.run();
return 0;
error:
std::cout << "miscount detected!" << std::endl;
return 1;
}