PX4 Firmware
PX4 Autopilot Software http://px4.io
hmc5883_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file hmc5883.cpp
36  *
37  * Driver for the HMC5883 / HMC5983 magnetometer connected via I2C or SPI.
38  */
39 
40 #include <px4_platform_common/getopt.h>
41 
42 #include "HMC5883.hpp"
43 #include "hmc5883.h"
44 
45 /*
46  * Driver 'main' command.
47  */
48 extern "C" __EXPORT int hmc5883_main(int argc, char *argv[]);
49 
55 };
56 
57 /**
58  * Local functions in support of the shell command.
59  */
60 namespace hmc5883
61 {
62 
63 /*
64  list of supported bus configurations
65  */
68  const char *devpath;
70  uint8_t busnum;
72 } bus_options[] = {
73  { HMC5883_BUS_I2C_EXTERNAL, "/dev/hmc5883_ext", &HMC5883_I2C_interface, PX4_I2C_BUS_EXPANSION, NULL },
74 #ifdef PX4_I2C_BUS_EXPANSION1
75  { HMC5883_BUS_I2C_EXTERNAL, "/dev/hmc5883_ext1", &HMC5883_I2C_interface, PX4_I2C_BUS_EXPANSION1, NULL },
76 #endif
77 #ifdef PX4_I2C_BUS_EXPANSION2
78  { HMC5883_BUS_I2C_EXTERNAL, "/dev/hmc5883_ext2", &HMC5883_I2C_interface, PX4_I2C_BUS_EXPANSION2, NULL },
79 #endif
80 #ifdef PX4_I2C_BUS_ONBOARD
81  { HMC5883_BUS_I2C_INTERNAL, "/dev/hmc5883_int", &HMC5883_I2C_interface, PX4_I2C_BUS_ONBOARD, NULL },
82 #endif
83 #ifdef PX4_SPIDEV_HMC
84  { HMC5883_BUS_SPI, "/dev/hmc5883_spi", &HMC5883_SPI_interface, PX4_SPI_BUS_SENSORS, NULL },
85 #endif
86 };
87 #define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
88 
89 /**
90  * start driver for a specific bus option
91  */
92 static bool
93 start_bus(struct hmc5883_bus_option &bus, enum Rotation rotation)
94 {
95  if (bus.dev != nullptr) {
96  PX4_ERR("bus option already started");
97  }
98 
99  device::Device *interface = bus.interface_constructor(bus.busnum);
100 
101  if (interface->init() != OK) {
102  delete interface;
103  PX4_WARN("no device on bus %u (type: %u)", (unsigned)bus.busnum, (unsigned)bus.busid);
104  return false;
105  }
106 
107  bus.dev = new HMC5883(interface, bus.devpath, rotation);
108 
109  if (bus.dev != nullptr && OK != bus.dev->init()) {
110  delete bus.dev;
111  bus.dev = NULL;
112  return false;
113  }
114 
115  int fd = px4_open(bus.devpath, O_RDONLY);
116 
117  if (fd < 0) {
118  return false;
119  }
120 
122  px4_close(fd);
123  PX4_ERR("Failed to setup poll rate");
124  return false;
125  }
126 
127  px4_close(fd);
128 
129  return true;
130 }
131 
132 /**
133  * Start the driver.
134  *
135  * This function call only returns once the driver
136  * is either successfully up and running or failed to start.
137  */
138 static int
139 start(enum HMC5883_BUS busid, enum Rotation rotation)
140 {
141  bool started = false;
142 
143  for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
144  if (busid == HMC5883_BUS_ALL && bus_options[i].dev != NULL) {
145  // this device is already started
146  continue;
147  }
148 
149  if (busid != HMC5883_BUS_ALL && bus_options[i].busid != busid) {
150  // not the one that is asked for
151  continue;
152  }
153 
154  started |= start_bus(bus_options[i], rotation);
155  }
156 
157  if (!started) {
158  return PX4_ERROR;
159  }
160 
161  return PX4_OK;
162 }
163 
164 static int
166 {
167  bool stopped = false;
168 
169  for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
170  if (bus_options[i].dev != nullptr) {
171  bus_options[i].dev->stop();
172  delete bus_options[i].dev;
173  bus_options[i].dev = nullptr;
174  stopped = true;
175  }
176  }
177 
178  return !stopped;
179 }
180 
181 /**
182  * find a bus structure for a busid
183  */
184 static struct hmc5883_bus_option *
186 {
187  for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
188  if ((busid == HMC5883_BUS_ALL ||
189  busid == bus_options[i].busid) && bus_options[i].dev != NULL) {
190  return &bus_options[i];
191  }
192  }
193 
194  PX4_ERR("bus %u not started", (unsigned)busid);
195  return nullptr;
196 }
197 
198 /**
199  * Automatic scale calibration.
200  *
201  * Basic idea:
202  *
203  * output = (ext field +- 1.1 Ga self-test) * scale factor
204  *
205  * and consequently:
206  *
207  * 1.1 Ga = (excited - normal) * scale factor
208  * scale factor = (excited - normal) / 1.1 Ga
209  *
210  * sxy = (excited - normal) / 766 | for conf reg. B set to 0x60 / Gain = 3
211  * sz = (excited - normal) / 713 | for conf reg. B set to 0x60 / Gain = 3
212  *
213  * By subtracting the non-excited measurement the pure 1.1 Ga reading
214  * can be extracted and the sensitivity of all axes can be matched.
215  *
216  * SELF TEST OPERATION
217  * To check the HMC5883L for proper operation, a self test feature in incorporated
218  * in which the sensor offset straps are excited to create a nominal field strength
219  * (bias field) to be measured. To implement self test, the least significant bits
220  * (MS1 and MS0) of configuration register A are changed from 00 to 01 (positive bias)
221  * or 10 (negetive bias), e.g. 0x11 or 0x12.
222  * Then, by placing the mode register into single-measurement mode (0x01),
223  * two data acquisition cycles will be made on each magnetic vector.
224  * The first acquisition will be a set pulse followed shortly by measurement
225  * data of the external field. The second acquisition will have the offset strap
226  * excited (about 10 mA) in the positive bias mode for X, Y, and Z axes to create
227  * about a ±1.1 gauss self test field plus the external field. The first acquisition
228  * values will be subtracted from the second acquisition, and the net measurement
229  * will be placed into the data output registers.
230  * Since self test adds ~1.1 Gauss additional field to the existing field strength,
231  * using a reduced gain setting prevents sensor from being saturated and data registers
232  * overflowed. For example, if the configuration register B is set to 0x60 (Gain=3),
233  * values around +766 LSB (1.16 Ga * 660 LSB/Ga) will be placed in the X and Y data
234  * output registers and around +713 (1.08 Ga * 660 LSB/Ga) will be placed in Z data
235  * output register. To leave the self test mode, change MS1 and MS0 bit of the
236  * configuration register A back to 00 (Normal Measurement Mode), e.g. 0x10.
237  * Using the self test method described above, the user can scale sensor
238  */
239 static int calibrate(enum HMC5883_BUS busid)
240 {
241  struct hmc5883_bus_option *bus = find_bus(busid);
242 
243  if (bus == nullptr) {
244  return 0;
245  }
246 
247  const char *path = bus->devpath;
248 
249  int fd = px4_open(path, O_RDONLY);
250 
251  if (fd < 0) {
252  PX4_ERR("%s open failed (try 'hmc5883 start' if the driver is not running", path);
253  return PX4_ERROR;
254  }
255 
256  int ret = 0;
257 
258  if (OK != (ret = px4_ioctl(fd, MAGIOCCALIBRATE, fd))) {
259  PX4_ERR("failed to enable sensor calibration mode");
260  }
261 
262  px4_close(fd);
263 
264  return ret;
265 }
266 
267 /**
268  * enable/disable temperature compensation
269  */
270 static int
271 temp_enable(enum HMC5883_BUS busid, bool enable)
272 {
273  struct hmc5883_bus_option *bus = find_bus(busid);
274 
275  if (bus == nullptr) {
276  return 0;
277  }
278 
279  const char *path = bus->devpath;
280 
281  int fd = px4_open(path, O_RDONLY);
282 
283  if (fd < 0) {
284  PX4_ERR("failed ");
285  return PX4_ERROR;
286  }
287 
288  if (px4_ioctl(fd, MAGIOCSTEMPCOMP, (unsigned)enable) < 0) {
289  PX4_ERR("set temperature compensation failed");
290  return PX4_ERROR;
291  }
292 
293  px4_close(fd);
294  return 0;
295 }
296 
297 /**
298  * Print a little info about the driver.
299  */
300 static int
302 {
303  struct hmc5883_bus_option *bus = find_bus(busid);
304 
305  if (bus == nullptr) {
306  return 0;
307  }
308 
309  if (bus != nullptr) {
310  PX4_INFO("running on bus: %u (%s)\n", (unsigned)bus->busid, bus->devpath);
311  bus->dev->print_info();
312  }
313 
314  return PX4_OK;
315 }
316 
317 static int
319 {
320  warnx("missing command: try 'start', 'info', 'calibrate'");
321  warnx("options:");
322  warnx(" -R rotation");
323  warnx(" -C calibrate on start");
324  warnx(" -X only external bus");
325 #if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_HMC)
326  warnx(" -I only internal bus");
327 #endif
328  return PX4_OK;
329 }
330 
331 } // namespace
332 
333 int
334 hmc5883_main(int argc, char *argv[])
335 {
336  int myoptind = 1;
337  int ch;
338  const char *myoptarg = nullptr;
339 
341  enum Rotation rotation = ROTATION_NONE;
342  bool calibrate = false;
343  bool temp_compensation = false;
344 
345  if (argc < 2) {
346  return hmc5883::usage();
347  }
348 
349  while ((ch = px4_getopt(argc, argv, "XISR:CT", &myoptind, &myoptarg)) != EOF) {
350  switch (ch) {
351  case 'R':
352  rotation = (enum Rotation)atoi(myoptarg);
353  break;
354 #if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_HMC)
355 
356  case 'I':
357  busid = HMC5883_BUS_I2C_INTERNAL;
358  break;
359 #endif
360 
361  case 'X':
362  busid = HMC5883_BUS_I2C_EXTERNAL;
363  break;
364 
365  case 'S':
366  busid = HMC5883_BUS_SPI;
367  break;
368 
369  case 'C':
370  calibrate = true;
371  break;
372 
373  case 'T':
374  temp_compensation = true;
375  break;
376 
377  default:
378  return hmc5883::usage();
379  }
380  }
381 
382  if (myoptind >= argc) {
383  return hmc5883::usage();
384  }
385 
386  const char *verb = argv[myoptind];
387 
388  /*
389  * Start/load the driver.
390  */
391  if (!strcmp(verb, "start")) {
392  hmc5883::start(busid, rotation);
393 
394  if (calibrate && hmc5883::calibrate(busid) != 0) {
395  PX4_ERR("calibration failed");
396  return -1;
397  }
398 
399  if (temp_compensation) {
400  // we consider failing to setup temperature
401  // compensation as non-fatal
402  hmc5883::temp_enable(busid, true);
403  }
404 
405  return 0;
406  }
407 
408  /*
409  * Stop the driver
410  */
411  if (!strcmp(verb, "stop")) {
412  return hmc5883::stop();
413  }
414 
415  /*
416  * enable/disable temperature compensation
417  */
418  if (!strcmp(verb, "tempoff")) {
419  return hmc5883::temp_enable(busid, false);
420  }
421 
422  if (!strcmp(verb, "tempon")) {
423  return hmc5883::temp_enable(busid, true);
424  }
425 
426  /*
427  * Print driver information.
428  */
429  if (!strcmp(verb, "info") || !strcmp(verb, "status")) {
430  return hmc5883::info(busid);
431  }
432 
433  /*
434  * Autocalibrate the scaling
435  */
436  if (!strcmp(verb, "calibrate")) {
437  if (hmc5883::calibrate(busid) == 0) {
438  PX4_INFO("calibration successful");
439  return 0;
440 
441  } else {
442  PX4_ERR("calibration failed");
443  return -1;
444  }
445  }
446 
447  return hmc5883::usage();
448 }
void print_info()
Diagnostics - print some basic information about the driver.
Definition: HMC5883.cpp:1014
static int info(enum HMC5883_BUS busid)
Print a little info about the driver.
#define SENSOR_POLLRATE_DEFAULT
poll at driver normal rate
Definition: drv_sensor.h:136
static int stop()
Definition: I2C.hpp:51
static int start(enum HMC5883_BUS busid, enum Rotation rotation)
Start the driver.
__EXPORT int hmc5883_main(int argc, char *argv[])
static int usage()
static bool start_bus(struct hmc5883_bus_option &bus, enum Rotation rotation)
start driver for a specific bus option
device::Device *(* HMC5883_constructor)(int)
Definition: hmc5883.h:55
#define SENSORIOCSPOLLRATE
Set the driver polling rate to (arg) Hz, or one of the SENSOR_POLLRATE constants. ...
Definition: drv_sensor.h:134
HMC5883_constructor interface_constructor
static int temp_enable(enum HMC5883_BUS busid, bool enable)
enable/disable temperature compensation
device::Device * HMC5883_I2C_interface(int bus)
Definition: hmc5883_i2c.cpp:68
#define MAGIOCCALIBRATE
perform self-calibration, update scale factors to canonical units
Definition: drv_mag.h:82
device::Device * HMC5883_SPI_interface(int bus)
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
#define warnx(...)
Definition: err.h:95
#define NUM_BUS_OPTIONS
HMC5883_BUS
int fd
Definition: dataman.cpp:146
Shared defines for the hmc5883 driver.
static struct hmc5883_bus_option * find_bus(enum HMC5883_BUS busid)
find a bus structure for a busid
int px4_open(const char *path, int flags,...)
struct hmc5883::hmc5883_bus_option bus_options[]
#define MAGIOCSTEMPCOMP
enable/disable temperature compensation
Definition: drv_mag.h:91
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OK
Definition: uavcan_main.cpp:71
static int calibrate(enum HMC5883_BUS busid)
Automatic scale calibration.
void stop()
Stop the automatic measurement state machine.
Definition: HMC5883.cpp:403
Local functions in support of the shell command.
int px4_close(int fd)
int px4_ioctl(int fd, int cmd, unsigned long arg)