class MotorSource extends core.SourceNode {
constructor(source) {
super(source);
this.leftPWM = 0;
this.rightPWM = 0;
this.maxSpeed = 2.0;
}
move(leftPWM, rightPWM) {
this.leftPWM = Math.min(100, Math.max(0, leftPWM));
this.rightPWM = Math.min(100, Math.max(0, rightPWM));
return this.updatePosition();
}
updatePosition() {
return new Promise((resolve, reject) => {
const speedLeft = this.maxSpeed * (this.leftPWM / 100.);
const speedRight = this.maxSpeed * (this.rightPWM / 100.);
const linearVelocity = new core.LinearVelocity(
speedLeft + speedRight,
0,
0
);
const angularVelocity = new core.AngularVelocity(
0,
0,
2 * Math.PI * 0.5 * (speedRight - speedLeft)
);
this.model
.findDataService(core.DataObject)
.findByUID(this.source.uid)
.then(storedObj => {
const position = storedObj.getPosition();
position.timestamp = core.TimeService.now();
position.linearVelocity = linearVelocity;
position.angularVelocity = angularVelocity;
const frame = new core.DataFrame(storedObj);
frame.createdTimestamp = core.TimeService.now();
frame.source.setPosition(position);
return this.push(frame);
})
.then(() => {
resolve();
})
.catch(ex => {
reject(ex);
});
});
}
onPull() {
return this.model
.findDataService(core.DataObject)
.findByUID(this.source.uid)
.then(storedObj => {
const frame = new core.DataFrame(storedObj);
frame.createdTimestamp = core.TimeService.now();
return this.push(frame);
});
}
}