-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline2.cpp
More file actions
224 lines (174 loc) · 4.83 KB
/
Copy pathline2.cpp
File metadata and controls
224 lines (174 loc) · 4.83 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
#include <iostream>
//#include <vector>
//#include <chrono>
//#include <thread>
#include "ev3dev.h"
using namespace std;
using namespace ev3dev;
class Color
{
public:
Color(const color_sensor sensor) :
_r( sensor.value( 0 ) ),
_g( sensor.value( 1 ) ),
_b( sensor.value( 2 ) )
{}
Color(int r, int g, int b) :
_r(r), _g(g), _b(b)
{}
Color(const Color&) = default;
Color& operator= (const Color& rhs) = default;
public:
int r() const { return _r; }
int g() const { return _g; }
int b() const { return _b; }
int dot(const Color& rhs) const {
return (_r * rhs._r) + (_g * rhs._g) + (_b * rhs._b);
}
private:
int _r = 0;
int _g = 0;
int _b = 0;
};
ostream& operator<< (ostream& os, const Color& col) {
return os << "[" << col.r() << " " << col.g() << " " << col.b() << "]";
}
/*
class Histogram {
public:
void add(const Color& col) { _data.push_back(col); }
size_t size() const { return _data.size(); }
void clear() { _data.clear(); }
private:
vector< Color > _data;
};
class DriveControler
{
public:
bool check() const {
return _motor_L.connected() && _motor_R.connected();
}
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_position_mode( motor::position_mode_relative );
_motor_R.set_position_mode( motor::position_mode_relative );
}
void stop() {
_motor_L.stop();
_motor_R.stop();
}
void start() {
_motor_L.start();
_motor_R.start();
}
public:
void forward() {
_motor_L.set_polarity_mode( dc_motor::polarity_inverted );
_motor_R.set_polarity_mode( dc_motor::polarity_inverted );
_motor_L.set_run_mode( motor::run_mode_forever );
_motor_R.set_run_mode( motor::run_mode_forever );
}
void update( int correction ) {
if (correction < 0) {
_motor_L.set_pulses_per_second_sp( _speed + abs(correction) );
_motor_R.set_pulses_per_second_sp( _speed );
} else {
_motor_L.set_pulses_per_second_sp( _speed );
_motor_R.set_pulses_per_second_sp( _speed + abs(correction) );
}
}
private:
static constexpr const int _speed = 80;
large_motor _motor_L = large_motor( OUTPUT_A );
large_motor _motor_R = large_motor( OUTPUT_D );
};
*/
class SensorControler {
public:
bool check() const {
return /*_dev_touch.connected() &&*/ _dev_color.connected() && _dev_motor.connected();
}
void init() {
_dev_color.set_mode( "RGB-RAW" );
_dev_motor.reset();
_dev_motor.set_run_mode( motor::run_mode_position );
_dev_motor.set_stop_mode( motor::stop_mode_hold );
_dev_motor.set_position_mode( motor::position_mode_relative );
_dev_motor.set_regulation_mode( motor::mode_on );
_dev_motor.set_pulses_per_second_sp( _speed );
}
public:
void update() {
/*
if ( _dev_touch.value() )
_dev_motor.stop();
if ( !_dev_motor.running() )
swipe();
_histogram.add( Color( _dev_color ) );
*/
Color color( _dev_color );
static int pos = -170;
if ( !_dev_motor.running() ) {
pos = -pos;
_dev_motor.set_position_sp( pos );
_dev_motor.start();
cout << endl;
}
cout << color;
//std::this_thread::sleep_for( 100ms );
}
protected:
/*
void swipe() {
_position = -_position; //invert direction
_dev_motor.set_position_sp( 170 );
_dev_motor.start();
_histogram.clear();
}
*/
private:
static constexpr const int _speed = 900;
//Histogram _histogram;
touch_sensor _dev_touch = touch_sensor( INPUT_AUTO );
color_sensor _dev_color = color_sensor( INPUT_AUTO );
medium_motor _dev_motor = medium_motor( OUTPUT_AUTO );
};
class MainControler {
public:
bool check() const {
return /*_drives.check() &&*/ _sensors.check();
}
public:
void run() {
_sensors.init();
//_drives.init();
//_drives.forward();
//_drives.start();
while ( update() );
//_drives.stop();
}
bool update() {
if ( button::enter.pressed() )
return false;
_sensors.update();
//_drives.update( correction/3 );
return true;
}
public:
//DriveControler _drives;
SensorControler _sensors;
};
int main() {
MainControler bot;
if ( !bot.check() ) {
cout << "miscount detected!" << endl;
return 1;
}
bot.run();
return 0;
}