Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion about adding new ICM support and FIFO support #195

Open
Levi--G opened this issue Sep 30, 2024 · 28 comments
Open

Discussion about adding new ICM support and FIFO support #195

Levi--G opened this issue Sep 30, 2024 · 28 comments
Labels
enhancement New feature or request

Comments

@Levi--G
Copy link
Contributor

Levi--G commented Sep 30, 2024

Hi,

So i wanted to contact you about features i'd like to contribute and maybe discuss this in advance so its clear on how you might want these added. I have quite alot of experience with these IMU's over the years and since i started making my ispindels noticed some improvements that could be made.

First of all i'd like to add support for the ICM-42670-P chip, this is another recent invensense IMU that is very low power and has great accuracy and has some nice features that could really augment my readings while saving power. To do this i suggest I create an interface to abstract the IMU, and change the IMU field to be a pointer assigned on detection. If this is fine with you of course. If you want me to do it in some other way i'm all ears.

For the FIFO i'm surprised it isn't used yet. It would leave the IMU on and collecting data while the main IC sleeps and then when the ESP wakes up all the samples are already there, collected over the entire time it was sleeping thus being more accurate over time and shortening the time the esp is online. I've already ran some tests on this and it seems very promising for this scenario. With MPU6050 i get a very consistent 0.01° accuracy using FIFO. On the MPU6050 however it might use more power than sleeping (i will measure exactly how much soon). But maybe adding it as an alternative option could be beneficial? I can fork/change the code and add the option but i don't know how you make your front-end and might need some help on that side. But first i will do the power/accuracy measurement on both the new and old imu's so you can see the difference.

Regarding the accuracy/power consumption comparison i will do, do i just test the default settings (every 15m push), or is there other data you are interested in to know if you want to add it? Basically how longer the sleep time, the more fifo will use compared to all sleeping, but the larger the accuracy difference as well.

If you don't like these features and rather not have them pulled into the main, i can always maintain my own fork, thats fine for me too. ;)

Thank you for the already amazing project and hope to hear from you soon.
Levi

@mp-se
Copy link
Owner

mp-se commented Sep 30, 2024

Hi, Thanks for reaching out. I welcome any improvements for the software. So far I have been focused on supporting the original iSpindel hardware but If there are ways to improve the hardware and improve on battery life lets try it out.

Adding additional gyro hardware is not a big issue, but there should be an easy way for users to build the hardware, i.e easy access to components, PCB and container they can use (of course it should be open source).

Currently I'm using another open source library for the MPU interaction (same as iSpindel) but I have not dug into the details on the FIFO feature and if that is used, I just base the gyro interaction on the examples.

I would expect that having the gyro running that would draw more power but also make the built in temp sensor unusable since that would not have time to cool down.

I'm planning to release the 2.0 version this week so if you use the dev branch as the base for your tests that should be a good start.

its hard to put some expectation on how to develop the features since I dont really see how much code it will be and how much that would differ from the current code. Start with making them work and we can discuss how to best refactor the code to make it easy to merge and also maintain / test it.

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 1, 2024

Ok will do :)

You are right, using FIFO would give the temp sensor an offset, but not more than you already have now, either way is the temp sensor of the MPU not really that good, i see a few degrees offset even with accel and gyro disabled. The gyro is bad for battery life but it gets duty cycled in fifo mode, so i will test how much impact it really has very soon. Ill use the dev branch and keep you updated.

@mp-se
Copy link
Owner

mp-se commented Oct 1, 2024

On the gyro temp sensor, i did quite a lot of tests before adding that feature and I agree that the accuracy is not that good, but comparable to having a DS18 on low resolution and you read it as soon as the chip started. After a while it will continue to increase and be way off and it requires at least 10 minutes to cool down again....

But do your own tests and we can compare notes later.

@mp-se
Copy link
Owner

mp-se commented Oct 12, 2024

