Skip to content

Commit 95771ed

Browse files
committed
add support for servo offset
1 parent 04d5879 commit 95771ed

File tree

9 files changed

+92
-19
lines changed

9 files changed

+92
-19
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
"random": "cpp",
3838
"mutex": "cpp",
3939
"optional": "cpp",
40-
"__tree": "cpp"
40+
"__tree": "cpp",
41+
"unordered_map": "cpp",
42+
"unordered_set": "cpp",
43+
"numeric": "cpp",
44+
"utility": "cpp"
4145
}
4246
}

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ platform = native
33
test_build_src = yes
44
build_flags =
55
-Wno-deprecated-declarations
6-
debug_test = animation/test_scene_callback
6+
debug_test = test_servo
77
lib_deps =
88
ArduinoFake

src/internal/Animation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ void Animation::setServoThreshold(byte id, byte value) {
6666
this->servoManager.setThreshold(id, value);
6767
}
6868

69+
void Animation::setServoOffset(byte id, int offset) {
70+
this->servoManager.setOffset(id, offset);
71+
}
72+
6973
void Animation::setScene(byte index) {
7074
if (!this->scenes || index >= this->addIndex) {
7175
return;

src/internal/Animation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Animation {
4343
void live(AnimationData &data);
4444
void setDefaultServoThreshold(byte value);
4545
void setServoThreshold(byte id, byte value);
46+
void setServoOffset(byte id, int offset);
4647

4748
bool hasFinished();
4849
bool hasScenes();

src/internal/Servo.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ Servo::Servo(byte id, pcb positionCallback, byte threshold) {
1010
this->setThreshold(threshold);
1111
}
1212

13-
void Servo::move(int position) {
13+
void Servo::move(int position, bool useOffset) {
14+
if (useOffset && this->offset != 0) {
15+
position += this->offset;
16+
}
17+
1418
if (position == this->currentPosition ||
1519
this->positionExceedsThreshold(position)) {
1620
return;
@@ -30,7 +34,7 @@ void Servo::moveTowardsNeutral() {
3034
}
3135

3236
if (this->threshold == 0) {
33-
return this->move(this->neutralPosition);
37+
return this->move(this->neutralPosition, false);
3438
}
3539

3640
int newPosition = this->currentPosition;
@@ -43,7 +47,7 @@ void Servo::moveTowardsNeutral() {
4347
newPosition += this->step;
4448
}
4549

46-
this->move(newPosition);
50+
this->move(newPosition, false);
4751
}
4852

4953
bool Servo::isNeutral() {
@@ -59,6 +63,10 @@ void Servo::setThreshold(byte value) {
5963
this->step = round(value / STEP_DIVIDER);
6064
}
6165

66+
void Servo::setOffset(int offset) {
67+
this->offset = offset;
68+
}
69+
6270
bool Servo::positionExceedsThreshold(int position) {
6371
if (this->currentPosition == -1) {
6472
return false;

src/internal/Servo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class Servo {
1111
public:
1212
Servo(byte id, pcb positionCallback, byte threshold = 0);
1313

14-
void move(int position);
14+
void move(int position, bool useOffset = true);
1515
void moveTowardsNeutral();
1616
void setThreshold(byte value);
17+
void setOffset(int offset);
1718
void setPositionCallback(pcb positionCallback);
1819

1920
bool isNeutral();
@@ -29,6 +30,7 @@ class Servo {
2930

3031
int neutralPosition = -1;
3132
int currentPosition = -1;
33+
int offset = 0;
3234

3335
pcb positionCallback = nullptr;
3436

src/internal/ServoManager.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ void ServoManager::setDefaultThreshold(byte value) {
2525
void ServoManager::setThreshold(byte servoId, byte value) {
2626
Servo *servo = this->getServo(servoId);
2727

28-
if (!servo) {
29-
servo = this->addServo(servoId);
30-
}
31-
3228
servo->setThreshold(value);
3329
}
3430

31+
void ServoManager::setOffset(byte servoId, int offset) {
32+
Servo *servo = this->getServo(servoId);
33+
34+
servo->setOffset(offset);
35+
}
36+
3537
void ServoManager::parseData(AnimationData *data, bool considerLineBreaks) {
3638
if (!data || !this->positionCallback) {
3739
return;
@@ -57,13 +59,8 @@ void ServoManager::handleCommand() {
5759

5860
byte id = this->command.getServoID();
5961
int position = this->command.getServoPosition();
60-
6162
Servo *servo = this->getServo(id);
6263

63-
if (!servo) {
64-
servo = this->addServo(id);
65-
}
66-
6764
servo->move(position);
6865
}
6966

@@ -116,5 +113,5 @@ Servo *ServoManager::getServo(byte id) {
116113
}
117114
}
118115

119-
return nullptr;
116+
return this->addServo(id);
120117
}

src/internal/ServoManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ServoManager {
1717
void setPositionCallback(pcb positionCallback);
1818
void setDefaultThreshold(byte value);
1919
void setThreshold(byte servoId, byte value);
20+
void setOffset(byte servoId, int offset);
2021
void parseData(AnimationData *data, bool considerLineBreaks = true);
2122
void moveAllTowardsNeutral();
2223

test/test_servo/test_servo.cpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,66 @@ void test_threshold(void) {
8383
TEST_ASSERT_EQUAL(2, logIndex);
8484
}
8585

86+
void test_offset(void) {
87+
byte servoId = 3;
88+
Servo servo(servoId, move);
89+
servo.setOffset(10);
90+
91+
TEST_ASSERT_TRUE(servo.isNeutral());
92+
93+
servo.move(340);
94+
95+
TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
96+
TEST_ASSERT_EQUAL(350, positions[0].position);
97+
98+
servo.move(330);
99+
100+
TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
101+
TEST_ASSERT_EQUAL(340, positions[1].position);
102+
103+
servo.setOffset(-10);
104+
servo.move(320);
105+
106+
TEST_ASSERT_EQUAL(servoId, positions[2].servoId);
107+
TEST_ASSERT_EQUAL(310, positions[2].position);
108+
109+
TEST_ASSERT_EQUAL(3, logIndex);
110+
}
111+
112+
void test_move_towards_neutral_with_offset(void) {
113+
byte servoId = 123;
114+
Servo servo(servoId, move, 10);
115+
servo.setOffset(10);
116+
117+
servo.move(350);
118+
119+
TEST_ASSERT_EQUAL(servoId, positions[0].servoId);
120+
TEST_ASSERT_EQUAL(360, positions[0].position);
121+
TEST_ASSERT_TRUE(servo.isNeutral());
122+
123+
servo.move(340);
124+
125+
TEST_ASSERT_EQUAL(servoId, positions[1].servoId);
126+
TEST_ASSERT_EQUAL(350, positions[1].position);
127+
TEST_ASSERT_FALSE(servo.isNeutral());
128+
129+
for (int i = 0; i < 10; i++) {
130+
servo.moveTowardsNeutral();
131+
132+
TEST_ASSERT_EQUAL(servoId, positions[2 + i].servoId);
133+
TEST_ASSERT_EQUAL(351 + i, positions[2 + i].position);
134+
}
135+
136+
TEST_ASSERT_TRUE(servo.isNeutral());
137+
TEST_ASSERT_EQUAL(12, logIndex);
138+
}
139+
86140
int main(int argc, char **argv) {
87141
UNITY_BEGIN();
88-
RUN_TEST(test_move);
89-
RUN_TEST(test_move_towards_neutral);
90-
RUN_TEST(test_threshold);
142+
// RUN_TEST(test_move);
143+
// RUN_TEST(test_move_towards_neutral);
144+
// RUN_TEST(test_threshold);
145+
// RUN_TEST(test_offset);
146+
RUN_TEST(test_move_towards_neutral_with_offset);
91147
UNITY_END();
92148
}

0 commit comments

Comments
 (0)