-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdreamlib.h
342 lines (287 loc) · 8.84 KB
/
dreamlib.h
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
#include <Arduino.h>
#ifndef Dreamlib_h
#define Dreamlib_h
#define DREAMLIB_VERSION 1.0.0
#ifndef IMPORT_FROM_ARDUINO_LIBRARIES_FOLDER
char debug_string_buffer[20];
// sprintf + serial of 20 bytes takes ~200us
// sprintf + serial of 10 bytes takes ~144us
// sprintf + serial of 5 bytes takes ~108us
#define debug(formato, valor) \
sprintf(debug_string_buffer, formato, valor); \
Serial.print(debug_string_buffer)
#endif
#define FORWARD Motors::kDirectionForward
#define BACKWARD Motors::kDirectionBackward
class Motors {
public:
typedef enum MotorDirections {
kDirectionForward = 'F',
kDirectionBackward = 'B'
} motorDirections_t;
Motors() {
pinMode(kMotorRP, OUTPUT);
pinMode(kMotorRN, OUTPUT);
pinMode(kMotorLP, OUTPUT);
pinMode(kMotorLN, OUTPUT);
stop();
left_motor_direction_ = 'F';
right_motor_direction_ = 'F';
left_motor_speed_ = 0;
right_motor_speed_ = 0;
}
void setLeftSpeed(int speed_percentage) {
// parameters:
// speed_percentage: 0 to 100
left_motor_speed_ = constrain(speed_percentage, 0, 100);
left_motor_speed_= map(left_motor_speed_, 0, 100, 0, 255);
setMotor('L', left_motor_direction_, left_motor_speed_);
}
void setRightSpeed(int speed_percentage) {
// parameters:
// speed_percentage: 0 to 100
right_motor_speed_ = constrain(speed_percentage, 0, 100);
right_motor_speed_= map(right_motor_speed_, 0, 100, 0, 255);
setMotor('R', right_motor_direction_, right_motor_speed_);
}
void setLeftDirection(motorDirections_t new_direction) {
// parameters:
// new_direction: motorDirections_t values
left_motor_direction_ = new_direction;
setMotor('L', left_motor_direction_, left_motor_speed_);
}
void setRightDirection(motorDirections_t new_direction) {
// parameters:
// new_direction: motorDirections_t values
right_motor_direction_ = new_direction;
setMotor('R', right_motor_direction_, right_motor_speed_);
}
void go(int left_speed_and_direction, int right_speed_and_direction) {
// sets all motors at once
// parameters:
// left_speed_and_direction: -100 to 100
// right_speed_and_direction: -100 to 100
// constrain speed between allowed values
left_motor_speed_ = constrain(left_speed_and_direction, -100, 100);
right_motor_speed_ = constrain(right_speed_and_direction, -100, 100);
// change direction when speed is negative
if (left_motor_speed_ < 0) {
left_motor_speed_ = left_motor_speed_ * -1;
left_motor_direction_ = kDirectionBackward;
} else {
left_motor_direction_ = kDirectionForward;
}
left_motor_speed_= map(left_motor_speed_, 0, 100, 0, 255);
if (right_motor_speed_ < 0) {
right_motor_speed_ = right_motor_speed_ * -1;
right_motor_direction_ = kDirectionBackward;
} else {
right_motor_direction_ = kDirectionForward;
}
right_motor_speed_= map(right_motor_speed_, 0, 100, 0, 255);
setMotor('L', left_motor_direction_, left_motor_speed_);
setMotor('R', right_motor_direction_, right_motor_speed_);
}
void stop() {
left_motor_speed_ = 0;
right_motor_speed_ = 0;
digitalWrite(kMotorLP, LOW);
digitalWrite(kMotorLN, LOW);
digitalWrite(kMotorRP, LOW);
digitalWrite(kMotorRN, LOW);
}
private:
static const int kMotorLP = 5;
static const int kMotorLN = 6;
static const int kMotorRP = 9;
static const int kMotorRN = 10;
char left_motor_direction_;
char right_motor_direction_ ;
int left_motor_speed_;
int right_motor_speed_;
void setMotor(char motor, char dir, int pwm) {
// parameters
// motor: 'R' or 'L'
// dir (direction): 'F' or 'B'
// pwm: 0 to 255
pwm = constrain(pwm, 0, 255);
if (motor == 'L') {
if (dir == 'F') {
analogWrite(kMotorRP, pwm);
digitalWrite(kMotorRN, LOW);
} else if (dir == 'B') {
analogWrite(kMotorRN, pwm);
digitalWrite(kMotorRP, LOW);
}
} else if (motor == 'R') {
if (dir == 'F') {
analogWrite(kMotorLP, pwm);
digitalWrite(kMotorLN, LOW);
} else if (dir == 'B') {
analogWrite(kMotorLN, pwm);
digitalWrite(kMotorLP, LOW);
}
}
}
};
class InfraredSensors {
public:
InfraredSensors() {
// no pin initialization: analogRead doesn't need setting pinMode
// to OUTPUT to work
right_sensor_ = 'R';
left_sensor_ = 'L';
}
int left() {
return infraredSensorMeasure(left_sensor_);
}
int right() {
return infraredSensorMeasure(right_sensor_);
}
private:
static const int kRSense = A0;
static const int kLSense = A1;
char right_sensor_ ;
char left_sensor_;
int infraredSensorMeasure(char sensor) {
// parameters:
// sensor: 'R' or 'L'
int ir_sensor_value = 0;
if (sensor == 'R') {
ir_sensor_value = analogRead(kRSense);
} else if (sensor == 'L') {
ir_sensor_value = analogRead(kLSense);
}
return ir_sensor_value;
}
};
class UltrasonicSensors {
public:
UltrasonicSensors() {
pinMode(kTriggerPinA, OUTPUT);
pinMode(kEchoPinA, INPUT);
pinMode(kTriggerPinB, OUTPUT);
pinMode(kEchoPinB, INPUT);
pinMode(kTriggerPinC, OUTPUT);
pinMode(kEchoPinC, INPUT);
last_good_known_value_a_ = 0;
last_good_known_value_b_ = 0;
last_good_known_value_c_ = 0;
// measuring 400 cm (the sensor's maximum sensing distance)
// takes at most, 24 ms
sensor_timeout_us_ = 24L * 1000L;
}
long a() {
return ultrasonicSensorMeasure(kSensorA);
}
long b() {
return ultrasonicSensorMeasure(kSensorB);
}
long c() {
return ultrasonicSensorMeasure(kSensorC);
}
void setMaximumSensingDistance(int distance) {
distance = constrain(distance, 0, 400);
// measuring 400 cm (the sensor's maximum sensing distance)
// takes at most, 24 ms
sensor_timeout_us_ = distance * 60L;
}
private:
static const int kTriggerPinA = A3;
static const int kEchoPinA = 8;
static const int kTriggerPinB = A4;
static const int kEchoPinB = 2;
static const int kTriggerPinC = A5;
static const int kEchoPinC = 7;
static const char kSensorA = 'A';
static const char kSensorB = 'B';
static const char kSensorC = 'C';
int last_good_known_value_a_;
int last_good_known_value_b_;
int last_good_known_value_c_;
unsigned int sensor_timeout_us_;
int ultrasonicSensorMeasure(char sensor) {
// parameters:
// sensor: 'A', 'B' or 'C'
const int kNumberOfSamples = 2; // only 16 readings fit inside an int
unsigned int duration = 0;
int distance = 0;
int pin_trigger = 0;
int pin_echo = 0;
int last_good_known_value = 0;
if (sensor == kSensorA) {
pin_trigger = kTriggerPinA;
pin_echo = kEchoPinA;
last_good_known_value = last_good_known_value_a_;
} else if (sensor == kSensorB) {
pin_trigger = kTriggerPinB;
pin_echo = kEchoPinB;
last_good_known_value = last_good_known_value_b_;
} else if (sensor == kSensorC) {
pin_trigger = kTriggerPinC;
pin_echo = kEchoPinC;
last_good_known_value = last_good_known_value_c_;
}
// read n times and average the readings
for (int i = 0; i < kNumberOfSamples; i++) {
digitalWrite(pin_trigger, HIGH);
digitalWrite(pin_trigger, LOW);
duration = pulseIn(pin_echo, HIGH, sensor_timeout_us_);
distance += duration * 5L / 291L; // us to cm
}
distance = distance / kNumberOfSamples;
if (distance >= 4000 || distance <= 0) {
distance = last_good_known_value;
} else {
if (sensor == kSensorA) {
last_good_known_value_a_ = distance;
} else if (sensor == kSensorB) {
last_good_known_value_b_ = distance;
} else if (sensor == kSensorC) {
last_good_known_value_c_ = distance;
}
}
return (distance + last_good_known_value) / 2;
}
};
class Leds {
public:
Leds() {
pinMode(kPinLedBlue, OUTPUT);
pinMode(kPinLedRed, OUTPUT);
pinMode(kPinLedGreen, OUTPUT);
}
void blueOn() {
digitalWrite(kPinLedBlue, HIGH);
}
void blueOff() {
digitalWrite(kPinLedBlue, LOW);
}
void blueToggle() {
digitalWrite(kPinLedBlue, (digitalRead(kPinLedBlue) == 0 ? HIGH : LOW));
}
void redOn() {
digitalWrite(kPinLedRed, HIGH);
}
void redOff() {
digitalWrite(kPinLedRed, LOW);
}
void redToggle() {
digitalWrite(kPinLedRed, (digitalRead(kPinLedRed) == 0 ? HIGH : LOW));
}
void greenOn() {
digitalWrite(kPinLedGreen, HIGH);
}
void greenOff() {
digitalWrite(kPinLedGreen, LOW);
}
void greenToggle() {
digitalWrite(kPinLedGreen, (digitalRead(kPinLedGreen) == 0 ? HIGH : LOW));
}
private:
// pins
static const int kPinLedBlue = 13;
static const int kPinLedRed = 12;
static const int kPinLedGreen = 11;
};
#endif