Factoring out a _onSensorValueChanged function.

This commit is contained in:
Evelyn Eastmond 2019-02-04 21:41:57 -05:00
parent e87dd01629
commit da3fc930b1

View file

@ -200,65 +200,70 @@ class GdxFor {
_onConnect () { _onConnect () {
const adapter = new ScratchLinkDeviceAdapter(this._scratchLinkSocket, BLEUUID); const adapter = new ScratchLinkDeviceAdapter(this._scratchLinkSocket, BLEUUID);
godirect.createDevice(adapter, {open: true, startMeasurements: false}).then(device => { godirect.createDevice(adapter, {open: true, startMeasurements: false}).then(device => {
// Setup device
this._device = device; this._device = device;
this._device.keepValues = false; // todo: possibly remove after updating Vernier godirect module this._device.keepValues = false; // todo: possibly remove after updating Vernier godirect module
this._startMeasurements();
// Enable sensors
this._device.sensors.forEach(sensor => {
sensor.setEnabled(true);
});
// Set sensor value-update behavior
this._device.on('measurements-started', () => {
const enabledSensors = this._device.sensors.filter(s => s.enabled);
enabledSensors.forEach(sensor => {
sensor.on('value-changed', s => {
this._onSensorValueChanged(s);
});
});
});
// Start device
this._device.start(10); // Set the period to 10 milliseconds
}); });
} }
/** // TODO: JSDoc
* Enable and begin reading measurements _onSensorValueChanged (sensor) {
* @private let val = sensor.value;
*/ const framesPerSec = 1000 / this._runtime.currentStepTime;
_startMeasurements () {
this._device.sensors.forEach(sensor => { switch (sensor.number) {
sensor.setEnabled(true); case GDXFOR_SENSOR.FORCE:
}); // Normalize the force, which can be measured between -50 and 50 N,
this._device.on('measurements-started', () => { // to be a value between -100 and 100.
const enabledSensors = this._device.sensors.filter(s => s.enabled); val = MathUtil.clamp(val * 2, -100, 100);
enabledSensors.forEach(sensor => { this._sensors.force = val;
sensor.on('value-changed', s => { break;
let val = s.value; // TODO: rename/replace case GDXFOR_SENSOR.ACCELERATION_X:
const framesPerSec = 1000 / this._runtime.currentStepTime; this._sensors.accelerationX = sensor.value;
switch (s.number) { break;
case GDXFOR_SENSOR.FORCE: case GDXFOR_SENSOR.ACCELERATION_Y:
// Normalize the force, which can be measured between -50 and 50 N, this._sensors.accelerationY = sensor.value;
// to be a value between -100 and 100. break;
val = MathUtil.clamp(val * 2, -100, 100); case GDXFOR_SENSOR.ACCELERATION_Z:
this._sensors.force = val; this._sensors.accelerationZ = sensor.value;
break; break;
case GDXFOR_SENSOR.ACCELERATION_X: case GDXFOR_SENSOR.SPIN_SPEED_X:
this._sensors.accelerationX = s.value; val = MathUtil.radToDeg(val);
break; val = val / framesPerSec; // convert to from degrees per sec to degrees per frame
case GDXFOR_SENSOR.ACCELERATION_Y: val = val * -1;
this._sensors.accelerationY = s.value; this._sensors.spinSpeedX = val;
break; break;
case GDXFOR_SENSOR.ACCELERATION_Z: case GDXFOR_SENSOR.SPIN_SPEED_Y:
this._sensors.accelerationZ = s.value; val = MathUtil.radToDeg(val);
break; val = val / framesPerSec; // convert to from degrees per sec to degrees per frame
case GDXFOR_SENSOR.SPIN_SPEED_X: val = val * -1;
val = MathUtil.radToDeg(val); this._sensors.spinSpeedY = val;
val = val / framesPerSec; // convert to from degrees per sec to degrees per frame break;
val = val * -1; case GDXFOR_SENSOR.SPIN_SPEED_Z:
this._sensors.spinSpeedX = val; val = MathUtil.radToDeg(val);
break; val = val / framesPerSec; // convert to from degrees per sec to degrees per frame
case GDXFOR_SENSOR.SPIN_SPEED_Y: val = val * -1;
val = MathUtil.radToDeg(val); this._sensors.spinSpeedZ = val;
val = val / framesPerSec; // convert to from degrees per sec to degrees per frame break;
val = val * -1; }
this._sensors.spinSpeedY = val;
break;
case GDXFOR_SENSOR.SPIN_SPEED_Z:
val = MathUtil.radToDeg(val);
val = val / framesPerSec; // convert to from degrees per sec to degrees per frame
val = val * -1;
this._sensors.spinSpeedZ = val;
break;
}
});
});
});
this._device.start(10); // Set the period to 10 milliseconds
} }
getForce () { getForce () {