-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDualMotorController.cpp
76 lines (66 loc) · 1.68 KB
/
DualMotorController.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
#include "WConstants.h"
#include "DualMotorController.h"
DualMotorController::DualMotorController(AF_DCMotor *left_motor, AF_DCMotor *right_motor) {
_left_motor = left_motor;
_right_motor = right_motor;
speed = 0;
_forward = 1;
current_direction = &DualMotorController::straight;
}
void DualMotorController::left() {
if (_forward) {
_left_motor->run(FORWARD);
_right_motor->run(BACKWARD);
} else {
_left_motor->run(BACKWARD);
_right_motor->run(FORWARD);
}
current_direction = &DualMotorController::left;
}
void DualMotorController::right() {
if (_forward) {
_left_motor->run(BACKWARD);
_right_motor->run(FORWARD);
} else {
_left_motor->run(FORWARD);
_right_motor->run(BACKWARD);
}
current_direction = &DualMotorController::right;
}
void DualMotorController::straight() {
if (_forward) {
_left_motor->run(FORWARD);
_right_motor->run(FORWARD);
} else {
_left_motor->run(BACKWARD);
_right_motor->run(BACKWARD);
}
current_direction = &DualMotorController::straight;
}
void DualMotorController::stop() {
_left_motor->run(RELEASE);
_right_motor->run(RELEASE);
speed = 0;
current_direction = &DualMotorController::stop;
}
void DualMotorController::forward() {
_forward = 1;
(this->*current_direction)();
}
void DualMotorController::reverse() {
_forward = 0;
(this->*current_direction)();
}
void DualMotorController::setSpeed(int new_speed) {
speed = new_speed;
_left_motor->setSpeed(speed);
_right_motor->setSpeed(speed);
}
int DualMotorController::isReversing() {
return (_forward == 0);
}
void DualMotorController::reset() {
current_direction = &DualMotorController::straight;
_forward = 1;
speed = 0;
}