pxt-ev3/sim/state/motor.ts

58 lines
2.3 KiB
TypeScript
Raw Normal View History

2017-12-18 22:04:17 +01:00
namespace pxsim {
enum MotorDataOff {
TachoCounts = 0, // int32
Speed = 4, // int8
Padding = 5, // int8[3]
TachoSensor = 8, // int32
Size = 12
}
export class EV3MotorState {
constructor() {
let data = new Uint8Array(12 * DAL.NUM_OUTPUTS)
MMapMethods.register("/dev/lms_motor", {
data,
beforeMemRead: () => {
2017-12-20 01:18:17 +01:00
const outputs = ev3board().outputNodes;
2017-12-20 02:15:53 +01:00
// console.log("motor before read");
2017-12-18 22:04:17 +01:00
for (let port = 0; port < DAL.NUM_OUTPUTS; ++port) {
2017-12-20 01:18:17 +01:00
const output = outputs[port];
2017-12-20 01:54:44 +01:00
const speed = output ? Math.round(outputs[port].getSpeed()) : 0;
const angle = output ? Math.round(outputs[port].getAngle()) : 0;
2017-12-20 02:15:53 +01:00
const tci = MotorDataOff.TachoCounts + port * MotorDataOff.Size;
2017-12-20 01:54:44 +01:00
const tsi = MotorDataOff.TachoSensor + port * MotorDataOff.Size;
2017-12-20 02:15:53 +01:00
data[tci] = data[tci + 1] = data[tci + 2] = data[tci + 3] = 0; // Tacho count
2017-12-20 01:54:44 +01:00
data[MotorDataOff.Speed + port * MotorDataOff.Size] = speed; // Speed
data[tsi] = angle & 0xff; // Count
data[tsi + 1] = (angle >> 8) & 0xff; // Count
data[tsi + 2] = (angle >> 16) & 0xff; // Count
2017-12-20 02:15:53 +01:00
data[tsi + 3] = (angle >> 24); // Count
2017-12-18 22:04:17 +01:00
}
},
read: buf => {
let v = "vSIM"
for (let i = 0; i < buf.data.length; ++i)
buf.data[i] = v.charCodeAt(i) || 0
console.log("motor read");
console.log(buf.data);
return buf.data.length
},
write: buf => {
if (buf.data.length == 0) return 2;
const cmd = buf.data[0];
console.log("motor write");
console.log(buf);
return 2
},
ioctl: (id, buf) => {
console.log("motor ioctl");
console.log(id);
console.log(buf);
return 2;
}
});
}
}
}