From 1524ff7f80ca9eebc8b90dc92bc7e09a17fafc7f Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Mon, 15 Feb 2016 13:43:21 +0100 Subject: [PATCH] HMC5883: added wrapper for DriverFramework --- cmake/configs/qurt_eagle_default.cmake | 7 + posix-configs/eagle/flight/px4-flight.config | 1 + src/drivers/gps/CMakeLists.txt | 2 +- .../drivers/df_hmc5883_wrapper/CMakeLists.txt | 46 +++ .../df_hmc5883_wrapper/df_hmc5883_wrapper.cpp | 333 ++++++++++++++++++ 5 files changed, 388 insertions(+), 1 deletion(-) create mode 100644 src/platforms/posix/drivers/df_hmc5883_wrapper/CMakeLists.txt create mode 100644 src/platforms/posix/drivers/df_hmc5883_wrapper/df_hmc5883_wrapper.cpp diff --git a/cmake/configs/qurt_eagle_default.cmake b/cmake/configs/qurt_eagle_default.cmake index e5bc74e0e7..3c6b13e7f8 100644 --- a/cmake/configs/qurt_eagle_default.cmake +++ b/cmake/configs/qurt_eagle_default.cmake @@ -39,6 +39,7 @@ set(config_module_list # $(EAGLE_DRIVERS_SRC)/csr_gps platforms/posix/drivers/df_mpu9250_wrapper platforms/posix/drivers/df_bmp280_wrapper + platforms/posix/drivers/df_hmc5883_wrapper # # System commands @@ -69,6 +70,11 @@ set(config_module_list modules/commander modules/controllib + # + # PX4 drivers + # + drivers/gps + # # Libraries # @@ -98,4 +104,5 @@ set(config_module_list set(config_df_driver_list mpu9250 bmp280 + hmc5883 ) diff --git a/posix-configs/eagle/flight/px4-flight.config b/posix-configs/eagle/flight/px4-flight.config index ed6916acab..c3ccf43c76 100644 --- a/posix-configs/eagle/flight/px4-flight.config +++ b/posix-configs/eagle/flight/px4-flight.config @@ -1,6 +1,7 @@ uorb start df_mpu9250_wrapper start df_bmp280_wrapper start +df_hmc5883_wrapper start sensors start commander start attitude_estimator_q start diff --git a/src/drivers/gps/CMakeLists.txt b/src/drivers/gps/CMakeLists.txt index 2441ba9ff7..21849c0aa0 100644 --- a/src/drivers/gps/CMakeLists.txt +++ b/src/drivers/gps/CMakeLists.txt @@ -45,4 +45,4 @@ px4_add_module( DEPENDS platforms__common ) -# vim: set noet ft=cmake fenc=utf-8 ff=unix : +# vim: set noet ft=cmake fenc=utf-8 ff=unix : diff --git a/src/platforms/posix/drivers/df_hmc5883_wrapper/CMakeLists.txt b/src/platforms/posix/drivers/df_hmc5883_wrapper/CMakeLists.txt new file mode 100644 index 0000000000..276771ab9a --- /dev/null +++ b/src/platforms/posix/drivers/df_hmc5883_wrapper/CMakeLists.txt @@ -0,0 +1,46 @@ +############################################################################ +# +# Copyright (c) 2016 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +include_directories(../../../../lib/DriverFramework/drivers) + +px4_add_module( + MODULE platforms__posix__drivers__df_hmc5883_wrapper + MAIN df_hmc5883_wrapper + SRCS + df_hmc5883_wrapper.cpp + DEPENDS + platforms__common + df_driver_framework + df_hmc5883 + ) +# vim: set noet ft=cmake fenc=utf-8 ff=unix : diff --git a/src/platforms/posix/drivers/df_hmc5883_wrapper/df_hmc5883_wrapper.cpp b/src/platforms/posix/drivers/df_hmc5883_wrapper/df_hmc5883_wrapper.cpp new file mode 100644 index 0000000000..9e884bae04 --- /dev/null +++ b/src/platforms/posix/drivers/df_hmc5883_wrapper/df_hmc5883_wrapper.cpp @@ -0,0 +1,333 @@ +/**************************************************************************** + * + * Copyright (c) 2016 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file df_hmc5883_wrapper.cpp + * Lightweight driver to access the HMC5883 of the DriverFramework. + * + * @author Julian Oes + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include +//#include +//#include + +#include +#include + + +extern "C" { __EXPORT int df_hmc5883_wrapper_main(int argc, char *argv[]); } + +using namespace DriverFramework; + + +class DfHmc9250Wrapper : public HMC5883 +{ +public: + DfHmc9250Wrapper(/*enum Rotation rotation*/); + ~DfHmc9250Wrapper(); + + + /** + * Start automatic measurement. + * + * @return 0 on success + */ + int start(); + + /** + * Stop automatic measurement. + * + * @return 0 on success + */ + int stop(); + +private: + int _publish(struct mag_sensor_data &data); + + //enum Rotation _rotation; + + orb_advert_t _mag_topic; + + int _mag_orb_class_instance; + + perf_counter_t _mag_sample_perf; + +}; + +DfHmc9250Wrapper::DfHmc9250Wrapper(/*enum Rotation rotation*/) : + HMC5883(MAG_DEVICE_PATH), + _mag_topic(nullptr), + _mag_orb_class_instance(-1), + _mag_sample_perf(perf_alloc(PC_ELAPSED, "df_mag_read")) + /*_rotation(rotation)*/ +{ +} + +DfHmc9250Wrapper::~DfHmc9250Wrapper() +{ + perf_free(_mag_sample_perf); +} + +int DfHmc9250Wrapper::start() +{ + // TODO: don't publish garbage here + mag_report mag_report = {}; + _mag_topic = orb_advertise_multi(ORB_ID(sensor_mag), &mag_report, + &_mag_orb_class_instance, ORB_PRIO_DEFAULT); + if (_mag_topic == nullptr) { + PX4_ERR("sensor_mag advert fail"); + return -1; + } + + /* Init device and start sensor. */ + int ret = init(); + + if (ret != 0) { + PX4_ERR("HMC5883 init fail: %d", ret); + return ret; + } + + ret = HMC5883::start(); + + if (ret != 0) { + PX4_ERR("HMC5883 start fail: %d", ret); + return ret; + } + + return 0; +} + +int DfHmc9250Wrapper::stop() +{ + /* Stop sensor. */ + int ret = HMC5883::stop(); + + if (ret != 0) { + PX4_ERR("HMC5883 stop fail: %d", ret); + return ret; + } + + return 0; +} + +int DfHmc9250Wrapper::_publish(struct mag_sensor_data &data) +{ + /* Publish mag first. */ + perf_begin(_mag_sample_perf); + + mag_report mag_report = {}; + mag_report.timestamp = data.last_read_time_usec; + + // TODO: remove these (or get the values) + mag_report.x_raw = NAN; + mag_report.y_raw = NAN; + mag_report.z_raw = NAN; + mag_report.x = data.field_x_ga; + mag_report.y = data.field_y_ga; + mag_report.z = data.field_z_ga; + + // TODO: get these right + //mag_report.scaling = -1.0f; + //mag_report.range_m_s2 = -1.0f; + + // TODO: when is this ever blocked? + if (!(m_pub_blocked)) { + + if (_mag_topic != nullptr) { + orb_publish(ORB_ID(sensor_mag), _mag_topic, &mag_report); + } + + } + perf_end(_mag_sample_perf); + + /* Notify anyone waiting for data. */ + DevMgr::updateNotify(*this); + + return 0; +}; + + +namespace df_hmc5883_wrapper +{ + +DfHmc9250Wrapper *g_dev = nullptr; + +int start(/* enum Rotation rotation */); +int stop(); +int info(); +void usage(); + +int start(/*enum Rotation rotation*/) +{ + g_dev = new DfHmc9250Wrapper(/*rotation*/); + + if (g_dev == nullptr) { + PX4_ERR("failed instantiating DfHmc9250Wrapper object"); + return -1; + } + + int ret = g_dev->start(); + if (ret != 0) { + PX4_ERR("DfHmc9250Wrapper start failed"); + return ret; + } + + // Open the MAG sensor + DevHandle h; + DevMgr::getHandle(MAG_DEVICE_PATH, h); + if (!h.isValid()) + { + DF_LOG_INFO("Error: unable to obtain a valid handle for the receiver at: %s (%d)", + MAG_DEVICE_PATH, h.getError()); + return -1; + } + + DevMgr::releaseHandle(h); + + return 0; +} + +int stop() +{ + if (g_dev == nullptr) { + PX4_ERR("driver not running"); + return 1; + } + + int ret = g_dev->stop(); + + if (ret != 0) { + PX4_ERR("driver could not be stopped"); + return ret; + } + + delete g_dev; + g_dev = nullptr; + return 0; +} + +/** + * Print a little info about the driver. + */ +int +info() +{ + if (g_dev == nullptr) { + PX4_ERR("driver not running"); + return 1; + } + + PX4_DEBUG("state @ %p", g_dev); + + return 0; +} + +void +usage() +{ + PX4_WARN("Usage: df_hmc5883_wrapper 'start', 'info', 'stop'"); + PX4_WARN("options:"); + //PX4_WARN(" -R rotation"); +} + +} // namespace df_hmc5883_wrapper + + +int +df_hmc5883_wrapper_main(int argc, char *argv[]) +{ + int ch; + // enum Rotation rotation = ROTATION_NONE; + int ret = 0; + int myoptind = 1; + const char *myoptarg = NULL; + + /* jump over start/off/etc and look at options first */ + while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) { + switch (ch) { + //case 'R': + // rotation = (enum Rotation)atoi(myoptarg); + // break; + + default: + df_hmc5883_wrapper::usage(); + return 0; + } + } + + if (argc <= 1) { + df_hmc5883_wrapper::usage(); + return 1; + } + + const char *verb = argv[myoptind]; + + + if (!strcmp(verb, "start")) { + ret = df_hmc5883_wrapper::start(/*rotation*/); + } + + else if (!strcmp(verb, "stop")) { + ret = df_hmc5883_wrapper::stop(); + } + + else if (!strcmp(verb, "info")) { + ret = df_hmc5883_wrapper::info(); + } + + else { + df_hmc5883_wrapper::usage(); + return 1; + } + + return ret; +}