Skip to content

Commit

Permalink
fix-sim-sync-metods
Browse files Browse the repository at this point in the history
  • Loading branch information
THEb0nny committed Mar 13, 2024
1 parent 46487f2 commit e106c7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 9 additions & 4 deletions sim/state/motornode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ namespace pxsim {
}

getSpeed() {
return Math.round(this.speed * (this._inverted ? -1 : 1));
return Math.round(this.speed * this.invertedFactor());
}

getAngle() {
return Math.round(this.angle * (this._inverted ? -1 : 1));
return Math.round(this.angle * this.invertedFactor());
}

// returns the secondary motor if any
Expand All @@ -57,6 +57,7 @@ namespace pxsim {
}

setSyncCmd(motor: MotorNode, cmd: DAL, values: number[]) {
console.log(`motor: ${motor.port}, speed: ${values[0]}, turnRatio: ${values[0]}`);
this.setSpeedCmd(cmd, values);
this._synchedMotor = motor;
}
Expand Down Expand Up @@ -88,6 +89,10 @@ namespace pxsim {
return this._inverted;
}

invertedFactor(): number {
return this._inverted ? -1 : 1
}

isLarge(): boolean {
return this.id == NodeType.LargeMotor;
}
Expand Down Expand Up @@ -206,15 +211,15 @@ namespace pxsim {
: this.tacho - this.speedCmdTacho;
// 0 is special case, run infinite
if (!stepsOrTime || dstep < stepsOrTime)
this.speed = speed;
this.speed = speed * this.invertedFactor();
else {
if (brake) this.speed = 0;
this.clearSpeedCmd();
}

// turn ratio is a bit weird to interpret
// see https://communities.theiet.org/blogs/698/1706
otherMotor.speed = this.speed * (100 - Math.abs(turnRatio)) / 100;
otherMotor.speed = this.speed * otherMotor.invertedFactor() * (100 - Math.abs(turnRatio)) / 100;

// clamp
this.speed = Math.max(-100, Math.min(100, this.speed >> 0));
Expand Down
6 changes: 4 additions & 2 deletions sim/state/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ namespace pxsim {
const motors = ev3board().getMotor(port);
// cancel any other sync command
for(const motor of ev3board().getMotors().filter(motor => motors.indexOf(motor) < 0)) {
motor.clearSyncCmd()
motor.clearSyncCmd();
}

// apply commands to all motors
for (const motor of motors) {
const invertedFactor = motor.isInverted() ? -1 : 1;
//console.log(`motor.port: ${motor.port}, invertedFactor: ${invertedFactor}, speed * inv: ${speed * invertedFactor}`);
const otherMotor = motors.filter(m => m.port != motor.port)[0];
motor.setSyncCmd(
otherMotor,
cmd, [speed, turnRatio, stepsOrTime, brake]);
cmd, [speed * invertedFactor, turnRatio, stepsOrTime, brake]);
}
return 2;
}
Expand Down

0 comments on commit e106c7f

Please sign in to comment.