From 994af4a65fa7ece2f11f45038c75408d8166784a Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Fri, 22 Nov 2019 15:22:51 -0700 Subject: common: mag_cal: update magnetometer to leverage kasa Update magnetometer calibration algorithm to leverage the new kasa standalone code. BUG=b:138303429,chromium:1023858 TEST=added unit test BRANCH=None Change-Id: I5c0403b66d9fe7c2925b2ec6244cf9e32ad5ea5f Signed-off-by: Yuval Peress Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1931464 Reviewed-by: Jack Rosenthal Reviewed-by: Gwendal Grignou --- test/build.mk | 2 ++ test/mag_cal.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/mag_cal.tasklist | 10 ++++++ test/test_config.h | 4 +++ 4 files changed, 107 insertions(+) create mode 100644 test/mag_cal.c create mode 100644 test/mag_cal.tasklist (limited to 'test') diff --git a/test/build.mk b/test/build.mk index 97997ed8b3..c577226fdb 100644 --- a/test/build.mk +++ b/test/build.mk @@ -45,6 +45,7 @@ test-list-host += kb_mkbp #test-list-host += kb_scan # crbug.com/976974 test-list-host += lid_sw test-list-host += lightbar +test-list-host += mag_cal test-list-host += math_util test-list-host += motion_angle test-list-host += motion_angle_tablet @@ -123,6 +124,7 @@ kb_mkbp-y=kb_mkbp.o kb_scan-y=kb_scan.o lid_sw-y=lid_sw.o lightbar-y=lightbar.o +mag_cal-y=mag_cal.o math_util-y=math_util.o motion_angle-y=motion_angle.o motion_angle_data_literals.o motion_common.o motion_angle_tablet-y=motion_angle_tablet.o motion_angle_data_literals_tablet.o motion_common.o diff --git a/test/mag_cal.c b/test/mag_cal.c new file mode 100644 index 0000000000..e1931c352a --- /dev/null +++ b/test/mag_cal.c @@ -0,0 +1,91 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "common.h" +#include "mag_cal.h" +#include "test_util.h" +#include + +/** + * Various samples that might be seen in the wild. Normal range for magnetic + * fields is around 80 uT. This translates to roughly +/-525 units for the + * lis2mdl sensor. + * + * Random numbers were generated using the range of [518,532] (+- 2.14 uT) for + * the high values and [-5,5] (+- 1.53 uT) for the low values. + */ +static intv3_t samples[] = { + { -522, 5, -5 }, + { -528, -3, 1 }, + { -531, -2, 0 }, + { -525, -1, 3 }, + + { 527, 3, -2 }, + { 523, -5, 1 }, + { 520, -3, 2 }, + { 522, 0, -4 }, + + { -3, -519, -2 }, + { 1, -521, 5 }, + { 2, -526, 4 }, + { 0, -532, -5 }, + + { -5, 528, 4 }, + { -2, 531, -4 }, + { 1, 522, 2 }, + { 5, 532, 3 }, + + { -5, 0, -524 }, + { -1, -2, -527 }, + { -3, 4, -532 }, + { 5, 3, -531 }, + + { 4, -2, 524 }, + { 1, 3, 520 }, + { 5, -5, 528 }, + { 0, 2, 521 }, +}; + +static int test_mag_cal_computes_bias(void) +{ + struct mag_cal_t cal; + int i; + + init_mag_cal(&cal); + cal.batch_size = ARRAY_SIZE(samples); + + /* Test that we don't calibrate until we added the final sample. */ + for (i = 0; i < cal.batch_size - 1; ++i) + TEST_EQ(0, mag_cal_update(&cal, samples[i]), "%d"); + /* Add the final sample and check calibration. */ + TEST_EQ(1, mag_cal_update(&cal, samples[cal.batch_size - 1]), "%d"); + TEST_EQ(525, FP_TO_INT(cal.radius), "%d"); + TEST_EQ(-1, cal.bias[0], "%d"); + TEST_EQ(1, cal.bias[1], "%d"); + TEST_EQ(-2, cal.bias[2], "%d"); + + /* + * State should have reset, run the same code again to verify that + * we get the same calibration. + */ + for (i = 0; i < cal.batch_size - 1; ++i) + TEST_EQ(0, mag_cal_update(&cal, samples[i]), "%d"); + TEST_EQ(1, mag_cal_update(&cal, samples[cal.batch_size - 1]), "%d"); + TEST_EQ(525, FP_TO_INT(cal.radius), "%d"); + TEST_EQ(-1, cal.bias[0], "%d"); + TEST_EQ(1, cal.bias[1], "%d"); + TEST_EQ(-2, cal.bias[2], "%d"); + + return EC_SUCCESS; +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_mag_cal_computes_bias); + + test_print_result(); +} diff --git a/test/mag_cal.tasklist b/test/mag_cal.tasklist new file mode 100644 index 0000000000..ff715f69cd --- /dev/null +++ b/test/mag_cal.tasklist @@ -0,0 +1,10 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ + diff --git a/test/test_config.h b/test/test_config.h index 9dbbd4e1d0..3c269c4946 100644 --- a/test/test_config.h +++ b/test/test_config.h @@ -65,6 +65,10 @@ #define CONFIG_MATH_UTIL #endif +#ifdef TEST_MAG_CAL +#define CONFIG_MAG_CALIBRATE +#endif + #ifdef TEST_STILLNESS_DETECTOR #define CONFIG_FPU #define CONFIG_ONLINE_CALIB -- cgit v1.2.1