APM:Libraries
AP_Compass_test.cpp
Go to the documentation of this file.
1 /*
2  * Example of APM_Compass library (HMC5843 sensor).
3  * Code by Jordi MuĂ’oz and Jose Julio. DIYDrones.com
4  */
5 
7 #include <AP_HAL/AP_HAL.h>
9 
11 
14 
15 uint32_t timer;
16 
17 static void setup()
18 {
19  hal.console->printf("Compass library test\n");
20 
21  board_config.init();
22 
23  if (!compass.init()) {
24  AP_HAL::panic("compass initialisation failed!");
25  }
26  hal.console->printf("init done - %u compasses detected\n", compass.get_count());
27 
28  // set offsets to account for surrounding interference
29  compass.set_and_save_offsets(0, 0, 0, 0);
30  // set local difference between magnetic north and true north
31  compass.set_declination(ToRad(0.0f));
32 
33  hal.scheduler->delay(1000);
35 }
36 
37 static void loop()
38 {
39  static const uint8_t compass_count = compass.get_count();
40  static float min[COMPASS_MAX_INSTANCES][3];
41  static float max[COMPASS_MAX_INSTANCES][3];
42  static float offset[COMPASS_MAX_INSTANCES][3];
43 
44  compass.accumulate();
45 
46  if ((AP_HAL::micros() - timer) > 100000L) {
48  compass.read();
49  const uint32_t read_time = AP_HAL::micros() - timer;
50 
51  for (uint8_t i = 0; i < compass_count; i++) {
52  float heading;
53 
54  hal.console->printf("Compass #%u: ", i);
55 
56  if (!compass.healthy()) {
57  hal.console->printf("not healthy\n");
58  continue;
59  }
60 
61  Matrix3f dcm_matrix;
62  // use roll = 0, pitch = 0 for this example
63  dcm_matrix.from_euler(0, 0, 0);
64  heading = compass.calculate_heading(dcm_matrix, i);
65 
66  const Vector3f &mag = compass.get_field(i);
67 
68  // capture min
69  min[i][0] = MIN(mag.x, min[i][0]);
70  min[i][1] = MIN(mag.y, min[i][1]);
71  min[i][2] = MIN(mag.z, min[i][2]);
72 
73  // capture max
74  max[i][0] = MAX(mag.x, max[i][0]);
75  max[i][1] = MAX(mag.y, max[i][1]);
76  max[i][2] = MAX(mag.z, max[i][2]);
77 
78  // calculate offsets
79  offset[i][0] = -(max[i][0] + min[i][0]) / 2;
80  offset[i][1] = -(max[i][1] + min[i][1]) / 2;
81  offset[i][2] = -(max[i][2] + min[i][2]) / 2;
82 
83  // display all to user
84  hal.console->printf("Heading: %.2f (%3d, %3d, %3d)",
85  (double)ToDeg(heading),
86  (int)mag.x,
87  (int)mag.y,
88  (int)mag.z);
89 
90  // display offsets
91  hal.console->printf(" offsets(%.2f, %.2f, %.2f)",
92  (double)offset[i][0],
93  (double)offset[i][1],
94  (double)offset[i][2]);
95 
96  hal.console->printf(" t=%u", (unsigned)read_time);
97 
98  hal.console->printf("\n");
99  }
100  } else {
101  hal.scheduler->delay(1);
102  }
103 }
104 
105 AP_HAL_MAIN();
AP_HAL::UARTDriver * console
Definition: HAL.h:110
#define ToRad(x)
Definition: AP_Common.h:53
static int max(int a, int b)
Definition: compat.h:35
static void setup()
bool init()
Definition: AP_Compass.cpp:487
void set_and_save_offsets(uint8_t i, const Vector3f &offsets)
void from_euler(float roll, float pitch, float yaw)
Definition: matrix3.cpp:26
void accumulate()
Definition: AP_Compass.cpp:970
bool read()
Definition: AP_Compass.cpp:979
uint32_t timer
#define MIN(a, b)
Definition: usb_conf.h:215
#define ToDeg(x)
Definition: AP_Common.h:54
static auto MAX(const A &one, const B &two) -> decltype(one > two ? one :two)
Definition: AP_Math.h:202
static Compass compass
float calculate_heading(const Matrix3f &dcm_matrix) const
Definition: AP_Compass.h:87
const Vector3f & get_field(uint8_t i) const
Return the current field as a Vector3f in milligauss.
Definition: AP_Compass.h:122
virtual void delay(uint16_t ms)=0
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
#define f(i)
T y
Definition: vector3.h:67
AP_HAL_MAIN()
static void loop()
T z
Definition: vector3.h:67
bool healthy[COMPASS_MAX_INSTANCES]
Definition: AP_Compass.h:301
const HAL & get_HAL()
uint8_t get_count(void) const
Definition: AP_Compass.h:119
void set_declination(float radians, bool save_to_eeprom=true)
#define COMPASS_MAX_INSTANCES
Definition: AP_Compass.h:46
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140
static AP_BoardConfig board_config
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
uint32_t micros()
Definition: system.cpp:152
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114
T x
Definition: vector3.h:67