From 0a63df25a63c683fc849c2431ccee472a3716bfa Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Tue, 16 Jun 2020 12:17:55 -0400 Subject: [PATCH] create fake_magnetometer "driver" to publish magnetic field in body frame - this can be helpful for orienting new unknown magnetometers - requires attitude and GPS position --- boards/px4/fmu-v4/default.cmake | 1 + boards/px4/fmu-v5/default.cmake | 1 + boards/px4/sitl/default.cmake | 1 + src/examples/fake_magnetometer/CMakeLists.txt | 46 ++++++ .../fake_magnetometer/FakeMagnetometer.cpp | 146 ++++++++++++++++++ .../fake_magnetometer/FakeMagnetometer.hpp | 82 ++++++++++ 6 files changed, 277 insertions(+) create mode 100644 src/examples/fake_magnetometer/CMakeLists.txt create mode 100644 src/examples/fake_magnetometer/FakeMagnetometer.cpp create mode 100644 src/examples/fake_magnetometer/FakeMagnetometer.hpp diff --git a/boards/px4/fmu-v4/default.cmake b/boards/px4/fmu-v4/default.cmake index fa50a73fd9..9e9a6bed9b 100644 --- a/boards/px4/fmu-v4/default.cmake +++ b/boards/px4/fmu-v4/default.cmake @@ -114,6 +114,7 @@ px4_add_board( ver work_queue EXAMPLES + fake_magnetometer fixedwing_control # Tutorial code from https://px4.io/dev/example_fixedwing_control hello hwtest # Hardware test diff --git a/boards/px4/fmu-v5/default.cmake b/boards/px4/fmu-v5/default.cmake index 2e0f37b918..9bede18cfd 100644 --- a/boards/px4/fmu-v5/default.cmake +++ b/boards/px4/fmu-v5/default.cmake @@ -118,6 +118,7 @@ px4_add_board( ver work_queue EXAMPLES + fake_magnetometer fixedwing_control # Tutorial code from https://px4.io/dev/example_fixedwing_control hello hwtest # Hardware test diff --git a/boards/px4/sitl/default.cmake b/boards/px4/sitl/default.cmake index 8f90549c24..85850dc6d3 100644 --- a/boards/px4/sitl/default.cmake +++ b/boards/px4/sitl/default.cmake @@ -75,6 +75,7 @@ px4_add_board( work_queue EXAMPLES dyn_hello # dynamically loading modules example + fake_magnetometer fixedwing_control # Tutorial code from https://px4.io/dev/example_fixedwing_control hello #hwtest # Hardware test diff --git a/src/examples/fake_magnetometer/CMakeLists.txt b/src/examples/fake_magnetometer/CMakeLists.txt new file mode 100644 index 0000000000..b60f611f5f --- /dev/null +++ b/src/examples/fake_magnetometer/CMakeLists.txt @@ -0,0 +1,46 @@ +############################################################################ +# +# Copyright (c) 2020 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. +# +############################################################################ + +px4_add_module( + MODULE examples__fake_magnetometer + MAIN fake_magnetometer + COMPILE_FLAGS + SRCS + FakeMagnetometer.cpp + FakeMagnetometer.hpp + DEPENDS + drivers_magnetometer + ecl_geo + git_ecl + px4_work_queue +) diff --git a/src/examples/fake_magnetometer/FakeMagnetometer.cpp b/src/examples/fake_magnetometer/FakeMagnetometer.cpp new file mode 100644 index 0000000000..7d9207c645 --- /dev/null +++ b/src/examples/fake_magnetometer/FakeMagnetometer.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** + * + * Copyright (c) 2020 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 "FakeMagnetometer.hpp" + +#include + +using namespace matrix; +using namespace time_literals; + +FakeMagnetometer::FakeMagnetometer() : + ModuleParams(nullptr), + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default), + _px4_mag(0, ORB_PRIO_MIN, ROTATION_NONE) +{ + _px4_mag.set_device_type(DRV_MAG_DEVTYPE_MAGSIM); + _px4_mag.set_external(false); +} + +bool FakeMagnetometer::init() +{ + ScheduleOnInterval(10_ms); // 100 Hz + return true; +} + +void FakeMagnetometer::Run() +{ + if (should_exit()) { + ScheduleClear(); + exit_and_cleanup(); + return; + } + + if (_vehicle_gps_position_sub.updated()) { + vehicle_gps_position_s gps; + + if (_vehicle_gps_position_sub.copy(&gps)) { + if (gps.eph < 1000) { + + const double lat = gps.lat / 1.e7; + const double lon = gps.lon / 1.e7; + + // magnetic field data returned by the geo library using the current GPS position + const float mag_declination_gps = math::radians(get_mag_declination(lat, lon)); + const float mag_inclination_gps = math::radians(get_mag_inclination(lat, lon)); + const float mag_strength_gps = 0.01f * get_mag_strength(lat, lon); // centi-Gauss (micro-Tesla) -> Gauss + + _mag_earth_pred = Dcmf(Eulerf(0, -mag_inclination_gps, mag_declination_gps)) * Vector3f(mag_strength_gps, 0, 0); + + _mag_earth_available = true; + } + } + } + + if (_mag_earth_available) { + vehicle_attitude_s attitude; + + if (_vehicle_attitude_sub.update(&attitude)) { + Vector3f expected_field = Dcmf{Quatf{attitude.q}} .transpose() * _mag_earth_pred; + + _px4_mag.update(hrt_absolute_time(), expected_field(0), expected_field(1), expected_field(2)); + } + } +} + +int FakeMagnetometer::task_spawn(int argc, char *argv[]) +{ + FakeMagnetometer *instance = new FakeMagnetometer(); + + if (instance) { + _object.store(instance); + _task_id = task_id_is_work_queue; + + if (instance->init()) { + return PX4_OK; + } + + } else { + PX4_ERR("alloc failed"); + } + + delete instance; + _object.store(nullptr); + _task_id = -1; + + return PX4_ERROR; +} + +int FakeMagnetometer::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + +int FakeMagnetometer::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description +Publish the earth magnetic field as a fake magnetometer (sensor_mag). +Requires vehicle_attitude and vehicle_gps_position. +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("fake_magnetometer", "driver"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + return 0; +} + +extern "C" __EXPORT int fake_magnetometer_main(int argc, char *argv[]) +{ + return FakeMagnetometer::main(argc, argv); +} diff --git a/src/examples/fake_magnetometer/FakeMagnetometer.hpp b/src/examples/fake_magnetometer/FakeMagnetometer.hpp new file mode 100644 index 0000000000..b10f53a495 --- /dev/null +++ b/src/examples/fake_magnetometer/FakeMagnetometer.hpp @@ -0,0 +1,82 @@ +/**************************************************************************** + * + * Copyright (c) 2020 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 FakeMagnetometer.cpp + * + * Publish the earth magnetic field as a fake magnetometer (sensor_mag). + * Requires vehicle_attitude and vehicle_gps_position + * + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class FakeMagnetometer : public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem +{ +public: + FakeMagnetometer(); + ~FakeMagnetometer() override = default; + + /** @see ModuleBase */ + static int task_spawn(int argc, char *argv[]); + + /** @see ModuleBase */ + static int custom_command(int argc, char *argv[]); + + /** @see ModuleBase */ + static int print_usage(const char *reason = nullptr); + + bool init(); + +private: + void Run() override; + + PX4Magnetometer _px4_mag; + + bool _mag_earth_available{false}; + + matrix::Vector3f _mag_earth_pred{}; + + uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; + uORB::Subscription _vehicle_gps_position_sub{ORB_ID(vehicle_gps_position)}; +};