Repository navigation
Description
Is your feature request related to a problem? Please describe.
Gyro calibration for a typical navigation task has (at least) these two aspects:
- Calibrating a few seconds while stationary to find the gyro offsets.
- Adjusting for what is actually 360 degrees when you make one full turn. Now a hub might consistently report
357or361, which varies by hub. In other words, the gyro is quite precise, but not accurate (off by about +/- 1%)?
Pybricks already implements 1 but not 2. Now that we have enabled persistent storage for settings, it is reasonable to implement 2 also.
Describe the solution you'd like
The goal is to obtain and store a correction factor so that one turn will result in 360 degrees. We could have a routine that interactively guides the user through these steps to find and save it.
This could be in a fancy window with animations and all, but we can start much simpler. For example, we could have the user run a script/block that will produce the following as you follow the instructions in the output window:
> Put the hub on a flat table. Align against a fixed reference like a wall or heavy book. Press hub button when ready.
Processing...
> Keep the hub flat and slide it to make a full turn clockwise. Put it against the same reference. Press hub button when ready.
Processing...
> So far so good! Now make 5 full clockwise turns.
Completed 1 out of 5 turns. Keep going!
Completed 2 out of 5 turns. Keep going!
Completed 3 out of 5 turns. Keep going!
Completed 4 out of 5 turns. Keep going!
Completed 5 out of 5 turns. Put it against the same reference. Press hub button when ready.
Processing...
> For every 360-degree turn, your sensor reported 363.8 degrees.
> The correction factor of `-3.8` degrees will be saved on your hub, in order to automatically improve the values you'll get from `heading()`.
After doing this hub.imu.heading(use_stored_correction=True) will give you the adjusted value. Or maybe it should be the default.
The value would persist until you update to a new firmware.
The value would only be useful for the hub orientation that was used to do the calibration.
Activity
[-][Feature] 1D heading correction routine[/-][+][Feature] Builtin 1D heading self-correction routine[/+]laurensvalk commentedon Jun 13, 2024
Would this help your students, @afarago , @DrTom, @ggramlich, @harikun77, @thehomelessguy, @MonongahelaCryptidCooperative, @TheWendyPower? Or would it be more confusing than helpful, even if it is optional?
How did you previously correct for the gyro being precise, but not accurate (off by about +/- 1%)?
Do your students notice it at all? Have they noticed that this varies from hub to hub?
As always, thank you for any feedback!
DrTom commentedon Jun 15, 2024
Yes, it has been noticed. It can be a problem but most times it is not an major aspect. At some point during a run some drift accumulates and I believe that causes quickly a larger problem than the systematic error.
Currently one team compensates for this problem; the others I am quite sure didn't even notice.
That said. It would be nice if a correction factor could be supplied when creating a hub along with the parameters for the axis. I do not think a build in, interactive process is necessary. The drivebase has to be manually calibrated with respect to wheel size and track width. It seems to me this is similar to that. Some good documentation would be helpful though.
afarago commentedon Jun 17, 2024
One of my teams noticed yet due to other uncertainties we agreed to limit the "uncontrolled" turns to max ~360 degrees. This way the offset was eliminated. Afterwards a wall align or clean start is required.
I completely agree with @DrTom any built-in procedure to the firmware seems unnecessary. i think it is good to have a best practice (even a code) to measure though.
Both in EV3 and SPIKE/RI it seemed that CCW and CW gyro shift was not uniform. Never had the time to really get to the bottom.
laurensvalk commentedon Jun 17, 2024
Thanks for the feedback! The potential issue with putting the correction factor in the hub initialization, is that your code becomes suitable only for that one hub. I was thinking that storing it on the hub might solve this.
This is definitely a good approach.
ggramlich commentedon Jun 19, 2024
Finally I read the full proposal and I am not sure, if I get all the implications.
We currently do not explicitly read the heading, but I assume that we implicitely use it with useGyro() on the drivebase.
Our team members all have their own hub and we noticed that fine tuning which was done by one team member did not work well on the other hubs. One cause probably was the inaccuracy of the gyro. Other causes were things like slightly different absolute positions of the motors for the arms.
This lead to some frustration first and later lead to a working mode where they only did the fine tuning on the robot used for the competition (which was also frustrating as the kids with the other hubs could barely experiment on their own and only during the meetings as the kids live more than 50km apart).
If it where possible to store a correction factor on the hub (with a reasonable default) that would be used with the drivebase useGyro() that would be awesome. Putting the correction factor as a value in the setup would be quite cumbersome for our use case, as the kids would always need to change it, when trying out the programs of their team mates.
The process you proposed would be nice, but also a simpler way like giving a sample program to measure the deviation and then a function call (python only, no block coding needed for that) to store the correction factor in persistent storage would be fine.
It would also be interesting to see a sample program to measure the deviation without having automatic correction, so that we can find out, how much the gyros ond the hubs differ currently.
laurensvalk commentedon Jun 19, 2024
Based on the rest of your reply, I think you understood it exactly right. Thanks for the input!
This is indeed what I was concerned about, and why it would be nice to have the value stored.
I see @DrTom's case of supplying a value directly also being useful.
I think we can essentially have both. Since the "builtin routine" is likely going to be a Python script, that script will have to make a call to eventually save the result. If we expose that call in the API, anyone who wants to store their own value can do so, or even make their own automatic routine.
Here is the script that we might include. I'd actually be curious for your findings! I have a few hubs that are reasonably spot on, but another that is quite far off. For that one I get:
It doesn't store the result yet. It just shows it to you.
What do you get? You can run it on any Prime / Technic / Essential / Inventor Hub. Flat on the table is the easiest. But it can be in the robot if you make sure to keep all wheels on the table as you spin it around.
laurensvalk commentedon Jun 19, 2024
The following proposal might have the best of both worlds. Green are new additions.
This is the more technical, to-the-point method where you can set/save and get the value directly. With an extra setting in the existing
.settingsmethod.Additionally, here is the simpler routine that most users can use. Possibly with a dedicated block.
afarago commentedon Jun 20, 2024
I have tested it an CW and CCW gives consistently different results even after checking with multiple hubs.
I compared CW and CCW for a single hub.
Not a huge amount, still something that worth checking here imo.
CW
For every 360-degree turn your gyro reports: 363.4356
Correction factor: 0.9905468
For every 360-degree turn your gyro reports: 363.1423
Correction factor: 0.9913468
CCW
For every 360-degree turn your gyro reports: -362.7817
Correction factor: -0.9923323
For every 360-degree turn your gyro reports: -362.7711
Correction factor: -0.9923612
laurensvalk commentedon Jun 20, 2024
Thank you for adding some data to this!
Agreed it should be looked into as well. Still, cutting down an error of 3.6 degrees (1%) to about 0.36 degrees (0.1%) with the proposed method is already a pretty big step. 🙂
2 remaining items
laurensvalk commentedon Jun 21, 2024
Excellent, nice to have some extra data! Because if we're finding a trend here, we could not only add the calibration routine but also guesstimate a baseline to improve the default for everyone.
For bonus points, would anyone want to retry this with their team's ready-built robot? If your hub is flat, the result should be approximately the same. But it is a bit trickier to complete the routine successfully, so I wonder if this is too hard or acceptable.
afarago commentedon Jun 23, 2024
This is the data for our FLL competition robot this year.
Interesting side fact that due to the 62.4mm wheel the robot is not perfectly level (our design flaw), yet the gyro seems to perform very well.
RI
CW
For every 360-degree turn your gyro reports: 361.6986
Correction factor: 0.9953037
For every 360-degree turn your gyro reports: 361.8342
Correction factor: 0.9949307
CCW
For every 360-degree turn your gyro reports: -361.6644
Correction factor: -0.9953979
For every 360-degree turn your gyro reports: -361.7399
Correction factor: -0.9951902
laurensvalk commentedon Jun 25, 2024
Although one way to correct for this is by adjusting the top axis, running the calibration routine will also correct for this 🙂
laurensvalk commentedon Jun 25, 2024
You can try it out now! Using this firmware file for Prime Hub or Technic Hub or Essential Hub
For @DrTom, there is a setting that lets you do it right below the hub initialization.
For @ggramlich, there is an interactive routine to determine the value.
In both cases, the value is saved on the hub in the same way.
After that, and even after reboot, the following program should result in near-perfect 360-degree values if you make a full turn:
Eventually, there might be blocks for this too.
If your hub has the default orientation (flat), then you can also simply click the
>>>button and pastehub.imu.update_heading_correction()into the terminal and press Enter. Instead of running a script like the above.ggramlich commentedon Sep 14, 2024
@laurensvalk
Sorry for not being responsive for the last months. We are now starting with preparation for the WRO Finals in November and plan to use the firmware you mentioned in your #1678 (comment) along with the Toggle Bluetooth feature and the heading correction.
Do you plan to release a new official firmware version with these features?
laurensvalk commentedon Sep 14, 2024
Yes. I think it would be good to release a beta version in the coming week. It will have all of these features enabled.
Then people don't have to search issues like these. With outdated firmware builds.
laurensvalk commentedon Oct 17, 2024
This has the potential of becoming a UI controlled feature just like Port View.
For the upcoming release, I think we'll want to add a block for this so users can do it as part of their program. See #1887
laurensvalk commentedon Oct 29, 2024
@DrTom, @afarago, @ggramlich : An alternative is suggested in #1907, where it can be done by simply rolling the hub on a table several times.
The improvement on X/Y is even more than on Z (at least for my hubs), where the correction is sometimes over 3 degrees per rotation. This is important if your hub is mounted sideways.
zoligyenge commentedon Nov 6, 2024
Hi. The build is no longer accessible, Which build can we now use for the 1D heading calibration? I think the averageing over 5 turns makes the calibration even better than the 3D calibration (which relies on a single rotation for each axis)
laurensvalk commentedon Nov 7, 2024
You can install it from https://beta.pybricks.com, no special build required.
Please do note that it is a beta feature, so it may still change before the final release.
Thanks for the input. We'll certainly further investigate the accuracy of the 3D method. We might ask the user to do multiple rotations for more accuracy.
We could also have both routines available, but I'd rather not make things too complicated for the average user.
zoligyenge commentedon Nov 7, 2024
Thanks. I wasn't aware that the beta also picks a different Firmware.
laurensvalk commentedon Dec 3, 2024
We are going to close this in favor of the more complete 3D routine given in #1907. This means that the
hub.imu.update_heading_correctionwill not be included since it is no longer necessary. The 3D routine will do the same, but along 3 axes. Since this feature was not included in a stable release, this is not a breaking change.The option to set
hub.imu.settings(heading_correction=354)manually will remain available however. This will be useful for power users who build their own calibration routine. For example, spin the robot 5 times in place and square against a line, to find the true gyro value (without taking the hub out of your robot).Please stay tuned for the documentation updates so this will all make sense. 🤖
Thanks everyone for your feedback! It has been very helpful. We have tried to cover all of the use case suggested here.
I'm going to close these intermediate stage discussions since much of this information no longer applies. Please feel free to open a dedicated issue or discussion for feedback on latest versions if you like.