Skip to content

Firmware tests: GoogleTest on the PC, with the wire bus suite - #217

Merged
fjp merged 18 commits into
noetic-develfrom
feat/firmware-googletest
Nov 23, 2026
Merged

fjp merged 18 commits into
noetic-develfrom
feat/firmware-googletest

Conversation

@fjp

@fjp fjp commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

What and why

The firmware's unit tests on the PC get a second test framework, GoogleTest, next to Unity. GoogleTest gives a suite a fixture for a shared setup, parameterized tests that run once per value, and failure messages that name the case. New tests of the firmware's core are GoogleTest suites, and the wire bus suite is converted as the pattern.

Before this change: one PlatformIO environment, native, runs eight Unity suites on the PC. The wire bus suite's fake Wire library keeps only the last transfer's bytes, so the suite doesn't see the device address or the order of the calls. Its test of four short reads loops over them and stops at the first failing case. The include check tools/check_includes.sh knows native and the two boards.

With this pull request:

  1. Two environments for the PC: platformio.ini keeps native for the Unity suites in test/test_<name>/ and adds native_googletest for the GoogleTest suites in test/googletest/test_<name>/, with GoogleTest pinned to 1.17.0. test_ignore and test_filter keep each environment to its own folders, also with --filter.
  2. One command: .docker/firmware/run.sh pio test -e native -e native_googletest runs both. The Firmware workflow's job unit tests (native) runs it, has PlatformIO write a JSON report, and fails when no test case of one of the two environments passed, which pio test alone lets pass.
  3. The wire bus suite is a GoogleTest suite, test/googletest/test_wire_bus. Its Unity copy is removed. The fake FakeWire records each call of beginTransmission(), write(), endTransmission() and requestFrom() with its arguments, and the fixture WireBusTest gives each test a fresh fake and bus. Twelve test cases check the calls of a write and a read in order, a second address, register and value, a whole sample's 18 data bytes, a failed register write, and the four short reads as named cases of a parameterized test. Each test checks one behavior.
  4. The include check also takes native_googletest. For both PC environments, it gives clang-tidy the definitions and include folders that pio test compiles a suite with: PLATFORMIO with PlatformIO's version, PIO_UNIT_TESTING, UNIT_TEST, the project's folders and the test libraries' public folders. It adds the test folders in the order of PlatformIO 6.2.0's ConfigureTestTarget(): a suite's own folder, the grouping folders above it up to the next enclosing suite's with the outermost first, then test. A shared file in no suite, such as a fake in test/fakes, gets its grouping folders the same way. A file outside test gets its own folder and test. A path is taken relative to diffbot_firmware first, without resolving symbolic links, so an absolute path gets the same folders, and a suite behind a link keeps the name under which PlatformIO finds it. A missing .pio/libdeps/<environment> names the command that downloads it.
  5. Samples for the include check in tools/include_check_samples, which the Firmware workflow checks. One needs GoogleMock and PlatformIO's definitions. Six need the test folders in PlatformIO's order, in a folder test that mirrors diffbot_firmware/test. Two include Unity and rosserial: they have to pass with native and fail with native_googletest. One sample is also checked by its absolute path.
  6. Texts: AGENTS.md, CONTRIBUTING.md, the firmware's README, the IMU library's README and the workflows' README name the command with both environments. AGENTS.md gives GoogleTest's form of a test name, TEST_F(WireBusTest, WriteRegisterFailsWhenTheAddressIsNotAcknowledged), and the include check's environments, flags and samples.

