You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Gryro Drift Issue During Longer Runs - How To Debug #1687
This is from this years WRO competition. One of the teams is experiencing quite severe gyro drift. This is an advanced team with their own gyro drive function base on reading imu.heading() and feeding the deviation with a simpleproportional controller into drivebase.drive(). This is used throughout the run for all straight movements.
The main run is composed of 4 sections, where in between those sections realignment against the boarder is performed and imu.reset_heading(angle) as well as drivebase.reset() is called.
This works very well for the first 3 sections or when each of the 4 sections is performed in isolation. However, if all sections are run in sequence imu.heading() drifts off significantly towards the very end of the run. We have observed deviations of more than 15 degrees. The deviation is so large that this seems not to be a simple accumulation as well as running isolated sections is inconspicuous. The deviation is always towards the same direction.
So we would like to debug this (unless there is a immediate solution).
For one thing we would suggest to investigate the current state of internal drift compensation and see how it changes under
certain circumstances and influences. Is there a way to read the values (and output them to the console)? Is there a way to force the hub explicitly to recalibrate during a run? How long would we have to pause the robot so that auto recalibration occurs?
Do you have any other ideas of what we can look at?
I can rebuild the firmware and perform some simple changes like changing constants if that helps.
This works very well for the first 3 sections or when each of the 4 sections is performed in isolation. However, if all sections are run in sequence imu.heading() drifts off significantly towards the very end of the run. We have observed deviations of more than 15 degrees.
Is the robot flat during its 4th run, or perhaps driving over objects or driving on ramps?
Does it also happen if you do the first 3 sections, twice?
Is there a way to read the values (and output them to the console)?
You can use the print command for this. You can print multiple columns:
For example you could do this in a loop, or as part of the rest of your code.
Then copy the output to Excel to easily make a graph.
I would be especially curious if that 15 degrees builds over time, or whether there is a discontinuous jump.
This is used throughout the run for all straight movements.
Do you run straight with respect to the start of that maneuver each time, or do you try to maintain an overal compass-like angle?
The main run is composed of 4 sections, where in between those sections realignment against the boarder is performed and imu.reset_heading(angle) as well as drivebase.reset() is called.
It's worth trying drivebase.stop(), as well. This will also reset any "holding mode", in case you were using it.
We can reproduce the drift by only running the first 3 sections (in a loop), too. But it takes quite a bit longer to manifest: about 5 to 6 minutes.
The fourth section shows the problem in much shorter time. There is more stopping, starting, forth and back, and also turning in the fourth section.
We suspect there is a particular operation in the fourth section which makes this worse. But we are still trying to isolate that.
Apart from that we found an external factor: ambient temperature. In hindsight the worst runs seem to have happen either when the ambient temperature was relatively high or wenn there were a lot of runs in a short time (we suspect some heat accumulated inside the hub) or both together. After heating up the entire robot to 45 degrees Celsius (above freezing point) the problem manifested rather quickly and much more severely.
These are the nominal sensitivities to temperature, for both the rate magnitude and the drift (the "zero rate"). Both will affect the result of a turn.
On a slightly unrelated note, I think we can also improve the usual calibration on boot a bit. Right now, we wait for stationarity but assume 0 until then. We could potentially save the offset values just like we will be saving a few other IMU calibration settings. Then it will drift a bit less until the first calibration has occured.
We can reproduce the drift by only running the first 3 sections (in a loop), too. But it takes quite a bit longer to manifest: about 5 to 6 minutes.
The fourth section shows the problem in much shorter time. There is more stopping, starting, forth and back, and also turning in the fourth section.
We suspect there is a particular operation in the fourth section which makes this worse. But we are still trying to isolate that.
Apart from that we found an external factor: ambient temperature. In hindsight the worst runs seem to have happen either when the ambient temperature was relatively high or wenn there were a lot of runs in a short time (we suspect some heat accumulated inside the hub) or both together. After heating up the entire robot to 45 degrees Celsius (above freezing point) the problem manifested rather quickly and much more severely.
@DrTom Would you be willing to show the phenomena on a video here or even in pm?
During FLL, WRO I have not seen this with pybricks and would be keen to lean and debug.
I run into a similor issue. I'm using SPIKE Prime hub on a drive base. The run takes about 20secs.
If the hub is freshly rebooted, it works fine. After running for several times, it start to drift.
After reading this thread, I tried to stop the base for 1sec between steps and print a serise of hub.imu.angular_velocity(Axis.Z). When it started to drift, the angular_velocity was >0.1 (sometimes >1 even). I can see the heading drifts quickly.
When it starts to drift, only reboot could get it back to normal (angular_velocity ~= 0). But If I run again at this time, it's more likely to drift again during the run.
I also suspect that heat accumulation might be the reason, because I used run_until_stall() a lot which might be power consuming and heat up the motor driver chips.
I will conitnue diagnose and post my finding and update.
Thanks for your note. Breaking it down into multiple moves does not impact gyro calibration, so this may be masking something else.
At the moment, the gyro calibrates mainly the first time it is stationary after boot, and then only gradually updates the calibrated value. Maybe we should measure the temperature and allow for faster re-calibration if the temperature has changed.
Since break down helps, maybe temperature is not the cause. I have also tried shaking and tile the base and table while running, it can't be reproduced. Only turn(-135) in the middle of the run could trigger that.
I will try to reproduce with simplest steps and provide a code later.
So to read temperature, we'd have to also change gyro and acceleration readings, in a way that still provides consistent timing.
I was afraid of this answer. So to rephrase: where it is it will read only once. If I pull it into the for loop (not sure if even possible) it will (likely) have adverse effects on the timing and the result may be or will be off :-(
Having temperature measurements may still not be a silver bullet by the way
Yes, I have that im impression too. For some other reason: when heating up the system there seem to be a peak where angular velocity is off most from initial state. After that it seems to ease somewhat again (never gets anywhere near the initial values). It took quite a while to see this, I don't remember but around 1/4 to 1/2 hour (and I only saw this by accident because I forgot about the experiment was still running). While this doesn't matter for the usual use case of a few minutes it indicates that a simple look-up table with temperature and drift correction (and I was hoping for that) might not be feasible.
I wonder where this effect is coming from (just for curiosity). I believe I read that soldering the chip has effects on the drift because of tension and this is one of the reasons why there needs calibration afterwards. If that is true some temperature gradient inside the hub might cause this. Such a gradient would ease off a little during a longer times. We should drill some holes into the hub and put a fan on it, that might resolve all of it ;-)
The heat and temperature discussion arose because rising temperature inside the hub can cause changes in the readout and finally cause the heading to drift off.
Heating the hub up by external heat source was considered (I actually made a little experiment a while ago in a convection oven, which hinted in this direction). But it was then suggested to use the hub itself to heat it up by either LEDs (@dlech ) and or the motor controls (@laurensvalk). We did that and we could observer that (the uncompensated) angular velocity indeed changes under this conditions.
I would like to have a measurement of temperature, too. Firstly to confirm correlation with angular drift and put some numbers to it. Finally I hoped it could help us to fix the problem. It seems now questionable how much can be achieved by that.
It makes more sense to use the existing temperature sensor inside the IMU than adding another external one.
Now, the code to readout of the IMU in pybricks ist quite optimised for efficiency. Unfortunately, it seems hard to add temperature readout to it. I succeeded to read the temperature once when the IMU is initialized; but it never gets updated from there on. For continuous readout it seems that a complete overhaul of the code is required. Since it is uncertain if anything is gained by this it is unlikely to happen.
I would like to have a measurement of temperature, too. Firstly to confirm correlation with angular drift and put some numbers to it. Finally I hoped it could help us to fix the problem. It seems now questionable how much can be achieved by that.
It could still be useful in principle. Even if the axes are not all the same, we could store the x/y/z bias at a few temperature levels (say, low, medium, high), and interpolate based on the current temperature. This is assuming that the bias variation is consistent and monotonic in temperature.
At the moment, my focus is on pbio/imu for working on the 3D algorithms and calibration for angular velocity scale (which is separate from bias).
Adding continuous raw temperature measurements and (potentially) restructuring the way we read angular velocity and acceleration could be a separate task to be worked on in drv/imu_lsm6ds3tr_c_stm32.
There are significant differences between hubs and axes. Some seem to be just OK over a wide range, others seem problematic.
For the most part linear regression fits nicely. Hub RG shows non linear behaviour in the beginning. That could be a side effect from my measurement procedure. I tried to increase the temperature slowly. But that didn't work quite well in the beginning here. However, this is also an indication that applying linear regression might not be simple or not be possible at all.
I have not yet measured the hub we had the most problems with and also not the temperature change during an actual run.
I will publish the code and all. I need some time to collect and clean all up. It is distributed over 3 projects now.
By the way: I can only do that on top of your development branch imu:
Please keep the option calibrated=False in pybricks, it is very valuable. 🙏
I have quite a lot of crashes. I don't know where they are coming from yet. Either from my hacky extension with I2C temperature reading or something other. They are definitely related to to the IMU. Moving the hub around is almost guaranteed to cause a hub cash in a very short time.
Update: the crashes are related to the temperature readout :-(
I must have one of those hubs that doesn't change so much when I did my test way back when.
Keep that one. You can maybe auction it: "prime hub with very stable IMU" 😂
one of the steps should be "put your hub in the oven"
😂 Actually, I didn't need to do that thanks to your and @laurensvalk input. Increasing brightness of the LED lights and DC of two stalled motors gets easily to 65 deg Celsius (even higher, I stopped it at 70). It just takes some time: a test runs for about 45 minutes.
Delta temperature vs delta time is the big unknown here. The diagrams do look useable because I made it quite a slow process. I am quite certain that in reality the rate is much higher. We will see.
At least all of this can be used to pick some axis of some hub which works reasonably well (if this option is available).
There is potentially a way we could incorporate this without making it too difficult to the user.
Instead of storing a single 3-axis bias persistently, we could store 2 sets of 3-axis bias + temperature and assume linearity between them.
The set for the upper end may not be available at first but could be set in the background when the hub is stable after reaching a higher temperature.
I have quite a lot of crashes. I don't know where they are coming from yet (...) Update: the crashes are related to the temperature readout :-(
Thanks for updating this - I was curious about that. If you do see issues with the new 3D integration (without temperature additions), feel free to share.
Getting the I2C operations right will be a bit of a chore but might work if we properly dive into it. Your data has demonstrated that this is potentially worth it.
It's been a while since we looked into the FIFO buffering, so I don't quite remember why we ultimately choose not to use it. But maybe we can use that to ensure consistently timed values and occasionally read a temperature sample in between.
We currently do a round-read of the acceleration and gyro registers. We can have a look whether the temperature can be read as part of that.
Here are our options (see picture above):
Unfortunately, that does not include temperature. So if we want to use temperature, we'll have to either read that separately, or perhaps use the device FIFO buffers instead of rounding as we do now. That could be worth doing, but this is a fairly extensive task.
If we do use the FIFO, this does seem to allow inclusion of temperature:
So maybe it is worth double checking if round-reading also supports it.
Otherwise, if we do use FIFO mode, maybe we can set this up to produce a single timestamped frame, since we don't necessarily need a FIFO buffer as such, unless this would allow us to pick a higher data rate. It looks like we would want to use continous mode with a watermark index to tell us to drain it in time.
All this does look like it would give a significantly higher I2C handling overhead.
Activity
laurensvalk commentedon Jun 24, 2024
Thanks for your detailed feedback.
You can use the print command for this. You can print multiple columns:
For example you could do this in a loop, or as part of the rest of your code.
Then copy the output to Excel to easily make a graph.
I would be especially curious if that 15 degrees builds over time, or whether there is a discontinuous jump.
Do you run straight with respect to the start of that maneuver each time, or do you try to maintain an overal compass-like angle?
It's worth trying
drivebase.stop(), as well. This will also reset any "holding mode", in case you were using it.DrTom commentedon Jun 28, 2024
Some preliminary observations:
We can reproduce the drift by only running the first 3 sections (in a loop), too. But it takes quite a bit longer to manifest: about 5 to 6 minutes.
The fourth section shows the problem in much shorter time. There is more stopping, starting, forth and back, and also turning in the fourth section.
We suspect there is a particular operation in the fourth section which makes this worse. But we are still trying to isolate that.
Apart from that we found an external factor: ambient temperature. In hindsight the worst runs seem to have happen either when the ambient temperature was relatively high or wenn there were a lot of runs in a short time (we suspect some heat accumulated inside the hub) or both together. After heating up the entire robot to 45 degrees Celsius (above freezing point) the problem manifested rather quickly and much more severely.
Does the IMU have temperature compensation? It seems to be an LsM6DS3(TR-C). I did not find anything hinting to this. However https://community.st.com/t5/mems-sensors/lsm6ds3-temperature-compensation/td-p/294475 seems to confirm the problem.
laurensvalk commentedon Jun 28, 2024
Good point!
It can measure temperature, but does not automatically account for it. Potentially, we could do that in our own driver.
I've attached the datasheet below in case anyone is looking for some light reading over the holidays 🌞
lsm6ds3tr-c.pdf
These are the nominal sensitivities to temperature, for both the rate magnitude and the drift (the "zero rate"). Both will affect the result of a turn.
laurensvalk commentedon Jun 28, 2024
On a slightly unrelated note, I think we can also improve the usual calibration on boot a bit. Right now, we wait for stationarity but assume 0 until then. We could potentially save the offset values just like we will be saving a few other IMU calibration settings. Then it will drift a bit less until the first calibration has occured.
afarago commentedon Jul 21, 2024
@DrTom Would you be willing to show the phenomena on a video here or even in pm?
During FLL, WRO I have not seen this with pybricks and would be keen to lean and debug.
DrTom commentedon Jul 24, 2024
@afarago
Yes we can do either. However, it will have to wait until we are back from vacations in about 3 weeks.
scatwang commentedon Sep 17, 2024
I run into a similor issue. I'm using SPIKE Prime hub on a drive base. The run takes about 20secs.
If the hub is freshly rebooted, it works fine. After running for several times, it start to drift.
After reading this thread, I tried to stop the base for 1sec between steps and print a serise of
hub.imu.angular_velocity(Axis.Z). When it started to drift, the angular_velocity was >0.1 (sometimes >1 even). I can see the heading drifts quickly.When it starts to drift, only reboot could get it back to normal (angular_velocity ~= 0). But If I run again at this time, it's more likely to drift again during the run.
I also suspect that heat accumulation might be the reason, because I used
run_until_stall()a lot which might be power consuming and heat up the motor driver chips.I will conitnue diagnose and post my finding and update.
scatwang commentedon Sep 18, 2024
Update:
I found the root cause, there is a
drive_base.turn(-135)usually cause the problem. It was fixed after I break that down likelaurensvalk commentedon Sep 18, 2024
Thanks for your note. Breaking it down into multiple moves does not impact gyro calibration, so this may be masking something else.
At the moment, the gyro calibrates mainly the first time it is stationary after boot, and then only gradually updates the calibrated value. Maybe we should measure the temperature and allow for faster re-calibration if the temperature has changed.
scatwang commentedon Sep 18, 2024
Since break down helps, maybe temperature is not the cause. I have also tried shaking and tile the base and table while running, it can't be reproduced. Only turn(-135) in the middle of the run could trigger that.
I will try to reproduce with simplest steps and provide a code later.
laurensvalk commentedon Sep 18, 2024
Does it really produce drift? That is, does hub.imu.heading() give a wrong value and does it drift up or down?
Or is the robot not moving by the intended amount?
Thanks for looking into this!
25 remaining items
laurensvalk commentedon Oct 25, 2024
The
for(;;)loop is continuously round reading this. There doesn't seem to be a way to do that and include temperature.So to read temperature, we'd have to also change gyro and acceleration readings, in a way that still provides consistent timing.
Having temperature measurements may still not be a silver bullet by the way, since it didn't seem to affect all axes in the same consistent way.
DrTom commentedon Oct 25, 2024
@laurensvalk
I was afraid of this answer. So to rephrase: where it is it will read only once. If I pull it into the for loop (not sure if even possible) it will (likely) have adverse effects on the timing and the result may be or will be off :-(
Yes, I have that im impression too. For some other reason: when heating up the system there seem to be a peak where angular velocity is off most from initial state. After that it seems to ease somewhat again (never gets anywhere near the initial values). It took quite a while to see this, I don't remember but around 1/4 to 1/2 hour (and I only saw this by accident because I forgot about the experiment was still running). While this doesn't matter for the usual use case of a few minutes it indicates that a simple look-up table with temperature and drift correction (and I was hoping for that) might not be feasible.
I wonder where this effect is coming from (just for curiosity). I believe I read that soldering the chip has effects on the drift because of tension and this is one of the reasons why there needs calibration afterwards. If that is true some temperature gradient inside the hub might cause this. Such a gradient would ease off a little during a longer times. We should drill some holes into the hub and put a fan on it, that might resolve all of it ;-)
scatwang commentedon Oct 25, 2024
Hi all,
I didn't read all the thread, but many discussion was about how to heat the sensor and read temperature. I have two dumb idea
DrTom commentedon Oct 25, 2024
@scatwang
The heat and temperature discussion arose because rising temperature inside the hub can cause changes in the readout and finally cause the heading to drift off.
Heating the hub up by external heat source was considered (I actually made a little experiment a while ago in a convection oven, which hinted in this direction). But it was then suggested to use the hub itself to heat it up by either LEDs (@dlech ) and or the motor controls (@laurensvalk). We did that and we could observer that (the uncompensated) angular velocity indeed changes under this conditions.
I would like to have a measurement of temperature, too. Firstly to confirm correlation with angular drift and put some numbers to it. Finally I hoped it could help us to fix the problem. It seems now questionable how much can be achieved by that.
It makes more sense to use the existing temperature sensor inside the IMU than adding another external one.
Now, the code to readout of the IMU in pybricks ist quite optimised for efficiency. Unfortunately, it seems hard to add temperature readout to it. I succeeded to read the temperature once when the IMU is initialized; but it never gets updated from there on. For continuous readout it seems that a complete overhaul of the code is required. Since it is uncertain if anything is gained by this it is unlikely to happen.
This is where we stand now.
DrTom commentedon Oct 25, 2024
@laurensvalk
I extended my initial one time temperature readout to continuous readout (it is quite a hack):
DrTom/pybricks-micropython@5dc7237
It works only if remove the comments from line 396 and 397: DrTom/pybricks-micropython@5dc7237#diff-382fccca3d04c7ad3438817576499b174b524cd37426af9561f4ed4ba0c9dac5R396-R397
This reads continuously the temperature, but of course breaks all other IMU readouts.
My intention was to force a retry about every second with lines 435-R438
DrTom/pybricks-micropython@5dc7237#diff-382fccca3d04c7ad3438817576499b174b524cd37426af9561f4ed4ba0c9dac5R435-R438
That never works, the temperature stays the same from the initial reading. What I am missing here?
dlech commentedon Oct 25, 2024
Because it is a protothread.
int ineeds to bestatic int i, otherwise the value is lost between yields.laurensvalk commentedon Oct 26, 2024
It could still be useful in principle. Even if the axes are not all the same, we could store the x/y/z bias at a few temperature levels (say, low, medium, high), and interpolate based on the current temperature. This is assuming that the bias variation is consistent and monotonic in temperature.
At the moment, my focus is on
pbio/imufor working on the 3D algorithms and calibration for angular velocity scale (which is separate from bias).Adding continuous raw temperature measurements and (potentially) restructuring the way we read angular velocity and acceleration could be a separate task to be worked on in
drv/imu_lsm6ds3tr_c_stm32.DrTom commentedon Oct 26, 2024
With the temperature readout working now I can finally share some results. Scatterplots of angular velocity vs temperature for two hubs:
There are significant differences between hubs and axes. Some seem to be just OK over a wide range, others seem problematic.
For the most part linear regression fits nicely. Hub RG shows non linear behaviour in the beginning. That could be a side effect from my measurement procedure. I tried to increase the temperature slowly. But that didn't work quite well in the beginning here. However, this is also an indication that applying linear regression might not be simple or not be possible at all.
I have not yet measured the hub we had the most problems with and also not the temperature change during an actual run.
dlech commentedon Oct 27, 2024
Oh, wow! I must have one of those hubs that doesn't change so much when I did my test way back when.
I guess when we get the fancy calibration UI, one of the steps should be "put your hub in the oven". 😆
laurensvalk commentedon Oct 27, 2024
Very nice! Just to make sure, is this data from angular_velocity() or angular_velocity(calibrated=False)?
DrTom commentedon Oct 27, 2024
@laurensvalk
angular_velocity(calibrated=False)I will publish the code and all. I need some time to collect and clean all up. It is distributed over 3 projects now.
By the way: I can only do that on top of your development branch
imu:calibrated=Falsein pybricks, it is very valuable. 🙏Update: the crashes are related to the temperature readout :-(
DrTom commentedon Oct 27, 2024
@dlech
Keep that one. You can maybe auction it: "prime hub with very stable IMU" 😂
😂 Actually, I didn't need to do that thanks to your and @laurensvalk input. Increasing brightness of the LED lights and DC of two stalled motors gets easily to 65 deg Celsius (even higher, I stopped it at 70). It just takes some time: a test runs for about 45 minutes.
Delta temperature vs delta time is the big unknown here. The diagrams do look useable because I made it quite a slow process. I am quite certain that in reality the rate is much higher. We will see.
At least all of this can be used to pick some axis of some hub which works reasonably well (if this option is available).
DrTom commentedon Oct 27, 2024
The code for the test, data and results are here: https://github.com/DrTom/spikeprime-pybricks-angular-drift-analysis
laurensvalk commentedon Oct 28, 2024
Thanks again for working on this. Super helpful!
There is potentially a way we could incorporate this without making it too difficult to the user.
Instead of storing a single 3-axis bias persistently, we could store 2 sets of 3-axis bias + temperature and assume linearity between them.
The set for the upper end may not be available at first but could be set in the background when the hub is stable after reaching a higher temperature.
Thanks for updating this - I was curious about that. If you do see issues with the new 3D integration (without temperature additions), feel free to share.
Getting the I2C operations right will be a bit of a chore but might work if we properly dive into it. Your data has demonstrated that this is potentially worth it.
It's been a while since we looked into the FIFO buffering, so I don't quite remember why we ultimately choose not to use it. But maybe we can use that to ensure consistently timed values and occasionally read a temperature sample in between.
laurensvalk commentedon Oct 28, 2024
If we do use the FIFO, this does seem to allow inclusion of temperature:
So maybe it is worth double checking if round-reading also supports it.
Otherwise, if we do use FIFO mode, maybe we can set this up to produce a single timestamped frame, since we don't necessarily need a FIFO buffer as such, unless this would allow us to pick a higher data rate. It looks like we would want to use continous mode with a watermark index to tell us to drain it in time.
All this does look like it would give a significantly higher I2C handling overhead.