From 44242145bc2dc8d8776bd49d6e1073d7adb7ab9a Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 20 Oct 2023 08:21:26 -0700 Subject: [PATCH] packet protocol optimization (#102) * packet optim * more optimziations for servo * handle switching mode between cont/pos * avoid overquerying * cleanup * fix requerying of consts * updated test * added rotary examples * simplify logic --- rotary-encoder/clients.ts | 4 +++ rotary-encoder/pxt.json | 16 ++++------- rotary-encoder/test.microbit.ts | 9 +++++++ routing.ts | 22 ++++++++++----- servo/client.ts | 48 ++++++++++++++++++++++----------- servo/test.microbit.ts | 19 ++++++++----- 6 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 rotary-encoder/clients.ts create mode 100644 rotary-encoder/test.microbit.ts diff --git a/rotary-encoder/clients.ts b/rotary-encoder/clients.ts new file mode 100644 index 000000000..3d49b40ae --- /dev/null +++ b/rotary-encoder/clients.ts @@ -0,0 +1,4 @@ +namespace modules { + //% fixedInstance whenUsed weight=2 block="rotary encoder2" + export const rotaryEncoder2 = new RotaryEncoderClient("rotary Encoder2") +} diff --git a/rotary-encoder/pxt.json b/rotary-encoder/pxt.json index 521e8ddaa..dc503e51f 100644 --- a/rotary-encoder/pxt.json +++ b/rotary-encoder/pxt.json @@ -2,20 +2,14 @@ "name": "jacdac-rotary-encoder", "version": "1.9.18", "description": "An incremental rotary encoder - converts angular motion of a shaft to digital signal.", - "files": [ - "constants.ts", - "client.ts" - ], - "supportedTargets": [ - "arcade", - "microbit", - "maker" - ], + "files": ["constants.ts", "client.ts", "clients.ts"], + "supportedTargets": ["arcade", "microbit", "maker"], "dependencies": { "core": "*", "jacdac": "github:microsoft/pxt-jacdac" }, - "testDependencies": { - "nucleo-f411re": "*" + "testFiles": ["test.microbit.ts"], + "fileDependencies": { + "test.microbit.ts": "target:microbit" } } diff --git a/rotary-encoder/test.microbit.ts b/rotary-encoder/test.microbit.ts new file mode 100644 index 000000000..fe8d4ccba --- /dev/null +++ b/rotary-encoder/test.microbit.ts @@ -0,0 +1,9 @@ + +forever(() => { + led.toggle(0, 0) + console.log(`clicks per turn1: ${modules.rotaryEncoder1.clicksPerTurn()}`) + console.log(`position1: ${modules.rotaryEncoder1.position()}`) + console.log(`clicks per turn2: ${modules.rotaryEncoder2.clicksPerTurn()}`) + console.log(`position2: ${modules.rotaryEncoder2.position()}`) + pause(50) +}) \ No newline at end of file diff --git a/routing.ts b/routing.ts index 4f0037236..3a29f972e 100644 --- a/routing.ts +++ b/routing.ts @@ -42,6 +42,7 @@ namespace jacdac { export const REFRESH = "refresh" export const REFRESH_REGISTER_POLL = 50 export const REGISTER_REFRESH_RATE = 250 + export const REGISTER_OPTIONAL_REFRESH_RATE = 1000 export const REGISTER_READ_TIMEOUT = 750 export class Bus extends jacdac.EventSource { @@ -918,20 +919,24 @@ namespace jacdac { * Queues a query to refresh the register */ query() { + const constHasValues = this.isConst && this.hasValues() const device = this.service.device - if (device) + // need to be connected, don't re-request consts + if (device && !constHasValues) { device.query( this.code, - REGISTER_REFRESH_RATE, + this.isConst + ? null + : this.isOptional + ? REGISTER_OPTIONAL_REFRESH_RATE + : REGISTER_REFRESH_RATE, this.service.serviceIndex ) + } } pauseUntilValues(timeOut?: number) { - const manualQuery = - this.code !== SystemReg.Reading && - // don't double query optional registers - (!this.isOptional || !this.hasValues()) + const manualQuery = this.code !== SystemReg.Reading if (manualQuery) this.query() if (!this.hasValues()) { pauseUntil(() => { @@ -1440,7 +1445,10 @@ namespace jacdac { ) } - query(reg: number, refreshRate = 1000, servIdx = 0) { + /** + * @param refreshRate null for const, otherwise ms + */ + query(reg: number, refreshRate: number, servIdx = 0) { let q = this.lookupQuery(reg, servIdx) if (!q) this.queries.push((q = new RegQuery(reg, servIdx))) diff --git a/servo/client.ts b/servo/client.ts index d73731f00..53a139b06 100644 --- a/servo/client.ts +++ b/servo/client.ts @@ -16,6 +16,7 @@ namespace modules { private readonly _maxPulse: jacdac.RegisterClient<[number]> private readonly _stallTorque: jacdac.RegisterClient<[number]> private readonly _responseSpeed: jacdac.RegisterClient<[number]> + private _continuousMode: boolean = undefined constructor(role: string) { super(jacdac.SRV_SERVO, role, jacdac.ServoRegPack.ActualAngle) @@ -95,18 +96,27 @@ namespace modules { //% value.min=0 //% value.max=180 setAngle(value: number) { + this.internalSetAngle(value) + this.internalSetContinuous(false) + } + + private internalSetAngle(value: number) { this.start() this.setEnabled(true) const values = this._angle.values as any[] - values[0] = value - this._angle.values = values as [number] + if (value !== values[0]) { + values[0] = value + this._angle.values = values as [number] + } } private internalSetContinuous(enabled: boolean) { - this.start() - this.setReg(jacdac.SystemReg.ClientVariant, "s", [ - enabled ? `cont=1` : ``, - ]) + if (this._continuousMode !== enabled) { + this._continuousMode = enabled + this.setReg(jacdac.SystemReg.ClientVariant, "s", [ + enabled ? `cont=1` : ``, + ]) + } } /** @@ -130,7 +140,7 @@ namespace modules { minAngle, maxAngle ) - this.setAngle(degrees) + this.internalSetAngle(degrees) this.internalSetContinuous(true) } @@ -175,8 +185,10 @@ namespace modules { setEnabled(value: boolean) { this.start() const values = this._enabled.values as any[] - values[0] = value ? 1 : 0 - this._enabled.values = values as [boolean] + if (!!value !== !!values[0]) { + values[0] = value ? 1 : 0 + this._enabled.values = values as [boolean] + } } /** @@ -199,8 +211,10 @@ namespace modules { setOffset(value: number) { this.start() const values = this._offset.values as any[] - values[0] = value - this._offset.values = values as [number] + if (value !== values[0]) { + values[0] = value + this._offset.values = values as [number] + } } /** @@ -238,8 +252,10 @@ namespace modules { setMinPulse(value: number) { this.start() const values = this._minPulse.values as any[] - values[0] = value - this._minPulse.values = values as [number] + if (value !== values[0]) { + values[0] = value + this._minPulse.values = values as [number] + } } /** @@ -277,8 +293,10 @@ namespace modules { setMaxPulse(value: number) { this.start() const values = this._maxPulse.values as any[] - values[0] = value - this._maxPulse.values = values as [number] + if (value !== values[0]) { + values[0] = value + this._maxPulse.values = values as [number] + } } /** diff --git a/servo/test.microbit.ts b/servo/test.microbit.ts index 899915024..c74ec3265 100644 --- a/servo/test.microbit.ts +++ b/servo/test.microbit.ts @@ -1,7 +1,14 @@ -basic.showIcon(IconNames.Happy) -input.onButtonPressed(Button.A, () => { - modules.servo1.run(100) -}) -input.onButtonPressed(Button.B, () => { - modules.servo2.run(50) +forever(() => { + modules.servo1.run(50) + // modules.servo2.run(50) + // modules.servo3.run(50) + basic.pause(1000) + modules.servo1.run(0) + // modules.servo2.run(0) + // modules.servo3.run(0) + // basic.pause(1000) }) + +modules.servo1.start() +//modules.servo2.start() +//modules.servo3.start()