-
Notifications
You must be signed in to change notification settings - Fork 4
/
sensors.js
119 lines (103 loc) · 2.62 KB
/
sensors.js
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
112
113
114
115
116
117
118
119
/**
* Sensors
* Provides a common interface for all sensors/sensor data
* currently only supports Ant+ and STM32/Android sensors
*
* Sensordata provided:
* mps => meters per second ... current speed (from the STM32)
* dist => distance ... distance travelled since last reading (from STM32)
* hr => heart rate ... current heart (from Ant+)
cad => cadence ... curent cadence (from Ant+)
servo => servo position current position of the servo controlling resistance (from STM32)
*
* @author Richard Deininger
* @version 1.0
*/
var Settings = require('./settings.js');
// using sensor mocks by default for testing
var AntSensors = require('./antplus/AntSensorsMock.js');
var stm = require('./stm/stmMock.js');
if (!Settings.Data.UseMockups) //only use the real sensors when set in settings
{
AntSensors = require('./antplus/AntSensors.js');
stm = require('./stm/newstm.js');
}
var SensorData =
{
mps: 0,
dist: 0,
hr:0,
cad:0,
servo:0
}
var sensorCallback = null;
var startCallback = null;
var antSensors = new AntSensors(Settings.Data.Sensors.HeartRate_AntId, Settings.Data.Sensors.Cadence_AntId);
var stmSensors = new stm(Settings.Data.Sensors.STM.ComPort);
stmSensors.on("SensorReady", function (data)
{
if (Settings.Data.DebugOutput)
{
console.log("Sensor Ready");
}
if (startCallback)
{
startCallback();
}
});
stmSensors.on("SensorData", function (data)
{
// expected data = { distance: 0.1, speedms: 0, servoPos: 0.1 }
if (sensorCallback)
{
sensorCallback(readSensorData(data));
}
});
var readSensorData = function(stmData)
{
var data = Object.create(SensorData);
if (stmData)
{
data.mps = stmData.speedms;
data.dist = stmData.distance;
data.servo = stmData.servoPos;
}
lastread = (new Date()).getTime();
data.hr = antSensors.Data.HeartRate;
data.cad = antSensors.Data.Cadence;
//console.log("data: ",data);
return data;
}
exports.SetSensorDataCallback = function(callback)
{
sensorCallback = callback;
}
// gets values from 0.0 to 1.0 (percentage of maxResistnac)
exports.SetServoPos = function(pos)
{
stmSensors.SetLevel(pos, false);
}
exports.GetSensorData = function()
{
return readSensorData();
}
exports.Start = function(stcallback)
{
startCallback = stcallback;
stmSensors.Connect();
antSensors.Open();
}
exports.Stop = function()
{ try
{
stmSensors.Disconnect();
antSensors.Close();
} catch(e)
{
console.log("Error:", e);
}
}
exports.End = function()
{
stmSensors.Destruct();
}