Skip to content

Commit 5a15425

Browse files
committed
version 1.0.3. Added a method to reverse the movement. Example with 2 servo
1 parent 40c81c1 commit 5a15425

File tree

9 files changed

+96
-10
lines changed

9 files changed

+96
-10
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Set the initial position of the servo. The position is a floating point number
101101
ranging from 0.0 to 1.0. If the value is greater than 1.0, ti is reset to 1.0
102102
and if lower than 0.0, it is reset to 0.0
103103

104+
### setReverted(reverted)
105+
106+
Reverse the movement. By default reverted is false. If set to true, the trajectories are mirrored across an axis at time = 0.5.
107+
104108
### goTo(position)
105109

106110
Go to the specified position by following the trajectory.

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.3 Added a method to do a mirror at time = .5
45
1.0.2 Default min and max have been changed to 1000 and 2000.
56
More accurate SMSSmoothBounce trajectory.
67
Fixed a problem with the initial position.

examples/PushButton/PushButton.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void setup()
2727
myServo.setMin(800);
2828
myServo.setMax(2200);
2929
myServo.setSpeed(1.5);
30+
myServo.setReverted(true);
3031
myServo.setInitialPosition(0.0);
3132
myServo.setPin(servoPin);
3233
digitalWrite(ledPin, HIGH);

examples/PushButton/PushButton.png

156 KB
Loading
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Drive a servo by using a push button
3+
* Uses the Bounce2 library. This library can be installed using the library manager
4+
*/
5+
6+
#include <Servo.h>
7+
#include <SlowMotionServo.h>
8+
#include <Bounce2.h>
9+
10+
SMSSmoothBounce myServoRight;
11+
SMSSmoothBounce myServoLeft;
12+
Bounce myButton;
13+
14+
const byte servoRightPin = 4;
15+
const byte servoLeftPin = 3;
16+
const byte buttonPin = 5;
17+
const byte ledPin = 13;
18+
19+
void setup()
20+
{
21+
pinMode(ledPin, OUTPUT);
22+
/* when the button is pressed, the input is LOW */
23+
pinMode(buttonPin, INPUT_PULLUP);
24+
25+
myButton.attach(buttonPin);
26+
/* scan interval for debounce */
27+
myButton.interval(5);
28+
29+
myServoRight.setMin(750);
30+
myServoRight.setMax(1800);
31+
myServoLeft.setMin(1100);
32+
myServoLeft.setMax(2200);
33+
34+
myServoRight.setSpeed(1.5);
35+
myServoLeft.setSpeed(1.5);
36+
37+
myServoLeft.setReverted(true);
38+
39+
myServoRight.setInitialPosition(0.0);
40+
myServoLeft.setInitialPosition(0.0);
41+
myServoRight.setPin(servoRightPin);
42+
myServoLeft.setPin(servoLeftPin);
43+
digitalWrite(ledPin, HIGH);
44+
}
45+
46+
void loop()
47+
{
48+
static float servoTarget = 0.0;
49+
50+
/* update the state of the button */
51+
myButton.update();
52+
/* update the position of the servo */
53+
SlowMotionServo::update();
54+
55+
if (myServoRight.isStopped() && myServoLeft.isStopped()) {
56+
digitalWrite(ledPin, LOW);
57+
if (myButton.fell()) {
58+
/* look at the button only when the servo is stopped */
59+
/* change the target */
60+
servoTarget = 1.0 - servoTarget;
61+
/* set the new target for the servo */
62+
myServoRight.goTo(servoTarget);
63+
myServoLeft.goTo(servoTarget);
64+
digitalWrite(ledPin, HIGH);
65+
}
66+
}
67+
}
68+

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SlowMotionServo KEYWORD1
1818
setMin KEYWORD2
1919
setMax KEYWORD2
2020
setMinMax KEYWORD2
21+
setReverted KEYWORD2
2122
setPin KEYWORD2
2223
setMinToMaxSpeed KEYWORD2
2324
setMaxToMinSpeed KEYWORD2
@@ -36,4 +37,3 @@ update KEYWORD2
3637
################################################
3738
# Constants (LITERAL1)
3839
################################################
39-

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.2
2+
version=1.0.3
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ SlowMotionServo::SlowMotionServo() :
5656
mState(SERVO_INIT),
5757
mDetachAtMin(false),
5858
mDetachAtMax(false),
59+
mReverted(false),
5960
mMinPulse(1000),
6061
mMaxPulse(2000),
6162
mInitialRelativeTime(0.0),
@@ -110,6 +111,14 @@ void SlowMotionServo::updatePulseAccordingToMinMax()
110111
writeMicroseconds(currentPosition);
111112
}
112113