From what I could find out reading the data sheet a MPU6050 (only chip) would draw 3.5 mA which would run on an 18650 battery for 38 days. But to extend that one could change the the sleep behaviour and allow the FIFO to work in intervals. For instance of there i a sleep interval of 15 minutes

  1. Deep sleep (esp + mpu) for 10 minutes
  2. Wakeup and enable FIFO for MPU, let esp go back into deep sleep
  3. Wakeup and read mpu, push data and then repeat.

This would extend the battery life quite a lot but still allow for better readings if I understand the fifo function.

So I wondering how long should it run for accurate readings in the background ? Does i need to run all the time or is interval an good apporach?

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 13, 2024

So the MPU uses 3.5mA while measuring and enabled, when using fifo you actually normally slow down the measurements and put the gyro in a mode that will only enable it while taking a fifo sample, this should lower the power usage alot. The higher the data rate the more it uses over time. I will try to get some accurate current measurements soon about this.

Theoretically you can slow down the measurements of the mpu to +-4Hz. Meaning only 4 times/s the gyro will wake and be used. With a 4kB memory and 14B per reading that means you can store 285 samples or 71 seconds maximum without data loss, however the fifo replaces old data when full so you just always have the last minute of data at your disposal when waking after that time. This is not alot, and its one of the reasons im going to add ICM-42670-P support.

For the ICM:
The gyro on the ICM uses much less power (0.55mA for gyro+accel in continuous use) and it can slow down to virtually any data rate, meaning the fifo will be adjustably filled according to the deep sleep time so power/storage can be optimally used. the MPU doesn't have those features yet (and has been discontinued for many years now). I have made a GY-521 compatible breakout for the ICM and its on the way from the PCB manufacturer, i will share results as soon as it arrives. Of course the design will be shared with the community once tested and if people request them i can also sell them premade.

My next actions:
Since i believe FIFO will only really shine on the ICM i will initially be comparing MPU in normal mode to ICM in fifo mode on the esp8266 (classic spindle board). Once those results are in and fifo seems worth it i can backport it to the mpu and find out if we need to tweak the power consumption to stay in line with the "classic" mode.

@mp-se
Copy link
Owner

mp-se commented Oct 13, 2024

I agree that the ICM options is much more interesting to look into, seams like a much better chip. Where to you buy the chips from? I would want to get a few so I can create a test setup.

One thing I lack with the standard iSpindle hardware is the missing interrupt pin connection, that could help to reduce the power consumption since one could start the measurement and then let the esp do the networking stuff. Having to manually check for status consumes a lot of unnecessary battery (i think the read takes almost 800 ms) which is quite a lot of wasted time.

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 13, 2024

I agree that the ICM options is much more interesting to look into, seams like a much better chip. Where to you buy the chips from? I would want to get a few so I can create a test setup.

The chips you can buy from any IC seller, Mouser is really good and has large stocks, but if you want to save some bucks you can buy them from china as well, however make sure to check the whoami register on receiving them, they can be fakes or different chips. Make sure you have the needed tools to solder these tiny smd chips though, its harder that it looks. 😉 Since i do this alot for me its no issue. If everything goes well and the PCB/tests work i have no issue sending you a sample board.

One thing I lack with the standard iSpindle hardware is the missing interrupt pin connection, that could help to reduce the power consumption since one could start the measurement and then let the esp do the networking stuff. Having to manually check for status consumes a lot of unnecessary battery (i think the read takes almost 800 ms) which is quite a lot of wasted time.

Yes i have also seen the same, atm the I2c readout without any processing is taking 930ms. I have also seen the setup also takes a fair chunk of this time which can be improved without changing any functionality. But ill come back to that in a later stage. The interrupt won't really improve that i believe since the time between the readings will still be the same. You could use the fifo at a high rate and then read them at once maybe before putting the chip to sleep? But you would still need to check the fifo byte count i guess. FYI just the fifo reading takes 0.56ms/sample so +-28ms for 50samples.