Limits:

  • One suite is converted: the other seven stay Unity suites until their code changes. The three suites of the rosserial code stay Unity suites.
  • gMock isn't used: FakeWire's list of calls does a mock's work for this suite. PlatformIO's GoogleTest library includes gMock for a later suite.
  • --ignore on the command line: a call's --ignore replaces an environment's test_ignore, so pio test -e native --ignore x --filter "googletest/*" builds the GoogleTest suite with Unity and fails.
  • A filter that matches nothing: the call passes locally with 0 test cases. In CI, the job's check catches an environment with no passed test case, not one suite that drops out while others pass.
  • New Unity suites: each has to be added to native's test_filter, as before.
  • PlatformIO's message for a failed suite: Program received signal SIGHUP (Hangup) with the status ERRORED, because PlatformIO 6.2.0 reads the test program's exit status 1 as a signal's number.
  • The GoogleTest pin: PlatformIO's runner asks for ^1.17.0, which the pinned 1.17.0 satisfies. The comment at the pin says to check the range when PlatformIO's version changes.
  • The include check for native: its flags leave out the Unity configuration that pio test generates, unity_config.h.
  • The include check's folder rule follows PlatformIO 6.2.0: when PlatformIO's version changes, compare it with ConfigureTestTarget() again. The samples check the script's rule, not PlatformIO's.

How it was tested

At the head 07b2936, in the firmware container's image (PlatformIO 6.2.0, g++ 12.2.0), with ShellCheck and actionlint in images of their own:

  • Tests: pio test -e native -e native_googletest passes with 101 test cases, 89 Unity and 12 GoogleTest. The tests of the include check's flag helper pass.
  • Include check: no warning with native on the BNO055 suite and header, the IMU publisher's suite and a fake, and with native_googletest on the wire bus suite and wire_bus.h. With teensy31, src/main.cpp and the base controller's header, which this pull request doesn't change, print the eight warnings of their older lines that CI: check the code style of C++, Python and the catkin packages #117 tracks.
  • Samples: the seven samples for native_googletest pass, the Unity and rosserial samples pass with native and end with the status 1 with native_googletest, the workflow's step, run with bash -e, passes, with the sample by its absolute path in the container, which the script of bfb0980 fails. A sample named with ./ gets the same result. Each earlier version of the folder rule fails on a sample: the shared folders before the suite's, the folders above a suite from the inside out, no folder test for a shared file, an enclosing suite's folder for a nested one, a folder named test inside a suite taken as the test folder, and a shared file's grouping folders from the inside out. A misspelled header in the rosserial sample fails the step at its check with native.
  • A failure on purpose: at b8108fb, whose suite and library are the same as at the head, with the edit of readRegisters() that the docs page shows, only the case BothShort fails, at test_wire_bus.cpp:254, and PlatformIO counts 13 test cases: 1 failed, 11 succeeded.
  • The returned bytes: a readRegisters() that leaves out the last byte, and one that returns a wrong second byte, each fail ReadRegistersReturnsAllBytesOfASample and no other test.
  • A suite behind a symbolic link: a scratch suite test/googletest/test_linked, a link to a folder outside test, with its source in cases/ and a header in the suite's folder. pio test finds the suite under the link's name and passes. The include check passes on its source by the relative and the absolute path, and the script of cf5a85e ends with the status 1, 'group_values.h' file not found.
  • Linters: ShellCheck 0.10.0 finds nothing in check_includes.sh, and actionlint 1.7.12 nothing in firmware.yml.
  • CI on GitHub's runners: Firmware run 45214605149 passes both board builds and the job unit tests (native), which prints native: 89 test cases passed and native_googletest: 12 test cases passed and passes the include checks and the sample step without a warning. industrial_ci 45214605121, the dev container 45214605142 and the description check pass too.

