Skip to content

Commit

Permalink
Gyro/Accelerometer: precision => fixed. Fixes gh-1124
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <[email protected]>
  • Loading branch information
rwaldron committed May 5, 2016
1 parent 4b7c483 commit 554c808
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
14 changes: 7 additions & 7 deletions lib/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ var Controllers = {
Controllers["MPU-6050"] = Controllers.MPU6050;
Controllers["TINKERKIT"] = Controllers.ANALOG;

function ToPrecision(val, precision) {
return +(val).toPrecision(precision);
function ToFixed(val, digits) {
return +(val).toFixed(digits);
}

function magnitude(x, y, z) {
Expand Down Expand Up @@ -512,7 +512,7 @@ function Accelerometer(opts) {
Math.atan2(x, Math.hypot(y, z)) :
Math.asin(constrain(x, -1, 1));

return ToPrecision(rads * rad2deg, 2);
return ToFixed(rads * rad2deg, 2);
}
},
/**
Expand All @@ -532,23 +532,23 @@ function Accelerometer(opts) {
Math.atan2(y, Math.hypot(x, z)) :
Math.asin(constrain(y, -1, 1));

return ToPrecision(rads * rad2deg, 2);
return ToFixed(rads * rad2deg, 2);
}
},
x: {
get: function() {
return ToPrecision(this.toGravity(state.x.value, "x"), 2);
return ToFixed(this.toGravity(state.x.value, "x"), 2);
}
},
y: {
get: function() {
return ToPrecision(this.toGravity(state.y.value, "y"), 2);
return ToFixed(this.toGravity(state.y.value, "y"), 2);
}
},
z: {
get: function() {
return this.hasAxis("z") ?
ToPrecision(this.toGravity(state.z.value, "z"), 2) : 0;
ToFixed(this.toGravity(state.z.value, "z"), 2) : 0;
}
},
acceleration: {
Expand Down
28 changes: 14 additions & 14 deletions lib/gyro.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var Board = require("./board"),
var priv = new Map();
var axes = ["x", "y", "z"];

function ToPrecision(val, precision) {
return +(val).toPrecision(precision);
function ToFixed(val, digits) {
return +(val).toFixed(digits);
}

var Controllers = {
Expand Down Expand Up @@ -262,40 +262,40 @@ function Gyro(opts) {
pitch: {
get: function() {
return {
rate: ToPrecision(this.rate.y, 2),
angle: ToPrecision(state.y.angle, 2)
rate: ToFixed(this.rate.y, 2),
angle: ToFixed(state.y.angle, 2)
};
}
},
roll: {
get: function() {
return {
rate: ToPrecision(this.rate.x, 2),
angle: ToPrecision(state.x.angle, 2)
rate: ToFixed(this.rate.x, 2),
angle: ToFixed(state.x.angle, 2)
};
}
},
yaw: {
get: function() {
return {
rate: this.z !== undefined ? ToPrecision(this.rate.z, 2) : 0,
angle: this.z !== undefined ? ToPrecision(state.z.angle, 2) : 0
rate: this.z !== undefined ? ToFixed(this.rate.z, 2) : 0,
angle: this.z !== undefined ? ToFixed(state.z.angle, 2) : 0
};
}
},
x: {
get: function() {
return ToPrecision(this.toNormal(state.x.value), 4);
return ToFixed(this.toNormal(state.x.value), 4);
}
},
y: {
get: function() {
return ToPrecision(this.toNormal(state.y.value), 4);
return ToFixed(this.toNormal(state.y.value), 4);
}
},
z: {
get: function() {
return state.z.hasValue ? ToPrecision(this.toNormal(state.z.value), 4) : undefined;
return state.z.hasValue ? ToFixed(this.toNormal(state.z.value), 4) : undefined;
}
},
rate: {
Expand All @@ -306,9 +306,9 @@ function Gyro(opts) {
this.toDegreesPerSecond(state.z.value, state.z.center) : 0;

return {
x: ToPrecision(x, 2),
y: ToPrecision(y, 2),
z: ToPrecision(z, 2)
x: ToFixed(x, 2),
y: ToFixed(y, 2),
z: ToFixed(z, 2)
};
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,9 @@ exports["Accelerometer -- ADXL345"] = {

test.ok(changeSpy.calledOnce);
test.deepEqual(changeSpy.args[0], [{
x: 0.012,
x: 0.01,
y: 0.02,
z: 1
z: 1.02
}]);

test.done();
Expand Down Expand Up @@ -649,9 +649,9 @@ exports["Accelerometer -- MMA7660"] = {

test.ok(changeSpy.calledOnce);
test.deepEqual(changeSpy.args[0], [{
x: 0.047,
y: 0.047,
z: 0.047
x: 0.05,
y: 0.05,
z: 0.05
}]);

test.done();
Expand Down

0 comments on commit 554c808

Please sign in to comment.