# Simulate IMU (2D gyro and accelerometer) data

If I have a robot path in 2D space,

i.e. a vector of (x,y) locations, and I need to generate artificial IMU data (simulate them), how would I go about it?

How do I model equations to generate the values given a time frame and positions?

I've come across imusim I'd like to know how to model them and generate using Matlab or something similar.

• are you trying to simulate robot behaviour in a graphical environment. or are you asking because you want to simulate hardware on robots where those devices aren't present? Commented Apr 1, 2015 at 7:01
• latter. Don't have a physical IMU yet but need to simulate those. 1) Need to generate a fake path/trajectory (x,y,phi) 2) Using those, need to generate IMU data.
– Raaj
Commented Apr 1, 2015 at 15:20

This is really simple. First of all, you need to understand how the sensor works. In other words, you need understand whether the measurements is coming from linear or nonlinear model. Second, what is the type of the sensor's noise?

CASE STUDY: Let's say you want to simulate DC Voltemeter to measure a battery's voltage of 5 Volt. In an ideal case, the model of the system is

$$V_{b}(k+1) = V_{b}(k)$$ It is just a constant value whether you measure it now or in the future, the value is 5 Volt. In the reality, this is not the case. If you measure it now, you might get 4.9 Volt, later on 5.1 Volt. The measurements vary which means the sensor is noisy. In many cases, the noise is Gaussian with zero mean and some variance (i.e. $\mathcal{N}(0, \sigma^{2}))$. Therefore, the measurement model is

$$Z(k+1) = V_{b}(k+1) + \delta$$

where $\delta$ is the measurement noise (i.e. $\mathcal{N}(0, \sigma^{2})$).

To simulate this scenario in Matlab,

clear all; clc;
ideal_value = 5;
sigma = 0.01;
real_value = ideal_value + sigma*randn(5,1)


The output is then

real_value =
5.0009
5.0173
4.9939
4.9926
4.9825


In reality, there is no way to know the ideal value. If you want to decrease the accuracy of your sensor, you need to increase the value of the $\sigma$. In 2D laser scenario, the input of the sensor is the Cartesian location ($x, y$) and the output is the polar coordinates ($r, \phi$) with some Gaussian noise. Let's say we have a 2D laser sensor that is located in the origin and we have a tree in planar environment. The sensor with update rate 10 Hz provides the range $r$ and the bearing $\phi$ to the tree with some noise in both the range and the bearing. Therefore the following code simulates the scenario

clear all
clc

tree_x = 4;
tree_y = 4;

sigma_r = 0.1; % m

dt = 1/10;

t = 0:dt:0.5;
for i = 1:numel(t)
r(i) = sqrt( tree_x^2 + tree_y^2 ) + sigma_r*randn();
p(i) = atan2( tree_y, tree_x ) + sigma_p*randn();
end
r


The results are

r =
5.7598    5.7581    5.6356    5.5525    5.6130    5.7552
p =
44.6549   45.6293   44.1343   44.7299   44.5913   44.7023

• i'm glad you included noise in your equations. Commented Apr 1, 2015 at 7:03
• Hi CroCo, I had a vague idea about this and looks like I was right about the for loop at least. Your answer clears a lot of doubts. Let me explain my problem. I'm trying to set up a synthetic environment for SLAM. I don't have a state space (robot poses) yet, and I don't have IMU data yet. Am I right in understanding that >> I have to generate a sequence of (x,y,theta) and then >> between adjacent (x,y,theta) values, activate my IMU If that's correct, you've helped me answer 2nd question. For the 1st, should I use distance formula and rotation matrix/ generate trajectory somehow?
– Raaj
Commented Apr 1, 2015 at 15:35
• @Raaj, would you please state what you are trying to do exactly? If you are interested in building SLAM for 2D scenario, read this book "Probabilistic Robotics ". The measurement model for 2D laser is provided in depth. Commented Apr 1, 2015 at 20:54
• @CroCo , Trying to implement this ---- www-cs.stanford.edu/people/dstavens/icra11/… Since I don't have a robot, or IMU, I'm trying to generate synthetic data to implement the paper: 1) Robot pose sequence, or a trajectory that has loop closures (returns to the same spot as it started). 2) Generate IMU data for the pose sequence. After this, I can get to understanding and implementing the SLAM algorithm presented in the link :)
– Raaj
Commented Apr 1, 2015 at 22:20
• @Raaj, read this book "Probabilistic Robotics". This is the way I learned SLAM. Commented Apr 7, 2015 at 23:31