compass heading using MPU9250 - Page 1
Forum section: electronics / beginners

author: hussamaldean
author_rank: Unknown
timestamp: August 11, 2018, 07:09:46 am
content: Hi all,I have MPU9250 and I want to use it as heading for my project which requires a heading to be entered via serialanyway, how do I convert the magnetometer values to heading values ?I tried atan2(y,x) and it didn't work at all

author: jeroen79
author_rank: Frequent Contributor
timestamp: August 11, 2018, 08:10:35 am
content: What values does the chip return for the magnetometer's X, Y and Z?Do they as expected when you rotate the chip and align each axis to the local magnetic field?How exactly does atan2 (x,y) "not work at all"?How are you testing it and what results are you getting?Atan2 (x,y) will only give valid results if you keep the Z axis vertical.If that is not the case then you need to involve the other sensors and some more math.Mind that the earth's magnetic field is not north aligned and that all sorts of global and local things influence it.You should first establish how the local magnetic field is oriented or provide a known field with some magnets.

author: hussamaldean
author_rank: Unknown
timestamp: August 11, 2018, 08:14:31 am
content: Quote from: jeroen79 on August 11, 2018, 08:10:35 amsee the red comment

author: jeroen79
author_rank: Frequent Contributor
timestamp: August 11, 2018, 09:27:53 am
content: QuoteWhen you align one axis parallel to the magnetic field it should give the maximum reading and the others should give 0.Hold one axis perpendicular to the magnetic field and rotate the chip around that same axis.Do the other axes follow a sine and cosine relative to the rotation?QuoteDoes it perhaps go from-180 to 180?If not, please be more precise.Could you post a table of the raw x, y, z values for each 30 degrees of rotation?QuoteI would find a pair of magnets to create a more controlled magnetic field.

author: hussamaldean
author_rank: Unknown
timestamp: August 11, 2018, 10:10:15 am
content: I got these datax-axis y-axis z-axis43.335868 -74.113182 16.53252647.050373 -74.997589 16.70296446.696613 -73.228775 17.04384044.750919 -73.759422 16.53252645.635326 -74.643829 17.04384045.635326 -74.643829 17.04384046.696613 -74.290061 17.04384045.458442 -73.759422 16.53252644.927799 -73.936302 16.02120944.927799 -72.875015 17.04384044.927799 -74.643829 14.31682644.927799 -74.643829 14.31682645.281562 -72.167488 16.36208746.696613 -74.290061 15.68033546.342849 -74.997589 15.68033547.404136 -74.290061 17.04384047.404136 -74.290061 17.043840and I am trying the magnet thing

author: hussamaldean
author_rank: Unknown
timestamp: August 11, 2018, 10:15:58 am
content: when I use the magnet, I get the maximum value of 5584.244140 and when I rotate the chip around the same axis, the sensor have same value and doesn't change at all

author: hussamaldean
author_rank: Unknown
timestamp: August 11, 2018, 11:04:54 am
content: Hi allI fixed the issue and I managed to get the heading correctly and the issue was the printed axis on the PCB was in correct and it was not helping me out

author: Mechatrommer
author_rank: Super Contributor
timestamp: August 11, 2018, 11:39:09 am
content: Quote from: hussamaldean on August 11, 2018, 10:10:15 amwe got lego hi technic compass sensor here. and it has calibrate method and describes how to do it... during calibrate command, rotate the sensor a little bit more than 360 degree in 2 minutes times (i'm guessing the slowness is to avoid excess vibration that will make invalid reading during data collection). i'm guessing the sensor will collect data during the process and interpolate/compensate appropriate heading angle after that. you should do the same and provide again the data while its being rotated in full rotation.Quote from: jeroen79 on August 11, 2018, 09:27:53 amif heading relative to earth is required, any man made / artificial magnetic field should be avoided. the calibration to earth pole should be conducted in any free artificial magnetic field influence. this given you are not located near north or south pole that doing calibration will give difficult result.from the data we should be able to see the maximum (or minimum) magnitude reading when sensor in parallel to the magnetic field. ymmv.Quote from: hussamaldean on August 11, 2018, 10:10:15 amedit: and can you provide data with comma separated so anybody can help you analysis in excel easily by copy pasting? 2cnts.

author: hussamaldean
author_rank: Unknown
timestamp: August 11, 2018, 11:41:09 am
content: Quote from: Mechatrommer on August 11, 2018, 11:39:09 amno needI figured out everythingthx for your help

author: jeroen79
author_rank: Frequent Contributor
timestamp: August 11, 2018, 01:28:26 pm
content: Quote from: Mechatrommer on August 11, 2018, 11:39:09 amI meant it for use on the bench when testing the sensor.When using it in the application any magnrtic disturbances must indeed be minimised.