adafruit / Adafruit_LSM6DS

Arduino library for LSM6DS
Other
47 stars 39 forks source link

Code dedup #33

Open eringerli opened 2 years ago

eringerli commented 2 years ago

added methods for converting the raw values to the ones in SI units

This change moves the code for the conversion of raw values to their own virtual methods. This enables the next commit:

LSM6DSO32: removed a lot of duplicated code

With dedicated converter methods, the LSM6DSO32 can do its conversion for the bigger ranges there, so duplicated code (the whole _read() method) can be removed. Also, getAccelRange() and setAccelRange() are exactly the same as in the base class, the difference is only the enum type. Replaced these methods with a call to the base class with the casted range.

ladyada commented 2 years ago

these functions should take a value and return a value, or take in a pointer to 3 floats and do the conversion?

eringerli commented 2 years ago

I thought, as these two methods are protected, they can access the data directly. As virtual methods can't be inlined, I took the most efficient way by doing the conversion in place, without using 6 (+6 in the most used way of accessing/converting temp&acc&gyro) references in code that gets called potentionally thousands of times a second.

I can do it, if you really want to, but I hope I wouldn't have to.

caternuson commented 2 years ago

so duplicated code (the whole _read() method) can be removed.

Don't the other derived classes still need _read() from the LSM6DS base class?

eringerli commented 2 years ago

The only class which reimpmement it is LSM6DSO32, because it has extended ranges. As the conversion is done in two virtual methods (first commit), I reimplemented the one which is actually different anew. _read() is exactly the same for all the chips in this lib, this could even be made non-virtual.

caternuson commented 2 years ago

Oh, sry, was reading the code diff wrong. The _read was removed from the derived class Adafruit_LSM6DSO32. Not the base class.

OK, this looks like some nice code clean up.

Did you test these changes?

eringerli commented 2 years ago

TBH, I don't own an LSM6DSO32. But after I worked on PR #34, the dedup was obvious.

eringerli commented 2 years ago

@caternuson It is almost three weeks later. Are there any changes I have to implement?

caternuson commented 2 years ago

Sorry. Wanted to find a time to test this on hardware. Went ahead and set something up for testing.

Looks like something isn't quite correct somewhere.

Running the adafruit_lsm6dso32_test.ino example on an Itsy M4 (PID 3800) with a LSM6DSO32 breakout (PID 4692) with current library produces expected output (accelo held flat on table): Screenshot from 2022-05-26 14-04-10

With PR code, readings are off: Screenshot from 2022-05-26 14-03-38

Looks like maybe a factor of 2 somewhere?

eringerli commented 2 years ago

I have found the error: getAccelRange() and setAccelRange() are not used in _read() in this branch. I suggest testing PR #34, there the ranges should be correct. I can close this and add the description to #34 if you like.

ladyada commented 2 years ago

@eringerli can you amend this PR so it is complete and working? alternatively, can reduce this PR - each PR should be independant so it can be tested/reverted as needed.