PDA

View Full Version : Digital Compass difficulty



Badeand
15th June 2011, 02:06
Hi, can someone see whats wrong with this formula/code? the compas works perfectly for North, south, east, west, but not the values inbetween. its very inaccurate in between.


float Instrument::bearing(QPointF *src, QPointF *dest) {

float dLon = dest->y() - src->y();

float y = sin(dLon) * cos(dest->x());
float x = cos(src->x()) * sin(dest->x()) - sin(src->x()) * cos(dest->x()) * cos(dLon);
float brng = atan2(x, y);


qDebug() << brng << "BRNG !!!!!!!!!!!!!!!!!!!";
brng *= 180/(4.0*atan(1.0)); // 180/pi
int deg = (((int)brng + 360) % 360);

return deg;

Please help =)

thanks,
Badeand

Added after 49 minutes:

Sorry guys I fixed it =)

This did the trick:


float Instrument::bearing(QPointF *src, QPointF *dest) {


qreal y = dest->y() - src->y();
qreal x = (dest->x() - src->x());
qreal brng = atan2(x, y);


qDebug() << brng << "BRNG !!!!!!!!!!!!!!!!!!!";
brng *= 180/(4.0*atan(1.0)); // 180/pi
int deg = (((int)brng + 360) % 360);

return deg;
}

love,
Badeand

Rachol
16th June 2011, 13:50
Due to the new functionality of the forum showing on the main page posts that were not replied (that means "not solved"), I assume that this one doesn't really belong there and therefore I write a post reply in order to make it rest in peace :)