2019-08-06 12:55:25 -04:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
2021-02-25 10:09:59 -05:00
|
|
|
* Copyright (c) 2019-2021 PX4 Development Team. All rights reserved.
|
2019-08-06 12:55:25 -04:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-08-13 14:40:29 -04:00
|
|
|
#include <lib/sensor_calibration/Gyroscope.hpp>
|
2019-08-07 05:05:48 -04:00
|
|
|
#include <lib/mathlib/math/Limits.hpp>
|
|
|
|
|
#include <lib/matrix/matrix/math.hpp>
|
2021-02-25 10:06:17 -05:00
|
|
|
#include <lib/mathlib/math/filter/LowPassFilter2p.hpp>
|
|
|
|
|
#include <lib/mathlib/math/filter/LowPassFilter2pArray.hpp>
|
|
|
|
|
#include <lib/mathlib/math/filter/NotchFilterArray.hpp>
|
2019-10-25 10:56:32 +02:00
|
|
|
#include <px4_platform_common/log.h>
|
|
|
|
|
#include <px4_platform_common/module_params.h>
|
2020-01-26 17:19:47 -05:00
|
|
|
#include <px4_platform_common/px4_config.h>
|
2020-05-30 11:07:54 -04:00
|
|
|
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
2019-08-06 12:55:25 -04:00
|
|
|
#include <uORB/Publication.hpp>
|
|
|
|
|
#include <uORB/Subscription.hpp>
|
|
|
|
|
#include <uORB/SubscriptionCallback.hpp>
|
2021-02-26 22:49:49 -05:00
|
|
|
#include <uORB/topics/esc_status.h>
|
2020-10-27 10:56:11 -04:00
|
|
|
#include <uORB/topics/estimator_selector_status.h>
|
2020-01-26 17:19:47 -05:00
|
|
|
#include <uORB/topics/estimator_sensor_bias.h>
|
2019-08-06 12:55:25 -04:00
|
|
|
#include <uORB/topics/parameter_update.h>
|
2019-08-07 05:05:48 -04:00
|
|
|
#include <uORB/topics/sensor_gyro.h>
|
2021-02-25 10:09:59 -05:00
|
|
|
#include <uORB/topics/sensor_gyro_fft.h>
|
2021-02-25 10:06:17 -05:00
|
|
|
#include <uORB/topics/sensor_gyro_fifo.h>
|
2020-01-26 17:19:47 -05:00
|
|
|
#include <uORB/topics/sensor_selection.h>
|
2020-01-27 16:44:01 -05:00
|
|
|
#include <uORB/topics/vehicle_angular_acceleration.h>
|
2019-08-07 05:05:48 -04:00
|
|
|
#include <uORB/topics/vehicle_angular_velocity.h>
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2021-01-10 21:09:15 -05:00
|
|
|
using namespace time_literals;
|
|
|
|
|
|
2020-01-27 18:18:04 -05:00
|
|
|
namespace sensors
|
|
|
|
|
{
|
|
|
|
|
|
2020-05-30 11:07:54 -04:00
|
|
|
class VehicleAngularVelocity : public ModuleParams, public px4::ScheduledWorkItem
|
2019-08-06 12:55:25 -04:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VehicleAngularVelocity();
|
2020-01-18 01:15:00 -05:00
|
|
|
~VehicleAngularVelocity() override;
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2021-02-25 10:09:59 -05:00
|
|
|
void PrintStatus();
|
2020-01-21 16:47:38 -05:00
|
|
|
bool Start();
|
|
|
|
|
void Stop();
|
2019-08-06 12:55:25 -04:00
|
|
|
|
|
|
|
|
private:
|
2020-01-21 16:47:38 -05:00
|
|
|
void Run() override;
|
|
|
|
|
|
2021-03-22 12:30:21 -04:00
|
|
|
void CalibrateAndPublish(const hrt_abstime ×tamp_sample, const matrix::Vector3f &angular_velocity,
|
|
|
|
|
const matrix::Vector3f &angular_acceleration, float scale = 1.f);
|
|
|
|
|
|
|
|
|
|
float FilterAngularVelocity(int axis, float data[], int N);
|
|
|
|
|
float FilterAngularAcceleration(int axis, float data[], int N, float dt_s);
|
|
|
|
|
|
2021-02-26 22:49:49 -05:00
|
|
|
void DisableDynamicNotchEscRpm();
|
2021-02-25 10:09:59 -05:00
|
|
|
void DisableDynamicNotchFFT();
|
2020-01-18 01:15:00 -05:00
|
|
|
void ParametersUpdate(bool force = false);
|
2021-03-22 12:30:21 -04:00
|
|
|
|
|
|
|
|
void ResetFilters();
|
2020-01-18 01:15:00 -05:00
|
|
|
void SensorBiasUpdate(bool force = false);
|
|
|
|
|
bool SensorSelectionUpdate(bool force = false);
|
2021-02-26 22:49:49 -05:00
|
|
|
void UpdateDynamicNotchEscRpm(bool force = false);
|
2021-02-25 10:09:59 -05:00
|
|
|
void UpdateDynamicNotchFFT(bool force = false);
|
|
|
|
|
bool UpdateSampleRate();
|
2021-02-25 10:06:17 -05:00
|
|
|
|
2021-03-22 12:01:12 -04:00
|
|
|
// scaled appropriately for current FIFO mode
|
|
|
|
|
matrix::Vector3f GetResetAngularVelocity() const;
|
2021-03-22 12:30:21 -04:00
|
|
|
matrix::Vector3f GetResetAngularAcceleration() const;
|
2021-03-22 12:01:12 -04:00
|
|
|
|
2020-10-08 19:01:44 -04:00
|
|
|
static constexpr int MAX_SENSOR_COUNT = 4;
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2020-01-27 16:44:01 -05:00
|
|
|
uORB::Publication<vehicle_angular_acceleration_s> _vehicle_angular_acceleration_pub{ORB_ID(vehicle_angular_acceleration)};
|
2021-02-25 10:06:17 -05:00
|
|
|
uORB::Publication<vehicle_angular_velocity_s> _vehicle_angular_velocity_pub{ORB_ID(vehicle_angular_velocity)};
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2020-10-27 10:56:11 -04:00
|
|
|
uORB::Subscription _estimator_selector_status_sub{ORB_ID(estimator_selector_status)};
|
2020-01-26 17:19:47 -05:00
|
|
|
uORB::Subscription _estimator_sensor_bias_sub{ORB_ID(estimator_sensor_bias)};
|
2021-02-25 10:09:59 -05:00
|
|
|
#if !defined(CONSTRAINED_FLASH)
|
2021-02-26 22:49:49 -05:00
|
|
|
uORB::Subscription _esc_status_sub {ORB_ID(esc_status)};
|
2021-02-25 10:09:59 -05:00
|
|
|
uORB::Subscription _sensor_gyro_fft_sub {ORB_ID(sensor_gyro_fft)};
|
|
|
|
|
#endif // !CONSTRAINED_FLASH
|
2021-01-10 21:09:15 -05:00
|
|
|
|
|
|
|
|
uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s};
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2020-01-21 16:47:38 -05:00
|
|
|
uORB::SubscriptionCallbackWorkItem _sensor_selection_sub{this, ORB_ID(sensor_selection)};
|
2020-09-01 13:53:50 -04:00
|
|
|
uORB::SubscriptionCallbackWorkItem _sensor_sub{this, ORB_ID(sensor_gyro)};
|
2021-02-25 10:06:17 -05:00
|
|
|
uORB::SubscriptionCallbackWorkItem _sensor_fifo_sub{this, ORB_ID(sensor_gyro_fifo)};
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2020-08-13 14:40:29 -04:00
|
|
|
calibration::Gyroscope _calibration{};
|
2020-01-27 10:05:33 -05:00
|
|
|
|
2020-12-14 10:27:57 -05:00
|
|
|
matrix::Vector3f _bias{};
|
2019-08-06 12:55:25 -04:00
|
|
|
|
2021-02-25 10:06:17 -05:00
|
|
|
matrix::Vector3f _angular_velocity{};
|
|
|
|
|
matrix::Vector3f _angular_acceleration{};
|
|
|
|
|
|
2020-12-14 10:27:57 -05:00
|
|
|
matrix::Vector3f _angular_velocity_prev{};
|
2021-02-25 10:06:17 -05:00
|
|
|
hrt_abstime _timestamp_sample_last{0};
|
2020-01-27 16:44:01 -05:00
|
|
|
|
2021-02-25 10:06:17 -05:00
|
|
|
hrt_abstime _publish_interval_min_us{0};
|
2020-01-27 10:05:33 -05:00
|
|
|
hrt_abstime _last_publish{0};
|
2021-02-25 10:06:17 -05:00
|
|
|
|
|
|
|
|
float _filter_sample_rate_hz{0.f};
|
|
|
|
|
|
2020-12-14 10:27:57 -05:00
|
|
|
static constexpr const float kInitialRateHz{1000.f}; /**< sensor update rate used for initialization */
|
2020-06-01 10:14:59 -04:00
|
|
|
|
2020-01-27 16:44:01 -05:00
|
|
|
// angular velocity filters
|
2021-02-25 10:06:17 -05:00
|
|
|
math::LowPassFilter2pArray _lp_filter_velocity[3] {{kInitialRateHz, 30.f}, {kInitialRateHz, 30.f}, {kInitialRateHz, 30.f}};
|
|
|
|
|
math::NotchFilterArray<float> _notch_filter_velocity[3] {};
|
2020-01-27 16:44:01 -05:00
|
|
|
|
2021-02-25 10:09:59 -05:00
|
|
|
#if !defined(CONSTRAINED_FLASH)
|
2021-03-08 10:35:48 -05:00
|
|
|
|
|
|
|
|
enum DynamicNotch {
|
|
|
|
|
EscRpm = 1,
|
|
|
|
|
FFT = 2,
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-26 22:49:49 -05:00
|
|
|
static constexpr int MAX_NUM_ESC_RPM = sizeof(esc_status_s::esc) / sizeof(esc_status_s::esc[0]);
|
2021-03-14 18:26:58 -04:00
|
|
|
static constexpr int MAX_NUM_ESC_RPM_HARMONICS = 3;
|
|
|
|
|
|
2021-02-25 10:09:59 -05:00
|
|
|
static constexpr int MAX_NUM_FFT_PEAKS = sizeof(sensor_gyro_fft_s::peak_frequencies_x) / sizeof(
|
|
|
|
|
sensor_gyro_fft_s::peak_frequencies_x[0]);
|
|
|
|
|
|
2021-03-14 18:26:58 -04:00
|
|
|
math::NotchFilterArray<float> _dynamic_notch_filter_esc_rpm[MAX_NUM_ESC_RPM][MAX_NUM_ESC_RPM_HARMONICS][3] {};
|
2021-02-26 22:49:49 -05:00
|
|
|
math::NotchFilterArray<float> _dynamic_notch_filter_fft[MAX_NUM_FFT_PEAKS][3] {};
|
|
|
|
|
|
2021-03-08 10:35:48 -05:00
|
|
|
perf_counter_t _dynamic_notch_filter_esc_rpm_update_perf{nullptr};
|
|
|
|
|
perf_counter_t _dynamic_notch_filter_fft_update_perf{nullptr};
|
|
|
|
|
perf_counter_t _dynamic_notch_filter_esc_rpm_perf{nullptr};
|
|
|
|
|
perf_counter_t _dynamic_notch_filter_fft_perf{nullptr};
|
2021-02-26 22:49:49 -05:00
|
|
|
|
|
|
|
|
bool _dynamic_notch_esc_rpm_available{false};
|
|
|
|
|
bool _dynamic_notch_fft_available{false};
|
2021-02-25 10:09:59 -05:00
|
|
|
#endif // !CONSTRAINED_FLASH
|
|
|
|
|
|
2020-01-27 16:44:01 -05:00
|
|
|
// angular acceleration filter
|
2021-02-25 10:06:17 -05:00
|
|
|
math::LowPassFilter2p _lp_filter_acceleration[3] {{kInitialRateHz, 30.f}, {kInitialRateHz, 30.f}, {kInitialRateHz, 30.f}};
|
|
|
|
|
|
|
|
|
|
uint32_t _selected_sensor_device_id{0};
|
|
|
|
|
|
2021-02-26 22:49:49 -05:00
|
|
|
float _last_scale{0.f};
|
2021-02-25 10:06:17 -05:00
|
|
|
|
|
|
|
|
bool _reset_filters{true};
|
|
|
|
|
bool _fifo_available{false};
|
2020-02-07 13:04:02 -05:00
|
|
|
|
2021-02-26 19:10:47 -05:00
|
|
|
perf_counter_t _filter_reset_perf{perf_alloc(PC_COUNT, MODULE_NAME": gyro filter reset")};
|
|
|
|
|
perf_counter_t _selection_changed_perf{perf_alloc(PC_COUNT, MODULE_NAME": gyro selection changed")};
|
|
|
|
|
|
2020-01-27 18:18:04 -05:00
|
|
|
DEFINE_PARAMETERS(
|
2021-02-26 22:49:49 -05:00
|
|
|
#if !defined(CONSTRAINED_FLASH)
|
|
|
|
|
(ParamInt<px4::params::IMU_GYRO_DYN_NF>) _param_imu_gyro_dyn_nf,
|
|
|
|
|
#endif // !CONSTRAINED_FLASH
|
2020-01-27 18:18:04 -05:00
|
|
|
(ParamFloat<px4::params::IMU_GYRO_CUTOFF>) _param_imu_gyro_cutoff,
|
|
|
|
|
(ParamFloat<px4::params::IMU_GYRO_NF_FREQ>) _param_imu_gyro_nf_freq,
|
|
|
|
|
(ParamFloat<px4::params::IMU_GYRO_NF_BW>) _param_imu_gyro_nf_bw,
|
2021-02-25 10:06:17 -05:00
|
|
|
(ParamInt<px4::params::IMU_GYRO_RATEMAX>) _param_imu_gyro_ratemax,
|
2021-02-26 22:49:49 -05:00
|
|
|
(ParamFloat<px4::params::IMU_DGYRO_CUTOFF>) _param_imu_dgyro_cutoff
|
2020-01-27 18:18:04 -05:00
|
|
|
)
|
2019-08-06 12:55:25 -04:00
|
|
|
};
|
2020-01-27 18:18:04 -05:00
|
|
|
|
|
|
|
|
} // namespace sensors
|