mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -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'
|
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.
|
* Threshold for pushing and pulling force, for the whenForcePushedOrPulled hat block.
|
||||||
* @type {number}
|
* @type {number}
|
||||||
|
@ -118,7 +131,6 @@ class GdxFor {
|
||||||
|
|
||||||
this.disconnect = this.disconnect.bind(this);
|
this.disconnect = this.disconnect.bind(this);
|
||||||
this._onConnect = this._onConnect.bind(this);
|
this._onConnect = this._onConnect.bind(this);
|
||||||
this._sensorsEnabled = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,7 +167,6 @@ class GdxFor {
|
||||||
* Disconnect from the GDX FOR.
|
* Disconnect from the GDX FOR.
|
||||||
*/
|
*/
|
||||||
disconnect () {
|
disconnect () {
|
||||||
this._sensorsEnabled = false;
|
|
||||||
if (this._scratchLinkSocket) {
|
if (this._scratchLinkSocket) {
|
||||||
this._scratchLinkSocket.disconnect();
|
this._scratchLinkSocket.disconnect();
|
||||||
}
|
}
|
||||||
|
@ -195,52 +206,57 @@ class GdxFor {
|
||||||
sensor.setEnabled(true);
|
sensor.setEnabled(true);
|
||||||
});
|
});
|
||||||
this._device.on('measurements-started', () => {
|
this._device.on('measurements-started', () => {
|
||||||
this._sensorsEnabled = true;
|
|
||||||
const enabledSensors = this._device.sensors.filter(s => s.enabled);
|
const enabledSensors = this._device.sensors.filter(s => s.enabled);
|
||||||
enabledSensors.forEach(sensor => {
|
enabledSensors.forEach(sensor => {
|
||||||
sensor.on('value-changed', s => {
|
sensor.on('value-changed', s => {
|
||||||
// log the sensor name, new value, and units.
|
let val = s.value; // TODO: rename/replace
|
||||||
// console.log(`Sensor: ${s.name} value: ${s.value} units: ${s.units}`);
|
const framesPerSec = 1000 / this._runtime.currentStepTime;
|
||||||
console.log(typeof(s.name));
|
switch (s.number) {
|
||||||
console.log(s.name.valueOf());
|
case GDXFOR_SENSOR.FORCE:
|
||||||
if (s.name.valueOf() === 'Force') {
|
// Normalize the force, which can be measured between -50 and 50 N,
|
||||||
this._senors.accelerationX = s.value;
|
// to be a value between -100 and 100.
|
||||||
console.log('HERE ============');
|
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
|
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 () {
|
getForce () {
|
||||||
if (this._canReadSensors()) {
|
return this._sensors.force;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getTiltX () {
|
getTiltX () {
|
||||||
if (this._canReadSensors()) {
|
|
||||||
let x = this.getAccelerationX();
|
let x = this.getAccelerationX();
|
||||||
let y = this.getAccelerationY();
|
let y = this.getAccelerationY();
|
||||||
let z = this.getAccelerationZ();
|
let z = this.getAccelerationZ();
|
||||||
|
@ -286,11 +302,8 @@ class GdxFor {
|
||||||
value += 0.5;
|
value += 0.5;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
getTiltY () {
|
getTiltY () {
|
||||||
if (this._canReadSensors()) {
|
|
||||||
let x = this.getAccelerationX();
|
let x = this.getAccelerationX();
|
||||||
let y = this.getAccelerationY();
|
let y = this.getAccelerationY();
|
||||||
let z = this.getAccelerationZ();
|
let z = this.getAccelerationZ();
|
||||||
|
@ -336,47 +349,29 @@ class GdxFor {
|
||||||
value += 0.5;
|
value += 0.5;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
getAccelerationX () {
|
getAccelerationX () {
|
||||||
return this._getAcceleration(2);
|
return this._sensors.accelerationX;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccelerationY () {
|
getAccelerationY () {
|
||||||
return this._getAcceleration(3);
|
return this._sensors.accelerationY;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccelerationZ () {
|
getAccelerationZ () {
|
||||||
return this._getAcceleration(4);
|
return this._sensors.accelerationZ;
|
||||||
}
|
|
||||||
|
|
||||||
_getAcceleration (sensorNum) {
|
|
||||||
if (!this._canReadSensors()) return 0;
|
|
||||||
const val = this._device.getSensor(sensorNum).value;
|
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getSpinSpeedX () {
|
getSpinSpeedX () {
|
||||||
return this._getSpinSpeed(5);
|
return this._sensors.spinSpeedX;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSpinSpeedY () {
|
getSpinSpeedY () {
|
||||||
return this._getSpinSpeed(6);
|
return this._sensors.spinSpeedY;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSpinSpeedZ () {
|
getSpinSpeedZ () {
|
||||||
return this._getSpinSpeed(7);
|
return this._sensors.spinSpeedZ;
|
||||||
}
|
|
||||||
|
|
||||||
_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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue