diff options
author | Yuval Peress <peress@chromium.org> | 2020-01-16 11:20:01 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-03-09 23:02:12 +0000 |
commit | a00ace498da24a9288fd39f9728c8fd80cd54336 (patch) | |
tree | 98ea22bd6f4d1098bb9dfdaafb98795e1024eb27 /test/online_calibration.c | |
parent | 5ccb5c7cc9c448b66c6f8e0abbb2dd785da0efb7 (diff) | |
download | chrome-ec-a00ace498da24a9288fd39f9728c8fd80cd54336.tar.gz |
common: online_calibration: Fire MKBP event on new calibration
Implement online calibration for accelerometers and fire a new
MKBP event when a new calibration value is computed.
TEST=Added new unit tests
BRANCH=None
BUG=b:138303429,chromium:1023858
Change-Id: I31ec7164be0d8c7dac210a1ac4b94ec9ecd6a60a
Signed-off-by: Yuval Peress <peress@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2012847
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Diffstat (limited to 'test/online_calibration.c')
-rw-r--r-- | test/online_calibration.c | 111 |
1 files changed, 106 insertions, 5 deletions
diff --git a/test/online_calibration.c b/test/online_calibration.c index f9fe0ee068..f51781f2ac 100644 --- a/test/online_calibration.c +++ b/test/online_calibration.c @@ -3,11 +3,18 @@ * found in the LICENSE file. */ +#include "accel_cal.h" +#include "accelgyro.h" +#include "hwtimer.h" #include "online_calibration.h" #include "test_util.h" -#include "hwtimer.h" #include "timer.h" -#include "accelgyro.h" +#include <stdio.h> + +int mkbp_send_event(uint8_t event_type) +{ + return 1; +} struct mock_read_temp_result { void *s; @@ -36,15 +43,71 @@ static int mock_read_temp(const struct motion_sensor_t *s, int *temp) return EC_ERROR_UNKNOWN; } +struct mock_get_range_result { + struct motion_sensor_t *s; + int ret; + struct mock_get_range_result *next; +}; + +static struct mock_get_range_result *mock_get_range_results; + +static int mock_get_range(const struct motion_sensor_t *s) +{ + struct mock_get_range_result *ptr = mock_get_range_results; + + while (ptr) { + if (ptr->s == s) + return ptr->ret; + ptr = ptr->next; + } + + return 4; +} + static struct accelgyro_drv mock_sensor_driver = { .read_temp = mock_read_temp, + .get_range = mock_get_range, }; static struct accelgyro_drv empty_sensor_driver = {}; +static struct accel_cal_algo base_accel_cal_algos[] = { + { + .newton_fit = NEWTON_FIT(4, 15, FLOAT_TO_FP(0.01f), + FLOAT_TO_FP(0.25f), + FLOAT_TO_FP(1.0e-8f), 100), + } +}; + +static struct accel_cal base_accel_cal_data = { + .still_det = STILL_DET(FLOAT_TO_FP(0.00025f), 800 * MSEC, 1200 * MSEC, + 5), + .algos = base_accel_cal_algos, + .num_temp_windows = ARRAY_SIZE(base_accel_cal_algos), +}; + +static bool next_accel_cal_accumulate_result; +static fpv3_t next_accel_cal_bias; + +bool accel_cal_accumulate( + struct accel_cal *cal, uint32_t sample_time, fp_t x, fp_t y, fp_t z, + fp_t temp) +{ + if (next_accel_cal_accumulate_result) { + cal->bias[X] = next_accel_cal_bias[X]; + cal->bias[Y] = next_accel_cal_bias[Y]; + cal->bias[Z] = next_accel_cal_bias[Z]; + } + return next_accel_cal_accumulate_result; +} + struct motion_sensor_t motion_sensors[] = { [BASE] = { + .type = MOTIONSENSE_TYPE_ACCEL, .drv = &mock_sensor_driver, + .online_calib_data[0] = { + .type_specific_data = &base_accel_cal_data, + }, }, [LID] = { .drv = &empty_sensor_driver, @@ -61,7 +124,7 @@ static int test_read_temp_on_stage(void) int rc; mock_read_temp_results = &expected; - data.sensor_num = 0; + data.sensor_num = BASE; rc = online_calibration_process_data( &data, &motion_sensors[0], __hw_clock_source_read()); @@ -79,7 +142,7 @@ static int test_read_temp_from_cache_on_stage(void) int rc; mock_read_temp_results = &expected; - data.sensor_num = 0; + data.sensor_num = BASE; rc = online_calibration_process_data( &data, &motion_sensors[0], __hw_clock_source_read()); TEST_EQ(rc, EC_SUCCESS, "%d"); @@ -101,7 +164,7 @@ static int test_read_temp_twice_after_cache_stale(void) int rc; mock_read_temp_results = &expected; - data.sensor_num = 0; + data.sensor_num = BASE; rc = online_calibration_process_data( &data, &motion_sensors[0], __hw_clock_source_read()); TEST_EQ(rc, EC_SUCCESS, "%d"); @@ -116,6 +179,43 @@ static int test_read_temp_twice_after_cache_stale(void) return EC_SUCCESS; } +static int test_new_calibration_value(void) +{ + struct mock_read_temp_result expected = { &motion_sensors[BASE], 200, + EC_SUCCESS, 0, NULL }; + struct ec_response_motion_sensor_data data; + struct ec_response_online_calibration_data cal_data; + int rc; + + mock_read_temp_results = &expected; + next_accel_cal_accumulate_result = false; + data.sensor_num = BASE; + + rc = online_calibration_process_data( + &data, &motion_sensors[BASE], __hw_clock_source_read()); + TEST_EQ(rc, EC_SUCCESS, "%d"); + TEST_EQ(online_calibration_has_new_values(), false, "%d"); + + next_accel_cal_accumulate_result = true; + next_accel_cal_bias[X] = 0.01f; /* expect: 81 */ + next_accel_cal_bias[Y] = -0.02f; /* expect: -163 */ + next_accel_cal_bias[Z] = 0; /* expect: 0 */ + rc = online_calibration_process_data( + &data, &motion_sensors[BASE], __hw_clock_source_read()); + TEST_EQ(rc, EC_SUCCESS, "%d"); + TEST_EQ(online_calibration_has_new_values(), true, "%d"); + + rc = online_calibration_read(BASE, cal_data.data); + TEST_EQ(rc, true, "%d"); + TEST_EQ(cal_data.data[X], 81, "%d"); + TEST_EQ(cal_data.data[Y], -163, "%d"); + TEST_EQ(cal_data.data[Z], 0, "%d"); + + TEST_EQ(online_calibration_has_new_values(), false, "%d"); + + return EC_SUCCESS; +} + void before_test(void) { mock_read_temp_results = NULL; @@ -129,6 +229,7 @@ void run_test(void) RUN_TEST(test_read_temp_on_stage); RUN_TEST(test_read_temp_from_cache_on_stage); RUN_TEST(test_read_temp_twice_after_cache_stale); + RUN_TEST(test_new_calibration_value); test_print_result(); } |