Snap backwards (#923)

* snap to multiple of 5

* normalize negative values
This commit is contained in:
Peli de Halleux 2019-09-27 15:49:21 -07:00 committed by GitHub
parent 8047cb2612
commit e862869327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -76,9 +76,12 @@ export class FieldSpeed extends Blockly.FieldSlider implements Blockly.FieldCust
}; };
setReadout_(readout: Element, value: string) { setReadout_(readout: Element, value: string) {
this.updateSpeed(parseFloat(value)); let x = parseFloat(value) || 0;
// snap on multiple of 5
x = Math.round(x / 5) * 5;
this.updateSpeed(x);
// Update reporter // Update reporter
this.reporter.textContent = `${value}%`; this.reporter.textContent = `${x}%`;
} }
private updateSpeed(speed: number) { private updateSpeed(speed: number) {

View File

@ -301,9 +301,17 @@ namespace motors {
case MoveUnit.Rotations: case MoveUnit.Rotations:
scale = 360; scale = 360;
r.useSteps = true; r.useSteps = true;
if (r.steps[1] < 0) {
r.speed = -r.speed;
r.steps[1] = -r.steps[1];
}
break; break;
case MoveUnit.Degrees: case MoveUnit.Degrees:
r.useSteps = true; r.useSteps = true;
if (r.steps[1] < 0) {
r.speed = -r.speed;
r.steps[1] = -r.steps[1];
}
break; break;
case MoveUnit.Seconds: case MoveUnit.Seconds:
scale = 1000; scale = 1000;
@ -416,28 +424,28 @@ namespace motors {
temp = Math.max(0, (value * 360) | 0); temp = Math.max(0, (value * 360) | 0);
if (phase == MovePhase.Acceleration) if (phase == MovePhase.Acceleration)
this._accelerationSteps = temp; this._accelerationSteps = temp;
else else
this._decelerationSteps = temp; this._decelerationSteps = temp;
break; break;
case MoveUnit.Degrees: case MoveUnit.Degrees:
temp = Math.max(0, value | 0); temp = Math.max(0, value | 0);
if (phase == MovePhase.Acceleration) if (phase == MovePhase.Acceleration)
this._accelerationSteps = temp; this._accelerationSteps = temp;
else else
this._decelerationSteps = temp; this._decelerationSteps = temp;
break; break;
case MoveUnit.Seconds: case MoveUnit.Seconds:
temp = Math.max(0, (value * 1000) | 0); temp = Math.max(0, (value * 1000) | 0);
if (phase == MovePhase.Acceleration) if (phase == MovePhase.Acceleration)
this._accelerationTime = temp; this._accelerationTime = temp;
else else
this._decelerationTime = temp; this._decelerationTime = temp;
break; break;
case MoveUnit.MilliSeconds: case MoveUnit.MilliSeconds:
temp = Math.max(0, value | 0); temp = Math.max(0, value | 0);
if (phase == MovePhase.Acceleration) if (phase == MovePhase.Acceleration)
this._accelerationTime = temp; this._accelerationTime = temp;
else else
this._decelerationTime = temp; this._decelerationTime = temp;
break; break;
} }
@ -741,10 +749,18 @@ namespace motors {
let stepsOrTime: number; let stepsOrTime: number;
switch (unit) { switch (unit) {
case MoveUnit.Rotations: case MoveUnit.Rotations:
if (value < 0) {
value = -value;
speed = -speed;
}
stepsOrTime = (value * 360) >> 0; stepsOrTime = (value * 360) >> 0;
useSteps = true; useSteps = true;
break; break;
case MoveUnit.Degrees: case MoveUnit.Degrees:
if (value < 0) {
value = -value;
speed = -speed;
}
stepsOrTime = value >> 0; stepsOrTime = value >> 0;
useSteps = true; useSteps = true;
break; break;