I did some more testing and apparently i need to rectify smth, i said earlier the gyro consumption would be less due to the cycling, apparently the mpu6050 doesn't support that yet so it will continuously use 3.82mA sadly. So i don't think continuous fifo will ever be viable for the mpu without a loss in battery lifetime. 😞

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 13, 2024

So for temporary reference measurements i will be using (on average):
ESP8266 + MPU8266: 7.339s using 67.07mA (492.26mC) on average before going back to sleep @ 170µA for 900s (153mC)
In total that is 645.26mC per cycle of +-15min
If you have different measurements feel free to share them 😉

So next week when my boards arrive i can do a proper comparison with those values.

@mp-se
Copy link
Owner

mp-se commented Oct 13, 2024

It was a long time since i did any measurements but i use this to calculate battery life on an esp8266: 15 mA / day (sleep) and 160 mA per hour when active (this include temp and gyro). My last fermentation had an average runtime of 4.2 seconds

@mp-se mp-se added the enhancement New feature or request label Oct 16, 2024
@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 19, 2024

Okay so the boards arrived, are soldered and are working. So good so far. I'm now adding multi IMU support to the code, however while doing so i noticed you are not using default VScode/c++ formatting, could you please share your settings so i can format the code in the same way? :) I'd hate to mess up your formatting.

@mp-se
Copy link
Owner

mp-se commented Oct 19, 2024

I use precommit for formatting, there is a config in the root ditectory. I have usually run the command before check-in. So it does not matter what settings are used

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 19, 2024

Sadly i could not get the precommit to work, I'll sort that out later.

On the bright side i got classic angle measurements working today, so i can do some 1on1 comparison with the mpu tomorrow. The ICM seems to have way less jitter and inaccuracies so if the current really is lower even without using the FIFO i see that as a win/upgrade already ;)

Sadly i did make a mistake on the pcb and missed a mandatory resistor i now soldered externally. So most likely ill have to make a new pcb first before others can use it.

@mp-se
Copy link
Owner

mp-se commented Oct 20, 2024

It wont work on windows, you need linux or macos. I use wsl on my windows machine

Great that you are making progress, looking forward to the results

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 20, 2024

Ah that explains alot 😆

Okay so the bare minimum power consumptions are in, to make these tests as fair as possible i eliminated all delays the firmware introduces due to wifi connection and reading json etc by just loading an empty sketch with the exact code from the imu handling. The ICM needs a longer setup delay (45ms) to smooth out the gyro according to the datasheet, so the setup time is longer than the MPU, i think the MPU might technically also need this, but since that is never done i left the code as is. So even though the ICM uses the extra delay and is more stable, due to the higher data rate the reading is shorter and it overall uses less power. So overall even without using different techniques like the fifo the ICM42670-p uses only 19.1% of the power the MPU uses. I am planning on improving this more by optimising some calls, and afterwards ill try to compare to the fifo method with the ICM.

PS: due to the lower power consumption the temperature also has a smaller offset 👍 😉

IMU Sleep Setup Max Setup Avg Setup Time Full Max Full Avg Full Time Time per sample Total Charge Charge per sample
MPU6050 30µA 10.52 mA 5.26 mA 2.77 ms 11.43 mA 5 mA 930.8 ms 18.616 ms/S 4.65 mC 93 µC/S
ICM42670-p 39µA 4.92 mA 556 µA 45 ms 4.92 mA 1.05 mA 85 ms 1.7 ms/S 89 µC 1.8 µC/S

@mp-se
Copy link
Owner

mp-se commented Oct 20, 2024

Good work, that is positive for the power consumption even though is quite small compared to the consumption of the esp. But in combination with ble transmission is should have an bigger effect on battery life

would be interresting to see the accuracy as well, how much that could be improved.

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 20, 2024

