From df09bc2c83c70716afcfd73e4eadf8cda0195848 Mon Sep 17 00:00:00 2001 From: Aseda Aboagye Date: Tue, 11 Jul 2017 14:44:24 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/567050 Commit-Ready: Aseda Aboagye Tested-by: Aseda Aboagye Reviewed-by: Gwendal Grignou Reviewed-by: Shawn N --- common/motion_lid.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'common/motion_lid.c') 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. -- cgit v1.2.1