Processing.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. HMC5883L Triple Axis Digital Compass. Output for HMC5883L_processing.pde
  3. Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-magnetometr-hmc5883l.html
  4. GIT: https://github.com/jarzebski/Arduino-HMC5883L
  5. Web: http://www.jarzebski.pl
  6. (c) 2014 by Korneliusz Jarzebski
  7. */
  8. #include <Wire.h>
  9. #include <HMC5883L.h>
  10. HMC5883L compass;
  11. int previousDegree;
  12. void setup()
  13. {
  14. Serial.begin(9600);
  15. // Initialize HMC5883L
  16. while (!compass.begin())
  17. {
  18. delay(500);
  19. }
  20. // Set measurement range
  21. compass.setRange(HMC5883L_RANGE_1_3GA);
  22. // Set measurement mode
  23. compass.setMeasurementMode(HMC5883L_CONTINOUS);
  24. // Set data rate
  25. compass.setDataRate(HMC5883L_DATARATE_30HZ);
  26. // Set number of samples averaged
  27. compass.setSamples(HMC5883L_SAMPLES_8);
  28. // Set calibration offset. See HMC5883L_calibration.ino
  29. compass.setOffset(0, 0);
  30. }
  31. void loop()
  32. {
  33. long x = micros();
  34. Vector norm = compass.readNormalize();
  35. // Calculate heading
  36. float heading = atan2(norm.YAxis, norm.XAxis);
  37. // Set declination angle on your location and fix heading
  38. // You can find your declination on: http://magnetic-declination.com/
  39. // (+) Positive or (-) for negative
  40. // For Bytom / Poland declination angle is 4'26E (positive)
  41. // Formula: (deg + (min / 60.0)) / (180 / M_PI);
  42. float declinationAngle = (2.0 + (28.0 / 60.0)) / (180 / M_PI);
  43. heading += declinationAngle;
  44. // Correct for heading < 0deg and heading > 360deg
  45. if (heading < 0)
  46. {
  47. heading += 2 * PI;
  48. }
  49. if (heading > 2 * PI)
  50. {
  51. heading -= 2 * PI;
  52. }
  53. // Convert to degrees
  54. float headingDegrees = heading * 180/M_PI;
  55. // Fix HMC5883L issue with angles
  56. float fixedHeadingDegrees;
  57. if (headingDegrees >= 1 && headingDegrees < 240)
  58. {
  59. fixedHeadingDegrees = map(headingDegrees, 0, 239, 0, 179);
  60. } else
  61. if (headingDegrees >= 240)
  62. {
  63. fixedHeadingDegrees = map(headingDegrees, 240, 360, 180, 360);
  64. }
  65. // Smooth angles rotation for +/- 3deg
  66. int smoothHeadingDegrees = round(fixedHeadingDegrees);
  67. if (smoothHeadingDegrees < (previousDegree + 3) && smoothHeadingDegrees > (previousDegree - 3))
  68. {
  69. smoothHeadingDegrees = previousDegree;
  70. }
  71. previousDegree = smoothHeadingDegrees;
  72. // Output
  73. Serial.print(norm.XAxis);
  74. Serial.print(":");
  75. Serial.print(norm.YAxis);
  76. Serial.print(":");
  77. Serial.print(norm.ZAxis);
  78. Serial.print(":");
  79. Serial.print(headingDegrees);
  80. Serial.print(":");
  81. Serial.print(fixedHeadingDegrees);
  82. Serial.print(":");
  83. Serial.print(smoothHeadingDegrees);
  84. Serial.println();
  85. // One loop: ~5ms @ 115200 serial.
  86. // We need delay ~28ms for allow data rate 30Hz (~33ms)
  87. delay(30);
  88. }