Earlier heads:

  • Mutations, at 98ba6c6: 23 wrong versions of lib/imu/wire_bus.h, each run against both suites. The GoogleTest suite fails for all 23, each of its tests for at least one. Since b8108fb, ReadRegistersUsesARepeatedStart checks the calls only, and the two mutations of the returned bytes fail ReadRegistersReturnsAllBytesOfASample alone, as the line above shows at the head. The base branch's Unity suite fails for 14. The nine others are outside what it checked, such as a wrong device address in one call or a request before the register write ends. For the short reads, the GoogleTest suite names the failing case, the Unity suite the line of its loop.
  • The workflow's check, at 98ba6c6: with the GoogleTest folder renamed so that PlatformIO finds no suite, and with every GoogleTest test skipped, pio test passes and the step fails with native_googletest: 0 test cases passed.
  • main()'s return value: with return 0, a failure in a fixture's SetUpTestSuite() ends with pio test's exit status 0. With return RUN_ALL_TESTS(), with 1.
  • The guards, at 98ba6c6: pio test -e native --filter "googletest/*" and pio test -e native_googletest --filter test_pid run no suite. A GoogleTest suite that includes <ros.h> doesn't build in native_googletest.
  • A fresh checkout with an empty PlatformIO core folder, at d96bc8d: the command downloads the native platform, Unity, rosserial and GoogleTest 1.17.0, and the tests pass.
  • Both boards: pio run -e teensy31 and pio run -e teensy40 succeed at 81dfa3e, whose firmware is the same, and in CI at the head.

Not tested: a Teensy, and the commands with PlatformIO installed on the PC instead of the container, which CONTRIBUTING.md and AGENTS.md give.

Documentation

ros-mobile-robots/ros-mobile-robots.github.io#137 adds the page Firmware Unit Tests: one test to run first, which framework a new test uses, how platformio.ini keeps the suites apart, and how to write a GoogleTest suite with the check of its includes, from a first file with one test to this pull request's fixture and parameterized test. It updates the test command and the unit test job on Testing and CI, Firmware Container and the IMU page.

The page walks through each test of the wire bus suite with its code and one picture per test: a time line of the values that the test sets, the calls that FakeWire records, the result and the checks.

Closes #167

fjp added 2 commits November 13, 2026 11:21
A second PlatformIO environment, native_googletest, runs GoogleTest suites on
the PC next to the Unity environment native. GoogleTest gives the tests on the
PC fixtures, parameterized tests and failure messages that name the case.

- Unity suites stay in test/test_<name>/, GoogleTest suites lie in
  test/googletest/test_<name>/. Every environment ignores the GoogleTest
  suites, and only native_googletest runs them and ignores the Unity suites.
  So no environment builds a suite with the other framework.
- GoogleTest is pinned to 1.17.0, the version that PlatformIO 6.2.0's runner
  asks for.
- The wire bus suite is the pattern: a fixture with the fake Wire library, and
  the four short reads as a parameterized test whose cases have names. Its
  Unity copy is removed.
- The Firmware workflow's unit test job runs both environments. Its name and
  the names of the required build jobs stay.
- The READMEs, AGENTS.md and CONTRIBUTING.md name both environments.
The successful reads' length of three bytes is READ_LENGTH_BYTES, and the
short reads' length says its unit, SAMPLE_LENGTH_BYTES. The values are
unchanged.
fjp added 16 commits November 18, 2026 09:34
FakeWire records each call of beginTransmission(), write(), endTransmission() and requestFrom() as text, in order. The tests compare those calls with the expected ones, so they check the device address, that the register write ends with a repeated start before the read request, and that no read request follows a failed register write.

New tests: a write that fails after a successful write on the same bus, as the Unity suite checked, and a successful read of a whole 18-byte sample with all bytes compared. The short-read cases also check that the read request asked for the whole sample.
pio test passes when an environment runs no test case, for example when PlatformIO finds no suite folder for it. The unit test step writes PlatformIO's JSON report and fails when native or native_googletest ran no test case. The job's name and its pio test command stay the same.
Two tests write to and read from 0x29, the BNO055's address with its ADR pin high, and compare the address in each call. The check of the calls after a failed register write prints GoogleTest's two call lists without a message of its own.
The unit test step counts each environment's test cases with the status PASSED in PlatformIO's JSON report. It fails when one of the two environments has none, also when every test of an environment is skipped.
The two tests at the BNO055's address 0x29 also write the reset value 0x20 to the system trigger register 0x3F and read from the chip ID register 0x00. So they show that WireBusT passes on each of its arguments, not the values of the other tests. Their names say that: WriteRegisterPassesOnTheAddressRegisterAndValue and ReadRegistersPassesOnTheAddressAndRegister.
tools/check_includes.sh takes the environment native_googletest, with the
flags of the PC tests and GoogleTest's include folder instead of Unity's
and rosserial's. The Firmware workflow runs it on the GoogleTest suite,
and AGENTS.md names the environment in "Check the includes".
For native and native_googletest, the include check defines PLATFORMIO
with PlatformIO's version, PIO_UNIT_TESTING and UNIT_TEST, and adds the
project's folders, GoogleMock's and GoogleTest's include folders, the
test folders and the checked file's own folder, as pio test does.

