From 5c3bc0d02523850b0c8ef82cc16e6f8c27ab3229 Mon Sep 17 00:00:00 2001 From: Gwendal Grignou Date: Sat, 1 Feb 2020 23:37:56 -0800 Subject: common: mag_cal: Re-add bias inversion Sensors add offset to raw value. kaza_compute calculate the bias (the hard iron) and it has to be remove from the raw value. Invert the vector in magnetometer calibration code. Fixes: 994af4a65fa7e ("common: mag_cal: update magnetometer to leverage kasa") BUG=b:144027014,b:149116125 BRANCH=none TEST=Check that after successful online calibration, value reported to the host are closer to 0 than before calibration. Change-Id: I6cfd711c82287b1c877912e85d84d2725b063cb2 Signed-off-by: Gwendal Grignou Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2034675 Reviewed-by: Yuval Peress (cherry picked from commit d60a962c3a4907d2f18c236015aef7b989e1d384) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2051100 Tested-by: Yuval Peress Auto-Submit: Yuval Peress --- common/mag_cal.c | 6 +++--- test/mag_cal.c | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/common/mag_cal.c b/common/mag_cal.c index 1dc40e34db..f7b1945933 100644 --- a/common/mag_cal.c +++ b/common/mag_cal.c @@ -132,9 +132,9 @@ int mag_cal_update(struct mag_cal_t *moc, const intv3_t v) /* 4. Kasa sphere fitting */ kasa_compute(&moc->kasa_fit, bias, &radius); if (radius > MIN_FIT_MAG && radius < MAX_FIT_MAG) { - moc->bias[X] = FP_TO_INT(bias[X]); - moc->bias[Y] = FP_TO_INT(bias[Y]); - moc->bias[Z] = FP_TO_INT(bias[Z]); + moc->bias[X] = -FP_TO_INT(bias[X]); + moc->bias[Y] = -FP_TO_INT(bias[Y]); + moc->bias[Z] = -FP_TO_INT(bias[Z]); moc->radius = radius; diff --git a/test/mag_cal.c b/test/mag_cal.c index e1931c352a..65ffa13d8f 100644 --- a/test/mag_cal.c +++ b/test/mag_cal.c @@ -62,9 +62,9 @@ static int test_mag_cal_computes_bias(void) /* 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"); + 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 @@ -74,9 +74,9 @@ static int test_mag_cal_computes_bias(void) 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"); + TEST_EQ(1, cal.bias[0], "%d"); + TEST_EQ(-1, cal.bias[1], "%d"); + TEST_EQ(2, cal.bias[2], "%d"); return EC_SUCCESS; } -- cgit v1.2.1