# C202 Lab: Measure Your IMU and Calibrate It to the Cameras

> The hands-on lab of the C202 IMU course: record a static IMU log and compute its Allan deviation with allan_variance_ros, write Kalibr's imu.yaml, then record a camera-IMU dataset and run kalibr_calibrate_imu_camera, judge the residuals and transform, and read and check the time offset. Every command is as the tools' documentation or source gives it, with checkpoints and what to do when a step fails.

Page: https://egocam.ai/university/c202/lab/

This lab measures your rig's IMU and ties it to the cameras. Part A records the IMU sitting still and turns the log into Kalibr's `imu.yaml`. Part B records the rig moving in front of the target and runs Kalibr's camera–IMU calibration, which gives the transform between IMU and cameras and the time offset between their clocks.

Every command below is copied from the tool's own documentation, or follows its documented form with your own file names; the source is linked at each step. Where a behaviour is stated from a tool's source code, the file and commit are named. Square brackets mark values you fill in.

Work through [Lessons 1 to 4](https://egocam.ai/university/c202/) first, finish the [C201 lab](https://egocam.ai/university/c201/lab/) for your cameras, and keep the [checklist](https://egocam.ai/university/c202/checklist/) next to you.

## Part A: the IMU on its own

Part A takes hours of recording. Start it first, and work on the Part B preparation while it runs.

### A1: Build allan_variance_ros

The [allan_variance_ros README](https://github.com/ori-drs/allan_variance_ros) says the tool "is designed for Ubuntu 20.04", and offers a devcontainer for other systems. Put the repository in the `src` folder of a ROS 1 catkin workspace, such as `~/catkin_ws/src/allan_variance_ros`, then from the workspace folder:

```bash
catkin build allan_variance_ros
source devel/setup.bash
```

**You should see** the package build without errors. The README's other route, for systems that are not Ubuntu 20.04, is its devcontainer (`.devcontainer/README.md` in the repository).

### A2: Prepare the rig

- **Configure the IMU exactly as you will record with it:** the same output rate and the same filter settings. Write them down. Lesson 4 shows that the filter changes the time offset; the noise belongs to one configuration too.
- **Let it warm up.** To the best of our knowledge, it is best to start the log once the rig has reached its working temperature, because a bias still settling shows up as a trend, not as noise ([Temperature and the limits of a static plot](https://egocam.ai/calibration/imu-noise-and-allan-variance/#temperature-and-the-limits-of-a-static-plot)).
- **Put it on a damped surface** where nothing will touch or bump it for the whole recording. The README: "Place your IMU on some damped surface and record your IMU data to a rosbag." Any vibration that reaches the IMU ends up in the Allan plot as if it were sensor noise.

### A3: Record the static log

Start `roscore` in one terminal, as the README's first step says, and the IMU driver. Then record only the IMU topic, in the form the [ROS rosbag command line](https://wiki.ros.org/rosbag/Commandline) page gives (`-O` names the bag, `--duration` stops it):

```bash
rosbag record --duration=[HOURS]h -O imu_static.bag [IMU_TOPIC]
```

How long: the README says "You must record at least 3 hours of data. The longer the sequence, the more accurate the results." The Kalibr wiki recommends a "~15-24 hour dataset recording of the IMU being stationary" ([IMU noise model](https://github.com/ethz-asl/kalibr/wiki/IMU-Noise-Model)). Lesson 2's table shows why: the random walk comes from the long-τ end, where a 3-hour log is uncertain.

**Checkpoint:** `rosbag info imu_static.bag` gives the bag's duration and message count ([rosbag command line](https://wiki.ros.org/rosbag/Commandline)).

**You should see** a duration of at least the length you planned, the IMU topic at close to its nominal rate, and a message count close to the IMU rate times the duration. A count well short of that means dropped samples; record again.

### A4: Put the messages in timestamp order

The README recommends this step:

```bash
rosrun allan_variance_ros cookbag.py --input original_rosbag --output cooked_rosbag
```

With your file names, `--input imu_static.bag --output imu_static_cooked.bag`. The script rewrites each message at its header timestamp (`scripts/cookbag.py`, commit `1d54b60`). The Allan computation averages consecutive samples, so they must be in the order they were sampled ([Temperature and the limits of a static plot](https://egocam.ai/calibration/imu-noise-and-allan-variance/#temperature-and-the-limits-of-a-static-plot)).

### A5: Compute the Allan deviation

Write a config file in the format of the repository's examples (`config/*.yaml`):

```yaml
imu_topic: "[IMU_TOPIC]"
imu_rate: [IMU_RATE_HZ]
measure_rate: [IMU_RATE_HZ] # Rate to which imu data is subsampled
sequence_time: [RECORD_LENGTH_S]
```

What the source does with these keys (`src/AllanVarianceComputor.cpp`, commit `1d54b60`):

- **`measure_rate`** sets a subsampling step of `imu_rate / measure_rate`. Some example configs subsample (400 to 100 Hz); `config/sim.yaml` does not. To the best of our knowledge, keeping `measure_rate` equal to `imu_rate` avoids the subsampling caveat of Kalibr's noise model ([Lesson 2](https://egocam.ai/university/c202/lesson-2-measuring-imu-noise/#how-long-to-record)).
- **`sequence_time`** caps how many seconds of the log are read: samples after it are skipped. Set it to your record length.
- **Gyroscope units.** The source reads the gyroscope values as radians per second, as ROS IMU messages carry them, and converts them to degrees for its output (a comment at the conversion says so).

Put the cooked bag **alone** in a folder: the tool lists the `.bag` files in the folder and processes only the first of them (`src/allan_variance.cpp`). Then run, as the README gives it:

```bash
rosrun allan_variance_ros allan_variance [path_to_folder_containing_bag] [path_to_config_file]
```

**You should see** progress messages as the log is read, and `allan_variance.csv` written into the bag's folder. The source evaluates averaging times from 0.1 s to about 1,000 s.

### A6: Read the plots and the numbers

Run the analysis, passing the config so the topic and rate go into the output file:

```bash
rosrun allan_variance_ros analysis.py --data allan_variance.csv --config [path_to_config_file]
```

The README says to press the space bar to go to the next figure. The script prints, per axis, the white noise (velocity or angle random walk), the bias instability and the random walk, saves `acceleration.png` and `gyro.png`, and writes `imu.yaml` (`--output` changes the name).

Work through [Lesson 2](https://egocam.ai/university/c202/lesson-2-measuring-imu-noise/):

1. **The shape.** A falling −½ stretch at short τ, a flat bottom, and a rising stretch. Slope −1 at the very start is quantization; a slope towards +1 at the right end is a trend, such as warm-up ([Failure cases](https://egocam.ai/calibration/imu-noise-and-allan-variance/#failure-cases)).
2. **The fitted lines.** In `analysis.py` (commit `1d54b60`), the −½ line is fitted to τ below 10 s and the +½ line to the whole curve, not just the rising branch. Check by eye that each line follows the part of the curve it stands for.
3. **The axes.** Compare the three axes of each sensor. If one disagrees, note it.
4. **Which axis went into the file.** Since commit `7a3a461` (October 2022, message `Use worst axis instead of average and support overlapping bins`), `analysis.py` writes the largest of the three axes' values into `imu.yaml`, not their mean (`np.amax`). The example `imu.yaml` in the README matches the three-axis mean, so it predates that change. Read the printed per-axis values next to the file.
5. **The datasheet.** Convert the datasheet's noise figures and compare ([Getting the values from a datasheet](https://egocam.ai/kalibr/kalibr-file-reference/#getting-the-values-from-a-datasheet)). A measured value can differ in either direction ([IMUs on Wearable Rigs](https://egocam.ai/hardware/imu-sensors/#noise)).

> **Diagram:** An Allan deviation plot from hours of a stationary IMU. White noise is read on the falling line at τ = 1 s (Kalibr's noise density) and rate random walk on the rising line at τ = 3 s (Kalibr's random walk). The flat minimum is what Woodman and allan_variance_ros report as bias instability; by the IEEE 952 derivation the coefficient B is that minimum divided by 0.664.

### A7: Write the imu.yaml for Kalibr

Start from the generated file and check every key against the [YAML formats](https://github.com/ethz-asl/kalibr/wiki/Yaml-formats) page:

```yaml
#Accelerometers
accelerometer_noise_density: [VALUE]   #Noise density (continuous-time)
accelerometer_random_walk:   [VALUE]   #Bias random walk

#Gyroscopes
gyroscope_noise_density:     [VALUE]   #Noise density (continuous-time)
gyroscope_random_walk:       [VALUE]   #Bias random walk

rostopic:                    [IMU_TOPIC]      #the IMU ROS topic
update_rate:                 [IMU_RATE_HZ]    #Hz (for discretization of the values above)
```

- **Units:** gyroscope values in radians, not degrees.
- **`update_rate`** is the rate the IMU really runs at in the Part B bag, and `rostopic` is its topic there.
- **Inflation.** Decide a factor. The Kalibr wiki: "for lowest-cost sensors, increasing the noise model parameters by a factor of 10x or more may be necessary" ([Inflating the noise values](https://egocam.ai/kalibr/kalibr-file-reference/#inflating-the-noise-values)). Save the raw file as `imu_allan.yaml` and the inflated one as `imu.yaml`, and write the factor down. Step B5 tells you whether it fits.

## Part B: the IMU against the cameras

### B1: Gather the inputs

- `[calib]-camchain.yaml` and `target.yaml` from the [C201 lab](https://egocam.ai/university/c201/lab/), for the same cameras at the same resolution and focus.
- `imu.yaml` from A7.
- Kalibr, set up as in [C201 lab step 1](https://egocam.ai/university/c201/lab/#step-1-set-up-kalibr), with your data folder mounted at `/data`.

### B2: Lock the settings

- **Exposure short and fixed**, in good, even light. The [camera–IMU guide](https://github.com/ethz-asl/kalibr/wiki/camera-imu-calibration) asks to "keep the motion blur low" with "low shutter times" and "good illumination". A fixed exposure also keeps the time offset fixed during the recording (Lesson 4).
- **The IMU in its production configuration**, the same as in A2.
- **Rates.** The guide reports that "Good results have been obtained by using a camera rate of 20 Hz and an IMU rate of 200 Hz." Use your rig's real IMU rate; it must match `update_rate`.

### B3: Record the camera–IMU dataset

Fix the target to a wall or table; it must not move. Record the image topics and the IMU topic together:

```bash
rosbag record -O imu_cam.bag [CAM0_TOPIC] [CAM1_TOPIC] [IMU_TOPIC]
```

If your recorder writes files instead, build the bag with `kalibr_bagcreater` from `cam0/`, `cam1/` and an `imu0.csv` with the header `timestamp,omega_x,omega_y,omega_z,alpha_x,alpha_y,alpha_z`, in "timestamps=[ns], omega=[rad/s], alpha=[m/s^2]" ([bag format](https://github.com/ethz-asl/kalibr/wiki/bag-format)):

```bash
kalibr_bagcreater --folder dataset-dir/. --output-bag awsome.bag
```

The motion ([Lesson 3](https://egocam.ai/university/c202/lesson-3-kalibr-camera-imu/#the-recording-the-target-stays-still-the-rig-moves)):

- Start with the rig at rest, then pick it up gently: "avoid shocks, especially at the beginning/end when you pick up the sensor". Why: [Kalibr workflow, step 7](https://egocam.ai/kalibr/kalibr-workflow/#7-record-the-cameraimu-dataset).
- Keep the target in view of the cameras throughout. Kalibr's only visual measurements are the target's corners, so a frame without it ties nothing to the cameras.
- Rotate about every axis and translate along every axis: "try to excite all IMU axes (rotation and translation)". Not only pans.
- Move briskly but smoothly. For scale, Furgale and colleagues' recordings were "about 90 seconds", at "an average absolute angular velocity of about 55° s−1".
- Put the rig down gently at the end.

**Checkpoint:** `rosbag info imu_cam.bag`.

**You should see** each image topic and the IMU topic, and an IMU message count close to the IMU rate times the duration.

### B4: Run the calibration

Inside the container, in the form of the guide's example:

```bash
rosrun kalibr kalibr_calibrate_imu_camera \
	--target /data/target.yaml \
	--imu /data/imu.yaml \
	--imu-models calibrated \
	--cam /data/[calib]-camchain.yaml \
	--bag /data/imu_cam.bag
```

Use `--imu-models scale-misalignment` if the IMU's scale and misalignment have not been corrected. For a rig with two IMUs, give both files and both models, the reference IMU first: `--imu /data/imu0.yaml /data/imu1.yaml --imu-models calibrated calibrated` ([Multi-IMU and IMU intrinsic calibration](https://github.com/ethz-asl/kalibr/wiki/Multi-IMU-and-IMU-intrinsic-calibration)).

**You should see** four files when it finishes: `imu_cam-camchain-imucam.yaml`, `imu_cam-imu.yaml`, `imu_cam-results-imucam.txt` and `imu_cam-report-imucam.pdf` ([The files at a glance](https://egocam.ai/kalibr/kalibr-file-reference/#the-files-at-a-glance)).

### B5: Judge the result

Open the report and the results file and work through [Lesson 3](https://egocam.ai/university/c202/lesson-3-kalibr-camera-imu/#judging-the-result):

1. **IMU timing.** The measurement-rates page is a flat line at the expected rate. "Inspect the IMU time dt plot carefully in the PDF report!" ([RealSense tutorial](https://github.com/ethz-asl/kalibr/wiki/Calibrating-Intel-RealSense-Cameras)). If it shows bursts and gaps, stop: fix the timestamps before anything else.
2. **IMU residuals** inside the dashed ±3σ lines, for the accelerometer and the gyroscope.
3. **Biases** inside their ±3σ bounds.
4. **Reprojection** errors inside the 3σ circle, and comparable to the camera calibration's.
5. **Normalised residuals** in the results file: compare them with the noise you assumed.

**If a check fails:** residuals far outside the lines mean the noise values are too small or the data is bad: raise the inflation or check the timestamps. Biases leaving their bounds mean "your trajectory was too dynamic, or your noise values are not good". Change one thing at a time and solve again.

### B6: Check the transform

In the results file, `T_ci` takes IMU coordinates to camera coordinates, so its translation is where the IMU sits in that camera's frame, in metres ([How Kalibr names transforms](https://egocam.ai/kalibr/kalibr-file-reference/#how-kalibr-names-transforms)). Compare it with a caliper measurement or the mechanical drawing: "Sanity check your final rotation and translation with hand-measured values." The rotation part should match how the IMU is mounted; its entries are close to 0 or ±1 if it is mounted square to the camera.

> **Diagram:** How Kalibr names its transforms. T_a_b takes a point given in frame b and returns it in frame a. Each camera block in camchain-imucam.yaml stores T_cam_imu (IMU to that camera), and every camera after cam0 stores T_cn_cnm1 (the previous camera to this one, so cam1 holds T_c1_c0).

### B7: Read the time offset

Find the line `timeshift cam0 to imu0: [s] (t_imu = t_cam + shift)` in the results file, and `timeshift_cam_imu` in `camchain-imucam.yaml` ([Lesson 4](https://egocam.ai/university/c202/lesson-4-time-offset-and-multiple-imus/)).

1. **Read the sign.** Negative: the camera's timestamps are late relative to the IMU's. Positive: the IMU's are late.
2. **Compare it with how your timestamps are made.** Do the frames carry the start, middle or end of exposure? Does the IMU have a filter delay? The [table of causes](https://egocam.ai/calibration/camera-imu-time-offset/#where-the-offset-comes-from) gives measured sizes for each.
3. **Cameras on one clock should agree.** On a stereo pair triggered together, the two cameras' shifts should be close; the wiki's example has −8.121e-05 s and −8.681e-05 s.
4. **Its bound.** The `--timeoffset-padding` option bounds how far the offset may move from Kalibr's correlation-based first guess. The multi-IMU page says "trouble finding an appropriate value likely hints to issues in the dataset": look at the timestamps before widening it.

### B8: Calibrate again and compare

Record a second camera–IMU dataset under the same settings and calibrate it the same way. Compare the two transforms and the two time offsets. From our conversations with people in the industry, values that move a lot between two good recordings were not well determined by either ([Kalibr workflow, step 9](https://egocam.ai/kalibr/kalibr-workflow/#9-judge-the-results)). For scale, Furgale and colleagues' offset estimates stayed "within a domain of ±0.2 ms" across datasets ([Offline estimation with Kalibr](https://egocam.ai/calibration/camera-imu-time-offset/#offline-estimation-with-kalibr)).

**Optional, the exposure slope.** If your frames may be stamped at the start or end of exposure, calibrate at several fixed exposure times and fit the offset against exposure. A slope near +0.5 means start-of-exposure stamps, near −0.5 end-of-exposure, near 0 mid-exposure. The [prompts page](https://egocam.ai/university/c202/prompts/) has a prompt that writes the fit.

### B9: Sign off and store

Go through the sign-off section of the [checklist](https://egocam.ai/university/c202/checklist/). Store `imu_allan.yaml`, `imu.yaml`, the inflation factor, `camchain-imucam.yaml`, the results file and the report with the data, versioned and tied to the unit's serial number and the IMU configuration. When you run a tracker that estimates the offset online, start it from the Kalibr value and check its sign convention against Kalibr's first ([Camera-IMU Time Offset, checklist](https://egocam.ai/calibration/camera-imu-time-offset/#checklist)).

## When a step fails

| Symptom | What the sources say to do |
|---|---|
| IMU timestamps in bursts, as in the wiki's example of "a burst of IMU are send at a rate of 1ms with gaps of 6ms between" | "You will need to rectify the timestamp problems." ([camera–IMU calibration](https://github.com/ethz-asl/kalibr/wiki/camera-imu-calibration)) |
| IMU residuals far outside ±3σ | inflate the noise values; check the timestamps |
| Time offset cannot settle within its bounds | the wiki: "trouble finding an appropriate value likely hints to issues in the dataset" |
| Target pose flips during the motion | "The use of an Aprilgrid is recommended to avoid this problem entirely." |
| Bag rejected | a ROS 2 bag: convert it to ROS 1 with `rosbags-convert` ([Kalibr workflow, step 4](https://egocam.ai/kalibr/kalibr-workflow/#4-get-the-data-into-a-ros-1-bag)) |
| Allan plot rises towards slope +1 at the right end | a trend such as warm-up: cut it or record again at a steady temperature |
| `allan_variance` processed the wrong log | more than one `.bag` in the folder: the tool processes only one |

The multi-IMU page sums up the biggest risk: "The most common failure mode of kalibr appears to be incorrect timestamps provided by the user."

## Sources

- Buchanan, R. [allan_variance_ros](https://github.com/ori-drs/allan_variance_ros), Oxford Robotics Institute: README, `config/*.yaml`, `scripts/analysis.py`, `scripts/cookbag.py`, `src/allan_variance.cpp` and `src/AllanVarianceComputor.cpp` (commit `1d54b60`, January 2025), and the history of `scripts/analysis.py` (commit `7a3a461`).
- Kalibr wiki, ETH Zurich ASL: [camera–IMU calibration](https://github.com/ethz-asl/kalibr/wiki/camera-imu-calibration), [Multi-IMU and IMU intrinsic calibration](https://github.com/ethz-asl/kalibr/wiki/Multi-IMU-and-IMU-intrinsic-calibration), [IMU noise model](https://github.com/ethz-asl/kalibr/wiki/IMU-Noise-Model), [YAML formats](https://github.com/ethz-asl/kalibr/wiki/Yaml-formats), [bag format](https://github.com/ethz-asl/kalibr/wiki/bag-format), [Calibrating Intel RealSense cameras](https://github.com/ethz-asl/kalibr/wiki/Calibrating-Intel-RealSense-Cameras).
- Kalibr source code, [github.com/ethz-asl/kalibr](https://github.com/ethz-asl/kalibr) (commit `1f60227`): [kalibr_calibrate_imu_camera](https://github.com/ethz-asl/kalibr/blob/master/aslam_offline_calibration/kalibr/python/kalibr_calibrate_imu_camera), [IccSensors.py](https://github.com/ethz-asl/kalibr/blob/master/aslam_offline_calibration/kalibr/python/kalibr_imu_camera_calibration/IccSensors.py), [IccUtil.py](https://github.com/ethz-asl/kalibr/blob/master/aslam_offline_calibration/kalibr/python/kalibr_imu_camera_calibration/IccUtil.py).
- ROS wiki: [rosbag command line](https://wiki.ros.org/rosbag/Commandline).
- Furgale, P., Rehder, J. and Siegwart, R. (2013). [Unified temporal and spatial calibration for multi-sensor systems](https://doi.org/10.1109/IROS.2013.6696514). *IEEE/RSJ IROS*.
- Field guide: [IMU Noise Models and Allan Variance](https://egocam.ai/calibration/imu-noise-and-allan-variance/), [Kalibr Workflow](https://egocam.ai/kalibr/kalibr-workflow/), [Kalibr File Reference](https://egocam.ai/kalibr/kalibr-file-reference/), [Camera-IMU Time Offset](https://egocam.ai/calibration/camera-imu-time-offset/), [IMUs on Wearable Rigs](https://egocam.ai/hardware/imu-sensors/).
