Skip to content

Commit 89f5105

Browse files
committed
use threshold to calculate move to neutral steps
1 parent 8af1d46 commit 89f5105

File tree

21 files changed

+72
-65
lines changed

21 files changed

+72
-65
lines changed

examples/SwitchModeButton/SwitchModeButton.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void modeChanged(byte prevMode, byte newMode) {
6060
BlenderServoAnimation::Animation animation(FPS, FRAMES);
6161

6262
// Servo object to manage the positions
63-
BlenderServoAnimation::Servo myBlenderServo(0, Bone, move);
63+
BlenderServoAnimation::Servo myBlenderServo(0, Bone, move, 100);
6464

6565
// Callback to be triggered on a short button press
6666
void onPressed() {

src/internal/Animation.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void Animation::run(unsigned long currentMicros) {
4545
this->handlePlayMode(currentMicros);
4646
break;
4747
case MODE_STOP:
48-
this->handleStopMode();
48+
this->handleStopMode(currentMicros);
4949
break;
5050
case MODE_LIVE:
5151
this->handleLiveMode();
@@ -88,9 +88,15 @@ void Animation::handlePlayMode(unsigned long currentMicros) {
8888
}
8989
}
9090

91-
void Animation::handleStopMode() {
91+
void Animation::handleStopMode(unsigned long currentMicros) {
9292
bool allNeutral = true;
9393

94+
if (currentMicros - this->lastMicros < 10000) {
95+
return;
96+
}
97+
98+
this->lastMicros = currentMicros;
99+
94100
for (int i = 0; i < MAX_SERVO_COUNT; i++) {
95101
Servo *servo = this->servos[i];
96102

@@ -100,14 +106,11 @@ void Animation::handleStopMode() {
100106
}
101107
}
102108

103-
if (allNeutral) {
104-
this->changeMode(MODE_DEFAULT);
109+
if (!allNeutral) {
105110
return;
106111
}
107112

108-
if (this->stopStepDelay > 0) {
109-
delay(this->stopStepDelay);
110-
}
113+
this->changeMode(MODE_DEFAULT);
111114
}
112115

113116
void Animation::handleLiveMode() {
@@ -158,13 +161,13 @@ void Animation::loop(unsigned long currentMicros) {
158161
this->changeMode(MODE_LOOP);
159162
}
160163

161-
void Animation::stop(byte stepDelay) {
164+
void Animation::stop(unsigned long currentMicros) {
162165
if (this->modeIsIn(2, MODE_DEFAULT, MODE_STOP)) {
163166
return;
164167
}
165168

166-
this->stopStepDelay = stepDelay;
167169
this->frame = 0;
170+
this->lastMicros = currentMicros;
168171
this->changeMode(MODE_STOP);
169172
}
170173

src/internal/Animation.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Animation {
1717
static const long SECOND_IN_MICROS = 1000000;
1818

1919
byte fps = 0;
20-
byte stopStepDelay = 20;
2120
byte mode = MODE_DEFAULT;
2221

2322
int diffPerSecond = 0;
@@ -35,7 +34,7 @@ class Animation {
3534

3635
void changeMode(byte mode);
3736
void handlePlayMode(unsigned long currentMicros);
38-
void handleStopMode();
37+
void handleStopMode(unsigned long currentMicros);
3938
void handleLiveMode();
4039

4140
public:
@@ -56,7 +55,7 @@ class Animation {
5655
void play(unsigned long currentMicros = micros());
5756
void pause();
5857
void loop(unsigned long currentMicros = micros());
59-
void stop(byte stepDelay = 20);
58+
void stop(unsigned long currentMicros = micros());
6059
void live(Stream &liveStream);
6160

6261
byte getFPS();

src/internal/Servo.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void Servo::moveByFrame(int frame) {
4040
}
4141

4242
void Servo::moveTowardsNeutral(bool inSteps) {
43+
int step = round(this->threshold / 10);
4344
int newPosition = this->currentPosition;
4445

4546
if (inSteps == false) {
@@ -48,9 +49,13 @@ void Servo::moveTowardsNeutral(bool inSteps) {
4849
}
4950

5051
if (this->currentPosition > this->neutralPosition) {
51-
newPosition--;
52+
newPosition -= step;
5253
} else if (this->currentPosition < this->neutralPosition) {
53-
newPosition++;
54+
newPosition += step;
55+
}
56+
57+
if (abs(this->neutralPosition - newPosition) < step) {
58+
newPosition = this->neutralPosition;
5459
}
5560

5661
this->move(newPosition);

src/internal/Show.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ void Show::pause() {
104104
this->changeMode(MODE_PAUSE);
105105
}
106106

107-
void Show::stop(byte stepDelay) {
107+
void Show::stop(unsigned long currentMicros) {
108108
if (!this->animation || this->modeIsIn(2, MODE_DEFAULT, MODE_STOP)) {
109109
return;
110110
}
111111

112-
this->animation->stop(stepDelay);
112+
this->animation->stop(currentMicros);
113113
this->changeMode(MODE_STOP);
114114
}
115115

src/internal/Show.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Show {
5151
void playRandom(unsigned long currentMicros = micros());
5252
void loop(unsigned long currentMicros = micros());
5353
void pause();
54-
void stop(byte stepDelay = 20);
54+
void stop(unsigned long currentMicros = micros());
5555
void live(Stream &liveStream);
5656
void reset();
5757

test/animation/test_live/test_live.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void test_prevented(void) {
9393
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
9494
animation.live(mock);
9595
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
96-
animation.stop();
96+
animation.stop(0);
9797
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
9898
animation.live(mock);
9999
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());

test/animation/test_loop/test_loop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ void test_prevented(void) {
6060
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
6161
animation.loop(0);
6262
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
63-
animation.stop();
63+
animation.stop(0);
6464
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
6565
animation.loop(0);
6666
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
67-
animation.run(0);
67+
animation.run(10000);
6868
animation.live(mock);
6969
TEST_ASSERT_EQUAL(Animation::MODE_LIVE, animation.getMode());
7070
animation.loop(0);

test/animation/test_mode_callback/test_mode_callback.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ void test_all_modes(void) {
5858
animation.loop(0);
5959
TEST_ASSERT_EQUAL(Animation::MODE_PAUSE, prevMode);
6060
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, newMode);
61-
animation.stop();
61+
animation.stop(0);
6262
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, prevMode);
6363
TEST_ASSERT_EQUAL(Animation::MODE_STOP, newMode);
64-
animation.run(0);
64+
animation.run(10000);
6565
TEST_ASSERT_EQUAL(Animation::MODE_STOP, prevMode);
6666
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, newMode);
6767
animation.live(mock);

test/animation/test_pause/test_pause.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ void test_prevented(void) {
8383
animation.pause();
8484
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
8585
animation.play(0);
86-
animation.stop();
86+
animation.stop(0);
8787
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
8888
animation.pause();
8989
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
90-
animation.run(0);
90+
animation.run(10000);
9191
animation.live(mock);
9292
TEST_ASSERT_EQUAL(Animation::MODE_LIVE, animation.getMode());
9393
animation.pause();

0 commit comments

Comments
 (0)