SOLVED MPU9150,how to add (show) N,E,S,W on the compass in the MPU9150.CPP file

Hello
I wonder,the MPU9150 is working good, Roll,Pitch,Heading is working fine,but i need to add,North,East,South,West on the Heading.
I dont now how to add this into MPU9150.CPP file,I have found this on the web:

:

#ifndef ??????????

  if((angle < 22.5) || (angle > 337.5 ))
    OSD.setCursor(6,5);
    OSD.print("South");
  if((angle > 22.5) && (angle < 67.5 ))
    OSD.setCursor(6,5);
    OSD.print("South-West");
  if((angle > 67.5) && (angle < 112.5 ))
    OSD.setCursor(6,5);
    OSD.print("West");
  if((angle > 112.5) && (angle < 157.5 ))
    OSD.setCursor(6,5);
    OSD.print("North-West");
  if((angle > 157.5) && (angle < 202.5 ))
    OSD.setCursor(6,5);
    OSD.print("North");
  if((angle > 202.5) && (angle < 247.5 ))
    OSD.setCursor(6,5);
    OSD.print("NorthEast");
  if((angle > 247.5) && (angle < 292.5 ))
    OSD.setCursor(6,5);
    OSD.print("East");
  if((angle > 292.5) && (angle < 337.5 ))
    OSD.setCursor(6,5);
    OSD.print("SouthEast");
#endif
return;

At the end,in the MPU9150.CPP file its stand:

void MPU9150Lib::printAngles(float *vec)
{ while(PINA&2);
  OSD.setCursor(3,3);
  OSD.print("ROLL:"); OSD.print(vec[VEC3_X] * RAD_TO_DEGREE,1);

  OSD.setCursor(3,4);  
  OSD.print("PITCH:"); OSD.print(vec[VEC3_Y] * RAD_TO_DEGREE,1);
 
  OSD.setCursor(3,5);
  OSD.print("HEADING:"); OSD.print(vec[VEC3_Z] * RAD_TO_DEGREE + 180,1);   
while(PINA&2); 
}

I hope someone can help me,to add this to the MPU9150.CPP file :slight_smile:

MPU9150Lib.cpp (15.7 KB)

you might find this interesting: - http://forum.arduino.cc/index.php?topic=94507.0 -

Hello

I have now solved this problem :slight_smile: I add this to the MPU9150Lib.cpp file:

OSD.setCursor(3,5);
  OSD.print("HEADING:"); OSD.print(vec[VEC3_Z] * RAD_TO_DEGREE + 180,1);
OSD.setCursor(17,5);
if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 22.5) || (vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 337.5 ))
OSD.print("NORD");

if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 22.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 67.5 ))
OSD.print("NORD-QST");

  if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 67.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 112.5 ))
OSD.print("QST");

if((vec[VEC3_Z] * RAD_TO_DEGREE + 180  > 112.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180  < 157.5 ))
OSD.print("SQR-QST");

  if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 157.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 202.5 ))
OSD.print("SQR");

if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 202.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 247.5 ))
OSD.print("SQR-VEST");

  if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 247.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 292.5 ))
OSD.print("VEST");

if((vec[VEC3_Z] * RAD_TO_DEGREE + 180 > 292.5) && (vec[VEC3_Z] * RAD_TO_DEGREE + 180 < 337.5 ))
OSD.print("NORD-VEST");
  while(PINA&2); 
   }

link to movie:

Your code can be simplified a bit. Check below

  • remove duplicate code
  • remove unneeded compares
  • replace float compares by int compares
OSD.setCursor(3,5);

  OSD.print("HEADING:"); 
  OSD.print(vec[VEC3_Z] * RAD_TO_DEGREE + 180,1);
  OSD.setCursor(17,5);

int degrees = vec[VEC3_Z] * RAD_TO_DEGREE + 180 - 22.5;

if ((degrees  < 0) || (degrees > 315 )) OSD.print("NORD");
else if(degrees < 45 )) OSD.print("NORD-QST");
else if(degrees < 90) OSD.print("QST");
else if(degrees  < 135) OSD.print("SQR-QST");
else if (degrees < 180) OSD.print("SQR");
else if (degrees < 225) OSD.print("SQR-VEST");
else if (degrees < 270) OSD.print("VEST");
else OSD.print("NORD-VEST");
  while(PINA&2);

I have now test your code,and is working,your code is much easier.thank you :slight_smile:

OSD.setCursor(3,5);
OSD.print("HEADING:"); 
  OSD.print(vec[VEC3_Z] * RAD_TO_DEGREE + 180,1);
  OSD.setCursor(17,5);

int degrees = vec[VEC3_Z] * RAD_TO_DEGREE + 180 - 22.5;

if ((degrees  < 0) || (degrees > 315 )) OSD.print("NORD");
else if(degrees < 45 ) OSD.print("NORD-QST");
else if(degrees < 90) OSD.print("QST");
else if(degrees  < 135) OSD.print("SQR-QST");
else if (degrees < 180) OSD.print("SQR");
else if (degrees < 225) OSD.print("SQR-VEST");
else if (degrees < 270) OSD.print("VEST");
else OSD.print("NORD-VEST");
  while(PINA&2);