It is indeed small compared to the esp, but the shorter readout (10x shorter) will also be seen in the total consumption since the esp can go sleep sooner, which is the next measurements i will do. Plus this might make it a candidate to use the fifo which could make for much better accuracy over time.

Regarding accuracy i have no way of measuring that in numbers, although the ICM sensor is more accurate by default, unless i spend a week pulling statistics i have no way of comparing them. What i might do is put an mpu and an icm in the same fermenter next time to compare the results? But that will not be for a few weeks.

@mp-se
Copy link
Owner

mp-se commented Oct 20, 2024

Yes, the shorter read time will probably have a bigger impact on the total consumption. Wifi is till the biggest battery killer according to my previous testing.

Would be interresting to get comparable values from the same device. It should be possible to have both sensors attached to a device and send data from, which then could be compared

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 20, 2024

Yes my current setup is without an actual soldered ISpindel, just an ESP8266 (the same one with the same firmware) where i swap the IMU just by plugging a new one in 😉

In that setup i now get the following results with the adjusted firmware for multi IMU support (that doesn't change anything to the MPU in theory) and without an external temp sensor:

MPU unit ICM unit
Sleep avg 197 µA 187 µA
Sleep time 900 s 900 s
Sleep 177,3 mC 168,3 mC
Active avg 65 mA 69 mA
Active time 4,434 s 3,375 s
Active 288,21 mC 232,875 mC
cycle time 904,434 s 903,375 s
cycle 465,51 mC 401,175 mC
% 100 % 0,861797 %

As you can see the +- 1s time gain means about 14% of energy gain per cycle.

PS: i don't know why but once attached to the ESP the MPU sleep usage is now higher, i think it was originally leaching off the I2C voltage somehow which didn't get measured, now i measured the entire circuit so the only power source was my measurement device. This should be more accurate. So even in sleep the ICM seems to use less now which corresponds to the datasheets.

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 20, 2024

Preliminary results for fifo mode with ICM suggest it uses 1/5th the power of the mpu in fifo mode. but that would still be twice as much overall compared to a normal cycle. However since there is no setup (the gyro is already on) i need to verify how much is recuperated due to the shorter esp cycle. But imo this comes close to actually being possible to use. In this mode there is a (very accurate) sample stored in fifo every 10s, compared to 50 samples (in 1s) every 15mins this should give an accuracy that is worlds apart.

tldr; ICM in fifo uses 665µA and ill test more next week after i added FIFO mode in the gravitymon code.

@mp-se
Copy link
Owner

mp-se commented Oct 20, 2024

Sounds promising, i managed to find a breakout board on aliexpress that i ordered so i can run the code. Should be here in 2 weeks

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 20, 2024

I don't know where you live but i can send one of mine if you don't mind the external resistor/shipping 😉

The aliexpress board isn't ispindel layout compatible btw, so don't forget to switch the pins around. (clk should be the third pin from VCC for ispindel layout)

@mp-se
Copy link
Owner

mp-se commented Oct 20, 2024

I don't know where you live but i can send one of mine if you don't mind the external resistor/shipping 😉

The aliexpress board isn't ispindel layout compatible btw, so don't forget to switch the pins around. (clk should be the third pin from VCC for ispindel layout)

I saw it had a different pinout but that s not a problem, will wire it to my dev board

@mp-se
Copy link
Owner

mp-se commented Oct 21, 2024

You can email me on [email protected] instead. I forgot i removed the mailservice for that domain.

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 27, 2024

Sorry i didn't get back sooner, I was busy with implementing and further testing FIFO with the ICM.

Regarding temperature: The ICM with its lower power consumption seems to have less issues with heating in FIFO mode, i tested the temperature and compared it to room temp and it consistently stays within <2°C from room temp, now i might just be lucky with my chip, but i like to think that isn't the case here. The stability is also reversed to the MPU, temp becomes stable the longer it has been since the setup/startup, since that phase uses more power.

Regarding power consumption:

Sleep avg 750,97 µA
Sleep time 900 s
Sleep 675,873 mC
Active avg 71 mA
Active time 3,21 s
Active 227,91 mC
cycle time 903,21 s
cycle 903,783 mC
% 194,149 %

The relative usage is sitting at +-200% so we are looking at half battery life compared to normal (measured on ESP8266, this will be better on ESP32). This is however close enough to consider adding at least as a compiler option for people who know what they are doing 😉

I'm also looking at the even newer ICM chips that can improve even further on this, but they are sitting at a 4€ per chip, which seems a bit much just to save some battery...

Either way except for some cleaning up i believe my current code has all the main features, how do you want to proceed on this? I expect to have a real life test this week if everything goes right and ill throw in some different boards (MPU, ICM and ICM fifo) to compare accuracy.

@Levi--G
Copy link
Contributor Author

Levi--G commented Oct 27, 2024

The current FIFO mode works as follows:

  • ESP configures IMU at power cycle according to settings (this needs work since changing sleep time setting will not work correctly atm)
  • IMU gathers 138 samples or as close to it as possible during the sleep time
  • ESP wakes and checks if the IMU is already configured => if yes skips setup phase
  • ESP reads all samples
    • All samples are individually checked for movement and discarded if needed
    • All good samples are averaged and converted
  • Angle and temp are used as normal
  • ESP sleeps again without sleeping the IMU

So far it seems very stable and accurate but the remaining issue is after changing the sleeptime the imu needs to be reset, and that is not easily possible atm, any ideas?

@mp-se
Copy link
Owner

mp-se commented Oct 28, 2024

Great news, I'm still waiting for my chip from china so I cannot do any testing/running the code until i receive that so please do some accuracy testing as well that would be very interesting to see.

On the sleep cycle, there is some logic in the code that changes the sleep cycle, especially if there is an error with WIFI connection, the device will go into sleep for 60 seconds and then retry again.

I think i will need to dig into the code and interface for the IMU to get a better understanding on how that works.

Feel free to open a PR when you think your code is ready. I will let you know when my board has arrived.

@Levi--G
Copy link
Contributor Author

Levi--G commented Nov 11, 2024

Sorry ive been busy the last week.

Currently i would like to address a few issues first before I'd consider a PR:

  • More testing (obviously)
  • Fix my board. Currently my board with the ICM chip on it started acting weird and doesn't wake from deep sleep every so often, i don't know if this is a hardware issue or a software issue so this needs investigating (but i'm fairly sure its hardware)

FiFo only issues (a PR for just supporting the new IMU would not need these fixed):

  • Mitigation for the gui/constant measure mode, currently this results in only one value every few tried readings due to the different data gathering
    => could be fixed if we use a different api/method for gui reads or for publishing reads, this way the IMU code can detect which read to use for the IMU
  • Mitigation for the issue with changing time in fifo mode requiring a power cycle to reset the IMU
    => could be fixed if we have a callback from the webserver or configuration every time it changed or is saved so we can reconfigure the IMU
  • Investigate the current code flow when no valid samples are returned, i think this isn't working properly with fifo atm, angle seems to just default to 0 instead of (not) registering a bad read
  • Either add FiFo mode option to gui or decide which will be default or available?
    => Adding the option will need a feedback to the imu similar to the time change callback, this could be a shared callback to just reload the imu

I can implement these solutions but they will need time to work out. I have some leave coming up so i should be able to work on them soon 😄

@mp-se
Copy link
Owner

mp-se commented Nov 17, 2024

You can always configure the IMU based on the startup mode, so in gravity it used the sleep timeout and when in config mode a constant read mode is used ? Then you should not need to do a power cycle if sleep timeout is changed.

Error handling is key to detect when there is no valid data or some of the logic will be way off...

I finally got my sample board from china last week so when you do a PR I can also do some testing (or at least run the code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants