* [PATCH v3] iio: cros_ec: Fix the maths for gyro scale calculation
@ 2019-03-13 11:40 Enric Balletbo i Serra
2019-03-16 16:09 ` Jonathan Cameron
0 siblings, 1 reply; 2+ messages in thread
From: Enric Balletbo i Serra @ 2019-03-13 11:40 UTC (permalink / raw)
To: Jonathan Cameron, linux-iio, linux-kernel
Cc: Gwendal Grignou, Peter Meerwald-Stadler, Guenter Roeck,
Benson Leung, Lars-Peter Clausen, kernel, Hartmut Knaack
From: Gwendal Grignou <gwendal@chromium.org>
Calculation did not use IIO_DEGREE_TO_RAD and implemented a variant to
avoid precision loss as we aim a nano value. The offset added to avoid
rounding error, though, doesn't give us a close result to the expected
value. E.g.
For 1000dps, the result should be:
(1000 * pi ) / 180 >> 15 ~= 0.000532632218
But with current calculation we get
$ cat scale
0.000547890
Fix the calculation by just doing the maths involved for a nano value
val * pi * 10e12 / (180 * 2^15)
so we get a closer result.
$ cat scale
0.000532632
Fixes: c14dca07a31d ("iio: cros_ec_sensors: add ChromeOS EC Contiguous Sensors driver")
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
---
Changes in v3:
- Fix 'warning: this decimal constant is unsigned only in ISO C90' spotted by kbuild test robot.
Changes in v2:
- Do the maths instead of play with dividers as pointed by Jonathan.
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
index 89cb0066a6e0..8d76afb87d87 100644
--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
@@ -103,9 +103,10 @@ static int cros_ec_sensors_read(struct iio_dev *indio_dev,
* Do not use IIO_DEGREE_TO_RAD to avoid precision
* loss. Round to the nearest integer.
*/
- *val = div_s64(val64 * 314159 + 9000000ULL, 1000);
- *val2 = 18000 << (CROS_EC_SENSOR_BITS - 1);
- ret = IIO_VAL_FRACTIONAL;
+ *val = 0;
+ *val2 = div_s64(val64 * 3141592653ULL,
+ 180 << (CROS_EC_SENSOR_BITS - 1));
+ ret = IIO_VAL_INT_PLUS_NANO;
break;
case MOTIONSENSE_TYPE_MAG:
/*
--
2.20.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v3] iio: cros_ec: Fix the maths for gyro scale calculation
2019-03-13 11:40 [PATCH v3] iio: cros_ec: Fix the maths for gyro scale calculation Enric Balletbo i Serra
@ 2019-03-16 16:09 ` Jonathan Cameron
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2019-03-16 16:09 UTC (permalink / raw)
To: Enric Balletbo i Serra
Cc: linux-iio, linux-kernel, Gwendal Grignou, Peter Meerwald-Stadler,
Guenter Roeck, Benson Leung, Lars-Peter Clausen, kernel,
Hartmut Knaack
On Wed, 13 Mar 2019 12:40:02 +0100
Enric Balletbo i Serra <enric.balletbo@collabora.com> wrote:
> From: Gwendal Grignou <gwendal@chromium.org>
>
> Calculation did not use IIO_DEGREE_TO_RAD and implemented a variant to
> avoid precision loss as we aim a nano value. The offset added to avoid
> rounding error, though, doesn't give us a close result to the expected
> value. E.g.
>
> For 1000dps, the result should be:
>
> (1000 * pi ) / 180 >> 15 ~= 0.000532632218
>
> But with current calculation we get
>
> $ cat scale
> 0.000547890
>
> Fix the calculation by just doing the maths involved for a nano value
>
> val * pi * 10e12 / (180 * 2^15)
>
> so we get a closer result.
>
> $ cat scale
> 0.000532632
>
> Fixes: c14dca07a31d ("iio: cros_ec_sensors: add ChromeOS EC Contiguous Sensors driver")
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
Applied to the fixes-togreg branch of iio.git and marked for stable.
Thanks,
Jonathan
> ---
>
> Changes in v3:
> - Fix 'warning: this decimal constant is unsigned only in ISO C90' spotted by kbuild test robot.
>
> Changes in v2:
> - Do the maths instead of play with dividers as pointed by Jonathan.
>
> drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> index 89cb0066a6e0..8d76afb87d87 100644
> --- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> +++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
> @@ -103,9 +103,10 @@ static int cros_ec_sensors_read(struct iio_dev *indio_dev,
> * Do not use IIO_DEGREE_TO_RAD to avoid precision
> * loss. Round to the nearest integer.
> */
> - *val = div_s64(val64 * 314159 + 9000000ULL, 1000);
> - *val2 = 18000 << (CROS_EC_SENSOR_BITS - 1);
> - ret = IIO_VAL_FRACTIONAL;
> + *val = 0;
> + *val2 = div_s64(val64 * 3141592653ULL,
> + 180 << (CROS_EC_SENSOR_BITS - 1));
> + ret = IIO_VAL_INT_PLUS_NANO;
> break;
> case MOTIONSENSE_TYPE_MAG:
> /*
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-03-16 16:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-13 11:40 [PATCH v3] iio: cros_ec: Fix the maths for gyro scale calculation Enric Balletbo i Serra
2019-03-16 16:09 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).