Skip to content

Commit

Permalink
packet protocol optimization (#102)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pelikhan authored Oct 20, 2023
1 parent acc82bd commit 4424214
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 39 deletions.
4 changes: 4 additions & 0 deletions rotary-encoder/clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace modules {
//% fixedInstance whenUsed weight=2 block="rotary encoder2"
export const rotaryEncoder2 = new RotaryEncoderClient("rotary Encoder2")
}
16 changes: 5 additions & 11 deletions rotary-encoder/pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
9 changes: 9 additions & 0 deletions rotary-encoder/test.microbit.ts
Original file line number Diff line number Diff line change
@@ -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)
})
22 changes: 15 additions & 7 deletions routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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)))

Expand Down
48 changes: 33 additions & 15 deletions servo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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` : ``,
])
}
}

/**
Expand All @@ -130,7 +140,7 @@ namespace modules {
minAngle,
maxAngle
)
this.setAngle(degrees)
this.internalSetAngle(degrees)
this.internalSetContinuous(true)
}

Expand Down Expand Up @@ -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]
}
}

/**
Expand All @@ -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]
}
}

/**
Expand Down Expand Up @@ -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]
}
}

/**
Expand Down Expand Up @@ -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]
}
}

/**
Expand Down
19 changes: 13 additions & 6 deletions servo/test.microbit.ts
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 4424214

Please sign in to comment.