# 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.

how should I set up this parameter initially.

Then you need to decide what you want it to be. Do you want zero to be whatever direction the robot is pointing when it's powered on, or do you want zero to be magnetic North?

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.

Then, if you want zero to be magnetic North, you set the initial gyro angle to be that absolute initial heading and you're done.

If you want zero to be initial vehicle heading then you can subtract the absolute initial heading from all subsequent magnetometer readings.

• I'm looking for the "recommended" way to do this. Would most suggest to set it up a certain way? Is there an industry standard for this in robotics, or does it depend upon use case? Feb 6, 2021 at 20:29
• @StephenTaylor - It doesn't matter; the "recommended" way is the way that meets your needs. Why are you even using a heading? I would imagine it's so you can navigate to various waypoints, so then the thing to consider is how your waypoints are defined. This would be related to how your coordinate axes are arranged - if you are trying to get to a point <1, 1>, are those x- and y-axes arranged relative to magnetic North? If not then basing your heading on magnetic North isn't going to help you at all. Feb 7, 2021 at 14:38