Three samples in tools/include_check_samples check those flags in the
workflow Firmware: one needs GoogleMock and the definitions and has to
pass, two include Unity and rosserial and have to fail.

A missing .pio/libdeps/<environment> gets the command that downloads its
libraries. The comments in firmware.yml and platformio.ini name both PC
environments and the three Unity suites that use rosserial.
For a file of a test suite on the PC, the include check adds the suite's
folder and then each folder above it up to test, in the order in which
pio test searches them. A header in the suite's folder now comes before
one of the same name in test/googletest. Another file gets its own folder.

A new sample, test_include_order/cases/order_case.cpp, has the same
header name in the suite's folder and the folder above it, and passes only
in that order. The Unity and rosserial samples have to pass with native
before they have to fail with native_googletest.

AGENTS.md says that the flags for native leave out the Unity
configuration that pio test generates.
The include check adds the test folders as PlatformIO 6.2.0's
ConfigureTestTarget() does: for a file of a suite, the suite's folder and
the folders above it up to the next enclosing suite's, then test. Any
other file gets its own folder and test, so a shared source or fake finds
the headers below test, such as fakes/fake_bno055.h.

Two new samples check the rule in the workflow Firmware: shared_source.cpp
needs the folder test, and inner_case.cpp, in a suite inside another
suite's folder, must not see the enclosing suite's header.
…or per test

The include check gives the folders between a suite and test with the
outermost first, as PlatformIO's prepends leave them. A sample with a
header in two such folders, deep/test_two_levels/two_levels_case.cpp,
passes only in that order.

ReadRegistersUsesARepeatedStart checks the calls only, as its name says.
ReadRegistersReturnsAllBytesOfASample checks the bytes. Both read lengths
are size_t, and their comments name the reads.

The platformio.ini comment names the one way around the guard, a call's
--ignore. The firmware README, a comment of the include check and a
sample's indentation follow the rules for links, width and indentation.
… files

The include check takes the first folder named test in a file's path as
its test folder, and the innermost folder test_* below it as the suite. A
folder named test inside a suite no longer moves either. A shared file in
no suite gets the grouping folders down to its own, the outermost first,
then the test folder, as a suite below them gets them.

Two samples check this in the workflow Firmware: details_case.cpp, in a
folder named test inside a suite, and grouped_source.cpp, a shared source
in a grouping folder with a header of the same name one level up.
The include check makes a file's path relative to diffbot_firmware before
it looks for the test folder, so an absolute path or one that starts with
./ gets the same include folders. A comment says to compare the folder
rule with PlatformIO's ConfigureTestTarget() again when PlatformIO's
version changes, and the workflow's comment on the samples is rewrapped.
…absolute path

The include check makes a path relative without resolving symbolic links,
so a suite behind a link keeps the name under which PlatformIO finds it.
The workflow Firmware also checks order_case.cpp by its absolute path in
the container.
@fjp
fjp merged commit c3d93b2 into noetic-devel Nov 23, 2026
11 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in DiffBot & Remo roadmap Nov 23, 2026
@fjp
fjp deleted the feat/firmware-googletest branch November 23, 2026 20:46
fjp added a commit to ros-mobile-robots/ros-mobile-robots.github.io that referenced this pull request Nov 23, 2026
## What and why

