-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.cpp
240 lines (183 loc) · 5.21 KB
/
line.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
#include <thread>
#include <chrono>
#include <iostream>
#include <ios>
#include "ev3dev.h"
using namespace ev3dev;
using namespace std;
using namespace std::literals::chrono_literals;
enum class Color : int {
none = 0,
black,
blue,
green,
yellow,
red,
white,
brown
};
ostream& operator<< (ostream& os, const ev3dev::sensor& dev) {
os << "[conn: " << dev.connected();
if ( dev.connected() )
os << " port: " << dev.port_name()
<< " mode: " << dev.mode();
return os << "]";
}
ostream& operator<< (ostream& os, const ev3dev::motor& dev) {
os << "[conn: " << dev.connected();
if ( dev.connected() )
os << " port: " << dev.port_name()
<< " type: " << dev.type();
return os << "]";
}
class DriveControl {
static constexpr const int _speed = 100;
public:
bool check() const {
return _motor_L.connected() && _motor_R.connected();
}
void dump() const {
cout << "left motor " << _motor_L << endl;
cout << "right motor " << _motor_R << endl;
}
void init() {
_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_pulses_per_second_sp( _speed );
_motor_R.set_pulses_per_second_sp( _speed );
}
void stop() {
_motor_L.stop();
_motor_R.stop();
}
void start() {
_motor_L.start();
_motor_R.start();
}
public:
void forward() {
stop();
_motor_L.set_polarity_mode( dc_motor::polarity_normal );
_motor_R.set_polarity_mode( dc_motor::polarity_normal );
_motor_L.set_run_mode( motor::run_mode_forever );
_motor_R.set_run_mode( motor::run_mode_forever );
start();
}
void turn( int position ) {
stop();
_motor_L.set_polarity_mode( dc_motor::polarity_inverted );
_motor_R.set_polarity_mode( dc_motor::polarity_normal );
_motor_L.set_run_mode( motor::run_mode_position );
_motor_R.set_run_mode( motor::run_mode_position );
_motor_L.set_position_mode( motor::position_mode_relative );
_motor_R.set_position_mode( motor::position_mode_relative );
_motor_L.set_position_sp( position );
_motor_R.set_position_sp( position );
start();
}
private:
large_motor _motor_L = large_motor( OUTPUT_A );
large_motor _motor_R = large_motor( OUTPUT_D );
int error_L = 0;
int error_R = 0;
};
class Controler {
public:
bool check() const {
return _sensor_color.connected() && _drive_control.check();
}
void dump() const {
cout << "color sensor " << _sensor_color << endl;
_drive_control.dump();
}
void run() {
cout << "initializing..." << endl;
_sensor_color.set_mode( color_sensor::mode_color );
_drive_control.init();
cout << "running..." << endl;
while ( update() );
cout << "exiting..." << endl;
_drive_control.stop();
}
protected:
bool update() {
if ( button::enter.pressed() )
return false;
Color color = static_cast< Color >( _sensor_color.value() );
if (color != _state) {
_state = color;
cout << "\rstate: " << static_cast<int>(color);
cout.flush();
state_changed();
}
std::this_thread::sleep_for( 100ms );
return true;
}
void state_changed() {
switch ( _state ) {
case Color::black:
//_drive_control.forward();
break;
case Color::white:
_drive_control.turn( 15 );
break;
default:
break;
}
}
private:
color_sensor _sensor_color = color_sensor( INPUT_AUTO );
DriveControl _drive_control;
Color _state = Color::none;
};
/*
class CorrectDir {
using std::chrono::high_resolution_clock;
public:
CorrectDir( Controler* bot ) :
_bot( bot ),
_position_L( _motor_L.position() )
_position_R( _motor_R.position() )
{}
public:
void run() {
//time demanding task first
int pos_L = _motor_L.position();
int pos_R = _motor_R.position();
int rot_L = pos_L - _position_L;
int rot_R = pos_R - _position_R;
_position_L = pos_L;
_position_R = pos_R;
//now we can measure time
high_resolution_clock::now time_now;
high_resolution_clock::duration time_diff = time_now - _last_run;
_last_run = time_now;
//after that we adjust speed
time_diff
_bot->motor_L().set_pulses_per_second_sp( _speed_L );
_bot->motor_R().set_pulses_per_second_sp( _speed_R );
}
private:
Controler* const _bot;
constexpr int speed = 400;
int _position_L;
int _position_R;
high_resolution_clock::time_point _last_run;
};
*/
int main()
{
cout << boolalpha;
Controler bot;
if ( !bot.check() ) {
cout << "miscount detected!" << endl;
bot.dump();
return 1;
}
bot.run();
return 0;
}