summaryrefslogtreecommitdiff
path: root/test/mag_cal.c
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2019-11-22 15:22:51 -0700
committerCommit Bot <commit-bot@chromium.org>2020-01-28 20:35:47 +0000
commit994af4a65fa7ece2f11f45038c75408d8166784a (patch)
treeecde395b031a499bb796542efa3e9c942eb6ce8b /test/mag_cal.c
parent1b4ce5849d520b67447bdc5b94e346432eac2126 (diff)
downloadchrome-ec-994af4a65fa7ece2f11f45038c75408d8166784a.tar.gz
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 <peress@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1931464 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Diffstat (limited to 'test/mag_cal.c')
-rw-r--r--test/mag_cal.c91
1 files changed, 91 insertions, 0 deletions
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 <stdio.h>
+
+/**
+ * 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();
+}