Skip to content

Commit 120a4f2

Browse files
committed
- update magnetometer calculations, work around for magnetometer issues, and make sure callback is always called on enableMPU9250/disableMPU9250
1 parent fdc7e11 commit 120a4f2

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/cc2650.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var CC2650SensorTag = function(peripheral) {
3131
this.mpu9250mask = 0;
3232
this.mpu9250notifyCount = 0;
3333

34-
this.onMPU9250ChangeBinded = this.onMPU9250Change.bind(this);
34+
this.onMPU9250ChangeBinded = this.onMPU9250Change.bind(this);
3535
this.onLuxometerChangeBinded = this.onLuxometerChange.bind(this);
3636
};
3737

@@ -98,20 +98,27 @@ CC2650SensorTag.prototype.setMPU9250Period = function(period, callback) {
9898
CC2650SensorTag.prototype.enableMPU9250 = function(mask, callback) {
9999
this.mpu9250mask |= mask;
100100

101-
this.writeUInt16LECharacteristic(MPU9250_UUID, MPU9250_CONFIG_UUID, this.mpu9250mask, callback);
101+
// for now, always write 0x007f, magnetometer does not seem to notify is specific mask is used
102+
this.writeUInt16LECharacteristic(MPU9250_UUID, MPU9250_CONFIG_UUID, 0x007f, callback);
102103
};
103104

104105
CC2650SensorTag.prototype.disableMPU9250 = function(mask, callback) {
105106
this.mpu9250mask &= ~mask;
106107

107-
this.writeUInt16LECharacteristic(MPU9250_UUID, MPU9250_CONFIG_UUID, this.mpu9250mask, callback);
108+
if (this.mpu9250mask === 0) {
109+
this.writeUInt16LECharacteristic(MPU9250_UUID, MPU9250_CONFIG_UUID, 0x0000, callback);
110+
} else if (typeof(callback) === 'function') {
111+
callback();
112+
}
108113
};
109114

110115
CC2650SensorTag.prototype.notifyMPU9250 = function(callback) {
111116
this.mpu9250notifyCount++;
112117

113118
if (this.mpu9250notifyCount === 1) {
114119
this.notifyCharacteristic(MPU9250_UUID, MPU9250_DATA_UUID, true, this.onMPU9250ChangeBinded, callback);
120+
} else if (typeof(callback) === 'function') {
121+
callback();
115122
}
116123
};
117124

@@ -120,6 +127,8 @@ CC2650SensorTag.prototype.unnotifyMPU9250 = function(callback) {
120127

121128
if (this.mpu9250notifyCount === 0) {
122129
this.notifyCharacteristic(MPU9250_UUID, MPU9250_DATA_UUID, false, this.onMPU9250ChangeBinded, callback);
130+
} else if (typeof(callback) === 'function') {
131+
callback();
123132
}
124133
};
125134

@@ -170,10 +179,10 @@ CC2650SensorTag.prototype.convertMPU9250Data = function(data, callback) {
170179
var y = data.readInt16LE(8) * 2.0 / 32768.0;
171180
var z = data.readInt16LE(10) * 2.0 / 32768.0;
172181

173-
// magnetometer
174-
var xM = data.readInt16LE(12);
175-
var yM = data.readInt16LE(14);
176-
var zM = data.readInt16LE(16);
182+
// magnetometer (page 50 of http://www.invensense.com/mems/gyro/documents/RM-MPU-9250A-00.pdf)
183+
var xM = data.readInt16LE(12) * 4912.0 / 32760.0;
184+
var yM = data.readInt16LE(14) * 4912.0 / 32760.0;
185+
var zM = data.readInt16LE(16) * 4912.0 / 32760.0;
177186

178187
callback(x, y, z, xG, yG, zG, xM, yM, zM);
179188
};

0 commit comments

Comments
 (0)