1818
1919#include < SlowMotionServo.h>
2020
21- static const byte NOPIN = 255 ;
21+ static const byte NOPIN = 63 ;
2222
2323enum {
24- SERVO_INIT,
25- SERVO_STOPPED,
26- SERVO_UP,
27- SERVO_DOWN,
28- SERVO_DELAYED_UP,
29- SERVO_DELAYED_DOWN
24+ SERVO_INIT = 0 ,
25+ SERVO_STOPPED = 1 ,
26+ SERVO_SETUP = 2 ,
27+ SERVO_UP = 3 ,
28+ SERVO_DOWN = 4 ,
29+ SERVO_DELAYED_UP = 5 ,
30+ SERVO_DELAYED_DOWN = 6 ,
31+ SERVO_NO_STATE = 7
3032};
3133
3234/*
@@ -54,9 +56,10 @@ unsigned int SlowMotionServo::sDelayUntilStop = 10;
5456 */
5557SlowMotionServo::SlowMotionServo () :
5658 mPin(NOPIN),
57- mState(SERVO_INIT),
58- mDetachAtMin(false ),
59+ mDetachAtMin(false ),
5960 mDetachAtMax(false ),
61+ mState(SERVO_INIT),
62+ mSavedState(SERVO_NO_STATE),
6063 mReverted(false ),
6164 mMinPulse(1000 ),
6265 mMaxPulse(2000 ),
@@ -126,16 +129,18 @@ unsigned int SlowMotionServo::normalizePos(const unsigned int inPos)
126129 */
127130void SlowMotionServo::setMinMax (unsigned int minPulse, unsigned int maxPulse)
128131{
129- minPulse = constrainPulse (minPulse);
130- maxPulse = constrainPulse (maxPulse);
131- if (minPulse <= maxPulse) {
132- mMinPulse = minPulse;
133- mMaxPulse = maxPulse;
134- }
135- else {
136- mMinPulse = mMaxPulse = (minPulse + maxPulse) / 2 ;
137- }
138- updatePulseAccordingToMinMax ();
132+ if (isSettable ()) {
133+ minPulse = constrainPulse (minPulse);
134+ maxPulse = constrainPulse (maxPulse);
135+ if (minPulse <= maxPulse) {
136+ mMinPulse = minPulse;
137+ mMaxPulse = maxPulse;
138+ }
139+ else {
140+ mMinPulse = mMaxPulse = (minPulse + maxPulse) / 2 ;
141+ }
142+ updatePulseAccordingToMinMax ();
143+ }
139144}
140145
141146/*
@@ -144,10 +149,12 @@ void SlowMotionServo::setMinMax(unsigned int minPulse, unsigned int maxPulse)
144149 */
145150void SlowMotionServo::setMin (unsigned int minPulse)
146151{
147- minPulse = constrainPulse (minPulse);
148- if (minPulse > mMaxPulse ) minPulse = mMaxPulse ;
149- mMinPulse = minPulse;
150- updatePulseAccordingToMinMax ();
152+ if (isSettable ()) {
153+ minPulse = constrainPulse (minPulse);
154+ if (minPulse > mMaxPulse ) minPulse = mMaxPulse ;
155+ mMinPulse = minPulse;
156+ updatePulseAccordingToMinMax ();
157+ }
151158}
152159
153160/*
@@ -156,10 +163,53 @@ void SlowMotionServo::setMin(unsigned int minPulse)
156163 */
157164void SlowMotionServo::setMax (unsigned int maxPulse)
158165{
159- maxPulse = constrainPulse (maxPulse);
160- if (maxPulse < mMinPulse ) maxPulse = mMinPulse ;
161- mMaxPulse = maxPulse;
162- updatePulseAccordingToMinMax ();
166+ if (isSettable ()) {
167+ maxPulse = constrainPulse (maxPulse);
168+ if (maxPulse < mMinPulse ) maxPulse = mMinPulse ;
169+ mMaxPulse = maxPulse;
170+ updatePulseAccordingToMinMax ();
171+ }
172+ }
173+
174+ /*
175+ * if inReverted is true, the movement is reverted. Otherwise it is not
176+ */
177+ void SlowMotionServo::setReverted (const bool inReverted)
178+ {
179+ if (isSettable ()) {
180+ mReverted = inReverted;
181+ }
182+ }
183+
184+ /*
185+ * Set the speed when travelling from minimum position to maximum positions
186+ */
187+ void SlowMotionServo::setMinToMaxSpeed (const float speed)
188+ {
189+ if (isSettable ()) {
190+ mTimeFactorUp = speed / 10000.0 ;
191+ }
192+ }
193+
194+ /*
195+ * Set the speed when travelling from maximum position to minimum positions
196+ */
197+ void SlowMotionServo::setMaxToMinSpeed (const float speed)
198+ {
199+ if (isSettable ()) {
200+ mTimeFactorDown = speed / 10000.0 ;
201+ }
202+ }
203+
204+ /*
205+ * Set the speed when travelling from minimum position to maximum positions and
206+ * from the maximum to minimum position.
207+ */
208+ void SlowMotionServo::setSpeed (const float speed)
209+ {
210+ if (isSettable ()) {
211+ mTimeFactorUp = mTimeFactorDown = speed / 10000.0 ;
212+ }
163213}
164214
165215/*
@@ -169,8 +219,10 @@ void SlowMotionServo::setMax(unsigned int maxPulse)
169219 */
170220void SlowMotionServo::setInitialPosition (float position)
171221{
172- position = constrainPosition (position);
173- mTargetRelativeTime = mInitialRelativeTime = mCurrentRelativeTime = position;
222+ if (isSettable ()) {
223+ position = constrainPosition (position);
224+ mTargetRelativeTime = mInitialRelativeTime = mCurrentRelativeTime = position;
225+ }
174226}
175227
176228/*
@@ -246,13 +298,73 @@ void SlowMotionServo::updatePosition()
246298}
247299
248300/*
249- * Returns true is the servo is stopped
301+ * Returns true if the servo is isStopped
250302 */
251303bool SlowMotionServo::isStopped ()
252304{
253- return mState == SERVO_STOPPED;
305+ return ( mState == SERVO_STOPPED) ;
254306}
255307
308+ /*
309+ * Returns true if attributes can be set. That means the servo is in the
310+ * SERVO_INIT state or in the SERVO_STOPPED state or in the SERVO_SETUP STATED
311+ */
312+ bool SlowMotionServo::isSettable ()
313+ {
314+ return (mState <= SERVO_SETUP);
315+ }
316+
317+ /*
318+ * setup the minimum position in Live
319+ */
320+ void SlowMotionServo::setupMin (uint16_t minPulse)
321+ {
322+ if (isSettable ()) {
323+ if (mState != SERVO_SETUP) {
324+ mSavedState = mState ;
325+ if (! attached ()) attach (mPin );
326+ }
327+ mState = SERVO_SETUP;
328+ minPulse = constrainPulse (minPulse);
329+ if (minPulse > mMaxPulse ) minPulse = mMaxPulse ;
330+ mMinPulse = minPulse;
331+ writeMicroseconds (minPulse);
332+ }
333+ }
334+
335+ /*
336+ * setup the maximum position in Live
337+ */
338+ void SlowMotionServo::setupMax (uint16_t maxPulse)
339+ {
340+ if (isSettable ()) {
341+ if (mState != SERVO_SETUP) {
342+ mSavedState = mState ;
343+ if (! attached ()) attach (mPin );
344+ }
345+ mState = SERVO_SETUP;
346+ maxPulse = constrainPulse (maxPulse);
347+ if (maxPulse < mMinPulse ) maxPulse = mMinPulse ;
348+ mMaxPulse = maxPulse;
349+ writeMicroseconds (maxPulse);
350+ }
351+ }
352+
353+ void SlowMotionServo::endSetup ()
354+ {
355+ if (mState == SERVO_SETUP) {
356+ mState = mSavedState ;
357+ if (isStopped ()) {
358+ if ((mCurrentRelativeTime == 0.0 && mDetachAtMin ) ||
359+ (mCurrentRelativeTime == 1.0 && mDetachAtMax ))
360+ {
361+ detach ();
362+ }
363+ }
364+ }
365+ }
366+
367+
256368/*
257369 * Class method to update all the servos
258370 */
0 commit comments