Skip to content

Commit

Permalink
version 1.0.3. Added a method to reverse the movement. Example with 2…
Browse files Browse the repository at this point in the history
… servo
  • Loading branch information
Koryphon committed Mar 2, 2018
1 parent 40c81c1 commit 5a15425
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Set the initial position of the servo. The position is a floating point number
ranging from 0.0 to 1.0. If the value is greater than 1.0, ti is reset to 1.0
and if lower than 0.0, it is reset to 0.0

### setReverted(reverted)

Reverse the movement. By default reverted is false. If set to true, the trajectories are mirrored across an axis at time = 0.5.

### goTo(position)

Go to the specified position by following the trajectory.
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
+---------------------------------------------------------------------------+
| SlowMotionServo changelog |
+---------------------------------------------------------------------------+
1.0.3 Added a method to do a mirror at time = .5
1.0.2 Default min and max have been changed to 1000 and 2000.
More accurate SMSSmoothBounce trajectory.
Fixed a problem with the initial position.
Expand Down
1 change: 1 addition & 0 deletions examples/PushButton/PushButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void setup()
myServo.setMin(800);
myServo.setMax(2200);
myServo.setSpeed(1.5);
myServo.setReverted(true);
myServo.setInitialPosition(0.0);
myServo.setPin(servoPin);
digitalWrite(ledPin, HIGH);
Expand Down
Binary file added examples/PushButton/PushButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions examples/PushButton2Servos/PushButton2Servos.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Drive a servo by using a push button
* Uses the Bounce2 library. This library can be installed using the library manager
*/

#include <Servo.h>
#include <SlowMotionServo.h>
#include <Bounce2.h>

SMSSmoothBounce myServoRight;
SMSSmoothBounce myServoLeft;
Bounce myButton;

const byte servoRightPin = 4;
const byte servoLeftPin = 3;
const byte buttonPin = 5;
const byte ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
/* when the button is pressed, the input is LOW */
pinMode(buttonPin, INPUT_PULLUP);

myButton.attach(buttonPin);
/* scan interval for debounce */
myButton.interval(5);

myServoRight.setMin(750);
myServoRight.setMax(1800);
myServoLeft.setMin(1100);
myServoLeft.setMax(2200);

myServoRight.setSpeed(1.5);
myServoLeft.setSpeed(1.5);

myServoLeft.setReverted(true);

myServoRight.setInitialPosition(0.0);
myServoLeft.setInitialPosition(0.0);
myServoRight.setPin(servoRightPin);
myServoLeft.setPin(servoLeftPin);
digitalWrite(ledPin, HIGH);
}

void loop()
{
static float servoTarget = 0.0;

/* update the state of the button */
myButton.update();
/* update the position of the servo */
SlowMotionServo::update();

if (myServoRight.isStopped() && myServoLeft.isStopped()) {
digitalWrite(ledPin, LOW);
if (myButton.fell()) {
/* look at the button only when the servo is stopped */
/* change the target */
servoTarget = 1.0 - servoTarget;
/* set the new target for the servo */
myServoRight.goTo(servoTarget);
myServoLeft.goTo(servoTarget);
digitalWrite(ledPin, HIGH);
}
}
}

2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SlowMotionServo KEYWORD1
setMin KEYWORD2
setMax KEYWORD2
setMinMax KEYWORD2
setReverted KEYWORD2
setPin KEYWORD2
setMinToMaxSpeed KEYWORD2
setMaxToMinSpeed KEYWORD2
Expand All @@ -36,4 +37,3 @@ update KEYWORD2
################################################
# Constants (LITERAL1)
################################################

2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SlowMotionServo
version=1.0.2
version=1.0.3
author=Jean-Luc - Locoduino
maintainer=Jean-Luc - Locoduino
sentence=This library allows to move multiple servos slowly.
Expand Down
15 changes: 12 additions & 3 deletions src/SlowMotionServo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ SlowMotionServo::SlowMotionServo() :
mState(SERVO_INIT),
mDetachAtMin(false),
mDetachAtMax(false),
mReverted(false),
mMinPulse(1000),
mMaxPulse(2000),
mInitialRelativeTime(0.0),
Expand Down Expand Up @@ -110,6 +111,14 @@ void SlowMotionServo::updatePulseAccordingToMinMax()
writeMicroseconds(currentPosition);
}

