mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Removing canReadSensors and sensorsEnabled, first pass.
This commit is contained in:
parent
eb4f6235d9
commit
d84dd72a8c
1 changed files with 138 additions and 143 deletions
|
@ -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,52 +206,57 @@ 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();
|
||||
|
@ -286,11 +302,8 @@ class GdxFor {
|
|||
value += 0.5;
|
||||
return value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
getTiltY () {
|
||||
if (this._canReadSensors()) {
|
||||
let x = this.getAccelerationX();
|
||||
let y = this.getAccelerationY();
|
||||
let z = this.getAccelerationZ();
|
||||
|
@ -336,47 +349,29 @@ class GdxFor {
|
|||
value += 0.5;
|
||||
return value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue