Skip to content

Commit

Permalink
introduce setServoRaw/getServoPositionRaw, which work with 0.5us duty…
Browse files Browse the repository at this point in the history
… cycle units. This changes semantics, as not setServo(1000) will send a servo.Action(5000)!
  • Loading branch information
SillyFreak committed Jul 10, 2019
1 parent 7213365 commit 9bf934a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 17 additions & 0 deletions hedgehog/client/hedgehogClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,27 @@ export class HedgehogClient {
}

public async setServo(port: number, position: number | null): Promise<void> {
if (position !== null) {
// position is in range 0..1000 but must be in range 1000..5000
position = 1000 + 4 * position;
}
await this.setServoRaw(port, position);
}

public async setServoRaw(port: number, position: number | null): Promise<void> {
// position is in range 1000..5000, which is the duty cycle length in 0.5us units
await this.send(new servo.Action(port, position));
}

public async getServoPosition(port: number): Promise<number | null> {
let position = await this.getServoPositionRaw(port);
if (position !== null) {
position = Math.floor((position - 1000) / 4);
}
return position;
}

public async getServoPositionRaw(port: number): Promise<number | null> {
let reply = await this.send<servo.CommandReply>(new servo.CommandRequest(port));
return reply.position;
}
Expand Down
4 changes: 2 additions & 2 deletions test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('Client', () => {
it('`setServo` should work', async () => {
mock_server(
[[new servo.Action(0, null)], [new ack.Acknowledgement()]],
[[new servo.Action(0, 1000)], [new ack.Acknowledgement()]],
[[new servo.Action(0, 5000)], [new ack.Acknowledgement()]],
);

await hedgehog.setServo(0, null);
Expand All @@ -205,7 +205,7 @@ describe('Client', () => {
it('`getServoPosition` should work', async () => {
mock_server(
[[new servo.CommandRequest(0)], [new servo.CommandReply(0, null)]],
[[new servo.CommandRequest(0)], [new servo.CommandReply(0, 1000)]],
[[new servo.CommandRequest(0)], [new servo.CommandReply(0, 5000)]],
);

assert.strictEqual(await hedgehog.getServoPosition(0), null);
Expand Down

0 comments on commit 9bf934a

Please sign in to comment.