/*
* Revert the pulse (mirror at time = .5)
*/
unsigned int SlowMotionServo::normalizePos(const unsigned int inPos)
{
return mReverted ? map(inPos, mMinPulse, mMaxPulse, mMaxPulse, mMinPulse) : inPos;
}

/*
* Change the min and max. Constrain both. Check min <= max.
* If not min and max are sit to the average of both.
Expand Down Expand Up @@ -196,7 +205,7 @@ void SlowMotionServo::updatePosition()
switch (mState) {
case SERVO_INIT:
position = slopeUp(mCurrentRelativeTime);
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
writeMicroseconds(normalizePos(position * (mMaxPulse - mMinPulse) + mMinPulse));
mState = SERVO_STOPPED;
break;
case SERVO_UP:
Expand All @@ -207,7 +216,7 @@ void SlowMotionServo::updatePosition()
mState = SERVO_DELAYED_UP;
}
position = slopeUp(mCurrentRelativeTime);
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
writeMicroseconds(normalizePos(position * (mMaxPulse - mMinPulse) + mMinPulse));
break;
case SERVO_DOWN:
mCurrentRelativeTime = mInitialRelativeTime -
Expand All @@ -217,7 +226,7 @@ void SlowMotionServo::updatePosition()
mState = SERVO_DELAYED_DOWN;
}
position = slopeDown(mCurrentRelativeTime);
writeMicroseconds(position * (mMaxPulse - mMinPulse) + mMinPulse);
writeMicroseconds(normalizePos(position * (mMaxPulse - mMinPulse) + mMinPulse));
break;
case SERVO_DELAYED_UP:
if ((millis() - mStartTime) > sDelayUntilStop) {
Expand Down
13 changes: 8 additions & 5 deletions src/SlowMotionServo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SlowMotionServo : public Servo
byte mState:3; // state of the servo
bool mDetachAtMin:1;
bool mDetachAtMax:1;
bool mReverted:1;
unsigned int mMinPulse; // minimum position of the servo in microseconds
unsigned int mMaxPulse; // maximum position of the servo in microseconds
unsigned long mStartTime; // time when the movement begin
Expand All @@ -44,17 +45,19 @@ class SlowMotionServo : public Servo

void updatePosition(); // update the position of the servo
void updatePulseAccordingToMinMax();

unsigned int normalizePos(const unsigned int inPos);

public:
SlowMotionServo();
SlowMotionServo(byte pin);
void setPin(byte pin) { mPin = pin; }
void setPin(const byte pin) { mPin = pin; }
void setMinMax(unsigned int minPulse, unsigned int maxPulse);
void setMin(unsigned int minPulse);
void setMax(unsigned int maxPulse);
void setMinToMaxSpeed(float speed) { mTimeFactorUp = speed / 10000.0; }
void setMaxToMinSpeed(float speed) { mTimeFactorDown = speed / 10000.0; }
void setSpeed(float speed) { mTimeFactorUp = mTimeFactorDown = speed / 10000.0; }
void setReverted(const bool inReverted) { mReverted = inReverted; }
void setMinToMaxSpeed(const float speed) { mTimeFactorUp = speed / 10000.0; }
void setMaxToMinSpeed(const float speed) { mTimeFactorDown = speed / 10000.0; }
void setSpeed(const float speed) { mTimeFactorUp = mTimeFactorDown = speed / 10000.0; }
void setInitialPosition(float position);
void setDetachAtMin(bool detach) { mDetachAtMin = detach; }
void setDetachAtMax(bool detach) { mDetachAtMax = detach; }
Expand Down

0 comments on commit 5a15425

Please sign in to comment.