polarity in synched motor (#945)

* account for polarity

* more comments

* handle dual motor in runtime

* invert steer

* don't use firmware polarity
This commit is contained in:
Peli de Halleux 2019-10-10 07:44:53 -07:00 committed by GitHub
parent b0de3d8c1b
commit 2cd2950496
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 22 deletions

View File

@ -159,6 +159,7 @@ namespace motors {
private _accelerationTime: number; private _accelerationTime: number;
private _decelerationSteps: number; private _decelerationSteps: number;
private _decelerationTime: number; private _decelerationTime: number;
private _inverted: boolean;
protected static output_types: number[] = [0x7, 0x7, 0x7, 0x7]; protected static output_types: number[] = [0x7, 0x7, 0x7, 0x7];
@ -176,6 +177,7 @@ namespace motors {
this._accelerationTime = 0; this._accelerationTime = 0;
this._decelerationSteps = 0; this._decelerationSteps = 0;
this._decelerationTime = 0; this._decelerationTime = 0;
this._inverted = false;
} }
/** /**
@ -225,9 +227,11 @@ namespace motors {
//% help=motors/motor/set-inverted //% help=motors/motor/set-inverted
setInverted(inverted: boolean) { setInverted(inverted: boolean) {
this.init(); this.init();
const b = mkCmd(this._port, DAL.opOutputPolarity, 1) this._inverted = inverted;
b.setNumber(NumberFormat.Int8LE, 2, inverted ? 0 : 1); }
writePWM(b)
protected invertedFactor(): number {
return this._inverted ? -1 : 1;
} }
/** /**
@ -294,8 +298,9 @@ namespace motors {
} }
private normalizeSchedule(speed: number, step1: number, step2: number, step3: number, unit: MoveUnit): MoveSchedule { private normalizeSchedule(speed: number, step1: number, step2: number, step3: number, unit: MoveUnit): MoveSchedule {
// motor polarity is not supported at the firmware level for sync motor operations
const r: MoveSchedule = { const r: MoveSchedule = {
speed: Math.clamp(-100, 100, speed >> 0), speed: Math.clamp(-100, 100, speed | 0) * this.invertedFactor(),
useSteps: true, useSteps: true,
steps: [step1 || 0, step2 || 0, step3 || 0] steps: [step1 || 0, step2 || 0, step3 || 0]
} }
@ -562,6 +567,7 @@ namespace motors {
private __init() { private __init() {
this.setOutputType(this._large); this.setOutputType(this._large);
this.setInverted(false);
} }
/** /**
@ -741,7 +747,7 @@ namespace motors {
//% help=motors/synced/steer //% help=motors/synced/steer
steer(turnRatio: number, speed: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) { steer(turnRatio: number, speed: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) {
this.init(); this.init();
speed = Math.clamp(-100, 100, speed >> 0); speed = Math.clamp(-100, 100, speed >> 0) * this.invertedFactor();
if (!speed) { if (!speed) {
this.stop(); this.stop();
return; return;

View File

@ -9,7 +9,6 @@ namespace pxsim {
private angle: number = 0; private angle: number = 0;
private tacho: number = 0; private tacho: number = 0;
private speed: number = 0; private speed: number = 0;
private polarity: number = 1; // -1, 1 or -1
private started: boolean; private started: boolean;
private speedCmd: DAL; private speedCmd: DAL;
@ -31,7 +30,7 @@ namespace pxsim {
} }
getSpeed() { getSpeed() {
return Math.round(this.speed * (!this._synchedMotor && this.polarity == 0 ? -1 : 1)); return Math.round(this.speed);
} }
getAngle() { getAngle() {
@ -82,16 +81,6 @@ namespace pxsim {
return this.id == NodeType.LargeMotor; return this.id == NodeType.LargeMotor;
} }
setPolarity(polarity: number) {
// Either 1 or 255 (reverse)
/*
-1 : Motor will run backward
0 : Motor will run opposite direction
1 : Motor will run forward
*/
this.polarity = polarity;
}
reset() { reset() {
// not sure what reset does... // not sure what reset does...
} }

View File

@ -119,11 +119,7 @@ namespace pxsim {
return 2; return 2;
} }
case DAL.opOutputPolarity: { case DAL.opOutputPolarity: {
// reverse console.error("opOutputPolarity not supported");
const port = buf.data[1];
const polarity = pxsim.BufferMethods.getNumber(buf, BufferMethods.NumberFormat.Int8LE, 2);
const motors = ev3board().getMotor(port);
motors.forEach(motor => motor.setPolarity(polarity));
return 2; return 2;
} }
case DAL.opOutputSetType: { case DAL.opOutputSetType: {