This repository was archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
298 lines (259 loc) · 5.92 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
var async = require('async');
// Registers
var POWER_MGMT_1 = 0x6b,
POWER_MGMT_2 = 0x6c;
var ACCEL_XOUT = 0x3b,
ACCEL_YOUT = 0x3d,
ACCEL_ZOUT = 0x3f;
var TEMP_OUT = 0x41;
var GYRO_XOUT = 0x43,
GYRO_YOUT = 0x45,
GYRO_ZOUT = 0x47;
var ACCEL_LSB_SENSITIVITY = 16384,
GYRO_LSB_SENSITIVITY = 131;
function toDegrees(angle) {
return angle * (180 / Math.PI);
}
function dist(a, b) {
return Math.sqrt(a*a + b*b);
}
function getRotation(a, b, c) {
var radians = Math.atan2(a, dist(b, c));
return -toDegrees(radians);
}
function getXRotation(x, y, z) {
return getRotation(y, x, z);
}
function getYRotation(x, y, z) {
return getRotation(x, y, z);
}
function scaleData(data, factor) {
var scaled = {};
for (var axis in data) {
scaled[axis] = data[axis] / factor;
}
return scaled;
}
function applyCalibration(data, cal) {
if (!cal) return data;
for (var axis in data) {
if (cal[axis] && typeof cal[axis] == 'number') {
data[axis] += cal[axis];
}
}
return data;
}
function Sensor(bus, address) {
if (!(this instanceof Sensor)) {
return new Sensor(bus, address);
}
this.bus = bus;
this.address = address;
this.calibration = {};
// Now wake the MPU6050 up as it starts in sleep mode
bus.writeByteSync(address, POWER_MGMT_1, 0);
}
Sensor.prototype.calibrateGyro = function (cal) {
this.calibration.gyro = cal;
};
Sensor.prototype.calibrateAccel = function (cal) {
this.calibration.accel = cal;
};
Sensor.prototype.readWord = function (cmd, done) {
var high, low;
var that = this;
async.series([
function (cb) {
that.bus.readByte(that.address, cmd, function (err, value) {
high = value;
cb(err);
});
},
function (cb) {
that.bus.readByte(that.address, cmd + 1, function (err, value) {
low = value;
cb(err);
});
}
], function (err) {
if (err) return done(err);
var value = (high << 8) + low;
done(null, value);
});
};
Sensor.prototype.readWordSync = function (cmd) {
var high = this.bus.readByteSync(this.address, cmd);
var low = this.bus.readByteSync(this.address, cmd+1);
var value = (high << 8) + low;
return value;
};
Sensor.prototype.readWord2c = function (cmd, done) {
this.readWord(cmd, function (err, value) {
if (err) return done(err);
if (value >= 0x8000) {
done(null, -((65535 - value) + 1));
} else {
done(null, value);
}
});
};
Sensor.prototype.readWord2cSync = function (cmd) {
var value = this.readWordSync(cmd);
if (value >= 0x8000) {
return -((65535 - value) + 1);
} else {
return value;
}
};
Sensor.prototype.readGyro = function (done) {
var data = {};
var that = this;
async.series([
function (cb) {
that.readWord2c(GYRO_XOUT, function (err, value) {
data.x = value;
cb(err);
});
},
function (cb) {
that.readWord2c(GYRO_YOUT, function (err, value) {
data.y = value;
cb(err);
});
},
function (cb) {
that.readWord2c(GYRO_ZOUT, function (err, value) {
data.z = value;
cb(err);
});
}
], function (err) {
if (err) return done(err);
data = scaleData(data, GYRO_LSB_SENSITIVITY);
data = applyCalibration(data, that.calibration.gyro);
done(null, data);
});
};
Sensor.prototype.readGyroSync = function () {
var data = {
x: this.readWord2cSync(GYRO_XOUT),
y: this.readWord2cSync(GYRO_YOUT),
z: this.readWord2cSync(GYRO_ZOUT)
};
data = scaleData(data, GYRO_LSB_SENSITIVITY);
data = applyCalibration(data, this.calibration.gyro);
return data;
};
Sensor.prototype.readAccel = function (done) {
var data = {};
var that = this;
async.series([
function (cb) {
that.readWord2c(ACCEL_XOUT, function (err, value) {
data.x = value;
cb(err);
});
},
function (cb) {
that.readWord2c(ACCEL_YOUT, function (err, value) {
data.y = value;
cb(err);
});
},
function (cb) {
that.readWord2c(ACCEL_ZOUT, function (err, value) {
data.z = value;
cb(err);
});
}
], function (err) {
if (err) return done(err);
data = scaleData(data, ACCEL_LSB_SENSITIVITY);
data = applyCalibration(data, that.calibration.accel);
done(null, data);
});
};
Sensor.prototype.readAccelSync = function () {
var data = {
x: this.readWord2cSync(ACCEL_XOUT),
y: this.readWord2cSync(ACCEL_YOUT),
z: this.readWord2cSync(ACCEL_ZOUT)
};
data = scaleData(data, ACCEL_LSB_SENSITIVITY);
data = applyCalibration(data, this.calibration.accel);
return data;
};
Sensor.prototype.readTemp = function (done) {
this.readWord2c(TEMP_OUT, function (err, value) {
if (err) return done(err);
var temp = value / 340 + 36.53; // In degrees Celcius
done(null, temp);
});
};
Sensor.prototype.readTempSync = function () {
return this.readWord2cSync(TEMP_OUT) / 340 + 36.53;
};
Sensor.prototype.readRotation = function (done) {
this.readAccel(function (err, accel) {
if (err) return done(err);
done(null, {
x: getXRotation(accel.x, accel.y, accel.z),
y: getYRotation(accel.x, accel.y, accel.z)
});
});
};
Sensor.prototype.readRotationSync = function (accel) {
if (!accel) {
accel = this.readAccelSync();
}
return {
x: getXRotation(accel.x, accel.y, accel.z),
y: getYRotation(accel.x, accel.y, accel.z)
};
};
Sensor.prototype.readSync = function () {
var gyro = this.readGyroSync();
var accel = this.readAccelSync();
var rotation = this.readRotationSync(accel);
var temp = this.readTempSync();
return {
gyro: gyro,
accel: accel,
rotation: rotation,
temp: temp
};
};
Sensor.prototype.read = function (done) {
var gyro, accel, rotation, temp;
var that = this;
async.series([
function (cb) {
that.readGyro(function (err, data) {
gyro = data;
cb(err);
});
},
function (cb) {
that.readAccel(function (err, data) {
accel = data;
cb(err);
});
},
function (cb) {
that.readTemp(function (err, data) {
temp = data;
cb(err);
});
}
], function (err) {
if (err) return done(err);
rotation = that.readRotationSync(accel);
done(null, {
gyro: gyro,
accel: accel,
rotation: rotation,
temp: temp
});
});
};
module.exports = Sensor;