fixing isReady query on motors (#197)

This commit is contained in:
Peli de Halleux 2018-01-06 00:00:04 -08:00 committed by GitHub
parent 77fb64043d
commit 9ae6482f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 15 deletions

View File

@ -270,13 +270,7 @@ namespace motors {
const buf = mkCmd(this._port, DAL.opOutputTest, 2); const buf = mkCmd(this._port, DAL.opOutputTest, 2);
readPWM(buf) readPWM(buf)
const flags = buf.getNumber(NumberFormat.UInt8LE, 2); const flags = buf.getNumber(NumberFormat.UInt8LE, 2);
// TODO: FIX with ~ support return (~flags & this._port) == this._port;
for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
const flag = 1 << i;
if ((this._port & flag) && (flags & flag))
return false;
}
return true;
} }
/** /**
@ -448,7 +442,7 @@ namespace motors {
private __setSpeed(speed: number) { private __setSpeed(speed: number) {
syncMotors(this._port, { syncMotors(this._port, {
speed: speed, speed: speed,
turnRatio: 100, // same speed turnRatio: 0, // same speed
useBrake: !!this._brake useBrake: !!this._brake
}) })
} }
@ -457,7 +451,7 @@ namespace motors {
syncMotors(this._port, { syncMotors(this._port, {
useSteps: steps, useSteps: steps,
speed: speed, speed: speed,
turnRatio: 100, // same speed turnRatio: 0, // same speed
stepsOrTime: stepsOrTime, stepsOrTime: stepsOrTime,
useBrake: this._brake useBrake: this._brake
}); });

View File

@ -22,6 +22,10 @@ namespace pxsim {
this.setLarge(large); this.setLarge(large);
} }
isReady() {
return !this.speedCmd;
}
getSpeed() { getSpeed() {
return this.speed * (this.polarity == 0 ? -1 : 1); return this.speed * (this.polarity == 0 ? -1 : 1);
} }
@ -154,8 +158,21 @@ namespace pxsim {
if (brake) this.speed = 0; if (brake) this.speed = 0;
this.clearSpeedCmd(); this.clearSpeedCmd();
} }
// send synched motor state
otherMotor.speed = Math.floor(this.speed * turnRatio / 100); // turn ratio is a bit weird to interpret
// see https://communities.theiet.org/blogs/698/1706
if (turnRatio < 0) {
otherMotor.speed = speed;
this.speed *= (100 + turnRatio) / 100;
} else {
otherMotor.speed = this.speed * (100 - turnRatio) / 100;
}
// clamp
this.speed = Math.max(-100, Math.min(100, this.speed >> 0));
otherMotor.speed = Math.max(-100, Math.min(100, otherMotor.speed >> 0));;
// stop other motor if needed
if (!this._synchedMotor) if (!this._synchedMotor)
otherMotor.clearSpeedCmd(); otherMotor.clearSpeedCmd();
break; break;

View File

@ -12,10 +12,24 @@ namespace pxsim {
data[i] = 0 data[i] = 0
}, },
read: buf => { read: buf => {
// console.log("pwm read");
if (buf.data.length == 0) return 2;
const cmd = buf.data[0];
switch (cmd) {
case DAL.opOutputTest:
const port = buf.data[1];
let r = 0;
ev3board().getMotor(port)
.filter(motor => !motor.isReady())
.forEach(motor => r |= (1 << motor.port));
pxsim.BufferMethods.setNumber(buf, BufferMethods.NumberFormat.UInt8LE, 2, r);
break;
default:
let v = "vSIM" let v = "vSIM"
for (let i = 0; i < buf.data.length; ++i) for (let i = 0; i < buf.data.length; ++i)
buf.data[i] = v.charCodeAt(i) || 0 buf.data[i] = v.charCodeAt(i) || 0
console.log("pwm read"); break;
}
return buf.data.length return buf.data.length
}, },
write: buf => { write: buf => {