114+
/*
115+
* Revert the pulse (mirror at time = .5)
116+
*/
117+
unsigned int SlowMotionServo::normalizePos(const unsigned int inPos)
118+
{
119+
return mReverted ? map(inPos, mMinPulse, mMaxPulse, mMaxPulse, mMinPulse) : inPos;
120+
}
121+
113122
/*
114123
* Change the min and max. Constrain both. Check min <= max.
115124
* If not min and max are sit to the average of both.
@@ -196,7 +205,7 @@ void SlowMotionServo::updatePosition()
196205
switch (mState) {
197206
case SERVO_INIT:
198207
position = slopeUp(mCurrentRelativeTime);
199-
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
208+
writeMicroseconds(normalizePos(position * (mMaxPulse - mMinPulse) + mMinPulse));
200209
mState = SERVO_STOPPED;
201210
break;
202211
case SERVO_UP:
@@ -207,7 +216,7 @@ void SlowMotionServo::updatePosition()
207216
mState = SERVO_DELAYED_UP;
208217
}
209218
position = slopeUp(mCurrentRelativeTime);
210-
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
219+
writeMicroseconds(normalizePos(position * (mMaxPulse - mMinPulse) + mMinPulse));
211220
break;
212221
case SERVO_DOWN:
213222
mCurrentRelativeTime = mInitialRelativeTime -
@@ -217,7 +226,7 @@ void SlowMotionServo::updatePosition()
217226
mState = SERVO_DELAYED_DOWN;
218227
}
219228
position = slopeDown(mCurrentRelativeTime);
220-
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
229+
writeMicroseconds(normalizePos(position * (mMaxPulse - mMinPulse) + mMinPulse));
221230
break;
222231
case SERVO_DELAYED_UP:
223232
if ((millis() - mStartTime) > sDelayUntilStop) {

src/SlowMotionServo.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SlowMotionServo : public Servo
2929
byte mState:3; // state of the servo
3030
bool mDetachAtMin:1;
3131
bool mDetachAtMax:1;
32+
bool mReverted:1;
3233
unsigned int mMinPulse; // minimum position of the servo in microseconds
3334
unsigned int mMaxPulse; // maximum position of the servo in microseconds
3435
unsigned long mStartTime; // time when the movement begin
@@ -44,17 +45,19 @@ class SlowMotionServo : public Servo
4445

4546
void updatePosition(); // update the position of the servo
4647
void updatePulseAccordingToMinMax();
47-
48+
unsigned int normalizePos(const unsigned int inPos);
49+
4850
public:
4951
SlowMotionServo();
5052
SlowMotionServo(byte pin);
51-
void setPin(byte pin) { mPin = pin; }
53+
void setPin(const byte pin) { mPin = pin; }
5254
void setMinMax(unsigned int minPulse, unsigned int maxPulse);
5355
void setMin(unsigned int minPulse);
5456
void setMax(unsigned int maxPulse);
55-
void setMinToMaxSpeed(float speed) { mTimeFactorUp = speed / 10000.0; }
56-
void setMaxToMinSpeed(float speed) { mTimeFactorDown = speed / 10000.0; }
57-
void setSpeed(float speed) { mTimeFactorUp = mTimeFactorDown = speed / 10000.0; }
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; }
5861
void setInitialPosition(float position);
5962
void setDetachAtMin(bool detach) { mDetachAtMin = detach; }
6063
void setDetachAtMax(bool detach) { mDetachAtMax = detach; }

0 commit comments

Comments
 (0)