Motor angle (#559)

* control angle with slider

* called changed state

* remove logging
This commit is contained in:
Peli de Halleux 2018-05-02 22:04:29 -07:00 committed by GitHub
parent 08a860bd80
commit 7e58b9b699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 26 deletions

View File

@ -17,7 +17,8 @@ namespace pxsim {
private speedCmdTime: number; private speedCmdTime: number;
private _synchedMotor: MotorNode; // non-null if synchronized private _synchedMotor: MotorNode; // non-null if synchronized
private manualSpeed: number = undefined; private manualReferenceAngle: number = undefined;
private manualAngle: number = undefined;
constructor(port: number, large: boolean) { constructor(port: number, large: boolean) {
super(port); super(port);
@ -103,14 +104,18 @@ namespace pxsim {
} }
manualMotorDown() { manualMotorDown() {
this.manualReferenceAngle = this.angle;
this.manualAngle = 0;
} }
manualMotorMove(speed: number) { // position: 0, 360
this.manualSpeed = speed; manualMotorAngle(angle: number) {
this.manualAngle = angle;
} }
manualMotorUp() { manualMotorUp() {
this.manualSpeed = undefined; delete this.manualReferenceAngle;
delete this.manualAngle;
} }
updateState(elapsed: number) { updateState(elapsed: number) {
@ -126,7 +131,7 @@ namespace pxsim {
} }
private updateStateStep(elapsed: number) { private updateStateStep(elapsed: number) {
if (!this.manualSpeed) { if (this.manualAngle === undefined) {
// compute new speed // compute new speed
switch (this.speedCmd) { switch (this.speedCmd) {
case DAL.opOutputSpeed: case DAL.opOutputSpeed:
@ -202,7 +207,11 @@ namespace pxsim {
} }
} }
else { 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 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, // if the motor was stopped or there are no speed commands,
// let it coast to speed 0 // 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 // decay speed 5% per tick
this.speed = Math.round(Math.max(0, Math.abs(this.speed) - 10) * sign(this.speed)); this.speed = Math.round(Math.max(0, Math.abs(this.speed) - 10) * sign(this.speed));
} }

View File

@ -13,7 +13,7 @@ namespace pxsim.visuals {
private static SLIDER_RADIUS = 100; private static SLIDER_RADIUS = 100;
private internalSpeed: number = 0; private internalAngle: number = 0;
getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) { getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) {
this.group = svg.elt("g") as SVGGElement; this.group = svg.elt("g") as SVGGElement;
@ -102,9 +102,9 @@ namespace pxsim.visuals {
} else if (dx >= 0 && dy <= 0) { } else if (dx >= 0 && dy <= 0) {
deg = 90 - deg; 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.updateDial();
this.prevVal = deg; this.prevVal = deg;
@ -119,7 +119,7 @@ namespace pxsim.visuals {
private handleSliderMove() { private handleSliderMove() {
this.dial.setAttribute('cursor', '-webkit-grabbing'); this.dial.setAttribute('cursor', '-webkit-grabbing');
const state = this.state; const state = this.state;
state.manualMotorMove(this.internalSpeed); state.manualMotorAngle(this.internalAngle);
} }
private handleSliderUp() { private handleSliderUp() {
@ -127,19 +127,18 @@ namespace pxsim.visuals {
const state = this.state; const state = this.state;
state.manualMotorUp(); state.manualMotorUp();
this.internalSpeed = 0; this.internalAngle = 0;
this.updateDial(); this.updateDial();
} }
private updateDial() { private updateDial() {
let speed = this.internalSpeed; let angle = this.internalAngle;
// Update dial position // Update dial position
const deg = speed / this.getMax() * 360; // degrees
const radius = MotorSliderControl.SLIDER_RADIUS; const radius = MotorSliderControl.SLIDER_RADIUS;
const dialRadius = 5; const dialRadius = 5;
const x = Math.ceil((radius - dialRadius) * Math.sin(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(deg * Math.PI / 180)) + radius; const y = Math.ceil((radius - dialRadius) * -Math.cos(angle * Math.PI / 180)) + radius;
this.dial.setAttribute('transform', `translate(${x}, ${y})`); this.dial.setAttribute('transform', `translate(${x}, ${y})`);
} }
@ -148,18 +147,10 @@ namespace pxsim.visuals {
return; return;
} }
const node = this.state; const node = this.state;
const speed = node.getSpeed(); const angle = node.getAngle() % 360;
// Update reporter // Update reporter
this.reporter.textContent = `${speed}`; this.reporter.textContent = `${angle}°`;
}
private getMin() {
return 0;
}
private getMax() {
return 100;
} }
} }