Skip to content

Commit 6802fa2

Browse files
committed
Live setup
1 parent 038aeb6 commit 6802fa2

File tree

4 files changed

+164
-38
lines changed

4 files changed

+164
-38
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
+---------------------------------------------------------------------------+
22
| SlowMotionServo changelog |
33
+---------------------------------------------------------------------------+
4+
1.0.7 Added methods to support live min max settings
45
1.0.6 Release to fix the problem of the duplicated 1.0.5. Nothing new
56
1.0.5 Minor fix in one of the accessor name
67
1.0.4 Added accessors to get almost all attributes of a SMS object

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SlowMotionServo
2-
version=1.0.6
2+
version=1.0.7
33
author=Jean-Luc - Locoduino
44
maintainer=Jean-Luc - Locoduino
55
sentence=This library allows to move multiple servos slowly.

src/SlowMotionServo.cpp

Lines changed: 143 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818

1919
#include <SlowMotionServo.h>
2020

21-
static const byte NOPIN = 255;
21+
static const byte NOPIN = 63;
2222

2323
enum {
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
*/
5557
SlowMotionServo::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
*/
127130
void 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
*/
145150
void 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
*/
157164
void 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
*/
170220
void 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
*/
251303
bool 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
*/

src/SlowMotionServo.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
class SlowMotionServo : public Servo
2626
{
2727
private:
28-
byte mPin; // connection pin
29-
byte mState:3; // state of the servo
28+
byte mPin:6; // connection pin 0..63
3029
bool mDetachAtMin:1;
3130
bool mDetachAtMax:1;
31+
byte mState:3; // state of the servo
32+
byte mSavedState:3; // saved state of the servo for setup
3233
bool mReverted:1;
3334
unsigned int mMinPulse; // minimum position of the servo in microseconds
3435
unsigned int mMaxPulse; // maximum position of the servo in microseconds
@@ -46,6 +47,7 @@ class SlowMotionServo : public Servo
4647
void updatePosition(); // update the position of the servo
4748
void updatePulseAccordingToMinMax();
4849
unsigned int normalizePos(const unsigned int inPos);
50+
bool isSettable();
4951

5052
public:
5153
SlowMotionServo();
@@ -54,10 +56,12 @@ class SlowMotionServo : public Servo
5456
void setMinMax(unsigned int minPulse, unsigned int maxPulse);
5557
void setMin(unsigned int minPulse);
5658
void setMax(unsigned int maxPulse);
57-
void setReverted(const bool inReverted) { mReverted = inReverted; }
58-
void setMinToMaxSpeed(const float speed) { mTimeFactorUp = speed / 10000.0; }
59-
void setMaxToMinSpeed(const float speed) { mTimeFactorDown = speed / 10000.0; }
60-
void setSpeed(const float speed) { mTimeFactorUp = mTimeFactorDown = speed / 10000.0; }
59+
unsigned int minPosition() { return mMinPulse; }
60+
unsigned int maxPosition() { return mMaxPulse; }
61+
void setReverted(const bool inReverted);
62+
void setMinToMaxSpeed(const float speed);
63+
void setMaxToMinSpeed(const float speed);
64+
void setSpeed(const float speed);
6165
void setInitialPosition(float position);
6266
void setDetachAtMin(bool detach) { mDetachAtMin = detach; }
6367
void setDetachAtMax(bool detach) { mDetachAtMax = detach; }
@@ -67,6 +71,15 @@ class SlowMotionServo : public Servo
6771
void goToMax() { goTo(1.0); }
6872
bool isStopped();
6973

74+
/*
75+
* Live configuration functions.
76+
*/
77+
void setupMin(uint16_t minPulse);
78+
void setupMax(uint16_t maxPulse);
79+
void adjustMin(int16_t increment) { setupMin(mMinPulse + increment); }
80+
void adjustMax(int16_t increment) { setupMax(mMaxPulse + increment); }
81+
void endSetup();
82+
7083
byte pin() { return mPin; }
7184
bool detachAtMin() { return mDetachAtMin; }
7285
bool detachAtMax() { return mDetachAtMax; }

0 commit comments

Comments
 (0)