-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServoController.cpp
More file actions
173 lines (140 loc) · 3.41 KB
/
Copy pathServoController.cpp
File metadata and controls
173 lines (140 loc) · 3.41 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
#include "ServoController.h"
ServoController::ServoController()
: leftAngle(Config::LEFT_SERVO_REST),
rightAngle(Config::RIGHT_SERVO_REST)
{
}
// Initializes both servos and places them in their resting positions.
void ServoController::begin()
{
leftServo.attach(Config::LEFT_SERVO_PIN);
rightServo.attach(Config::RIGHT_SERVO_PIN);
writeServos(
Config::LEFT_SERVO_REST,
Config::RIGHT_SERVO_REST
);
delay(300);
// Reapply the rest position after attachment settles.
writeServos(
Config::LEFT_SERVO_REST,
Config::RIGHT_SERVO_REST
);
delay(300);
}
// Applies the requested servo angles after clamping them to 0–180 degrees.
void ServoController::writeServos(int left, int right)
{
leftAngle = constrain(left, 0, 180);
rightAngle = constrain(right, 0, 180);
leftServo.write(leftAngle);
rightServo.write(rightAngle);
}
// Moves each servo in small increments until both reach their targets.
void ServoController::moveServosSmooth(
int targetLeft,
int targetRight
)
{
targetLeft = constrain(targetLeft, 0, 180);
targetRight = constrain(targetRight, 0, 180);
const int stepSize = Config::SERVO_ANGLE_STEP;
const unsigned long stepDelay =
Config::SERVO_STEP_DELAY_MS;
while (
leftAngle != targetLeft ||
rightAngle != targetRight
)
{
// Move left servo toward its target.
if (leftAngle < targetLeft)
{
leftAngle += stepSize;
if (leftAngle > targetLeft)
{
leftAngle = targetLeft;
}
}
else if (leftAngle > targetLeft)
{
leftAngle -= stepSize;
if (leftAngle < targetLeft)
{
leftAngle = targetLeft;
}
}
// Move right servo toward its target.
if (rightAngle < targetRight) {
rightAngle += stepSize;
if (rightAngle > targetRight) {
rightAngle = targetRight;
}
}
else if (rightAngle > targetRight) {
rightAngle -= stepSize;
if (rightAngle < targetRight) {
rightAngle = targetRight;
}
}
leftServo.write(leftAngle);
rightServo.write(rightAngle);
delay(stepDelay);
}
}
// Returns both servos to their resting positions.
void ServoController::reset()
{
moveServosSmooth(
Config::LEFT_SERVO_REST,
Config::RIGHT_SERVO_REST
);
}
// Feeds/arms both servos.
void ServoController::arm()
{
moveServosSmooth(
Config::LEFT_SERVO_ARM,
Config::RIGHT_SERVO_ARM
);
}
// Fires both servos and returns them to rest.
void ServoController::fire()
{
moveServosSmooth(
Config::LEFT_SERVO_FIRE,
Config::RIGHT_SERVO_FIRE
);
delay(Config::SERVO_FIRE_HOLD_MS);
reset();
}
// Fires only the left servo.
// The right servo stays at its current position during firing.
void ServoController::fireLeft()
{
const int stationaryRightAngle = rightAngle;
moveServosSmooth(
Config::LEFT_SERVO_FIRE,
stationaryRightAngle
);
delay(Config::SERVO_FIRE_HOLD_MS);
reset();
}
// Fires only the right servo.
// The left servo stays at its current position during firing.
void ServoController::fireRight()
{
const int stationaryLeftAngle = leftAngle;
moveServosSmooth(
stationaryLeftAngle,
Config::RIGHT_SERVO_FIRE
);
delay(Config::SERVO_FIRE_HOLD_MS);
reset();
}
// Prints the current servo angles.
void ServoController::printStatus(Stream &output) const
{
output.print(F("Left servo: "));
output.print(leftAngle);
output.print(F(" Right servo: "));
output.println(rightAngle);
}