Emulating IMU Measurements¶
This tutorial covers inertial measurement unit (IMU) emulation: turning a sequence of ground truth camera poses into accelerometer and gyroscope readings, and integrating those readings back into a pose estimate. See the sensor section for the measurement model, the meaning of each noise term and the drift they cause over time.
Prerequisites¶
You need ground truth poses, either a transforms.json file or a transforms.db database. Any of the rendering guides produces one, and the emulator does not need the rendered frames themselves.
Emulating Measurements¶
Run the emulator over a dataset:
visionsim emulate.imu --input-dir=transforms.db --output-file=imu.csv --dt=0.02
The --input-dir option takes either file, and --dt is the sampling period in seconds, here 50 Hz. It has to match the real spacing between consecutive poses, since the emulator differentiates the pose spline at those times instead of resampling it. The remaining flags set --gravity (in the world frame, in m/s^2), the constant parts of the biases with --init-bias-acc and --init-bias-gyro, the noise parameters --std-acc, --std-gyro, --std-bias-acc and --std-bias-gyro, and --seed for reproducible noise.
The output is a CSV table with one row per sample:
t,acc_x,acc_y,acc_z,gyro_x,gyro_y,gyro_z,bias_ax,bias_ay,bias_az,bias_gx,bias_gy,bias_gz
0.0,-0.2252,9.3807,-4.9132,0.0072,-0.0048,-0.0054,0.0,0.0,0.0,0.0,0.0,0.0
0.02,-0.2171,9.2758,-4.9026,0.0052,-0.0115,0.0025,1.8e-06,-3.4e-06,5.5e-06,1.2e-06,6.1e-06,4.3e-06
t is the timestamp in seconds, acc_* the measured acceleration in m/s^2, gyro_* the angular velocity in rad/s and bias_* the current bias on each axis. All three are expressed in the camera frame. Both the bias, which starts at its initial value and takes a random walk from there, and the white noise are added to the true readings before they are written out. Omit --output-file to print the table to stdout instead.
Estimating Pose¶
imu_integration turns readings back into a trajectory with forward Euler integration:
import numpy as np
from visionsim.emulate.imu import imu_integration
data = np.loadtxt("imu.csv", skiprows=1, delimiter=",")
acc_reading, gyro_reading = data[:, 1:4], data[:, 4:7]
estimated = np.array(
list(
imu_integration(
acc_pos=acc_reading,
vel_ang=gyro_reading,
pose_init=pose_init, # first ground truth pose, in world coordinates
vel_init=vel_init, # initial velocity, in world coordinates
dt=0.02,
)
)
)
It yields the pose at the start of each step, so the estimate lines up with the readings it consumed. Gravity is applied here rather than in the emulator, so it has to be passed again when the emulator was run with a non-default value. Both the noise the emulator added and the bias random walk survive into the integration, so the estimate starts on the ground truth and then drifts away from it at a rate set by the noise parameters.
Coordinate Frames¶
The pose matrices in transforms.json and transforms.db come from Blender, and they map the OpenGL camera frame (+X right, +Y up, +Z back) straight into Blender’s world axes (+Z up). They can therefore not be read as a plain rotation and translation: the rotation mixes the camera convention with the world one. emulate_imu works entirely in Blender’s convention instead, so the CLI runs every matrix through camcoord_opengl_to_blender before emulating. Pass --no-convert-coords to skip that conversion and emulate the matrices exactly as they are. When you drive the API directly, do the conversion yourself.
imu_integration instead works in the world frame: pose_init and vel_init are both given in world coordinates, and the initial velocity is rotated into the camera frame internally. Mixing the two conventions, for instance by handing the emulator poses that were already converted, makes the estimate leave the ground truth immediately.
Ground Truth vs Integrated¶
The heart of the example is the emulate-then-integrate loop:
def sim_and_integrate(db_path: Path = DB_PATH, plot_path: Path = PLOT_PATH):
dataset = Metadata.from_path(db_path)
dt = 1.0 / (dataset.fps * dataset.keyframe_scale)
poses = np.array(dataset.poses)
num_poses = len(poses)
# Convert to Blender's coordinate convention (+Z up) for both world and camera frames
poses = np.array([camcoord_opengl_to_blender(T) for T in poses])
# Interpolate poses to increase temporal resolution, the render might be 100fps, but an IMU can be 1000Hz
interp_dt = dt / INTERP_FACTOR
times = np.arange(num_poses) * dt
interp_times = np.arange(num_poses * INTERP_FACTOR) * interp_dt
interp_poses = pose_interp(poses, times)(interp_times)
_, interp_velocities_w = pose_interp(poses, times)(interp_times, order=1)
init_vel_w = interp_velocities_w[0]
print(
f"Found {num_poses} poses, which were interpolated by a factor of {INTERP_FACTOR} (dt: {dt:.4f} -> {interp_dt:.4f})"
)
print(f"Initial velocity in world frame: {init_vel_w}")
std_acc = 8e-3 * NOISE_MULT
std_bias_acc = 5.5e-5 * NOISE_MULT
std_gyro = 1.2e-3 * NOISE_MULT
std_bias_gyro = 2e-5 * NOISE_MULT
integrated_trajectories = []
# Emulate IMU data and integrate trajectories
for _ in range(NUM_SAMPLES):
imu_data = list(
emulate_imu(
interp_poses,
dt=interp_dt,
gravity=GRAVITY,
std_acc=std_acc,
std_bias_acc=std_bias_acc,
std_gyro=std_gyro,
std_bias_gyro=std_bias_gyro,
)
)
integrated_trajectories.append(
np.array(
list(
imu_integration(
acc_pos=np.array([d["acc_reading"] for d in imu_data]),
vel_ang=np.array([d["gyro_reading"] for d in imu_data]),
pose_init=interp_poses[0],
vel_init=init_vel_w, # world frame; imu_integration expresses it in the camera frame
gravity=GRAVITY,
dt=interp_dt,
)
)
)
)
# Down-interpolate to same number of poses as original
trajectories = []
for tr in integrated_trajectories:
tr_interp = pose_interp(tr, interp_times)(times)
trajectories.append(tr_interp)
# Plot the true and estimated trajectories
i, j = "xyz".index(PLANE[0]), "xyz".index(PLANE[1])
gt_positions = poses[:, :3, -1] # use original (down-sampled) ground truth
fig, ax = plt.subplots(1, 1)
ax.plot(gt_positions[:, i], gt_positions[:, j], color="k", label="Ground Truth")
for tr in trajectories:
positions = tr[:, :3, -1]
ax.plot(positions[:, i], positions[:, j])
ax.set_xlabel(PLANE[0].upper())
ax.set_ylabel(PLANE[1].upper())
ax.set_title(f"IMU-Integrated vs. Ground Truth Trajectories ({PLANE.upper()} Plane)")
ax.legend()
ax.set_aspect("equal")
fig.savefig(plot_path)
print(f"Plot saved to {plot_path}")
The example loads a dataset, interpolates its poses up to an IMU-like rate, emulates a batch of noisy measurements, integrates each batch from the same initial pose and velocity, and plots the results against the ground truth. Every run starts on the ground truth and fans out as noise accumulates. The noise is scaled by NOISE_MULT, which applies to all four noise parameters at once, so raising it makes the divergence visible over a shorter trajectory; NUM_SAMPLES sets how many estimates are drawn and PLANE which two axes they are plotted on. The paths, noise and plot settings all live in constants at the top of the file.
Run it from a checkout with the quick-start dataset in the working directory:
python examples/sensors/emulate_imu/sim_and_integrate.py
Trajectory Preview¶
The same integration drawn in three dimensions: examples/blender/imu_preview.py
integrates five trajectories from the ultramodern ground truth, draws each one as a
frustum-and-trail marker next to the ground truth, and renders the room around them.
The ground-truth camera in blue and five estimates in grey; each estimate starts on the ground truth and fans out as the bias random walk accumulates.
The script runs in the ordinary interpreter, because the integration needs scipy, and
starts examples/blender/imu_preview_service.py in its own Blender process: a
BlenderService subclass that draws
the trajectories as geometry, renders the room once for the backdrop, then renders
only the markers over a transparent film for the script to composite. Run it from a
checkout with the quick-start dataset in the working directory:
python examples/blender/imu_preview.py
The marker geometry is an asset, not code: examples/blender/mesh_camera.json holds
the geometry nodes group as a Tree Clipper export, which the service imports and
applies to a cube. Reading it needs the tree-clipper package in Blender’s own
python (pip install tree-clipper) and Blender 5.0 or newer, because Tree Clipper
uses node types that only 5.x has.
See also
Data Format and Loading for the
transforms.db/transforms.jsonthe emulator reads.