From eb4f6235d972228776a718104c1cc9d7f03693c2 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Sun, 3 Feb 2019 17:52:54 -0500 Subject: [PATCH 1/8] Testing new sensor array to store sensor data on/offline. --- src/extension-support/extension-manager.js | 6 ++-- src/extensions/scratch3_gdx_for/index.js | 35 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index bdebb2ac..7a905af0 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -19,7 +19,7 @@ const Scratch3Ev3Blocks = require('../extensions/scratch3_ev3'); const Scratch3MakeyMakeyBlocks = require('../extensions/scratch3_makeymakey'); // todo: only load this extension once we have a compatible way to load its // Vernier module dependency. -// const Scratch3GdxForBlocks = require('../extensions/scratch3_gdx_for'); +const Scratch3GdxForBlocks = require('../extensions/scratch3_gdx_for'); const builtinExtensions = { pen: Scratch3PenBlocks, @@ -31,8 +31,8 @@ const builtinExtensions = { videoSensing: Scratch3VideoSensingBlocks, speech2text: Scratch3Speech2TextBlocks, ev3: Scratch3Ev3Blocks, - makeymakey: Scratch3MakeyMakeyBlocks - // gdxfor: Scratch3GdxForBlocks + makeymakey: Scratch3MakeyMakeyBlocks, + gdxfor: Scratch3GdxForBlocks }; /** diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 0a1b2bee..46f00a47 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -101,6 +101,21 @@ class GdxFor { */ this._extensionId = extensionId; + /** + * The most recently received value for each sensor. + * @type {Object.} + * @private + */ + this._sensors = { + force: 0, + accelerationX: 0, + accelerationY: 0, + accelerationZ: 0, + gyroX: 0, + gyroY: 0, + gyroZ: 0 + }; + this.disconnect = this.disconnect.bind(this); this._onConnect = this._onConnect.bind(this); this._sensorsEnabled = null; @@ -181,6 +196,26 @@ class GdxFor { }); this._device.on('measurements-started', () => { this._sensorsEnabled = true; + const enabledSensors = this._device.sensors.filter(s => s.enabled); + enabledSensors.forEach(sensor => { + sensor.on('value-changed', s => { + // log the sensor name, new value, and units. + // console.log(`Sensor: ${s.name} value: ${s.value} units: ${s.units}`); + console.log(typeof(s.name)); + console.log(s.name.valueOf()); + if (s.name.valueOf() === 'Force') { + this._senors.accelerationX = s.value; + console.log('HERE ============'); + } + if (s.name === 'Y-axis acceleration') { + this._senors.accelerationY = s.value; + } + if (s.name === 'Z-axis acceleration') { + this._senors.accelerationZ = s.value; + } + // console.log(this._sensors); + }); + }); }); this._device.start(10); // Set the period to 10 milliseconds } From d84dd72a8c23c0f38acc132d7264ca74a1e819cc Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Sun, 3 Feb 2019 18:31:45 -0500 Subject: [PATCH 2/8] Removing canReadSensors and sensorsEnabled, first pass. --- src/extensions/scratch3_gdx_for/index.js | 281 +++++++++++------------ 1 file changed, 138 insertions(+), 143 deletions(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 46f00a47..9067555a 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -25,6 +25,19 @@ const BLEUUID = { responseChar: 'b41e6675-a329-40e0-aa01-44d2f444babe' }; +/** + * TODO + */ +const GDXFOR_SENSOR = { + FORCE: 1, + ACCELERATION_X: 2, + ACCELERATION_Y: 3, + ACCELERATION_Z: 4, + SPIN_SPEED_X: 5, + SPIN_SPEED_Y: 6, + SPIN_SPEED_Z: 7 +}; + /** * Threshold for pushing and pulling force, for the whenForcePushedOrPulled hat block. * @type {number} @@ -118,7 +131,6 @@ class GdxFor { this.disconnect = this.disconnect.bind(this); this._onConnect = this._onConnect.bind(this); - this._sensorsEnabled = null; } @@ -155,7 +167,6 @@ class GdxFor { * Disconnect from the GDX FOR. */ disconnect () { - this._sensorsEnabled = false; if (this._scratchLinkSocket) { this._scratchLinkSocket.disconnect(); } @@ -195,188 +206,172 @@ class GdxFor { sensor.setEnabled(true); }); this._device.on('measurements-started', () => { - this._sensorsEnabled = true; const enabledSensors = this._device.sensors.filter(s => s.enabled); enabledSensors.forEach(sensor => { sensor.on('value-changed', s => { - // log the sensor name, new value, and units. - // console.log(`Sensor: ${s.name} value: ${s.value} units: ${s.units}`); - console.log(typeof(s.name)); - console.log(s.name.valueOf()); - if (s.name.valueOf() === 'Force') { - this._senors.accelerationX = s.value; - console.log('HERE ============'); + let val = s.value; // TODO: rename/replace + const framesPerSec = 1000 / this._runtime.currentStepTime; + switch (s.number) { + case GDXFOR_SENSOR.FORCE: + // Normalize the force, which can be measured between -50 and 50 N, + // to be a value between -100 and 100. + val = MathUtil.clamp(val * 2, -100, 100); + this._sensors.force = val; + break; + case GDXFOR_SENSOR.ACCELERATION_X: + this._sensors.accelerationX = s.value; + break; + case GDXFOR_SENSOR.ACCELERATION_Y: + this._sensors.accelerationY = s.value; + break; + case GDXFOR_SENSOR.ACCELERATION_Z: + this._sensors.accelerationZ = s.value; + break; + case GDXFOR_SENSOR.GYRO_X: + val = MathUtil.radToDeg(val); + val = val / framesPerSec; // convert to from degrees per sec to degrees per frame + val = val * -1; + this._sensors.spinSpeedX = val; + break; + case GDXFOR_SENSOR.GYRO_Y: + val = MathUtil.radToDeg(val); + val = val / framesPerSec; // convert to from degrees per sec to degrees per frame + val = val * -1; + this._sensors.spinSpeedY = val; + break; + case GDXFOR_SENSOR.GYRO_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; } - if (s.name === 'Y-axis acceleration') { - this._senors.accelerationY = s.value; - } - if (s.name === 'Z-axis acceleration') { - this._senors.accelerationZ = s.value; - } - // console.log(this._sensors); }); }); }); this._device.start(10); // Set the period to 10 milliseconds } - /** - * Device is connected and measurements enabled - * @return {boolean} - whether the goforce is connected and measurements started. - */ - _canReadSensors () { - return this.isConnected() && this._sensorsEnabled; - } - getForce () { - if (this._canReadSensors()) { - let force = this._device.getSensor(1).value; - // Normalize the force, which can be measured between -50 and 50 N, - // to be a value between -100 and 100. - force = MathUtil.clamp(force * 2, -100, 100); - return force; - } - return 0; + return this._sensors.force; } getTiltX () { - if (this._canReadSensors()) { - let x = this.getAccelerationX(); - let y = this.getAccelerationY(); - let z = this.getAccelerationZ(); + let x = this.getAccelerationX(); + let y = this.getAccelerationY(); + let z = this.getAccelerationZ(); - let xSign = 1; - let ySign = 1; - let zSign = 1; + let xSign = 1; + let ySign = 1; + let zSign = 1; - if (x < 0.0) { - x *= -1.0; xSign = -1; - } - if (y < 0.0) { - y *= -1.0; ySign = -1; - } - if (z < 0.0) { - z *= -1.0; zSign = -1; - } - - // Compute the yz unit vector - const z2 = z * z; - const x2 = x * x; - let value = z2 + x2; - value = Math.sqrt(value); - - // For sufficiently small zy vector values we are essentially at 90 degrees. - // The following snaps to 90 and avoids divide-by-zero errors. - // The snap factor was derived through observation -- just enough to - // still allow single degree steps up to 90 (..., 87, 88, 89, 90). - if (value < 0.35) { - value = 90; - } else { - // Compute the x-axis angle - value = y / value; - value = Math.atan(value); - value *= 57.2957795; // convert from rad to deg - } - // Manage the sign of the result - let xzSign = xSign; - if (z > x) xzSign = zSign; - if (xzSign === -1) value = 180.0 - value; - value *= ySign; - // Round the result to the nearest degree - value += 0.5; - return value; + if (x < 0.0) { + x *= -1.0; xSign = -1; } - return 0; + if (y < 0.0) { + y *= -1.0; ySign = -1; + } + if (z < 0.0) { + z *= -1.0; zSign = -1; + } + + // Compute the yz unit vector + const z2 = z * z; + const x2 = x * x; + let value = z2 + x2; + value = Math.sqrt(value); + + // For sufficiently small zy vector values we are essentially at 90 degrees. + // The following snaps to 90 and avoids divide-by-zero errors. + // The snap factor was derived through observation -- just enough to + // still allow single degree steps up to 90 (..., 87, 88, 89, 90). + if (value < 0.35) { + value = 90; + } else { + // Compute the x-axis angle + value = y / value; + value = Math.atan(value); + value *= 57.2957795; // convert from rad to deg + } + // Manage the sign of the result + let xzSign = xSign; + if (z > x) xzSign = zSign; + if (xzSign === -1) value = 180.0 - value; + value *= ySign; + // Round the result to the nearest degree + value += 0.5; + return value; } getTiltY () { - if (this._canReadSensors()) { - let x = this.getAccelerationX(); - let y = this.getAccelerationY(); - let z = this.getAccelerationZ(); + let x = this.getAccelerationX(); + let y = this.getAccelerationY(); + let z = this.getAccelerationZ(); - let xSign = 1; - let ySign = 1; - let zSign = 1; + let xSign = 1; + let ySign = 1; + let zSign = 1; - if (x < 0.0) { - x *= -1.0; xSign = -1; - } - if (y < 0.0) { - y *= -1.0; ySign = -1; - } - if (z < 0.0) { - z *= -1.0; zSign = -1; - } - - // Compute the yz unit vector - const z2 = z * z; - const y2 = y * y; - let value = z2 + y2; - value = Math.sqrt(value); - - // For sufficiently small zy vector values we are essentially at 90 degrees. - // The following snaps to 90 and avoids divide-by-zero errors. - // The snap factor was derived through observation -- just enough to - // still allow single degree steps up to 90 (..., 87, 88, 89, 90). - if (value < 0.35) { - value = 90; - } else { - // Compute the x-axis angle - value = x / value; - value = Math.atan(value); - value *= 57.2957795; // convert from rad to deg - } - // Manage the sign of the result - let yzSign = ySign; - if (z > y) yzSign = zSign; - if (yzSign === -1) value = 180.0 - value; - value *= xSign; - // Round the result to the nearest degree - value += 0.5; - return value; + if (x < 0.0) { + x *= -1.0; xSign = -1; } - return 0; + if (y < 0.0) { + y *= -1.0; ySign = -1; + } + if (z < 0.0) { + z *= -1.0; zSign = -1; + } + + // Compute the yz unit vector + const z2 = z * z; + const y2 = y * y; + let value = z2 + y2; + value = Math.sqrt(value); + + // For sufficiently small zy vector values we are essentially at 90 degrees. + // The following snaps to 90 and avoids divide-by-zero errors. + // The snap factor was derived through observation -- just enough to + // still allow single degree steps up to 90 (..., 87, 88, 89, 90). + if (value < 0.35) { + value = 90; + } else { + // Compute the x-axis angle + value = x / value; + value = Math.atan(value); + value *= 57.2957795; // convert from rad to deg + } + // Manage the sign of the result + let yzSign = ySign; + if (z > y) yzSign = zSign; + if (yzSign === -1) value = 180.0 - value; + value *= xSign; + // Round the result to the nearest degree + value += 0.5; + return value; } getAccelerationX () { - return this._getAcceleration(2); + return this._sensors.accelerationX; } getAccelerationY () { - return this._getAcceleration(3); + return this._sensors.accelerationY; } getAccelerationZ () { - return this._getAcceleration(4); - } - - _getAcceleration (sensorNum) { - if (!this._canReadSensors()) return 0; - const val = this._device.getSensor(sensorNum).value; - return val; + return this._sensors.accelerationZ; } getSpinSpeedX () { - return this._getSpinSpeed(5); + return this._sensors.spinSpeedX; } getSpinSpeedY () { - return this._getSpinSpeed(6); + return this._sensors.spinSpeedY; } getSpinSpeedZ () { - return this._getSpinSpeed(7); - } - - _getSpinSpeed (sensorNum) { - if (!this._canReadSensors()) return 0; - let val = this._device.getSensor(sensorNum).value; - val = MathUtil.radToDeg(val); - const framesPerSec = 1000 / this._runtime.currentStepTime; - val = val / framesPerSec; // convert to from degrees per sec to degrees per frame - val = val * -1; - return val; + return this._sensors.spinSpeedZ; } } From 9390cd0f8ad067efb812e892c2b97be522a2b815 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Sun, 3 Feb 2019 18:37:00 -0500 Subject: [PATCH 3/8] Fixing 'gyro' labels to 'spin speed'. --- src/extensions/scratch3_gdx_for/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 9067555a..ec6ebfb5 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -124,9 +124,9 @@ class GdxFor { accelerationX: 0, accelerationY: 0, accelerationZ: 0, - gyroX: 0, - gyroY: 0, - gyroZ: 0 + spinSpeedX: 0, + spinSpeedY: 0, + spinSpeedZ: 0 }; this.disconnect = this.disconnect.bind(this); @@ -227,19 +227,19 @@ class GdxFor { case GDXFOR_SENSOR.ACCELERATION_Z: this._sensors.accelerationZ = s.value; break; - case GDXFOR_SENSOR.GYRO_X: + case GDXFOR_SENSOR.SPIN_SPEED_X: val = MathUtil.radToDeg(val); val = val / framesPerSec; // convert to from degrees per sec to degrees per frame val = val * -1; this._sensors.spinSpeedX = val; break; - case GDXFOR_SENSOR.GYRO_Y: + case GDXFOR_SENSOR.SPIN_SPEED_Y: val = MathUtil.radToDeg(val); val = val / framesPerSec; // convert to from degrees per sec to degrees per frame val = val * -1; this._sensors.spinSpeedY = val; break; - case GDXFOR_SENSOR.GYRO_Z: + 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; From 93aecc88be41bff0a6494f59636729f2828b0701 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Sun, 3 Feb 2019 18:45:21 -0500 Subject: [PATCH 4/8] Filling in some comments. --- src/extensions/scratch3_gdx_for/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index ec6ebfb5..52e3aa59 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -26,7 +26,7 @@ const BLEUUID = { }; /** - * TODO + * Sensor ID numbers for the GDX-FOR. */ const GDXFOR_SENSOR = { FORCE: 1, From e87dd01629d0338ae1be0069a159ef4855c7b9c0 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Mon, 4 Feb 2019 19:43:50 -0500 Subject: [PATCH 5/8] Add clearing of values on disconnect. --- src/extensions/scratch3_gdx_for/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 52e3aa59..187aa55f 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -167,6 +167,15 @@ class GdxFor { * Disconnect from the GDX FOR. */ disconnect () { + this._sensors = { + force: 0, + accelerationX: 0, + accelerationY: 0, + accelerationZ: 0, + spinSpeedX: 0, + spinSpeedY: 0, + spinSpeedZ: 0 + }; if (this._scratchLinkSocket) { this._scratchLinkSocket.disconnect(); } From da3fc930b146695c7969872013c7e266c8f67a51 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Mon, 4 Feb 2019 21:41:57 -0500 Subject: [PATCH 6/8] Factoring out a _onSensorValueChanged function. --- src/extensions/scratch3_gdx_for/index.js | 113 ++++++++++++----------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 187aa55f..22ede13e 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -200,65 +200,70 @@ class GdxFor { _onConnect () { const adapter = new ScratchLinkDeviceAdapter(this._scratchLinkSocket, BLEUUID); godirect.createDevice(adapter, {open: true, startMeasurements: false}).then(device => { + // Setup device this._device = device; 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 }); } - /** - * Enable and begin reading measurements - * @private - */ - _startMeasurements () { - this._device.sensors.forEach(sensor => { - sensor.setEnabled(true); - }); - this._device.on('measurements-started', () => { - const enabledSensors = this._device.sensors.filter(s => s.enabled); - enabledSensors.forEach(sensor => { - sensor.on('value-changed', s => { - let val = s.value; // TODO: rename/replace - const framesPerSec = 1000 / this._runtime.currentStepTime; - switch (s.number) { - case GDXFOR_SENSOR.FORCE: - // Normalize the force, which can be measured between -50 and 50 N, - // to be a value between -100 and 100. - val = MathUtil.clamp(val * 2, -100, 100); - this._sensors.force = val; - break; - case GDXFOR_SENSOR.ACCELERATION_X: - this._sensors.accelerationX = s.value; - break; - case GDXFOR_SENSOR.ACCELERATION_Y: - this._sensors.accelerationY = s.value; - break; - case GDXFOR_SENSOR.ACCELERATION_Z: - this._sensors.accelerationZ = s.value; - break; - case GDXFOR_SENSOR.SPIN_SPEED_X: - val = MathUtil.radToDeg(val); - val = val / framesPerSec; // convert to from degrees per sec to degrees per frame - val = val * -1; - this._sensors.spinSpeedX = val; - break; - case GDXFOR_SENSOR.SPIN_SPEED_Y: - val = MathUtil.radToDeg(val); - val = val / framesPerSec; // convert to from degrees per sec to degrees per frame - 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 + // TODO: JSDoc + _onSensorValueChanged (sensor) { + let val = sensor.value; + const framesPerSec = 1000 / this._runtime.currentStepTime; + + switch (sensor.number) { + case GDXFOR_SENSOR.FORCE: + // Normalize the force, which can be measured between -50 and 50 N, + // to be a value between -100 and 100. + val = MathUtil.clamp(val * 2, -100, 100); + this._sensors.force = val; + break; + case GDXFOR_SENSOR.ACCELERATION_X: + this._sensors.accelerationX = sensor.value; + break; + case GDXFOR_SENSOR.ACCELERATION_Y: + this._sensors.accelerationY = sensor.value; + break; + case GDXFOR_SENSOR.ACCELERATION_Z: + this._sensors.accelerationZ = sensor.value; + break; + case GDXFOR_SENSOR.SPIN_SPEED_X: + val = MathUtil.radToDeg(val); + val = val / framesPerSec; // convert to from degrees per sec to degrees per frame + val = val * -1; + this._sensors.spinSpeedX = val; + break; + case GDXFOR_SENSOR.SPIN_SPEED_Y: + val = MathUtil.radToDeg(val); + val = val / framesPerSec; // convert to from degrees per sec to degrees per frame + 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; + } } getForce () { From 8da82cfe0106907854982d1324a645744dfe4688 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Mon, 4 Feb 2019 21:45:23 -0500 Subject: [PATCH 7/8] Adding JSDoc and removing extension manager gdx uncommenting. --- src/extension-support/extension-manager.js | 6 +++--- src/extensions/scratch3_gdx_for/index.js | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index 7a905af0..bdebb2ac 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -19,7 +19,7 @@ const Scratch3Ev3Blocks = require('../extensions/scratch3_ev3'); const Scratch3MakeyMakeyBlocks = require('../extensions/scratch3_makeymakey'); // todo: only load this extension once we have a compatible way to load its // Vernier module dependency. -const Scratch3GdxForBlocks = require('../extensions/scratch3_gdx_for'); +// const Scratch3GdxForBlocks = require('../extensions/scratch3_gdx_for'); const builtinExtensions = { pen: Scratch3PenBlocks, @@ -31,8 +31,8 @@ const builtinExtensions = { videoSensing: Scratch3VideoSensingBlocks, speech2text: Scratch3Speech2TextBlocks, ev3: Scratch3Ev3Blocks, - makeymakey: Scratch3MakeyMakeyBlocks, - gdxfor: Scratch3GdxForBlocks + makeymakey: Scratch3MakeyMakeyBlocks + // gdxfor: Scratch3GdxForBlocks }; /** diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 22ede13e..75276771 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -224,7 +224,11 @@ class GdxFor { }); } - // TODO: JSDoc + /** + * Handler for sensor value changes from the goforce device. + * @param {object} sensor - goforce device sensor whose value has changed + * @private + */ _onSensorValueChanged (sensor) { let val = sensor.value; const framesPerSec = 1000 / this._runtime.currentStepTime; From 39f69bd8c172973cc49eb02439659f5497a62060 Mon Sep 17 00:00:00 2001 From: Evelyn Eastmond Date: Mon, 4 Feb 2019 21:56:20 -0500 Subject: [PATCH 8/8] Separate out _spinSpeedFromGyro function. --- src/extensions/scratch3_gdx_for/index.js | 29 ++++++++++-------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 75276771..e6c0ea82 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -230,15 +230,11 @@ class GdxFor { * @private */ _onSensorValueChanged (sensor) { - let val = sensor.value; - const framesPerSec = 1000 / this._runtime.currentStepTime; - switch (sensor.number) { case GDXFOR_SENSOR.FORCE: // Normalize the force, which can be measured between -50 and 50 N, // to be a value between -100 and 100. - val = MathUtil.clamp(val * 2, -100, 100); - this._sensors.force = val; + this._sensors.force = MathUtil.clamp(sensor.value * 2, -100, 100); break; case GDXFOR_SENSOR.ACCELERATION_X: this._sensors.accelerationX = sensor.value; @@ -250,26 +246,25 @@ class GdxFor { this._sensors.accelerationZ = sensor.value; break; case GDXFOR_SENSOR.SPIN_SPEED_X: - val = MathUtil.radToDeg(val); - val = val / framesPerSec; // convert to from degrees per sec to degrees per frame - val = val * -1; - this._sensors.spinSpeedX = val; + this._sensors.spinSpeedX = this._spinSpeedFromGyro(sensor.value); break; case GDXFOR_SENSOR.SPIN_SPEED_Y: - val = MathUtil.radToDeg(val); - val = val / framesPerSec; // convert to from degrees per sec to degrees per frame - val = val * -1; - this._sensors.spinSpeedY = val; + this._sensors.spinSpeedY = this._spinSpeedFromGyro(sensor.value); 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; + this._sensors.spinSpeedZ = this._spinSpeedFromGyro(sensor.value); break; } } + _spinSpeedFromGyro (val) { + const framesPerSec = 1000 / this._runtime.currentStepTime; + val = MathUtil.radToDeg(val); + val = val / framesPerSec; // convert to from degrees per sec to degrees per frame + val = val * -1; + return val; + } + getForce () { return this._sensors.force; }