# How to set initial heading

I'm confused how to set up an initial heading for my mobile robot project. I've determined heading from gyro integration and also using magnetometer readings. Gyro initializes at 0 and magnetometer initializes to a random value between -180 and 180 based on direction of the imu.

My question is how should I set up this parameter initially. I plan keep the range between -pi and pi, but do i want to use the global magnetic field as my reference and navigate based upon this, or do i want to offset that value to agree with the gyro at startup?

Thanks for any help!

magnetometer initializes to a random value between -180 and 180

It's not a random value, it's

based on direction of the imu.

Either way, you need to grab the magnetometer output on startup (or maybe average the first five or ten samples.) Store it in a variable called absoluteInitialHeading.