The firmware's unit tests on the PC use two test frameworks with ros-mobile-robots/diffbot#217: Unity and GoogleTest, each with a PlatformIO environment of its own. A new page, Firmware Unit Tests, runs one test first, says which framework a new test uses, explains how PlatformIO keeps the suites apart, and walks through writing a GoogleTest suite with the check of its includes. The pages that give the test command name both environments.

**Before this change:** on `main`, Testing and CI has one paragraph about the firmware's unit tests, with the command `pio test -e native`. Firmware Container and the IMU page give the same command. No page says how the tests are built or how to write one.

**With this pull request:**

1. [Firmware Unit Tests](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/), a new page under Contributing, after Testing and CI:
    - [Run the tests](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/#run-the-tests): one test by name with its expected result, then every suite, one environment or one suite.
    - [What a failed test looks like](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/#what-a-failed-test-looks-like): `readRegisters()` of `wire_bus.h`, the edit that breaks it on purpose, the failed case and GoogleTest's message, and what PlatformIO's `SIGHUP` line means.
    - [Which framework for a new test](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/#which-framework-for-a-new-test): GoogleTest for portable code, Unity for the rosserial code and for tests on the Teensy, a figure of the two environments and their folders, and the settings of `platformio.ini`.
    - [Write a GoogleTest suite](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/#write-a-googletest-suite): a first suite of one test and the include check of its file, the wire bus suite's fixture with its tests of `writeRegister()` and `readRegisters()`, the short reads as a parameterized test, seeing each test fail once, and the checks before a pull request. Each test of the wire bus suite has a picture: a time line of its calls, its result and its checks, in three figures next to the tests of [`writeRegister()`](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/#tests-that-share-a-setup-a-fixture), of `readRegisters()` and of the [short reads](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-unit-tests/#one-behavior-with-several-inputs-a-parameterized-test).
2. [Testing and CI](https://ros-mobile-robots.com/pr-preview/pr-137/development/ci/#firmware-unit-tests): the Firmware row names both environments, the include check and its samples, and the section "Firmware unit tests" and the bullet under "Running the checks locally" name both environments and link the new page.
3. [Firmware Container](https://ros-mobile-robots.com/pr-preview/pr-137/development/firmware-container/#use-it) and the [IMU page](https://ros-mobile-robots.com/pr-preview/pr-137/packages/imu/driver-bno055/#how-the-firmware-reads-it): the test command has both environments.
4. [Contributing](https://ros-mobile-robots.com/pr-preview/pr-137/development/#best-practices): a link to Firmware Unit Tests next to the one to Testing and CI.
5. `mkdocs.yml`: the menu entry, and the pin `firmware_tests_src_raw` and `firmware_tests_src_url` for the new page's excerpts, code links and pictures, at `c3d93b2`, the merge of ros-mobile-robots/diffbot#217.
6. `scripts/figures/firmware_unit_tests_figures.py`: writes the three figures of the wire bus tests into `includes/`, as inline SVG in the site's light and dark colors. `tests/test_firmware_unit_tests_figures.py` checks that the script writes the committed includes, that each test and case of the suite has its picture, the call texts, the short reads and the drawing. `scripts/figures/board_drawings.py` gets `svg_element()` with its arrowhead marker and `Drawing.number_disc()`. All three generators, the wire bus one, `pid_figures.py` and `low_level_figures.py`, call `svg_element()`, and the wire bus and PID generators call `number_disc()`. The PID and low-level includes stay the same.

**Where the page gets its content:**

- The excerpts of `platformio.ini`, `test_wire_bus.cpp` and `wire_bus.h` are ros-mobile-robots/diffbot#217's merge `c3d93b2`, included at build time through the pin. Its tree is the same as the reviewed head `07b2936`. The excerpt of the Teensy 4's Wire library is `WireIMXRT.cpp` of PaulStoffregen/Wire at commit `eec0c44`, and the one of PlatformIO is `native.py` of platformio-core at the tag `v6.2.0`.
- The output of the failed run is from the page's edit at `b8108fb`, whose suite and library are the same at `07b2936`. The outputs of the one test and of every suite are from runs at `98ba6c6`, whose tests and libraries give the same names and counts at `07b2936`.
- The pictures hold the values of `test_wire_bus.cpp` at `c3d93b2`, and the calls as `FakeWire` records them.
- Three statements come from our own checks and say so: what a `--filter` for the wrong environment does, why `main()` returns the tests' result, and the 13 test cases that the failed run counts.

**Limits:**

- **The pictures' values:** the script holds the values and calls of the wire bus tests. A change of those tests needs the same change in the script. No check compares the two automatically.
- **PlatformIO on the PC:** the page says how the commands run without the container. That route wasn't tested with the GoogleTest environment.

## How it was checked

At `aa63e477`, with the excerpts from GitHub at ros-mobile-robots/diffbot#217's merge `c3d93b2`:

- **Build and spelling:** `mkdocs build --strict`, `codespell` and the repository's tests pass in a `python:3.12-slim` container.
- **Against the code:** each excerpt has the same lines at `c3d93b2` as at the reviewed head `07b2936`, and the pictures' 13 values are those of `test_wire_bus.cpp` at `c3d93b2`. The excerpt of `wire_bus.h` shows its lines with the class's indentation removed, as the site's snippets setting does. Each link with a line range to `test_wire_bus.cpp` and `wire_bus.h` lies inside its file, and the failure output's line `test_wire_bus.cpp:254` is the check of the short reads.
- **Against our own runs:** the page's edit of `readRegisters()` fails only `BothShort`, at line 254, with 13 test cases, 1 failed and 11 succeeded. The page's first suite is found without a change of `platformio.ini`, passes, and its include check prints no warning.
- **The pictures against a run:** in a run of the wire bus suite at `07b2936` with a temporary log in `FakeWire`, each test's calls, the results, the calls of `available()` and the number of `read()` calls are those of its picture, for all 12. The run's last call of `available()` in a short read is the test's own check.
- **The figure scripts:** flake8 with the repository's settings prints nothing for the six changed Python files, and mypy reports no issues. The three scripts write their committed includes again, byte for byte.
- **In headless Chrome, the preview at `3f46569a`:** the three figures at 390 and 1440 px, light and dark. No text leaves its picture or overlaps other text, and the page doesn't scroll sideways at 390 px.
- **Links:** each section links a destination once, except a source link before an excerpt. `grep ';'` finds nothing in the added lines.
- **In headless Chrome, the preview at `7b856112`, before the last two text fixes:** the new page and the four changed pages are no wider than a 390 px window, so no page scrolls sideways. The new page renders all 26 code blocks, the eight collapsed ones included, with no unresolved excerpt. Screenshots of the new page at 390 and 1440 px, light and dark.

Closes ros-mobile-robots/diffbot#167
fjp added a commit that referenced this pull request Nov 23, 2026
noetic-devel brings GoogleTest for the firmware (#217) and the IMU tests (#221). The IMU library's README keeps the new test command with both environments and this branch's sentence on the bench checks.
fjp added a commit that referenced this pull request Nov 24, 2026
## What and why

**Before:** `noetic-devel` has 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 in [`diffbot_firmware/lib/imu/README.md`](https://github.com/ros-mobile-robots/diffbot/blob/7d428c282c073ad4b99a76cab834276daadb5a6f/diffbot_firmware/lib/imu/README.md#L46) and the comment on the variances in [`diffbot_base_config.h`](https://github.com/ros-mobile-robots/diffbot/blob/7d428c282c073ad4b99a76cab834276daadb5a6f/diffbot_firmware/lib/config/diffbot_base_config.h#L55-L64) tell 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:**

1. `diffbot_bringup/scripts/imu_noise_at_rest.py` reads a rosbag recorded with the robot at rest and prints a report: `rosrun diffbot_bringup imu_noise_at_rest.py <bag>`, with `--topic` for another `sensor_msgs/Imu` topic than `/diffbot/imu/data_raw`.
2. The report has four lines about the messages: their number and `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.
3. For the angular velocity and for the acceleration, the report has a table with one row per axis: the mean, the sample variance, the standard deviation and the number of distinct values, and the number of readings under the field's name. The last line is the length of the mean acceleration, gravity as the IMU measures it.
4. For an input that the tool can't interpret, it prints one line with the file, the topic where it matters and the reason, and ends with status 1.
5. The firmware's configuration header says what the tool's two results are for. The IMU README names the tool and links the IMU page's section that explains them.
6. The IMU README says that its bench checks ran on Remo, with a Teensy 3.2 and with Remo's own Teensy 4.0, and links their results in #169. Its table says that the covariances' starting values are larger than the noise at rest measured there.
7. An install rule, a nose test file registered with `catkin_add_nosetests`, `exec_depend` on genpy, rosbag and sensor_msgs, `test_depend` on 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](https://github.com/ori-drs/allan_variance_ros) and [imu_utils](https://github.com/gaowenliang/imu_utils) use hours of data to estimate noise density and drift.

**How the tool reads a bag** (`main()` calls `read_samples()`, `imu_statistics()` and `format_report()`):

1. `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 to `rosbag reindex`.
2. `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 than `sensor_msgs/Imu`, such as a camera's, is refused without loading its messages. For a topic named `sensor_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's `sensor_msgs/Imu`, down to the fields of its fields.
3. `recorded_samples()` reads the messages, converts each to an `ImuSample` when 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.
4. `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, which `sensor_msgs/Imu` defines as "no measurement". A covariance of zeros means "not known" and keeps the reading.
5. `check_samples()` runs six checks in the order of `SAMPLE_CHECKS`: at least two messages, no empty `frame_id`, one `frame_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.
6. `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's `statistics` module. 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:**

- **The header stamps:** the firmware takes a message's stamp before it reads the IMU. The intervals show how regularly the recorded samples were stamped. They don't show when a message arrived, so the report can't show a late delivery. A recording with a repeated or an earlier stamp gets no rate.
- **The 30 ms:** one and a half periods of the firmware's 50 Hz. The limit doesn't adapt to a topic with another rate. The report then still has the shortest and the longest interval.
- **At rest only:** the variances describe one recording of a standing robot.
- **Damaged files:** a file with a changed byte in a reading's digits can still give a report with a wrong number. Uncompressed bags have no checksum.
- **The two recordings of #169** weren't available when this pull request was reworked, so the tool at this head has not run on them. Their numbers in the documentation are those of the tool's first version (`de659f8`). On the bags that were available, both versions print the same numbers (see the tests).
- `diffbot_base_config.h` gains four comment lines above `IMU_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_variance` moves from line 71 to line 75.

## How it was tested

The branch's head is `afe2e15`. It builds on `78616d5`, the merge of `noetic-devel` at `8c83f53` (#217 and #221) into `4ef85e8`. That merge had one conflict, in `diffbot_firmware/lib/imu/README.md`: the unit tests' line keeps `noetic-devel`'s command with both PlatformIO environments and this branch's sentence on the bench checks. `0a26e49`, `d459abe` and `afe2e15` then 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`:**
  - The tool's tests with nose in the dev container's image (Python 3.8.10): 82 tests, OK. These are the 77 tests before, one for a repeated stamp among stamps that advance, one for the unit at 999.96 ms, two for `format_longest_interval()` and one for the report's intervals line just above the 30 ms limit.
  - With `<= 0` changed to `< 0` in `mean_rate()`, the new test of a repeated stamp fails, besides the error of the test with three stamps of zero. With `format_duration()` instead of `format_longest_interval()` in the report's intervals line, the new report test fails.
  - Lint as #207's `AGENTS.md` asks, with the pinned tools of `scripts/requirements-lint.txt` in a `python:3.12-slim` container: `flake8 .` finds nothing, and `mypy .` reports no issues in 17 source files.
  - CI passed: industrial_ci ([run 45302201191](https://github.com/ros-mobile-robots/diffbot/actions/runs/45302201191)), the dev container ([run 45302201011](https://github.com/ros-mobile-robots/diffbot/actions/runs/45302201011)), both firmware builds and the firmware's unit tests ([run 45302201267](https://github.com/ros-mobile-robots/diffbot/actions/runs/45302201267)), and the Robot container's builds on arm64 and amd64 ([run 45302201080](https://github.com/ros-mobile-robots/diffbot/actions/runs/45302201080)).
- **At `4ef85e8`:** CI passed: industrial_ci ([run 45198721727](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198721727)), the dev container ([run 45198722120](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198722120)), both firmware builds and the firmware's unit tests ([run 45198721497](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198721497)), and the Robot container's builds on arm64 and amd64 ([run 45198721827](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198721827)).

- **At `c9a39f9`,** in the dev container's image (ROS Noetic, Python 3.8.10):
  - `catkin build` succeeds for all 13 packages.
  - `catkin run_tests` for `diffbot_bringup` and `diffbot_control`, and `catkin_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.
  - Lint: flake8 7.5.0 with ament_flake8's settings and plugins, and mypy 1.10.1 with `--python-version 3.8 --disallow-untyped-defs` on 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](https://github.com/ros-mobile-robots/diffbot/actions/runs/44611799352)). The dev container ([run 44611799376](https://github.com/ros-mobile-robots/diffbot/actions/runs/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 parent `13f77a7`. The two differ in one line of the report's first line. `9b3f336`, the head before the merge, differs from `6bf992f` in the words of two docstrings: the tool's 77 tests, flake8 and mypy were rerun on it.

- **The tool's tests,** in the dev container's image (ROS Noetic, Python 3.8.10), without network:
  - `catkin build diffbot_bringup`: no warnings.
  - `catkin run_tests diffbot_bringup` and `catkin_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.
- **Lint:** flake8 7.1.2 with ament_flake8's settings and plugins, and mypy 1.10.1 with `--python-version 3.8 --disallow-untyped-defs`: no findings in the tool and its test.
- **The first version (`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.
- **Inputs that the first version got wrong,** through the installed command:
  - A gyroscope marked as not measured: the first version printed a bias and a noise of zero. This head prints "0 readings".
  - Stamps 0, 20, 20, 10 and 80 ms: the first version printed 50.00 Hz. This head prints no rate, one repeated and one earlier stamp.
  - Stamps 0, 40, 20, 60 and 80 ms with a recorder's clock that goes back together with them: the first version printed 50.00 Hz and no interval over 30 ms. This head prints no rate and one earlier stamp.
  - Two frames with gravity on +z and −z: the first version printed a gravity of 3.270 m/s². This head refuses the bag, also when the `frame_id`s are empty.
  - A NaN reading, a `std_msgs/String` topic, messages named `sensor_msgs/Imu` with 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.
  - The readings 7 · 10⁻³⁰⁴, 0 and 0: the first version ended with Python 3.8's AssertionError "negative sum of square deviations". This head prints a variance of 0.
  - A camera topic in a bag of 211 MiB: this head refuses the topic with a peak memory of 45 MiB.
  - Every byte of an uncompressed, a BZ2 and an LZ4 bag of three messages inverted in turn, at `13f77a7`: each of the 28,489 files gives a report or one line, never a traceback.
  - A line break in a stored checksum, in a `frame_id` and in the topic's name: the line break is shown as `\n`, and the line stays one line.
- **The recording command of the documentation,** at `13f77a7`: `rosbag record -o imu_still --duration=3 /diffbot/imu/data_raw` twice 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 `-O` and no publisher replaces a recording of 61 kB with an empty bag, which the tool refuses with "0 messages".
- **Firmware:** `.docker/firmware/run.sh pio run -e teensy31`, `pio run -e teensy40` and `pio test -e native` (93 tests) pass with the changed comment in `diffbot_base_config.h`, at `3ae3b50`. After that, the firmware's files changed only in `lib/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.
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

Status: Done

Development

Successfully merging this pull request may close these issues.

Firmware tests: GoogleTest and gMock for the tests on the PC

1 participant