Re: [PATCH v2] power: supply: core: Use library interpolation

Baolin Wang
2021-11-16T14:35:51Z

On 2021/11/16 21:34, Linus Walleij wrote:
> On Tue, Nov 16, 2021 at 3:28 AM Baolin Wang
> <baolin.wang@linux.alibaba.com> wrote:
>
>> Thanks for your patch, and overall looks good to me. But I still think
>> we should not do interpolation if the temperature is larger than the
>> maximum value of the table, we can just return the maximum value of the
>> table instead. Something like below untested code, how do you think?
>
> You are right, but if I understand correctly
> fixp_linear_interpolate() already does what you want,
> perhaps a bit unintuitively. See include/linux/fixp-arith.h:
>
> static inline int fixp_linear_interpolate(int x0, int y0, int x1, int y1, int x)
> {
> if (y0 == y1 || x == x0)
> return y0;
> if (x1 == x0 || x == x1)
> return y1;
>
> return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
> }
>

Sorry for confusing, let me try to make it clear. Suppose we have a
temperature table as below, and try to get the resistance percent in the
temp=-20 Celsius.

resistance-temp-table = <20 100>, <10 90>, <0 80>, <(-10) 60>;

With your patch, we will get i=table_len-1, which is 3. Then high=2 and
low=3.

+ for (i = 0; i < table_len - 1; i++)
if (temp > table[i].temp)
break;

So in fixp_linear_interpolate(): x0=-10, y0=60, x1=0, y1=80, x=-20, then
will return 60 + (80-60)*(-20-(-10))/(0-(-10)) = 40.

But actually the -20 Celsius is less than (-10), which is the last
member in the array, we do not need the interpolation, return 60
directly if I understand correctly. Which means for any other lower
temperature points, the resistance of the baterry is always 60% of the
battery internal resistence in normal temperature.