Boussole

Test de la boussole

Le miniQ 2WD possède également un circuit permettant d'obtenir son orientation.

Télécharger la librairie ici et tester le programme ci-dessous qui doit afficher les informations du capteur.

1
/*
2
  HMC5883L Triple Axis Digital Compass. Compass Example.
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
9
#include <Wire.h>
10
#include <HMC5883L.h>
11
12
HMC5883L compass;
13
14
void setup()
15
{
16
17
  Serial.begin(9600);
18
19
  // Initialize Initialize HMC5883L
20
  Serial.println("Initialize HMC5883L");
21
  while (!compass.begin())
22
  {
23
    Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
24
    delay(500);
25
  }
26
27
  // Set measurement range
28
  compass.setRange(HMC5883L_RANGE_1_3GA);
29
30
  // Set measurement mode
31
  compass.setMeasurementMode(HMC5883L_CONTINOUS);
32
33
  // Set data rate
34
  compass.setDataRate(HMC5883L_DATARATE_30HZ);
35
36
  // Set number of samples averaged
37
  compass.setSamples(HMC5883L_SAMPLES_8);
38
39
  // Set calibration offset. See HMC5883L_calibration.ino
40
  compass.setOffset(0, 0);
41
}
42
43
void loop()
44
{
45
  Vector norm = compass.readNormalize();
46
47
  // Calculate heading
48
  float heading = atan2(norm.YAxis, norm.XAxis);
49
50
  // Set declination angle on your location and fix heading
51
  // You can find your declination on: http://magnetic-declination.com/
52
  // (+) Positive or (-) for negative
53
  // For Bytom / Poland declination angle is 4'26E (positive)
54
  // Formula: (deg + (min / 60.0)) / (180 / M_PI);
55
  float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
56
  heading += declinationAngle;
57
58
  // Correct for heading < 0deg and heading > 360deg
59
  if (heading < 0)
60
  {
61
    heading += 2 * PI;
62
  }
63
64
  if (heading > 2 * PI)
65
  {
66
    heading -= 2 * PI;
67
  }
68
69
  // Convert to degrees
70
  float headingDegrees = heading * 180/M_PI; 
71
72
  // Output
73
  Serial.print(" Heading = ");
74
  Serial.print(heading);
75
  Serial.print(" Degress = ");
76
  Serial.print(headingDegrees);
77
  Serial.println();
78
79
  delay(100);
80
81
82
83
}
84
85
86
87

Expliquer précisément le fonctionnement de celui-ci :

Réaliser un programme qui fixe le robot en permanence vers le SUD. Si une perturbation survient il tourne sur lui-même en fonction.