summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAseda Aboagye <aaboagye@google.com>2017-07-11 14:44:24 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-07-13 08:32:58 -0700
commitdf09bc2c83c70716afcfd73e4eadf8cda0195848 (patch)
tree3db3512c7a07655f2c389df3a49e6f33da21279e
parent1b9a553ece24a8f3b05e3a53f1ee96c24e41ef5e (diff)
downloadchrome-ec-df09bc2c83c70716afcfd73e4eadf8cda0195848.tar.gz
motion_lid: Increase precision in noisy mag check.
This commit increases the precision used in the noisy magnitude deviation check by multiplying the scaled sensor data by 8 in the intermediate calculations. Prior to this, due to some bits being lost, certain devices would determine the lid angle as unreliable in specific angles, even though the device was at rest. BUG=b:63148973 BRANCH=eve,gru,reef TEST=Flash bob, set DUT on desk, run `while true; do ectool motionsense lid_angle; sleep 0.1; done`, slowly move the lid from 15 degrees until ~350. Verify that no particular angle results in a unreliable lid angle reading. TEST=Run `evtest` and examing cros-ec-buttons, fold screen all the way back to make tablet mode, shake device for at least 30s. Verify that there are no spurious transitions of the tablet mode switch. TEST=Repeat above tests on kevin. Change-Id: Iff06c1df2dd33c60e26a59183f62f29b71548729 Signed-off-by: Aseda Aboagye <aaboagye@google.com> Reviewed-on: https://chromium-review.googlesource.com/567050 Commit-Ready: Aseda Aboagye <aaboagye@chromium.org> Tested-by: Aseda Aboagye <aaboagye@chromium.org> Reviewed-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-by: Shawn N <shawnn@chromium.org>
-rw-r--r--common/motion_lid.c28
1 files changed, 20 insertions, 8 deletions
diff --git a/common/motion_lid.c b/common/motion_lid.c
index d36ecc5e64..38efea172e 100644
--- a/common/motion_lid.c
+++ b/common/motion_lid.c
@@ -256,16 +256,28 @@ static int calculate_lid_angle(const vector_3_t base, const vector_3_t lid,
lid_range = accel_lid->drv->get_range(accel_lid);
for (i = X; i <= Z; i++) {
- scaled_base[i] = base[i] * base_range * 10 / (1 << 15);
- scaled_lid[i] = lid[i] * lid_range * 10 / (1 << 15);
+ /*
+ * To increase precision, we'll use 8x the sensor data in the
+ * intermediate calculation. We would normally divide by 2^15.
+ *
+ * This is safe because even at a range of 8g, calculating the
+ * magnitude squared should still be less than the max of a
+ * 32-bit signed integer.
+ *
+ * The max that base[i] could be is 32768, resulting in a max
+ * value for scaled_base[i] of 640 @ 8g range and force.
+ * Typically our range is set to 2g.
+ */
+ scaled_base[i] = base[i] * base_range * 10 >> 12;
+ scaled_lid[i] = lid[i] * lid_range * 10 >> 12;
}
- base_magnitude2 = scaled_base[X] * scaled_base[X] +
- scaled_base[Y] * scaled_base[Y] +
- scaled_base[Z] * scaled_base[Z];
- lid_magnitude2 = scaled_lid[X] * scaled_lid[X] +
- scaled_lid[Y] * scaled_lid[Y] +
- scaled_lid[Z] * scaled_lid[Z];
+ base_magnitude2 = (scaled_base[X] * scaled_base[X] +
+ scaled_base[Y] * scaled_base[Y] +
+ scaled_base[Z] * scaled_base[Z]) >> 6;
+ lid_magnitude2 = (scaled_lid[X] * scaled_lid[X] +
+ scaled_lid[Y] * scaled_lid[Y] +
+ scaled_lid[Z] * scaled_lid[Z]) >> 6;
/*
* Check to see if they differ than more than NOISY_MAGNITUDE_DEVIATION.