-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotorController.h
More file actions
159 lines (100 loc) · 3.01 KB
/
MotorController.h
File metadata and controls
159 lines (100 loc) · 3.01 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
#define PWMA 5 //Speed control
#define AIN1 20 //Direction
#define AIN2 6 //Direction
#define PWMB 10 //Speed control
#define BIN1 15 //Direction
#define BIN2 12 //Direction
#define STBY 14 // Standby pin
class MotorController {
int motorSpeed[2] = {0, 0};
float speedRatio[2] = {1.0, 1.0};
MotionDirType motorDir[2] = {FORWARD, FORWARD};
DCSequenceProvider dcSequenceProvider;
Timeline dcTimeline;
boolean moveFinished = false;
unsigned long motionDuration = 0;
// ----------------------------------------------
public:
void setup() {
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(STBY, OUTPUT);
}
void moveStraight( int duration, int speed, MotionDirType dir ) {
speedRatio[0] = speedRatio[1] = 1.0;
motorDir[0] = motorDir[1] = dir;
startMove( duration, speed );
}
void turn( int duration, int speed, MotionDirType dir ) {
if ( dir == LEFT ) {
motorDir[0] = FORWARD;
motorDir[1] = BACKWARD;
} else if ( dir == RIGHT ) {
motorDir[0] = BACKWARD;
motorDir[1] = FORWARD;
}
speedRatio[0] = speedRatio[1] = 1.0;
startMove( duration, speed );
}
void moveArc( int duration, int speed, MotionDirType dir, MotionDirType turnDir, float ratio ) {
if ( turnDir == LEFT ) {
speedRatio[0] = ratio;
speedRatio[1] = 1.0;
} else if ( turnDir == RIGHT ) {
speedRatio[0] = 1.0;
speedRatio[1] = ratio;
}
motorDir[0] = motorDir[1] = dir;
startMove( duration, speed );
}
void startMove( int duration, int speed ){
dcSequenceProvider.generateSequence( SQUARE, duration, speed );
dcTimeline.play( dcSequenceProvider.keyframeBuffer, dcSequenceProvider.keyframeCount );
}
void brake() {
}
void move(int motor, int speed, MotionDirType dir) {
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if (dir == FORWARD ) {
inPin1 = HIGH;
inPin2 = LOW;
}
if (motor == 1) {
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
} else {
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void update() {
dcTimeline.update();
int currentSpeed = int(dcTimeline.getCurrentValue());
move( 0, currentSpeed * speedRatio[0], motorDir[0] );
move( 1, currentSpeed * speedRatio[1], motorDir[1] );
moveFinished = dcTimeline.isFinished();
if( dcTimeline.isFinished() ){
Serial.println( "MOVE FINISHED" );
}
}
void reset(){
dcTimeline.reset();
}
void sleep() {
reset();
digitalWrite(STBY, LOW);
}
void wake() {
digitalWrite(STBY, HIGH);
}
bool isMoveFinished(){
return moveFinished;
}
};