PX4 Firmware
PX4 Autopilot Software http://px4.io
mag.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014, 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  * @author Pavel Kirienko <pavel.kirienko@gmail.com>
36  */
37 
38 #include "mag.hpp"
39 
40 #include <drivers/drv_hrt.h>
41 #include <systemlib/err.h>
42 
43 const char *const UavcanMagnetometerBridge::NAME = "mag";
44 
46  UavcanCDevSensorBridgeBase("uavcan_mag", "/dev/uavcan/mag", MAG_BASE_DEVICE_PATH, ORB_ID(sensor_mag)),
47  _sub_mag(node),
48  _sub_mag2(node)
49 {
50  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_HMC5883; // <-- Why?
51 
52  _scale.x_scale = 1.0F;
53  _scale.y_scale = 1.0F;
54  _scale.z_scale = 1.0F;
55 }
56 
57 int
59 {
60  int res = device::CDev::init();
61 
62  if (res < 0) {
63  return res;
64  }
65 
67 
68  if (res < 0) {
69  PX4_ERR("failed to start uavcan sub: %d", res);
70  return res;
71  }
72 
74 
75  if (res < 0) {
76  PX4_ERR("failed to start uavcan sub2: %d", res);
77  return res;
78  }
79 
80  return 0;
81 }
82 
83 int
84 UavcanMagnetometerBridge::ioctl(struct file *filp, int cmd, unsigned long arg)
85 {
86  switch (cmd) {
87 
88  case MAGIOCSSCALE: {
89  std::memcpy(&_scale, reinterpret_cast<const void *>(arg), sizeof(_scale));
90  return 0;
91  }
92 
93  case MAGIOCGSCALE: {
94  std::memcpy(reinterpret_cast<void *>(arg), &_scale, sizeof(_scale));
95  return 0;
96  }
97 
98  case MAGIOCGEXTERNAL: {
99  return 1; // declare it external rise it's priority and to allow for correct orientation compensation
100  }
101 
102  case MAGIOCCALIBRATE:
103  case MAGIOCSRANGE:
104  case MAGIOCEXSTRAP: {
105  return -EINVAL;
106  }
107 
108  default: {
109  return CDev::ioctl(filp, cmd, arg);
110  }
111  }
112 }
113 
114 void
115 UavcanMagnetometerBridge::mag_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::ahrs::MagneticFieldStrength>
116  &msg)
117 {
118  lock();
119  /*
120  * FIXME HACK
121  * This code used to rely on msg.getMonotonicTimestamp().toUSec() instead of HRT.
122  * It stopped working when the time sync feature has been introduced, because it caused libuavcan
123  * to use an independent time source (based on hardware TIM5) instead of HRT.
124  * The proper solution is to be developed.
125  */
127  _report.device_id = _device_id.devid;
128  _report.is_external = true;
129 
130  _report.x = (msg.magnetic_field_ga[0] - _scale.x_offset) * _scale.x_scale;
131  _report.y = (msg.magnetic_field_ga[1] - _scale.y_offset) * _scale.y_scale;
132  _report.z = (msg.magnetic_field_ga[2] - _scale.z_offset) * _scale.z_scale;
133  unlock();
134 
135  publish(msg.getSrcNodeID().get(), &_report);
136 }
137 
138 void
140  uavcan::ReceivedDataStructure<uavcan::equipment::ahrs::MagneticFieldStrength2> &msg)
141 {
142  lock();
143  /*
144  * FIXME HACK
145  * This code used to rely on msg.getMonotonicTimestamp().toUSec() instead of HRT.
146  * It stopped working when the time sync feature has been introduced, because it caused libuavcan
147  * to use an independent time source (based on hardware TIM5) instead of HRT.
148  * The proper solution is to be developed.
149  */
151  _report.device_id = _device_id.devid;
152  _report.is_external = true;
153 
154  _report.x = (msg.magnetic_field_ga[0] - _scale.x_offset) * _scale.x_scale;
155  _report.y = (msg.magnetic_field_ga[1] - _scale.y_offset) * _scale.y_scale;
156  _report.z = (msg.magnetic_field_ga[2] - _scale.z_offset) * _scale.z_scale;
157  unlock();
158 
159  publish(msg.getSrcNodeID().get(), &_report);
160 }
#define MAGIOCGSCALE
copy the mag scaling constants to the structure pointed to by (arg)
Definition: drv_mag.h:76
#define MAGIOCGEXTERNAL
determine if mag is external or onboard
Definition: drv_mag.h:88
void mag2_sub_cb(const uavcan::ReceivedDataStructure< uavcan::equipment::ahrs::MagneticFieldStrength2 > &msg)
Definition: mag.cpp:139
#define MAGIOCEXSTRAP
excite strap
Definition: drv_mag.h:85
void lock()
Take the driver lock.
Definition: CDev.hpp:264
uavcan::Subscriber< uavcan::equipment::ahrs::MagneticFieldStrength, MagCbBinder > _sub_mag
Definition: mag.hpp:75
#define DRV_MAG_DEVTYPE_HMC5883
Sensor type definitions.
Definition: drv_sensor.h:55
sensor_mag_s _report
Definition: mag.hpp:79
UavcanMagnetometerBridge(uavcan::INode &node)
Definition: mag.cpp:45
High-resolution timer with callouts and timekeeping.
#define MAGIOCSRANGE
set the measurement range to handle (at least) arg Gauss
Definition: drv_mag.h:79
void mag_sub_cb(const uavcan::ReceivedDataStructure< uavcan::equipment::ahrs::MagneticFieldStrength > &msg)
Definition: mag.cpp:115
static char msg[NUM_MSG][CONFIG_USART1_TXBUFSIZE]
Definition: px4io.c:89
float x_offset
Definition: drv_mag.h:57
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
This is the base class for redundant sensors with an independent ORB topic per each redundancy channe...
device identifier information
Definition: Device.hpp:240
#define MAGIOCSSCALE
set the mag scaling constants to the structure pointed to by (arg)
Definition: drv_mag.h:73
#define MAG_BASE_DEVICE_PATH
Definition: drv_mag.h:47
#define MAGIOCCALIBRATE
perform self-calibration, update scale factors to canonical units
Definition: drv_mag.h:82
bool is_external
Definition: sensor_mag.h:64
float y_offset
Definition: drv_mag.h:59
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
float z_offset
Definition: drv_mag.h:61
uavcan::MethodBinder< UavcanMagnetometerBridge *, void(UavcanMagnetometerBridge::*)(const uavcan::ReceivedDataStructure< uavcan::equipment::ahrs::MagneticFieldStrength2 > &) > Mag2CbBinder
Definition: mag.hpp:73
virtual int init()
Definition: CDev.cpp:57
static const char *const NAME
Definition: mag.hpp:50
uint64_t timestamp
Definition: sensor_mag.h:53
int ioctl(struct file *filp, int cmd, unsigned long arg) override
Definition: mag.cpp:84
struct @83::@85::@87 file
uint32_t device_id
Definition: sensor_mag.h:55
uavcan::Subscriber< uavcan::equipment::ahrs::MagneticFieldStrength2, Mag2CbBinder > _sub_mag2
Definition: mag.hpp:76
uavcan::MethodBinder< UavcanMagnetometerBridge *, void(UavcanMagnetometerBridge::*)(const uavcan::ReceivedDataStructure< uavcan::equipment::ahrs::MagneticFieldStrength > &) > MagCbBinder
Definition: mag.hpp:68
int init() override
Starts the bridge.
Definition: mag.cpp:58
mag_calibration_s _scale
Definition: mag.hpp:78
void unlock()
Release the driver lock.
Definition: CDev.hpp:269
void publish(const int node_id, const void *report)
Sends one measurement into appropriate ORB topic.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).