forked from PX4/PX4-Autopilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Daniel Agar <[email protected]>
- Loading branch information
Showing
22 changed files
with
379 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2023 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 "ekf.h" | ||
|
||
#include "aux_global_position.hpp" | ||
|
||
#if defined(CONFIG_EKF2_AUX_GLOBAL_POSITION) && defined(MODULE_NAME) | ||
|
||
void AuxGlobalPosition::update(Ekf &ekf, const estimator::imuSample &imu_delayed) | ||
{ | ||
|
||
#if defined(MODULE_NAME) | ||
|
||
if (_aux_global_position_sub.updated()) { | ||
|
||
vehicle_global_position_s aux_global_position{}; | ||
_aux_global_position_sub.copy(&aux_global_position); | ||
|
||
const int64_t time_us = aux_global_position.timestamp_sample - static_cast<int64_t>(_param_ekf2_agp_delay.get() * 1000); | ||
|
||
AuxGlobalPositionSample sample{}; | ||
sample.time_us = time_us; | ||
sample.latitude = aux_global_position.lat; | ||
sample.longitude = aux_global_position.lon; | ||
sample.altitude_amsl = aux_global_position.alt; | ||
sample.eph = aux_global_position.eph; | ||
sample.epv = aux_global_position.epv; | ||
|
||
_aux_global_position_buffer.push(sample); | ||
|
||
_time_last_buffer_push = imu_delayed.time_us; | ||
} | ||
|
||
#endif // MODULE_NAME | ||
|
||
AuxGlobalPositionSample sample; | ||
|
||
if (_aux_global_position_buffer.pop_first_older_than(imu_delayed.time_us, &sample)) { | ||
|
||
if (!(_param_ekf2_agp_ctrl.get() & static_cast<int32_t>(Ctrl::HPOS))) { | ||
return; | ||
} | ||
|
||
estimator_aid_source2d_s aid_src{}; | ||
Vector2f position; | ||
|
||
if (ekf.global_origin_valid()) { | ||
position = ekf.global_origin().project(sample.latitude, sample.longitude); | ||
//const float hgt = ekf.getEkfGlobalOriginAltitude() - (float)sample.altitude; | ||
// relax the upper observation noise limit which prevents bad measurements perturbing the position estimate | ||
float pos_noise = math::max(sample.eph, _param_ekf2_agp_noise.get()); | ||
const float pos_var = sq(pos_noise); | ||
const Vector2f pos_obs_var(pos_var, pos_var); | ||
ekf.updateHorizontalPositionAidSrcStatus(sample.time_us, | ||
position, // observation | ||
pos_obs_var, // observation variance | ||
math::max(_param_ekf2_agp_gate.get(), 1.f), // innovation gate | ||
aid_src); | ||
} | ||
|
||
const bool starting_conditions = PX4_ISFINITE(sample.latitude) && PX4_ISFINITE(sample.longitude) | ||
&& ekf.control_status_flags().yaw_align; | ||
const bool continuing_conditions = starting_conditions | ||
&& ekf.global_origin_valid(); | ||
|
||
switch (_state) { | ||
case State::stopped: | ||
/* FALLTHROUGH */ | ||
case State::starting: | ||
if (starting_conditions) { | ||
_state = State::starting; | ||
|
||
if (ekf.global_origin_valid()) { | ||
ekf.enableControlStatusAuxGpos(); | ||
_state = State::active; | ||
|
||
} else { | ||
// Try to initialize using measurement | ||
if (ekf.setEkfGlobalOrigin(sample.latitude, sample.longitude, sample.altitude_amsl, sample.eph, sample.epv)) { | ||
ekf.enableControlStatusAuxGpos(); | ||
_state = State::active; | ||
} | ||
} | ||
} | ||
break; | ||
|
||
case State::active: | ||
if (continuing_conditions) { | ||
ekf.fuseHorizontalPosition(aid_src); | ||
|
||
} else { | ||
ekf.disableControlStatusAuxGpos(); | ||
_state = State::stopped; | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
#if defined(MODULE_NAME) | ||
aid_src.timestamp = hrt_absolute_time(); | ||
_estimator_aid_src_aux_global_position_pub.publish(aid_src); | ||
#endif // MODULE_NAME | ||
} else if ((_state != State::stopped) && isTimedOut(_time_last_buffer_push, imu_delayed.time_us, (uint64_t)5e6)) { | ||
ekf.disableControlStatusAuxGpos(); | ||
_state = State::stopped; | ||
ECL_WARN("Aux global position data stopped"); | ||
} | ||
} | ||
|
||
#endif // CONFIG_EKF2_AUX_GLOBAL_POSITION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/**************************************************************************** | ||
* | ||
* Copyright (c) 2023 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. | ||
* | ||
****************************************************************************/ | ||
|
||
#ifndef EKF_AUX_GLOBAL_POSITION_HPP | ||
#define EKF_AUX_GLOBAL_POSITION_HPP | ||
|
||
// interface? | ||
// - ModuleParams | ||
// - Base class EKF | ||
// - bool update(imu) | ||
// how to get delay? | ||
// WelfordMean for init? | ||
// WelfordMean for rate | ||
|
||
#include "common.h" | ||
#include "RingBuffer.h" | ||
|
||
#if defined(CONFIG_EKF2_AUX_GLOBAL_POSITION) && defined(MODULE_NAME) | ||
|
||
#if defined(MODULE_NAME) | ||
# include <px4_platform_common/module_params.h> | ||
# include <uORB/PublicationMulti.hpp> | ||
# include <uORB/Subscription.hpp> | ||
# include <uORB/topics/estimator_aid_source2d.h> | ||
# include <uORB/topics/vehicle_global_position.h> | ||
#endif // MODULE_NAME | ||
|
||
class Ekf; | ||
|
||
class AuxGlobalPosition : public ModuleParams | ||
{ | ||
public: | ||
AuxGlobalPosition() : ModuleParams(nullptr) {} | ||
~AuxGlobalPosition() = default; | ||
|
||
void update(Ekf &ekf, const estimator::imuSample &imu_delayed); | ||
|
||
void updateParameters() | ||
{ | ||
updateParams(); | ||
} | ||
|
||
private: | ||
bool isTimedOut(uint64_t last_sensor_timestamp, uint64_t time_delayed_us, uint64_t timeout_period) const | ||
{ | ||
return (last_sensor_timestamp == 0) || (last_sensor_timestamp + timeout_period < time_delayed_us); | ||
} | ||
|
||
struct AuxGlobalPositionSample { | ||
uint64_t time_us{}; ///< timestamp of the measurement (uSec) | ||
double latitude{}; | ||
double longitude{}; | ||
float altitude_amsl{}; | ||
float eph{}; | ||
float epv{}; | ||
}; | ||
|
||
RingBuffer<AuxGlobalPositionSample> _aux_global_position_buffer{20}; // TODO: size with _obs_buffer_length and actual publication rate | ||
uint64_t _time_last_buffer_push{0}; | ||
|
||
enum Ctrl : uint8_t { | ||
HPOS = (1<<0), | ||
VPOS = (1<<1) | ||
}; | ||
|
||
enum class State { | ||
stopped, | ||
starting, | ||
active, | ||
}; | ||
|
||
State _state{State::stopped}; | ||
|
||
#if defined(MODULE_NAME) | ||
uORB::PublicationMulti<estimator_aid_source2d_s> _estimator_aid_src_aux_global_position_pub{ORB_ID(estimator_aid_src_aux_global_position)}; | ||
uORB::Subscription _aux_global_position_sub{ORB_ID(aux_global_position)}; | ||
|
||
DEFINE_PARAMETERS( | ||
(ParamInt<px4::params::EKF2_AGP_CTRL>) _param_ekf2_agp_ctrl, | ||
(ParamFloat<px4::params::EKF2_AGP_DELAY>) _param_ekf2_agp_delay, | ||
(ParamFloat<px4::params::EKF2_AGP_NOISE>) _param_ekf2_agp_noise, | ||
(ParamFloat<px4::params::EKF2_AGP_GATE>) _param_ekf2_agp_gate | ||
) | ||
|
||
#endif // MODULE_NAME | ||
}; | ||
|
||
#endif // CONFIG_EKF2_AUX_GLOBAL_POSITION | ||
|
||
#endif // !EKF_AUX_GLOBAL_POSITION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.