I am having troubles with tilt compensating my compass.
I am using the LSM303DLHC accelerometer/compass in combination together with the Adafruit_LSM303_U.h.
I was able to get my compass to the right direction withthis little snippet:
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
I also was able to get my roll and tilt angle with converting my X and Y of my accelerometer to angles:
headingNew = asin(eventA.acceleration.x/10); float tilt = headingNew*180/PI/90*100; headingNew = asin(eventA.acceleration.y/10); float roll = headingNew*180/PI/90*100;
Now I need to compensate my angle to get a new x and y for inside my heading calculation but the problem is that I can't seem to get the right functions. Online I found the functions below from a large tutorial about the sensor but this gives me completely wrong values.
//Xh = event.magnetic.x*cos(tilt)+event.magnetic.z*sin(tilt); //Yh = event.magnetic.x*sin(roll)*sin(tilt)+event.magnetic.y*cos(roll)-event.magnetic.z*sin(roll)*cos(tilt);
or in the code itself:
Xh = event.magnetic.x*(eventA.acceleration.z/10)+event.magnetic.z*(eventA.acceleration.x/10); Yh = event.magnetic.x*(eventA.acceleration.y/10)*(eventA.acceleration.x/10)+event.magnetic.y*(eventA.acceleration.z/10)-event.magnetic.z*(eventA.acceleration.y/10)*(eventA.acceleration.z/10);
I actually get the right angle on a flat surface. But it is even more inaccurate at an angle as the normal heading. Does anybody have any experience with this sensor? Already a big thanks for the help.