-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.js
More file actions
111 lines (102 loc) · 4.05 KB
/
sensor.js
File metadata and controls
111 lines (102 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var util = require('util');
var async = require('async');
var SensorTag = require('sensortag');
var Mediator = require('./mediator'),
Logger = require('./logger'),
Guide = require('./guide');
var degreeOfOrientation = 0;
const THRESHOLD = 10;
var getDegreeOfOrientation = function(){
return degreeOfOrientation;
}
var startSensing = function() {
SensorTag.discover(function(sensorTag){
//console.log('discovered');
sensorTag.on('disconnect', function() {
Logger.log('warn','Sensors are disconnected! Try connecting again!');
Guide.speak('Please reconnect your sensor.');
//process.exit(0);
});
async.series([
function(callback) {
Logger.log('info','Connecting Sensor');
sensorTag.connect(callback);
},
function(callback) {
//console.log('discoverServicesAndCharacteristics');
sensorTag.discoverServicesAndCharacteristics(callback);
},
function(callback) {
//console.log('enableMagnetometer');
sensorTag.enableMagnetometer(callback);
},
function(callback) {
sensorTag.setMagnetometerPeriod(200,callback);
},
function(callback) {
//console.log('enableGyroscope');
sensorTag.enableGyroscope(callback);
},
function(callback) {
setTimeout(callback, 2000);
},
function(callback) {
sensorTag.on('magnetometerChange', function(x, y, z) {
//console.log('\tx = %d μT', x.toFixed(4));
//console.log('\ty = %d μT', y.toFixed(4));
//console.log('\tz = %d μT', z.toFixed(4));
x=x+36;
y=y-42;
z=z+23;
//console.log(x.toFixed(4)+','+y.toFixed(4)+','+z.toFixed(4));
var degrees = 40+[Math.atan2(y,x)]*180/Math.PI;
//console.log(degrees);
if(Math.abs(degrees-degreeOfOrientation)>THRESHOLD){
Mediator.pubsub.emit('orientationChanged',JSON.stringify({'orientation':degrees}));
degreeOfOrientation = degrees;
}
//console.log("========================> "+degrees);
});
sensorTag.notifyMagnetometer(function() {
//console.log('M notified!');
});
callback();
},
/*function(callback){
sensorTag.on('gyroscopeChange', function(x, y, z) {
console.log('\tx = %d °/s', x.toFixed(4));
console.log('\ty = %d °/s', y.toFixed(4));
console.log('\tz = %d °/s', z.toFixed(4));
});
sensorTag.notifyGyroscope(function() {
//console.log('G notified!');
});
callback();
}, */
function(callback) {
//console.log('readSimpleRead');
sensorTag.on('simpleKeyChange', function(left, right) {
//console.log('left: ' + left);
//console.log('right: ' + right);
if(right){
Mediator.pubsub.emit('rightKeyPressed');
}
if(left){
Mediator.pubsub.emit('leftKeyPressed');
}
// if (left && right) {
// sensorTag.notifySimpleKey(callback);
// }
});
sensorTag.notifySimpleKey(function() {
});
callback();
},
function(callback){
Guide.speak("Sensors ready! Please select your destination by pressing the right key.");
callback();
}
]);
});
}
module.exports.startSensing = startSensing;