-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlowMotionServo.cpp
290 lines (265 loc) · 7.24 KB
/
SlowMotionServo.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
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
/*
* Slow Motion Servo Library for Arduino
*
* Copyright Jean-Luc Béchennec 2015
*
* This software is distributed under the GNU Public Licence v2 (GPLv2)
*
* Please read the LICENCE file
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <SlowMotionServo.h>
static const byte NOPIN = 255;
enum { SERVO_STOPPED, SERVO_UP, SERVO_DOWN, SERVO_DELAYED_UP, SERVO_DELAYED_DOWN };
/*
* Static member to store a list of servos. This allow to update the
* positions of all declared servos easily.
*/
SlowMotionServo *SlowMotionServo::sServoList = NULL;
/*
* Delay between the last pulse chage in a movement and the time the
* servos go to the SERVO_STOPPED state.
* 10 ms by default. Can be changed by SlowMotionServo::setDelayUntilStop
*/
unsigned int SlowMotionServo::sDelayUntilStop = 10;
/*
* Constructor :
* Set the connection pin at NOPIN.
* The state is SERVO_STOPPED.
* The servo is not detached by default when stopped.
* Min and max pulse are set according to min and max value of the servo library.
* Servo is at the min.
* Speed are set to 1 (mTimeFactorUp and mTimeFactorDown).
* The servo is added to the list.
*/
SlowMotionServo::SlowMotionServo() :
mPin(NOPIN),
mState(SERVO_STOPPED),
mDetachAtMin(false),
mDetachAtMax(false),
mMinPulse(544),
mMaxPulse(2400),
mInitialRelativeTime(0.0),
mTargetRelativeTime(0.0),
mCurrentRelativeTime(0.0),
mTimeFactorUp(0.0001),
mTimeFactorDown(0.0001)
{
mNext = sServoList;
sServoList = this;
}
/*
* Constructor with the pin
*/
SlowMotionServo::SlowMotionServo(byte pin) :
SlowMotionServo()
{
mPin = pin;
}
/*
* Function to constrain a pulse between the values allowed
* by the servo library
*/
static unsigned int constrainPulse(unsigned int pulse)
{
if (pulse < 544) pulse = 544;
else if (pulse > 2400) pulse = 2400;
return pulse;
}
/*
* Function to constrain a position between 0.0 and 1.0
*/
static float constrainPosition(float position)
{
if (position > 1.0) position = 1.0;
else if (position < 0.0) position = 0.0;
return position;
}
/*
* Method to update the current pulse of the servo
* according to the min and max
*/
void SlowMotionServo::updatePulseAccordingToMinMax()
{
unsigned int currentPosition = readMicroseconds();
if (currentPosition < mMinPulse) currentPosition = mMinPulse;
else if (currentPosition > mMaxPulse) currentPosition = mMaxPulse;
writeMicroseconds(currentPosition);
}
/*
* Change the min and max. Constrain both. Check min <= max.
* If not min and max are sit to the average of both.
*/
void SlowMotionServo::setMinMax(unsigned int minPulse, unsigned int maxPulse)
{
minPulse = constrainPulse(minPulse);
maxPulse = constrainPulse(maxPulse);
if (minPulse <= maxPulse) {
mMinPulse = minPulse;
mMaxPulse = maxPulse;
}
else {
mMinPulse = mMaxPulse = (minPulse + maxPulse) / 2;
}
updatePulseAccordingToMinMax();
}
/*
* Change the min. Constrain it. Check min <= max.
* If not min is set to max.
*/
void SlowMotionServo::setMin(unsigned int minPulse)
{
minPulse = constrainPulse(minPulse);
if (minPulse > mMaxPulse) minPulse = mMaxPulse;
mMinPulse = minPulse;
updatePulseAccordingToMinMax();
}
/*
* Change the max. Constrain it. Check min <= max.
* If not max is set to min.
*/
void SlowMotionServo::setMax(unsigned int maxPulse)
{
maxPulse = constrainPulse(maxPulse);
if (maxPulse < mMinPulse) maxPulse = mMinPulse;
mMaxPulse = maxPulse;
updatePulseAccordingToMinMax();
}
/*
* Set the initial position of the servo. The initial position
* is supposed to be retrieved from the EEPROM or from
* an other mean
*/
void SlowMotionServo::setInitialPosition(float position)
{
position = constrainPosition(position);
mTargetRelativeTime = mInitialRelativeTime = mCurrentRelativeTime = position;
}
/*
* Go to a new position. Compute the direction and change the state
* of the servo. Attach it if the pin is set.
*/
void SlowMotionServo::goTo(float position)
{
position = constrainPosition(position);
mTargetRelativeTime = position;
mInitialRelativeTime = mCurrentRelativeTime;
if (mTargetRelativeTime > mInitialRelativeTime) {
mState = SERVO_UP;
if (mPin != NOPIN) attach(mPin);
}
else if (mTargetRelativeTime < mInitialRelativeTime) {
mState = SERVO_DOWN;
if (mPin != NOPIN) attach(mPin);
}
else mState = SERVO_STOPPED;
mStartTime = millis();
}
/*
* Update the position of the servo.
*/
void SlowMotionServo::updatePosition()
{
float position;
float time;
unsigned long date = millis();
switch (mState) {
case SERVO_UP:
mCurrentRelativeTime = (float)(date - mStartTime) * mTimeFactorUp +
mInitialRelativeTime;
if (mCurrentRelativeTime > mTargetRelativeTime) {
mCurrentRelativeTime = mTargetRelativeTime;
mState = SERVO_DELAYED_UP;
}
position = slopeUp(mCurrentRelativeTime);
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
break;
case SERVO_DOWN:
mCurrentRelativeTime = mInitialRelativeTime -
(float)(date - mStartTime) * mTimeFactorDown;
if (mCurrentRelativeTime < mTargetRelativeTime) {
mCurrentRelativeTime = mTargetRelativeTime;
mState = SERVO_DELAYED_DOWN;
}
position = slopeDown(mCurrentRelativeTime);
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
break;
case SERVO_DELAYED_UP:
if ((millis() - mStartTime) > sDelayUntilStop) {
mState = SERVO_STOPPED;
if (mDetachAtMax) detach();
mStartTime = date;
}
break;
case SERVO_DELAYED_DOWN:
if ((millis() - mStartTime) > sDelayUntilStop) {
mState = SERVO_STOPPED;
if (mDetachAtMin) detach();
mStartTime = date;
}
break;
}
}
/*
* Returns true is the servo is stopped
*/
bool SlowMotionServo::isStopped()
{
return mState == SERVO_STOPPED;
}
/*
* Class method to update all the servos
*/
void SlowMotionServo::update()
{
SlowMotionServo *servo = sServoList;
while (servo != NULL) {
servo->updatePosition();
servo = servo->mNext;
}
}
/*
* Set the delay between the end of a movement and the time the
* servo is stopped and detached if needed.
*/
void SlowMotionServo::setDelayUntilStop(unsigned int delayUntilStop)
{
sDelayUntilStop = delayUntilStop;
}
float SMSLinear::slopeUp(float time)
{
return slope(time);
}
float SMSLinear::slopeDown(float time)
{
return slope(time);
}
float SMSSmooth::slopeUp(float time)
{
return slope(time);
}
float SMSSmooth::slopeDown(float time)
{
return slope(time);
}
float SMSSmoothBounce::slopeUp(float time)
{
if (time <= 0.79) {
return (1.0 - cos(time * PI))/1.8;
}
else {
float timeOff = 10.0 * (time - 0.55);
return (0.834 + 1.0 / (timeOff * timeOff));
}
}
float SMSSmoothBounce::slopeDown(float time)
{
return (1.0 - cos(time * PI))/2.265;
}