Weird timing

Since I reconfigured the sampling rate to 500Hz from 1kHz, everything has been working much better.  I’ve increased flight times to 11.5s (1.5s warm-up, 2s take-off, 6 seconds hover and 2 seconds landing), and also been able to move to within 2m of both Chloe and Phoebe without risk of them slashing one of my arteries.  All great.  And all because there’s now enough time between samples for motion processing to take place, and hence now samples are lost.

The only thing that’s odd though is timing: all of HoG’s timing is based on the sampling rate: elapsed time = number of samples / sampling rate.

In the current flights I get 5760 samples = 11.52 seconds elapsed time as expected.

But if I wrap the flight code with a couple of time.time()’s to measure the actually flight time, it comes out at 8.64 seconds, suggesting that the sampling rate isn’t 500Hz as configured, but more like (11.52 / 8.64) * 500Hz = 666.666666666666… to 32 digit accuracy!  And that’s weird as it’s not possible to configure the IMU to sample at this rate – sampling rates are (1kHz / an integer).

The nearest thing I can find in the MPU-9250 data sheet section “4.4 Register 25 – Sample Rate Divider” is this:

Data should be sampled at or above sample rate; SMPLRT_DIV is only used for 1kHz internal sampling.

Perhaps by setting the ADC sample rate to 500Hz, the MPU-9250 also sets the data ready interrupt frequency to 666Hz to ensure the above rule is maintained.  There is no mention of 666Hz throughout the documentation; if my supposition is correct, then it’s a little poor to hide this fact to be inferred from a single sentence embedded in the depths of the very large register map document!


P.S. As well as affecting the length of flights, it also has an effect on velocities and angles which both use integration over time of the accelerometer and gyro respectively. The former isn’t significant in the grand scheme of things, but the latter probably is: merging angles from the accelerometer and integrated gyro will mean short term, the gyro angles are over-estimated. The ‘fix’ is simple, though again a bit of a hack as it needs to know through testing the difference in ADC sampling- and data ready interrupt frequencies.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.