Skip to content

Contributing: document the rules and add local code checks - #207

Merged
fjp merged 19 commits into
noetic-develfrom
docs/code-rules
Nov 23, 2026
Merged

fjp merged 19 commits into
noetic-develfrom
docs/code-rules

Conversation

@fjp

@fjp fjp commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

What and why

AGENTS.md now holds the repository's rules for code, tests, comments, READMEs, shell scripts, workflows and documentation, and says what to run before a push. Two checks test the rules on a contributor's PC: flake8 and mypy on every Python file, and clang-tidy's include check on the C++ files, with a script for the firmware. Contributors and coding agents read these files first, and CI doesn't check the style of a pull request yet (#117).

  • AGENTS.md: "Before you push" lists what to run for each kind of change. "Commands" has a table of the style checks. "Code and tests" has the rules for design, names, comments and docstrings, READMEs, tests, Python, C++, and launch files, shell scripts and workflows, with a short example where a rule is easy to misread. CONTRIBUTING.md has the short form with links, and the pull request template's comments remind of the tests and their walkthrough.
  • Python check: flake8 . and mypy . in the repository's root. setup.cfg holds the settings of ROS 2's ament_flake8 and asks mypy for type hints on every function, for Python 3.8. scripts/requirements-lint.txt pins flake8 7.5.0 with its eight plugins and mypy 1.10.1. .gitignore lists mypy's cache.
  • Include check: diffbot_firmware/tools/check_includes.sh {native|teensy31|teensy40} FILE… runs clang-tidy's misc-include-cleaner with the flags of the PC build or of a board, and installs the pinned clang-tidy 18.1.8. board_flags.py turns PlatformIO's command for src/main.cpp into flags that clang understands. compiledb_path.py has PlatformIO write that command into a build folder of the check's own. diffbot_base and diffbot_gazebo_plugins are checked with catkin's compile commands, as the guide shows.
  • AI tools: saying whether one used AI stays optional, and the guides no longer ask contributors to leave out attribution lines for AI tools.
  • CI: the workflow Firmware runs the unit tests of board_flags.py and the include check on two fixed files in each job, so a change that breaks the script fails there.

Limits:

  • No code is changed to follow the rules. Older files keep their form until they change.
  • CI doesn't check the files of a pull request for style yet: CI: check the code style of C++, Python and the catkin packages #117 covers flake8, mypy and clang-tidy, and no issue covers shellcheck. Until then, a contributor runs the checks and names the checked C++ files in the pull request.
  • State of the older code: all 15 .py files pass. With the same flags as the script, the 13 firmware files checked with a board's flags have 32 include warnings, and the seven files of diffbot_base and diffbot_gazebo_plugins have 74 (counted at 9686c21). Four of the six older shell scripts have shellcheck findings. The rules differ for the two: a C++ change has no include warning in the lines it adds or touches, and a changed shell script has no shellcheck finding that its version on noetic-devel doesn't have, anywhere in the file.
  • diffbot_gazebo/test/test_geometry_consistency.py uses subTest in its loops over the robots, under nose. It puts the robot into the assertion's message, as the rule asks. A test per robot would change the test count that the Geometry Test page quotes, so that change gets a pull request of its own.

Merge order: this pull request first, then ros-mobile-robots/ros-mobile-robots.github.io#115, whose pages link this file, its section "Tests" and the include check's script.

How it was tested

At the head, 129a11b, unless an item names another commit. After 8943d60, ac1b99a changes only the docstring of compiledb_path.py, and 129a11b only the sentence on AI tools in CONTRIBUTING.md and AGENTS.md:

  • Python check, in python:3.12-slim at 8943d60, and in python:3.11-slim and python:3.14-slim at 5b4d45f, whose Python files differ only in the test's import lines:
    • The pins install into a new virtual environment, and flake8 lists its eight plugins.
    • flake8 . and mypy . exit with status 0 on the 15 files: Success: no issues found in 15 source files.
    • Badly written files planted in diffbot_firmware/.pio, build and devel are left out. One in diffbot_control/scripts makes both tools exit with status 1.
  • Include check, in the firmware container, with the script of 5b4d45f, and at 8943d60 for the failed install:
    • teensy31 on src/main.cpp and lib/base_controller/base_controller.h ends with status 0 and the older warnings. A compile_commands.json of one's own keeps its checksum, firmware.hex stays, and the next build compiles no file. The flags are the same as those from PlatformIO's database in diffbot_firmware. teensy40 and native end with status 0 too.
    • A clang-tidy in .pio/clang-tidy that reports 17.0.6 is replaced by 18.1.8, and without one, the script installs it. With 18.1.8 there, it installs nothing.
    • A failing PlatformIO ends with status 1 and an error: line, and so does a failed install of clang-tidy. An unknown environment, a missing file and a call without arguments end with status 2.
    • A copy of test/fakes/fake_bno055.h without its include of i2c_bus.h is reported: no header providing "diffbot::I2cBus" is directly included (at 339c106, the same check).
    • ShellCheck 0.10.0 finds nothing in the script, and the 9 unit tests of board_flags.py pass.
  • CI at 129a11b: all checks pass: industrial_ci (run 45172244360), the dev container, the description check, and the workflow Firmware with the include check on two fixed files in each job and the tests of board_flags.py (run 45172244342).
  • Earlier results that still hold, because the later commits change no code that they ran:
    • At 2b649da, in the dev container: catkin build succeeds for all 13 packages, and catkin_test_results build reports 81 tests, 0 errors, 0 failures. sim_drive_diffbot.test and sim_drive_remo.test end with RESULT: SUCCESS. On the PC, both firmware builds end with [SUCCESS], and the unit tests with 93 test cases: 93 succeeded.
    • At 9686c21, the two clang-tidy calls for diffbot_base and diffbot_gazebo_plugins run in the dev container. With koalaman/shellcheck:v0.10.0, the shellcheck comparison gives the same two findings for .docker/robot/smoke_test.sh on noetic-devel and on this branch.
    • In the dev container's image, sourcing /opt/ros/noetic/setup.bash under set -u stops at ROS_MASTER_URI: unbound variable, the reason for the shell rule's exception.
  • Not tested: the C++ examples in the text were compared with their files and not compiled on their own. The Robot container workflow doesn't run, because no file of the robot image changes.

Documentation

ros-mobile-robots/ros-mobile-robots.github.io#115: the Contributing page names this file as the place of the code rules, and Testing and CI links its rules for tests and names the include check of the workflow Firmware.

AGENTS.md gets a section "Code and tests" (design, names, comments and docstrings, tests, Python, C++, launch files, shell scripts and workflows) and says what a feature's documentation holds. CONTRIBUTING.md and the pull request template get the short form.
fjp added 17 commits November 18, 2026 11:40
AGENTS.md:
- "Before you push" comes first: a list by the kind of change, with what
  to run and a link to the section that has the details. It takes the
  place of "Before you call a change done".
- "Commands" names the place of each command group and the result to
  expect, and has a table of the style checks.
- Python: the commands are one function, lint_changed_python. It checks
  the base branch and the common commit first, and ends with status 2 and
  an error line when Git can't work out the changed files. Status 0 is
  left for passing checks and for a change without Python files. The
  lint environment lives outside the checkout. The text says which files
  are checked: every .py file, and not dynamic_reconfigure's cfg files.
- C++: the section covers diffbot_base, diffbot_gazebo_plugins and the
  firmware. "Check the includes" has the commands for clang-tidy's
  misc-include-cleaner: with catkin's compile commands in the dev
  container, and with the PC's and the board's flags in the firmware
  container.
- "Ask or change" uses takeSamplingSlot() as its example, which returns
  an answer and changes state.
- Shell scripts: the rule is for Bash scripts. A script may leave out an
  option of set -euo pipefail at an environment's boundary, with the
  reason at the line, as scripts that source ROS's setup.bash do for -u.
  shellcheck has its command, and the robot image's entry point is
  checked by the image's smoke test.
- "Commits and pull requests": the branch prefixes refactor/ and test/, a
  closing keyword only when the pull request resolves the issue, signed
  commits.

CONTRIBUTING.md has the short form of these points, and the comment at
catkin_test_results gives the form of the summary line without a count.
AGENTS.md:
- Python: lint_changed_python compares with the branch that the pull
  request goes to. Without an argument, it takes origin/noetic-devel and
  stops with an error line when origin isn't this repository, as in a
  clone of a fork. The block shows the call for a fork's clone, and the
  error for a missing branch names the branch that was asked for.
- "Check the includes": the board's flags come from a function,
  set_board_flags, with three checked steps. It stops with an error line
  and an empty array when PlatformIO writes no compile commands or when
  they have no command for src/main.cpp, and it leaves out the switches
  that only GCC knows.
- shellcheck: a new script has no finding, and a changed script has no
  finding that noetic-devel's version doesn't have, with the two
  commands to compare.
- A colon after the label "Pass criterion".

CONTRIBUTING.md: the rules for the topic and the branch have labels with
a colon.
AGENTS.md:
- lint_changed_python takes origin without an argument only when its
  address is this repository's on github.com, over HTTPS or SSH. An
  address that merely contains the repository's name stops the function
  with an error line.
- set_board_flags stops when the command for src/main.cpp lacks the
  board's processor, its name or an include folder. The two clang-tidy
  calls are chained to the function with &&, so the block's status tells
  whether every step worked.
AGENTS.md: lint_changed_python reads origin's address with
git remote get-url, which gives the address that Git fetches from, also
when origin has several addresses or a rewrite rule. The whole address
still has to be this repository's on github.com.
A README says what its folder contains, how to build, run and test it, and the facts a contributor looks up while changing the code. A design or a feature is explained on its page on ros-mobile-robots.com, which the README links instead of explaining it again. Between two headings, a README links each destination once.
… install line's

The Python style check writes .mypy_cache/ into the checkout, which .gitignore now lists. AGENTS.md says that flake8 runs with the eight plugins that the install command lists. ament_flake8 itself declares only some of them, depending on the ROS 2 release.
The style check for Python is now flake8 and mypy on the whole
repository, with their settings in setup.cfg and the tools pinned in
scripts/requirements-lint.txt, because every .py file passes both
today. The guide keeps the one-time setup and the two commands.

The firmware's include check is diffbot_firmware/tools/check_includes.sh
with the environment as its first argument. Its helper board_flags.py
turns PlatformIO's command for src/main.cpp into clang's flags and has
unit tests. The workflow Firmware runs those tests and the script on
two fixed files with each board, so a change that breaks the script
shows up in CI. Checking the files of a pull request stays with #117.
PlatformIO writes a board's compile commands for the include check into
.pio/check-includes, through the extra script compiledb_path.py and a
build folder of the check's own. The board's build and a
compile_commands.json of one's own are never touched, also when a check
is interrupted. The same extra script in the board's normal build folder
would make PlatformIO clean that build.

The script reinstalls clang-tidy when the version in .pio/clang-tidy
isn't the pinned 18.1.8. The guide names the Python versions the check
ran with, and the package that Ubuntu needs for python3 -m venv.
The helper's test imports one name per line, as the Python rules ask. The pins' comment and the guide say "flake8 with its eight plugins". A failed clang-tidy install ends with an error: line and status 1, and the exit statuses name missing libraries.
The docstring names the check's own build folder, .pio/check-includes, where check_includes.sh has PlatformIO write the compile commands.
@fjp fjp changed the title Contributing: code, test and documentation rules in AGENTS.md Contributing: document the rules and add local code checks Nov 23, 2026
The guides keep "You don't need to say whether you used AI" and no longer ask contributors to leave out attribution lines for AI tools. Whether a contributor adds such a line is the contributor's choice.
@fjp
fjp merged commit c853bf0 into noetic-devel Nov 23, 2026
11 checks passed
@fjp
fjp deleted the docs/code-rules branch November 23, 2026 12:25
@github-project-automation github-project-automation Bot moved this from In review to Done in DiffBot & Remo roadmap Nov 23, 2026
fjp added a commit to ros-mobile-robots/ros-mobile-robots.github.io that referenced this pull request Nov 23, 2026
## What and why

Contributors can now find the writing and code rules for their change, the checks to run locally, and which checks GitHub runs for each kind of pull request. The simulation walkthrough applies the excerpt and link rules, and the testing guide shows how the project registers ROS and firmware tests. The Contributing page lists robot_component_models with its guide, and the pull request template asks for the checked commit and the limits. The guides no longer ask contributors to leave out attribution lines for AI tools: saying whether one used AI stays optional. Before, the repository had four writing rules and none for its own code, `CONTRIBUTING.md` said that CI checks every pull request, and Testing and CI named gtest, which no package uses.

## How it was checked

At `9530ced5`, which contains `4c2e0bed`, the merge of `main` at `fc93367f` with #69, in a `python:3.12-slim` container: `mkdocs build --strict`, `codespell` and the 102 repository tests pass.

- **Facts:** the triggers in the table come from `.github/workflows/ci.yml`, `lint.yml` and `preview.yml`, and GitHub's page [Events that trigger workflows](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows) gives the activity types that start them. That a contributor who has never had a change merged here needs a maintainer to approve the runs of a pull request from a fork comes from the repository's Actions setting for forks. The registration of the three kinds of tests comes from `diffbot_gazebo/CMakeLists.txt` and `diffbot_firmware/platformio.ini` of the diffbot repository at 27d72a4. The sentences of the code walkthrough were compared with `test_drive.py` and `sim_drive_diffbot.test` at the commit that the page pins, 7190c9a. The Python examples were run, and the style check's results for the named files were reproduced. The four examples of diffbot's code rules were compared with its `AGENTS.md` at ros-mobile-robots/diffbot#207's head, and the glossary sentence with `includes/glossary.yml` and `hooks/glossary.py`.
- **Links:** the outside reference and guide addresses were checked, with archived copies for the rostest and xacro wiki pages. The walkthrough's code links were compared with the pinned diffbot source. The strict build checks the links between pages and their anchors.
- **Rendering,** at 62c257d: Contributing, Testing and CI and Simulation Test, built locally and opened in Chrome at 390 and 1440 px, light and dark. The later commits change sentences and table cells of these pages, not their layout. No page is wider than the screen, every in-page link finds its anchor, and every box that scrolls sideways reaches its end.

## Limits

- Text only, apart from the renamed example and two lines in `.gitignore` for mypy's cache. No script or test changes to follow the new rules. Older files keep their form until they change, and `AGENTS.md` names examples of the ones that don't follow the rules yet.
- The repository has no flake8 or mypy configuration and no lint job for Python. The style check is two commands that a contributor runs.
- On an existing page, the writing rules hold in the sections a pull request changes. The unchanged sections of the three pages keep their form.
- As examples, the text names only pages of this documentation repository that are on `main`. Once #89 is merged, "A feature's tests" can name its tests page as a second example. Once #96 is merged, the rules for tests can name the browser tests.

**Merge order:** after ros-mobile-robots/diffbot#207. Testing and CI links that pull request's section "Tests" in diffbot's `AGENTS.md` and its include check's script.
fjp added a commit that referenced this pull request Nov 23, 2026
## What and why

The simulated IMU's tests pass two streams that are faulty: a yaw rate that never changes, and samples whose stamps don't advance after a reset of the simulation. This pull request makes both fail, so that a pass shows what the tests' names say.

**Before**, on `noetic-devel` at `7d428c2`:

- [`test_imu.py`](https://github.com/ros-mobile-robots/diffbot/blob/7d428c2/diffbot_gazebo/test/test_imu.py#L84-L121) checks the yaw rate's mean only in the left turn, against 0.5 ± 0.15 rad/s. At rest, the test checks only that each yaw rate is finite. A yaw rate that stays at +0.5 rad/s passes both tests.
- The left turn takes the mean over however many messages arrived. One message is enough for a pass. No message raises `ZeroDivisionError`, which is an error without the check's message.
- [`test_imu_reset.py`](https://github.com/ros-mobile-robots/diffbot/blob/7d428c2/diffbot_gazebo/test/test_imu_reset.py#L66-L77) counts every sample that arrives after the reset with a stamp below `RECOVERY`, 1 s. Ten samples with one and the same stamp count as a recovery, also with the stamp 0 s. Between two resets of the clock, the IMU sample validator forwards one sample with a given stamp at most.

**With this pull request:**

1. **At rest,** the mean yaw rate of the 2 s of `MEASURE` must lie within `YAW_RATE_AT_REST_TOLERANCE` of 0 rad/s. The tolerance is `TURN_RATE / 10`, 0.05 rad/s.
2. **In the left turn,** the test checks the messages' rate before it takes the mean: more than 90 messages in the 2 s of `MEASURE`. Both tests use one helper for this check, `assert_message_rate()`. One message or none now fails with the rate's message.
3. **After the reset,** `fresh_stamps()` counts a stamp only when the stamp is later than 0 s, later than the last stamp that counted, and earlier than `RECOVERY`. A stream that repeats one stamp has one fresh sample at most, so the test fails.

**Limits:**

- The two tests of `test_imu.py` catch a constant yaw rate together. A yaw rate near 0 rad/s fails the left turn, and a yaw rate near the turn rate fails at rest. Neither test alone shows that the yaw rate follows the robot's motion.
- A repeated stamp after the reset is not a failure of its own. The stamp doesn't count, and the test fails when ten advancing stamps below 1 s don't arrive within `RECOVERY_TIMEOUT`.
- Both tests still read only the upward axis: the yaw rate and the upward reading.
- The tests' line numbers change. The Tests page of the IMU simulation quotes both files by line ranges at `cd41ead`.

## How it was tested

The branch's head is `be317b9`, the merge of `noetic-devel` at `c853bf0` (#222, #224 and #207) into `36506b0`. Git merged it without a conflict. `36506b0` is the merge of `noetic-devel` at `7d428c2` (#220) into `6ec9087`. That merge brings changes of `diffbot_control` only. This pull request changes no file there.

- **At `be317b9`:**
  - 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 15 source files.
  - CI: industrial_ci ([run 45198738094](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198738094)), the dev container ([run 45198738066](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198738066)), and both firmware builds with the firmware's unit tests ([run 45198738156](https://github.com/ros-mobile-robots/diffbot/actions/runs/45198738156)) pass.

- **The whole workspace at the head `36506b0`,** in the dev container's image built from `27d72a4`, with Gazebo headless and remo_description 2.5.0:
    - `catkin build` succeeds for all 13 packages. `catkin build --catkin-make-args run_tests` and `catkin_test_results build` report 86 tests, 0 errors, 0 failures, 0 skipped.
    - `sim_drive_diffbot.test` and `sim_drive_remo.test` pass with 7 test cases each. DiffBot drove 0.560 m forward and turned 1.490 rad, Remo 0.570 m and 1.499 rad, by the odometry and by the true pose.
    - The reset test's line for DiffBot: `last stamp before the reset 3.000 s; after the reset 10 samples, first stamps ['0.020', '0.040', '0.060'], 10 of them with advancing stamps earlier than 1.0 s`. For Remo, the last stamp before the reset was 3.019 s, with the same first stamps.
    - The 12 Python files of the repository pass flake8 with ament's settings and mypy 1.10.1 for Python 3.8, in a Python 3.12 container.
- **CI at `36506b0`:** industrial_ci builds the workspace and runs its tests with the released and with the testing ROS packages ([CI run 44611416597](https://github.com/ros-mobile-robots/diffbot/actions/runs/44611416597)). The dev container builds and runs the tests ([run 44611416951](https://github.com/ros-mobile-robots/diffbot/actions/runs/44611416951)). Both firmware builds and the firmware's unit tests pass.

The items below ran at `6ec9087`, the head before the merge. The simulation tests and the faulty streams ran in the dev container's image built from `27d72a4`, with Gazebo headless and remo_description 2.5.0. The style checks ran in a Python 3.12 container:

- **The simulation tests:** `rostest diffbot_gazebo sim_drive_diffbot.test` and `rostest diffbot_gazebo sim_drive_remo.test` pass, each with 7 tests, 0 errors and 0 failures.
    - The measured motions: DiffBot drove 0.560 m forward and turned 1.490 rad, by its odometry and by its true pose. Remo drove 0.560 m and turned 1.501 rad.
    - The reset test's line for DiffBot: `last stamp before the reset 3.015 s; after the reset 10 samples, first stamps ['0.020', '0.040', '0.060'], 10 of them with advancing stamps earlier than 1.0 s`. For Remo, the last stamp before the reset was 3.018 s, with the same first stamps.
- **The margins of the two mean checks:** three more runs per robot of the two IMU test nodes alone, from a copy of `test_imu.py` that prints each mean. The print is not part of this pull request. These runs were at `e0effd3`, which differs from `6ec9087` only in two docstrings.
    - At rest, the mean yaw rate was −0.00034, +0.00081 and +0.00073 rad/s for DiffBot and +0.00047, −0.00024 and +0.00001 rad/s for Remo. The tolerance is 0.05 rad/s.
    - In the left turn, the mean yaw rate was 0.49974, 0.50038 and 0.50041 rad/s for DiffBot and 0.49976, 0.50032 and 0.49916 rad/s for Remo. Each window had 100 messages.
- **The faulty streams,** fed into the test methods without a simulation, in the same image:

    | Stream | `noetic-devel` at `27d72a4` | This branch |
    |:--|:--|:--|
    | Yaw rate stays at +0.5 rad/s, 100 messages: the test at rest | passes | fails: "at rest, the yaw rate should be about zero, the IMU measured 0.500 rad/s about z" |
    | Yaw rate stays at +0.5 rad/s, 100 messages: the left turn | passes | passes |
    | Yaw rate stays at 0 rad/s, 100 messages: the left turn | fails | fails |
    | Left turn with one message of +0.5 rad/s | passes | fails: "published at 0.5 Hz, expected 50 Hz" |
    | Left turn with no message | `ZeroDivisionError` | fails: "published at 0.0 Hz, expected 50 Hz" |
    | After the reset, ten samples stamped 0.0 s | recovery | no recovery |
    | After the reset, ten samples stamped 0.02 s | recovery | no recovery |
    | After the reset, nine advancing stamps and one repeat | recovery | no recovery |
    | After the reset, ten stamps from 0.02 s to 0.20 s | recovery | recovery |
    | The same ten stamps after one late sample from before the reset | recovery | recovery |

- **Style:** `diffbot_gazebo/test/*.py` pass flake8 with ament's settings and mypy 1.10.1 for Python 3.8.

## Documentation

ros-mobile-robots/ros-mobile-robots.github.io#89 explains both test files on the Tests page of the IMU simulation, at `cd41ead`, and names the two gaps under "What the tests don't show". After this pull request merges, that page's pin moves to the merge commit: the page's excerpts get the new line ranges, and the two limits become 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

documentation Improvements or additions to documentation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant