From 4963bf776ad4dc2c8550367d8172d30fce6fed06 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 1 Nov 2016 08:16:03 -0700 Subject: [PATCH] basic servo simulation (#297) --- libs/core/pins.cpp | 1 + libs/core/pxtparts.json | 31 +++++++++++++++++++++++++++++++ libs/core/shims.d.ts | 3 ++- sim/dalboard.ts | 13 +++++++++---- sim/state/edgeconnector.ts | 8 ++++++-- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/libs/core/pins.cpp b/libs/core/pins.cpp index 3cd31cc2..74c41332 100644 --- a/libs/core/pins.cpp +++ b/libs/core/pins.cpp @@ -201,6 +201,7 @@ namespace pins { */ //% help=pins/servo-write-pin weight=20 //% blockId=device_set_servo_pin block="servo write|pin %name|to %value" blockGap=8 + //% parts=microservo trackArgs=0 void servoWritePin(AnalogPin name, int value) { PINOP(setServoValue(value)); } diff --git a/libs/core/pxtparts.json b/libs/core/pxtparts.json index 4afc3f78..09369c1a 100644 --- a/libs/core/pxtparts.json +++ b/libs/core/pxtparts.json @@ -29,6 +29,37 @@ {"pinIndices": [2, 3]} ] }, + "microservo": { + "simulationBehavior": "microservo", + "visual": { + "builtIn": "microservo", + "width": 74.85, + "height": 200, + "pinDistance": 10, + "pinLocations": [ + {"x": 30, "y": 5}, + {"x": 37, "y": 5}, + {"x": 45, "y": 5} + ] + }, + "numberOfPins": 3, + "pinDefinitions": [ + {"target": {"pinInstantiationIdx": 0}, "style": "croc", "orientation": "+Z"}, + {"target": "threeVolt", "style": "croc", "orientation": "+Z"}, + {"target": "ground", "style": "croc", "orientation": "+Z"} + ], + "instantiation": { + "kind": "function", + "fullyQualifiedName": "pins.servoWritePin", + "argumentRoles": [ + {"pinInstantiationIdx": 0, "partParameter": "name"} + ] + }, + "assembly": [ + {"part": true, "pinIndices": [2]}, + {"pinIndices": [0, 1]} + ] + }, "neopixel": { "simulationBehavior": "neopixel", "visual": { diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index d2346ad3..e060c3ff 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -585,7 +585,8 @@ declare namespace pins { * @param value angle or rotation speed, eg:180,90,0 */ //% help=pins/servo-write-pin weight=20 - //% blockId=device_set_servo_pin block="servo write|pin %name|to %value" blockGap=8 shim=pins::servoWritePin + //% blockId=device_set_servo_pin block="servo write|pin %name|to %value" blockGap=8 + //% parts=microservo trackArgs=0 shim=pins::servoWritePin function servoWritePin(name: AnalogPin, value: number): void; /** diff --git a/sim/dalboard.ts b/sim/dalboard.ts index b888a7bc..67140948 100644 --- a/sim/dalboard.ts +++ b/sim/dalboard.ts @@ -12,7 +12,9 @@ namespace pxsim { lightSensorState: LightSensorState; buttonPairState: ButtonPairState; radioState: RadioState; + // TODO: not singletons neopixelState: NeoPixelState; + microServoState: MicroServoState; constructor() { super() @@ -48,7 +50,7 @@ namespace pxsim { 0, 0, DAL.MICROBIT_ID_IO_P19, - DAL.MICROBIT_ID_IO_P20 + DAL.MICROBIT_ID_IO_P20 ] }); this.builtinParts["radio"] = this.radioState = new RadioState(runtime); @@ -58,14 +60,17 @@ namespace pxsim { this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState(); this.builtinParts["compass"] = this.compassState = new CompassState(); this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState(); + this.builtinParts["microservo"] = this.microServoState = new MicroServoState(); this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView(); this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView(); - this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView(); + this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView(); + this.builtinVisuals["microservo"] = () => new visuals.MicroServoView(); this.builtinPartVisuals["buttonpair"] = (xy: visuals.Coord) => visuals.mkBtnSvg(xy); this.builtinPartVisuals["ledmatrix"] = (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8); - this.builtinPartVisuals["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy); + this.builtinPartVisuals["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy); + this.builtinPartVisuals["microservo"] = (xy: visuals.Coord) => visuals.mkMicroServoPart(xy); } receiveMessage(msg: SimulatorMessage) { @@ -97,7 +102,7 @@ namespace pxsim { const cmpDefs = msg.partDefinitions || {}; const fnArgs = msg.fnArgs; - const opts : visuals.BoardHostOpts = { + const opts: visuals.BoardHostOpts = { state: this, boardDef: boardDef, partsList: cmpsList, diff --git a/sim/state/edgeconnector.ts b/sim/state/edgeconnector.ts index 1f4cef29..feae5343 100644 --- a/sim/state/edgeconnector.ts +++ b/sim/state/edgeconnector.ts @@ -11,7 +11,7 @@ namespace pxsim.input { if (!pin) return; pin.isTouched(); pxtcore.registerWithDal(pin.id, DAL.MICROBIT_BUTTON_EVT_UP, handler); - } + } export function pinIsPressed(pinId: number): boolean { let pin = getPin(pinId); @@ -73,7 +73,11 @@ namespace pxsim.pins { export function servoWritePin(pinId: number, value: number) { analogSetPeriod(pinId, 20000); - // TODO + // TODO: per pin state + if (board().microServoState.angle != value) { + board().microServoState.angle = value; + runtime.queueDisplayUpdate(); + } } export function servoSetPulse(pinId: number, micros: number) {