-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_control.cpp
167 lines (125 loc) · 3.25 KB
/
motor_control.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
/* motor_control.c
*
* Forward, backward, and stop functions are stateless and should set pins and
* hardware to their respective and appropriate values. This should prevent
* other bot functionality to be maintained.
*
* The bot speed is held in a static global to avoid the overhead of structs
*/
#include "Arduino.h"
#include "motor_control.h"
#define SPEED_CONST 60
#define LEFT_CALIB 1
#define RIGHT_CALIB 1.2
#define TURN_CONST 5
void setupMotorControl(void) {
pinMode(ML1, OUTPUT);
pinMode(ML2, OUTPUT);
pinMode(MR1, OUTPUT);
pinMode(MR2, OUTPUT);
pinMode(EL, OUTPUT);
pinMode(ER, OUTPUT);
}
/* initialize to some speed */
static int speed = SPEED_CONST;
static void enable(const int s)
{
analogWrite(EL, LEFT_CALIB * s);
analogWrite(ER, RIGHT_CALIB * s);
}
static void disable(void) { analogWrite(EL, 0); analogWrite(ER, 0); }
void set_speed(const int s) { speed = s; }
int get_speed(void) { return speed; }
void forward(void)
{
disable();
digitalWrite(ML1, HIGH);
digitalWrite(ML2, LOW);
digitalWrite(MR1, HIGH);
digitalWrite(MR2, LOW);
enable(speed);
}
void backward(void)
{
disable();
digitalWrite(ML1, LOW);
digitalWrite(ML2, HIGH);
digitalWrite(MR1, LOW);
digitalWrite(MR2, HIGH);
enable(2 * speed);
}
/* turn both motors in opposite directions to pivot */
void turn(const int angle)
{
disable();
/* these constants were found experimentally */
int turn_constant = TURN_CONST;
if (angle > 0) { /* turn left */
/* turn left motor forward */
digitalWrite(ML1, HIGH);
digitalWrite(ML2, LOW);
/* turn right motor backward */
digitalWrite(MR1, LOW);
digitalWrite(MR2, HIGH);
} else { /* turn right */
/* turn left motor backward */
digitalWrite(ML1, LOW);
digitalWrite(ML2, HIGH);
/* turn right motor forward */
digitalWrite(MR1, HIGH);
digitalWrite(MR2, LOW);
}
enable(SPEED_CONST);
angle = (angle > 0) ? angle : 0 - angle; /* abs(angle) */
delay(angle * turn_constant); /* measured constant */
disable();
}
void turnRight(void)
{
disable();
/* turn left motor forward */
digitalWrite(ML1, LOW);
digitalWrite(ML2, HIGH);
/* turn right motor backward */
digitalWrite(MR1, HIGH);
digitalWrite(MR2, LOW);
enable(SPEED_CONST);
}
void turnRight(const int speed)
{
disable();
/* turn left motor forward */
digitalWrite(ML1, LOW);
digitalWrite(ML2, HIGH);
/* turn right motor backward */
digitalWrite(MR1, HIGH);
digitalWrite(MR2, LOW);
enable(SPEED_CONST);
}
void turnLeft(void)
{
disable();
/* turn left motor backward */
digitalWrite(ML1, HIGH);
digitalWrite(ML2, LOW);
/* turn right motor forward */
digitalWrite(MR1, LOW);
digitalWrite(MR2, HIGH);
enable(SPEED_CONST);
}
void turnLeft(const int speed)
{
disable();
/* turn left motor backward */
digitalWrite(ML1, HIGH);
digitalWrite(ML2, LOW);
/* turn right motor forward */
digitalWrite(MR1, LOW);
digitalWrite(MR2, HIGH);
enable(speed);
}
void stop(void) { disable(); }
#undef SPEED_CONST
#undef LEFT_CALIB
#undef RIGHT_CALIB
#undef TURN_CONST