From 7e58b9b6992192436756f9864b87fcfc69ac4f61 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 2 May 2018 22:04:29 -0700 Subject: [PATCH] Motor angle (#559) * control angle with slider * called changed state * remove logging --- sim/state/motornode.ts | 24 +++++++++++++++++------- sim/visuals/controls/motorSlider.ts | 29 ++++++++++------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/sim/state/motornode.ts b/sim/state/motornode.ts index 7a939f33..d7ac70a4 100644 --- a/sim/state/motornode.ts +++ b/sim/state/motornode.ts @@ -17,7 +17,8 @@ namespace pxsim { private speedCmdTime: number; private _synchedMotor: MotorNode; // non-null if synchronized - private manualSpeed: number = undefined; + private manualReferenceAngle: number = undefined; + private manualAngle: number = undefined; constructor(port: number, large: boolean) { super(port); @@ -103,14 +104,18 @@ namespace pxsim { } manualMotorDown() { + this.manualReferenceAngle = this.angle; + this.manualAngle = 0; } - manualMotorMove(speed: number) { - this.manualSpeed = speed; + // position: 0, 360 + manualMotorAngle(angle: number) { + this.manualAngle = angle; } manualMotorUp() { - this.manualSpeed = undefined; + delete this.manualReferenceAngle; + delete this.manualAngle; } updateState(elapsed: number) { @@ -126,7 +131,7 @@ namespace pxsim { } private updateStateStep(elapsed: number) { - if (!this.manualSpeed) { + if (this.manualAngle === undefined) { // compute new speed switch (this.speedCmd) { case DAL.opOutputSpeed: @@ -202,7 +207,11 @@ namespace pxsim { } } else { - this.speed = this.manualSpeed; + // the user is holding the handle - so position is the angle + this.speed = 0; + // rotate by the desired angle change + this.angle = this.manualReferenceAngle + this.manualAngle; + this.setChangedState(); } this.speed = Math.round(this.speed); // integer only @@ -217,7 +226,8 @@ namespace pxsim { // if the motor was stopped or there are no speed commands, // let it coast to speed 0 - if (this.speed && !(this.started || this.speedCmd)) { + if ((this.manualReferenceAngle === undefined) + && this.speed && !(this.started || this.speedCmd)) { // decay speed 5% per tick this.speed = Math.round(Math.max(0, Math.abs(this.speed) - 10) * sign(this.speed)); } diff --git a/sim/visuals/controls/motorSlider.ts b/sim/visuals/controls/motorSlider.ts index d94cabb5..85394bbd 100644 --- a/sim/visuals/controls/motorSlider.ts +++ b/sim/visuals/controls/motorSlider.ts @@ -13,7 +13,7 @@ namespace pxsim.visuals { private static SLIDER_RADIUS = 100; - private internalSpeed: number = 0; + private internalAngle: number = 0; getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) { this.group = svg.elt("g") as SVGGElement; @@ -102,9 +102,9 @@ namespace pxsim.visuals { } else if (dx >= 0 && dy <= 0) { deg = 90 - deg; } - const value = Math.abs(Math.ceil((deg % 360) / 360 * this.getMax())); + const value = Math.abs(Math.ceil((deg % 360))); - this.internalSpeed = value; + this.internalAngle = value; this.updateDial(); this.prevVal = deg; @@ -119,7 +119,7 @@ namespace pxsim.visuals { private handleSliderMove() { this.dial.setAttribute('cursor', '-webkit-grabbing'); const state = this.state; - state.manualMotorMove(this.internalSpeed); + state.manualMotorAngle(this.internalAngle); } private handleSliderUp() { @@ -127,19 +127,18 @@ namespace pxsim.visuals { const state = this.state; state.manualMotorUp(); - this.internalSpeed = 0; + this.internalAngle = 0; this.updateDial(); } private updateDial() { - let speed = this.internalSpeed; + let angle = this.internalAngle; // Update dial position - const deg = speed / this.getMax() * 360; // degrees const radius = MotorSliderControl.SLIDER_RADIUS; const dialRadius = 5; - const x = Math.ceil((radius - dialRadius) * Math.sin(deg * Math.PI / 180)) + radius; - const y = Math.ceil((radius - dialRadius) * -Math.cos(deg * Math.PI / 180)) + radius; + const x = Math.ceil((radius - dialRadius) * Math.sin(angle * Math.PI / 180)) + radius; + const y = Math.ceil((radius - dialRadius) * -Math.cos(angle * Math.PI / 180)) + radius; this.dial.setAttribute('transform', `translate(${x}, ${y})`); } @@ -148,18 +147,10 @@ namespace pxsim.visuals { return; } const node = this.state; - const speed = node.getSpeed(); + const angle = node.getAngle() % 360; // Update reporter - this.reporter.textContent = `${speed}`; - } - - private getMin() { - return 0; - } - - private getMax() { - return 100; + this.reporter.textContent = `${angle}°`; } }