Repository navigation
Bringup: a tool for an IMU's bias and noise at rest - #178
Merged
Merged
Conversation
imu_noise_at_rest.py reads the sensor_msgs/Imu messages of a rosbag recorded with the robot at rest. For each axis of the angular velocity and the acceleration, it prints the mean, the sample variance, the standard deviation and the number of distinct values. At rest, the angular velocity's mean estimates the bias that remains in the published readings, and the acceleration's mean is gravity plus the accelerometer's remaining error. It also prints the rate and the gaps between the stamps, and the length of the mean acceleration, gravity as the IMU measures it. The bench checks on Remo (#169) measured the IMU's bias and noise with an ad-hoc script. This tool makes the measurement reproducible. The variances at rest are a baseline for the EKF's IMU variances, to be checked while driving too. A unit test checks the statistics on readings with known results, without a bag. On the two recordings at rest from the bench checks, with the Teensy 3.2 and the Teensy 4.0, the tool prints the same numbers as the ad-hoc script.
This was referenced Nov 11, 2026
format_report() builds the report, and main() prints it. A test reads the whole report for a small set of statistics: the stamps' lines, a table for the angular velocity, one for the acceleration, and gravity. The printed text is the same.
IMU_PERIOD names the firmware's 20 ms, and LONG_GAP is one and a half periods of it. Its value is 0.030 s as before.
The variance test expects a variance of zero, and the gap test counts the gaps longer than LONG_GAP, whatever its value is.
The tool takes a field's readings only from the messages that measured the field. sensor_msgs/Imu marks a field without a measurement with -1 as the first element of the field's covariance. A field with fewer than two readings gets the reason instead of a bias and a noise of zero, and each table shows its field's reading count. The header stamps are integers in nanoseconds, so the intervals between them and their comparison with 30 ms are exact. The report calls them header intervals: they show how regularly the samples were taken, not when the messages arrived. Repeated and earlier stamps are counted, and a recording with one of them has no rate. A duration below one second is printed in milliseconds. The tool ends with one line and status 1 for a file that can't be opened or isn't a rosbag, for messages of another type, for more than one frame_id and for a reading that isn't finite. The count of distinct values is described as the output levels that the recording took, not as the sensor's resolution. Each test has a docstring, and new tests run the tool as a command on small bags that they write.
Bench check 3 in lib/imu/README.md and the comment on the variances in diffbot_base_config.h name imu_noise_at_rest.py and say what its two results are. The variances at rest are a baseline for the settings. The mean angular velocity is the remaining bias in rad/s, which is no variance. Before a setting changes, a recording while driving has to tell the vibration apart from the robot's real motion.
The tool reads the header stamps in the order in which the bag stored the messages. rosbag's reader sorts by the recorder's time, so a clock that was set back for the recorder and the IMU alike looked like stamps that advance, with a steady rate. The topic's message types are checked before a message is read, and each message is converted when it is read. A camera topic chosen by mistake no longer loads its images. Messages with an empty frame_id are refused: nothing says that their readings share axes. A file whose first line isn't text, such as a bag packed with gzip, gives one line instead of a traceback. When no field has two readings, the message gives each field's count. The text about long header intervals names possible reasons instead of one cause, and --help says that the 30 ms belong to the firmware's 50 Hz.
The command for the noise tool in lib/imu/README.md ended in a placeholder in angle brackets, which a shell reads as a redirection. It shows an example of the name that the recorder prints.
A bag whose index is intact opens, also when the data of its messages is damaged. Reading then failed with a traceback. The error of rosbag or of the chunk's decompression is now the reason of the one line that the tool prints, with the bag and the topic. For a topic with messages of another type, the line names all the topic's types, because rosbag info shows only one type for a topic with several. The text about long header intervals gives a regular 10 Hz topic as the example of a source whose intervals are all long.
rosbag's parser and a message's deserialization fail on damaged data with their own errors and with others, such as a KeyError. Each of them is now the reason of the one line that the tool prints. The file's first bytes are checked before rosbag opens the file, and a recording that was cut off gets a hint to rosbag reindex. A reading beyond 1e6 rad/s or m/s^2 is refused: no IMU measures it, and its square left a float's range. The sample variance has its own function, because Python 3.8's statistics.variance() stops on deviations whose squares are zero as floats. With every byte of an uncompressed, a BZ2 and an LZ4 bag of three messages inverted in turn, the tool prints a report or one line for each of the 28,489 files.
The sample variance comes from statistics.variance() with the readings as fractions. A mean that is rounded to a float can double the variance of two neighbouring floats, and Python 3.8 stops on deviations whose squares are zero as floats. Fractions hold every float exactly. Before a message is read, the tool compares the checksum of the topic's message definition with that of ROS's sensor_msgs/Imu. Messages that carry the type's name with another definition can lack the fields that the tool reads. An error of rosbag's reader while the tool asks for the topic's types is the reason of the tool's one line. The line about a reading beyond the tool's limit has the field's unit.
A bag stores the definition of each message type, and rosbag builds the messages' class from it. The checksum of a definition doesn't tell whether the class has the fields that the tool reads: an array of one vector has the checksum of the vector. Before a message is read, the tool builds the class from the bag's definition and compares its fields, and the fields of its fields, with ROS's sensor_msgs/Imu. A definition that differs in its comments passes. The limit for a reading's size is described as the tool's limit for a plausible reading of a robot at rest.
A bag stores a checksum and a definition text for a topic's message type. rosbag reads the messages as the text says. When the text isn't the definition with which the messages were written, one field's bytes are read as another field. The tool builds the class from the text and refuses a topic whose stored checksum isn't the class's. The reason for a field of a field names the field's path without a stray period.
A checksum or a frame_id that a bag stores can hold a line break. In the tool's one line about an input that it refuses, and in the first line of the report, a line break is shown as a backslash and an n.
The report's first line shows a line break in the topic's name as a backslash and an n, as it does for the frame_id.
The docstrings of the checksum check and of its test said that the definition's text is the wrong one. The stored checksum and the stored text disagree, and the bag doesn't say which of them belongs to the messages.
The docstring says that the tool refuses the topic before it decodes a message, instead of what rosbag would read. The test's docstring says that rosbag would try to read the bytes.
The README said that the bench checks were still open and still needed, and its table called the covariances not measured yet. The checks ran on Remo with a Teensy 3.2 and with Remo's own Teensy 4.0 (diffbot#169). The first section now says so and links the results, and the paragraph on the unit tests says what the bench checks test. The table's covariance cell points to the comment that explains the starting values, and says that they are larger than the noise at rest measured on Remo. The first sentence names what is published instead of "it".
…ial_ci job for Noetic
… edges The variance takes the values as fractions because Python 3.8 stops with "negative sum of square deviations" on deviations whose squares are zero as floats. The docstring no longer gives the neighbouring floats as the reason: their variance is exact with floats too. A new test checks one repeated stamp among stamps that advance: no rate and one repeat. The tests' helper returns what the tool did as a ToolRun, and assert_tool_printed_a_report() checks it. The report reads 999.96 ms as "1.0 s", and gives the longest interval with ns when one decimal would read as the 30 ms limit while the count calls it longer. The IMU library's bench check links the IMU page's section instead of explaining the results itself.
format_report() gives the longest interval of 30.04 ms as "30.040000 ms" next to "1 longer than 30 ms". With format_duration() in format_stamps() instead of format_longest_interval(), this test fails.
The report test for the longest interval just above LONG_INTERVAL writes its 40 µs as 40 * MILLISECOND // 1000, as the tests of format_longest_interval() do.
fjp
added a commit
to ros-mobile-robots/ros-mobile-robots.github.io
that referenced
this pull request
Nov 26, 2026
## What and why **Before:** The BNO055 bench checks suggested measuring the stationary gyroscope's bias and variance but gave only the recording command. **With this PR:** [Bias and noise at rest](https://ros-mobile-robots.com/packages/imu/driver-bno055/#bias-and-noise-at-rest) shows how to make a recording, read the report and compare Remo's measured yaw-rate variance with the firmware setting that reaches the EKF. The [tests walkthrough](https://ros-mobile-robots.com/packages/imu/noise-at-rest-tests/) traces accepted and refused inputs through the command and explains the checks against known readings. **Limits:** - The report that the page shows is the tool's output for a bag of three messages, which the tool's test `test_tool_prints_the_report_of_three_readings_one_output_step_apart` writes and compares line by line. The two Remo recordings weren't run through the tool again. Remo's numbers are those of the tool's first version. - The trace figure shows a made-up gyroscope whose bias is about 30 times Remo's. Remo's bias of +0.00005 rad/s would lie on the zero line at the figure's scale. - The commands of the tab "Robot container" and the `sftp` copy weren't run on a robot. The page says so. The recording command and the tool ran in the dev container's image against a 50 Hz publisher. - The tests page has seven figures for 82 tests, not one per test. The checks of the input, the report's text and the refused inputs have no figure of their own. The flow figure in "The test classes" shows where on the tool's path their tests check. - Some text in code font stays without a link: the report's labels and lines, the tool's reasons, commands and their options, example names, folders on the reader's computers, values and symbols inside display formulas. **Merge order:** ros-mobile-robots/diffbot#178 is merged as `22d4d28`, and the pin names that commit. This pull request and #89 can be squash-merged in either order. The second one points the measured-noise link on From Measurement to Message to `#bias-and-noise-at-rest`. ## How it was checked At `3064a25c`, the head, with the excerpts from GitHub at `22d4d28`, the merge commit of ros-mobile-robots/diffbot#178: - **Build and spelling:** `mkdocs build --strict`, `codespell` and the repository's 161 tests pass in Python 3.12. flake8 with the repository's settings and mypy (`--disallow-untyped-defs`) report nothing for the figure script and its test. A test checks that the trace legend's mean label stands next to the solid mean line, and it fails when the label gets the output step's mark. - **Figure tests:** six tests of the figure script were run on broken copies of what they check: drawings outside their wrapper or in a second figure, the stamp figures without their marks, marks with wrong numbers or without numbers, the mean line dashed or at the mean plus s, a signed value with a hyphen, one decimal more, no plus sign or the decimals ignored, both spread lines above the mean, and an extra test count on the page. Each test fails on its broken copies and passes on the head. - **Excerpts:** each of the ten excerpts starts and ends at a whole function, test or block at `22d4d28`, and its code block's first line number is the excerpt's first line. - **Line links:** a script fetched the pinned file of every GitHub line link and excerpt on the changed lines. No range leaves its file, and every link text that names lines names the linked ones. - **The figures:** the script writes the eight includes byte for byte as they are in the repository, and each starts with the comment that names the script. - **Numbers:** Remo's numbers against the reports of the two recordings. The three-message report against the tool's test at the pin, which passes there. The worked example by hand: Δ = 1/900 rad/s gives 0.00111 rad/s and 1.235 · 10⁻⁶ (rad/s)². Gravity: the root of 0.0536 + 0.0053 + 95.8181 is 9.792. - **Rendering,** in Chrome from a local build, light and dark, 390 and 1440 px wide: the IMU page, the tests page and the Localization page have no horizontal page overflow and no formula error, and their Mermaid diagrams are drawn. The flow figure has no text outside its drawing, no overlapping text and no text that leaves its card. The trace, flow and two stamp figures show their wide layout at 1440 px and their narrow layout at 390 px, with no text outside a drawing, over other text or outside its card. The three figures with numbered stamps and steps, light and dark, 390 and 1440 px: every number lies inside its disc. The rendering was checked at `2fc20e4b`. All 12 SVG drawings at this head are byte for byte identical to those at `2fc20e4b`. The flow, measured-field and backward-stamp captions have different wording. - **Preview:** the deployed preview serves this head, and the description's preview links open their page and section. - No changed line joins sentences with a semicolon.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Before:
noetic-develhas no tool for an IMU's bias and noise. The bench checks on Remo (#169) measured both with a script that isn't in the repository. Bench check 3 indiffbot_firmware/lib/imu/README.mdand the comment on the variances indiffbot_base_config.htell the reader to replace the covariances' starting values with "the variance and the bias" of a stationary recording, without saying how to measure them.With this pull request:
diffbot_bringup/scripts/imu_noise_at_rest.pyreads a rosbag recorded with the robot at rest and prints a report:rosrun diffbot_bringup imu_noise_at_rest.py <bag>, with--topicfor anothersensor_msgs/Imutopic than/diffbot/imu/data_raw.frame_id, the time from the first header stamp to the last with the rate, the shortest and the longest interval between two header stamps with the number of intervals over 30 ms, and the number of stamps that repeat the stamp before or are earlier than it. The longest interval is printed with ns when one decimal would read as the 30 ms limit.catkin_add_nosetests,exec_dependon genpy, rosbag and sensor_msgs,test_dependon python3-nose, rospy and std_msgs.What the numbers are: at rest, the mean of the angular velocity estimates the bias that remains in the published readings. With the BNO055 in its IMU mode, the chip has corrected these readings for its offset already. The mean of the acceleration is gravity's share on each axis plus the accelerometer's remaining error. The variances are the noise at rest, a baseline for the diagonal of the message's covariances, which the EKF uses to weigh the readings. Driving adds vibration, so a setting changes only after a check while driving.
Why not an Allan-deviation tool: the bench check uses a three-minute recording to measure the variance of the 50 Hz IMU readings and flag repeated or backward header stamps and gaps over 30 ms, while Allan-deviation tools such as allan_variance_ros and imu_utils use hours of data to estimate noise density and drift.
How the tool reads a bag (
main()callsread_samples(),imu_statistics()andformat_report()):open_bag()checks that the file starts as a bag file does and opens the file with rosbag. A recording that was cut off has no index and gets a hint torosbag reindex.check_topic()asks for the message type of each of the topic's connections before a message is read: the type's name, its checksum and the text of its definition, which the bag stores. A topic with another type thansensor_msgs/Imu, such as a camera's, is refused without loading its messages. For a topic namedsensor_msgs/Imu, the tool builds the messages' class from the bag's definition, as rosbag does. The class's checksum has to be the stored one, and its fields have to be those of ROS'ssensor_msgs/Imu, down to the fields of its fields.recorded_samples()reads the messages, converts each to anImuSamplewhen it is read and sorts the samples by their position in the file. rosbag's reader returns the messages sorted by the recorder's time. A clock that is set back for the recorder and the IMU alike would hide in that order.to_sample()keeps the header stamp as an integer in nanoseconds, so the intervals and their comparison with 30 ms are exact. A field counts as measured unless the first element of its covariance is −1, whichsensor_msgs/Imudefines as "no measurement". A covariance of zeros means "not known" and keeps the reading.check_samples()runs six checks in the order ofSAMPLE_CHECKS: at least two messages, no emptyframe_id, oneframe_id, finite readings, readings within the tool's limit for a plausible reading (1,000,000 rad/s or m/s²), and two readings of at least one field.imu_statistics()calculates the stamps' statistics and each field's statistics from the messages that measured the field. The mean and the sample variance come from Python'sstatisticsmodule. The variance gets the readings as fractions, so that no rounding of the mean or of a squared deviation changes the result. A field with fewer than two readings gets two lines instead of its table, and the tool still ends with status 0.Limits:
de659f8). On the bags that were available, both versions print the same numbers (see the tests).diffbot_base_config.hgains four comment lines aboveIMU_PUBLISHER_CONFIG. A documentation page that links one of the lines below them by number needs the new number when its pin moves:angular_velocity_variancemoves from line 71 to line 75.How it was tested
The branch's head is
afe2e15. It builds on78616d5, the merge ofnoetic-develat8c83f53(#217 and #221) into4ef85e8. That merge had one conflict, indiffbot_firmware/lib/imu/README.md: the unit tests' line keepsnoetic-devel's command with both PlatformIO environments and this branch's sentence on the bench checks.0a26e49,d459abeandafe2e15then change the tool's docstring on the fractions and the report's two display edges, the tests' helpers and one test value, add five tests, and shorten the README's bench check 3 to a link.At
afe2e15:format_longest_interval()and one for the report's intervals line just above the 30 ms limit.<= 0changed to< 0inmean_rate(), the new test of a repeated stamp fails, besides the error of the test with three stamps of zero. Withformat_duration()instead offormat_longest_interval()in the report's intervals line, the new report test fails.AGENTS.mdasks, with the pinned tools ofscripts/requirements-lint.txtin apython:3.12-slimcontainer:flake8 .finds nothing, andmypy .reports no issues in 17 source files.At
4ef85e8: CI passed: industrial_ci (run 45198721727), the dev container (run 45198722120), both firmware builds and the firmware's unit tests (run 45198721497), and the Robot container's builds on arm64 and amd64 (run 45198721827).At
c9a39f9, in the dev container's image (ROS Noetic, Python 3.8.10):catkin buildsucceeds for all 13 packages.catkin run_testsfordiffbot_bringupanddiffbot_control, andcatkin_test_results build: 111 tests, 0 errors, 0 failures, 0 skipped. These are the tool's 77 tests, the IMU sample validator's 30 and the EKF's two stepped-clock tests, each counted once by rostest and once by its test node.--python-version 3.8 --disallow-untyped-defson the tool and its test in one call: no findings.CI at
c9a39f9: industrial_ci builds the workspace and runs its tests with the released and with the testing ROS packages (CI run 44611799352). The dev container (run 44611799376), both firmware builds and the firmware's unit tests, and the Robot container's builds and smoke tests pass.The items below ran before the merge. Tested commit:
6bf992f, except where an item names its parent13f77a7. The two differ in one line of the report's first line.9b3f336, the head before the merge, differs from6bf992fin the words of two docstrings: the tool's 77 tests, flake8 and mypy were rerun on it.catkin build diffbot_bringup: no warnings.catkin run_tests diffbot_bringupandcatkin_test_results build/diffbot_bringup: 77 tests, 0 errors, 0 failures, 0 skipped. 54 tests call the tool's functions with values whose results can be calculated by hand. 23 run the script as a command on small bags that they write.--python-version 3.8 --disallow-untyped-defs: no findings in the tool and its test.de659f8) against this head: both ran on a generated bag of 8,990 messages like a 180 s recording and on two IMU topics of six recorded bags of the EKF's stepped-clock test. 12 of the 13 reports have the same numbers in every line. On the generated bag, the first version prints the shortest interval of 14,049,979 ns as 14.1 ms, this head as 14.0 ms: the first version subtracted stamps as floating-point seconds.frame_ids are empty.std_msgs/Stringtopic, messages namedsensor_msgs/Imuwith other fields than ROS's or with a checksum that doesn't fit their definition, a missing file, a text file, a gzip-packed bag, a recording without its index, a bag with a damaged chunk and a message that lacks its last byte: the first version printed NaN statistics or a traceback. This head prints one line and ends with status 1.13f77a7: each of the 28,489 files gives a report or one line, never a traceback.frame_idand in the topic's name: the line break is shown as\n, and the line stays one line.13f77a7:rosbag record -o imu_still --duration=3 /diffbot/imu/data_rawtwice against a 50 Hz publisher writes two files with the date and time in their names, and the tool reads the first: 137 messages at 50.00 Hz. A third run with the capital-Oand no publisher replaces a recording of 61 kB with an empty bag, which the tool refuses with "0 messages"..docker/firmware/run.sh pio run -e teensy31,pio run -e teensy40andpio test -e native(93 tests) pass with the changed comment indiffbot_base_config.h, at3ae3b50. After that, the firmware's files changed only inlib/imu/README.md.Documentation
ros-mobile-robots/ros-mobile-robots.github.io#83 adds the section "Bias and noise at rest" to the IMU page and the page "Tests: IMU Bias and Noise at Rest", which walks through the tool's tests. The IMU library's README links that section for the tool's results. The link works once #83 is merged, which follows this pull request.
Part of #160.