2020-03-17 12:15:34 -03:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
2021-04-22 08:39:08 -03:00
|
|
|
* Copyright (c) 2021 PX4 Development Team. All rights reserved.
|
2020-03-17 12:15:34 -03: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-02 15:14:18 -03:00
|
|
|
#include "autopilot_tester.h"
|
2020-07-29 09:04:02 -03:00
|
|
|
#include "math_helpers.h"
|
2019-10-02 15:14:18 -03:00
|
|
|
#include <iostream>
|
|
|
|
#include <future>
|
2021-03-17 05:38:37 -03:00
|
|
|
#include <thread>
|
2021-03-25 13:37:03 -03:00
|
|
|
#include <unistd.h>
|
2021-07-20 04:11:17 -03:00
|
|
|
#include <cmath>
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2019-10-03 06:16:03 -03:00
|
|
|
std::string connection_url {"udp://"};
|
2021-03-17 05:38:37 -03:00
|
|
|
std::optional<float> speed_factor {std::nullopt};
|
|
|
|
|
|
|
|
AutopilotTester::AutopilotTester() :
|
|
|
|
_real_time_report_thread([this]()
|
|
|
|
{
|
|
|
|
report_speed_factor();
|
|
|
|
})
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AutopilotTester::~AutopilotTester()
|
|
|
|
{
|
|
|
|
_should_exit = true;
|
|
|
|
_real_time_report_thread.join();
|
|
|
|
}
|
2019-10-03 06:16:03 -03:00
|
|
|
|
2019-10-02 15:14:18 -03:00
|
|
|
void AutopilotTester::connect(const std::string uri)
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
ConnectionResult ret = _mavsdk.add_any_connection(uri);
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(ret == ConnectionResult::Success);
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2021-02-09 03:45:38 -04:00
|
|
|
std::cout << time_str() << "Waiting for system connect" << std::endl;
|
2020-03-17 12:15:34 -03:00
|
|
|
REQUIRE(poll_condition_with_timeout(
|
2021-07-23 01:44:42 -03:00
|
|
|
[this]() { return _mavsdk.systems().size() > 0; }, std::chrono::seconds(25)));
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2021-07-20 04:11:17 -03:00
|
|
|
auto system = get_system();
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2020-03-17 12:15:34 -03:00
|
|
|
_action.reset(new Action(system));
|
2020-06-11 09:18:09 -03:00
|
|
|
_failure.reset(new Failure(system));
|
2020-07-09 11:42:13 -03:00
|
|
|
_info.reset(new Info(system));
|
2020-07-31 04:45:12 -03:00
|
|
|
_manual_control.reset(new ManualControl(system));
|
2020-03-17 12:15:34 -03:00
|
|
|
_mission.reset(new Mission(system));
|
2021-03-25 13:37:03 -03:00
|
|
|
_mission_raw.reset(new MissionRaw(system));
|
2020-03-17 12:15:34 -03:00
|
|
|
_offboard.reset(new Offboard(system));
|
2020-06-11 09:18:09 -03:00
|
|
|
_param.reset(new Param(system));
|
|
|
|
_telemetry.reset(new Telemetry(system));
|
2023-10-20 08:36:14 -03:00
|
|
|
_mavlink_passthrough.reset(new MavlinkPassthrough(system));
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::wait_until_ready()
|
|
|
|
{
|
2022-10-26 10:39:29 -03:00
|
|
|
std::cout << time_str() << "Waiting for system to be ready (system health ok & able to arm)" << std::endl;
|
|
|
|
|
|
|
|
// Wiat until the system is healthy
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(poll_condition_with_timeout(
|
2020-07-31 04:41:08 -03:00
|
|
|
[this]() { return _telemetry->health_all_ok(); }, std::chrono::seconds(30)));
|
2020-07-15 08:47:22 -03:00
|
|
|
|
2022-10-26 10:39:29 -03:00
|
|
|
// Note: There is a known bug in MAVSDK (https://github.com/mavlink/MAVSDK/issues/1852),
|
|
|
|
// where `health_all_ok()` returning true doesn't actually mean vehicle is ready to accept
|
|
|
|
// global position estimate as valid (due to hysteresis). This needs to be fixed properly.
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2022-10-26 10:39:29 -03:00
|
|
|
// However, this is mitigated by the `is_armable` check below as a side effect, since
|
|
|
|
// when the vehicle considers global position to be valid, it will then allow arming
|
|
|
|
|
|
|
|
// Wait until we can arm
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(poll_condition_with_timeout(
|
2022-10-26 10:39:29 -03:00
|
|
|
[this]() { return _telemetry->health().is_armable; }, std::chrono::seconds(20)));
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:41:28 -04:00
|
|
|
void AutopilotTester::store_home()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
request_ground_truth();
|
2021-02-09 03:45:38 -04:00
|
|
|
std::cout << time_str() << "Waiting to get home position" << std::endl;
|
2020-05-26 06:36:30 -03:00
|
|
|
CHECK(poll_condition_with_timeout(
|
|
|
|
[this]() {
|
|
|
|
_home = _telemetry->ground_truth();
|
|
|
|
return std::isfinite(_home.latitude_deg) && std::isfinite(_home.longitude_deg);
|
|
|
|
}, std::chrono::seconds(10)));
|
2020-03-06 07:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::check_home_within(float acceptance_radius_m)
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(ground_truth_horizontal_position_close_to(_home, acceptance_radius_m));
|
2020-03-06 07:41:28 -04:00
|
|
|
}
|
|
|
|
|
2020-07-31 04:45:12 -03:00
|
|
|
void AutopilotTester::check_home_not_within(float min_distance_m)
|
|
|
|
{
|
|
|
|
CHECK(ground_truth_horizontal_position_far_from(_home, min_distance_m));
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:14:18 -03:00
|
|
|
void AutopilotTester::set_takeoff_altitude(const float altitude_m)
|
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
CHECK(Action::Result::Success == _action->set_takeoff_altitude(altitude_m));
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto result = _action->get_takeoff_altitude();
|
2020-06-10 14:26:11 -03:00
|
|
|
CHECK(result.first == Action::Result::Success);
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(result.second == Approx(altitude_m));
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:52:14 -04:00
|
|
|
void AutopilotTester::set_rtl_altitude(const float altitude_m)
|
|
|
|
{
|
|
|
|
CHECK(Action::Result::Success == _action->set_return_to_launch_altitude(altitude_m));
|
|
|
|
const auto result = _action->get_return_to_launch_altitude();
|
|
|
|
CHECK(result.first == Action::Result::Success);
|
|
|
|
CHECK(result.second == Approx(altitude_m));
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:29:00 -03:00
|
|
|
void AutopilotTester::set_height_source(AutopilotTester::HeightSource height_source)
|
|
|
|
{
|
|
|
|
switch (height_source) {
|
|
|
|
case HeightSource::Baro:
|
2022-07-29 10:24:37 -03:00
|
|
|
CHECK(_param->set_param_int("EKF2_HGT_REF", 0) == Param::Result::Success);
|
2020-07-02 09:29:00 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HeightSource::Gps:
|
2022-07-29 10:24:37 -03:00
|
|
|
CHECK(_param->set_param_int("EKF2_HGT_REF", 1) == Param::Result::Success);
|
2020-07-02 09:29:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 13:19:53 -03:00
|
|
|
void AutopilotTester::set_rc_loss_exception(AutopilotTester::RcLossException mask)
|
|
|
|
{
|
|
|
|
switch (mask) {
|
|
|
|
case RcLossException::Mission:
|
|
|
|
CHECK(_param->set_param_int("COM_RCL_EXCEPT", 1 << 0) == Param::Result::Success);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RcLossException::Hold:
|
|
|
|
CHECK(_param->set_param_int("COM_RCL_EXCEPT", 1 << 1) == Param::Result::Success);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RcLossException::Offboard:
|
|
|
|
CHECK(_param->set_param_int("COM_RCL_EXCEPT", 1 << 2) == Param::Result::Success);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:14:18 -03:00
|
|
|
void AutopilotTester::arm()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto result = _action->arm();
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(result == Action::Result::Success);
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::takeoff()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto result = _action->takeoff();
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(result == Action::Result::Success);
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::land()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto result = _action->land();
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(result == Action::Result::Success);
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
2020-01-25 11:04:02 -04:00
|
|
|
void AutopilotTester::transition_to_fixedwing()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto result = _action->transition_to_fixedwing();
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(result == Action::Result::Success);
|
2020-01-25 11:04:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::transition_to_multicopter()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto result = _action->transition_to_multicopter();
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(result == Action::Result::Success);
|
2020-01-25 11:04:02 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 06:42:06 -03:00
|
|
|
void AutopilotTester::wait_until_disarmed(std::chrono::seconds timeout_duration)
|
2019-10-02 15:14:18 -03:00
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
REQUIRE(poll_condition_with_timeout(
|
2020-05-05 06:42:06 -03:00
|
|
|
[this]() { return !_telemetry->armed(); }, timeout_duration));
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::wait_until_hovering()
|
|
|
|
{
|
2021-12-28 08:42:00 -04:00
|
|
|
wait_for_landed_state(Telemetry::LandedState::InAir, std::chrono::seconds(45));
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:52:14 -04:00
|
|
|
void AutopilotTester::wait_until_altitude(float rel_altitude_m, std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_telemetry->subscribe_position([&prom, rel_altitude_m, this](Telemetry::Position new_position) {
|
|
|
|
if (fabs(rel_altitude_m - new_position.relative_altitude_m) <= 0.5) {
|
|
|
|
_telemetry->subscribe_position(nullptr);
|
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(timeout) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
2023-10-20 08:37:54 -03:00
|
|
|
void AutopilotTester::wait_until_fixedwing(std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
REQUIRE(poll_condition_with_timeout(
|
|
|
|
[this]() { return _telemetry->vtol_state() == Telemetry::VtolState::Fw; }, timeout));
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:14:18 -03:00
|
|
|
void AutopilotTester::prepare_square_mission(MissionOptions mission_options)
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto ct = get_coordinate_transformation();
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2020-06-10 14:26:11 -03:00
|
|
|
Mission::MissionPlan mission_plan {};
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({mission_options.leg_length_m, 0.}, mission_options, ct));
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({mission_options.leg_length_m, mission_options.leg_length_m},
|
|
|
|
mission_options, ct));
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({0., mission_options.leg_length_m}, mission_options, ct));
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2020-03-17 12:15:34 -03:00
|
|
|
_mission->set_return_to_launch_after_mission(mission_options.rtl_at_end);
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2021-02-12 06:39:56 -04:00
|
|
|
REQUIRE(_mission->upload_mission(mission_plan) == Mission::Result::Success);
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
2020-07-09 12:36:24 -03:00
|
|
|
void AutopilotTester::prepare_straight_mission(MissionOptions mission_options)
|
|
|
|
{
|
|
|
|
const auto ct = get_coordinate_transformation();
|
|
|
|
|
|
|
|
Mission::MissionPlan mission_plan {};
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({0, 0.}, mission_options, ct));
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({mission_options.leg_length_m, 0}, mission_options, ct));
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({2 * mission_options.leg_length_m, 0}, mission_options, ct));
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({3 * mission_options.leg_length_m, 0}, mission_options, ct));
|
|
|
|
mission_plan.mission_items.push_back(create_mission_item({4 * mission_options.leg_length_m, 0}, mission_options, ct));
|
|
|
|
|
|
|
|
_mission->set_return_to_launch_after_mission(mission_options.rtl_at_end);
|
|
|
|
|
2021-02-12 06:39:56 -04:00
|
|
|
REQUIRE(_mission->upload_mission(mission_plan) == Mission::Result::Success);
|
2020-07-09 12:36:24 -03:00
|
|
|
}
|
|
|
|
|
2019-10-02 15:14:18 -03:00
|
|
|
void AutopilotTester::execute_mission()
|
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
std::promise<void> prom;
|
|
|
|
auto fut = prom.get_future();
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2021-11-18 10:37:54 -04:00
|
|
|
|
|
|
|
REQUIRE(poll_condition_with_timeout(
|
|
|
|
[this]() { return _mission->start_mission() == Mission::Result::Success; }, std::chrono::seconds(3)));
|
2019-10-02 15:14:18 -03:00
|
|
|
|
2020-03-17 12:15:34 -03:00
|
|
|
// TODO: Adapt time limit based on mission size, flight speed, sim speed factor, etc.
|
2019-10-03 06:20:59 -03:00
|
|
|
|
2021-12-28 08:42:00 -04:00
|
|
|
wait_for_mission_finished(std::chrono::seconds(90));
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:18:09 -03:00
|
|
|
void AutopilotTester::execute_mission_and_lose_gps()
|
|
|
|
{
|
|
|
|
CHECK(_param->set_param_int("SYS_FAILURE_EN", 1) == Param::Result::Success);
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
start_and_wait_for_mission_sequence(1);
|
2020-06-11 09:18:09 -03:00
|
|
|
|
2021-02-10 08:33:28 -04:00
|
|
|
CHECK(_failure->inject(Failure::FailureUnit::SensorGps, Failure::FailureType::Off, 0) == Failure::Result::Success);
|
2020-06-11 09:18:09 -03:00
|
|
|
|
2020-06-26 06:25:41 -03:00
|
|
|
// We expect that a blind land is performed.
|
2021-02-10 08:33:28 -04:00
|
|
|
wait_for_flight_mode(Telemetry::FlightMode::Land, std::chrono::seconds(30));
|
2020-06-11 09:18:09 -03:00
|
|
|
}
|
|
|
|
|
2020-06-26 06:25:41 -03:00
|
|
|
void AutopilotTester::execute_mission_and_lose_mag()
|
|
|
|
{
|
|
|
|
CHECK(_param->set_param_int("SYS_FAILURE_EN", 1) == Param::Result::Success);
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
start_and_wait_for_mission_sequence(1);
|
2020-06-26 06:25:41 -03:00
|
|
|
|
2021-02-10 08:33:28 -04:00
|
|
|
CHECK(_failure->inject(Failure::FailureUnit::SensorMag, Failure::FailureType::Off, 0) == Failure::Result::Success);
|
2020-06-26 06:25:41 -03:00
|
|
|
|
|
|
|
// We except the mission to continue without mag just fine.
|
|
|
|
REQUIRE(poll_condition_with_timeout(
|
|
|
|
[this]() {
|
|
|
|
auto progress = _mission->mission_progress();
|
|
|
|
return progress.current == progress.total;
|
2020-07-31 04:41:08 -03:00
|
|
|
}, std::chrono::seconds(90)));
|
2020-06-26 06:44:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::execute_mission_and_lose_baro()
|
|
|
|
{
|
|
|
|
CHECK(_param->set_param_int("SYS_FAILURE_EN", 1) == Param::Result::Success);
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
start_and_wait_for_mission_sequence(1);
|
2020-06-26 06:44:53 -03:00
|
|
|
|
2021-02-10 08:33:28 -04:00
|
|
|
CHECK(_failure->inject(Failure::FailureUnit::SensorBaro, Failure::FailureType::Off, 0) == Failure::Result::Success);
|
2020-06-26 06:44:53 -03:00
|
|
|
|
|
|
|
// We except the mission to continue without baro just fine.
|
|
|
|
REQUIRE(poll_condition_with_timeout(
|
|
|
|
[this]() {
|
|
|
|
auto progress = _mission->mission_progress();
|
|
|
|
return progress.current == progress.total;
|
2020-07-31 04:41:08 -03:00
|
|
|
}, std::chrono::seconds(90)));
|
2020-06-26 09:48:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::execute_mission_and_get_baro_stuck()
|
|
|
|
{
|
|
|
|
CHECK(_param->set_param_int("SYS_FAILURE_EN", 1) == Param::Result::Success);
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
start_and_wait_for_mission_sequence(1);
|
2020-06-26 06:44:53 -03:00
|
|
|
|
2021-02-10 08:33:28 -04:00
|
|
|
CHECK(_failure->inject(Failure::FailureUnit::SensorBaro, Failure::FailureType::Stuck, 0) == Failure::Result::Success);
|
2020-06-26 09:48:53 -03:00
|
|
|
|
|
|
|
// We except the mission to continue with a stuck baro just fine.
|
|
|
|
REQUIRE(poll_condition_with_timeout(
|
|
|
|
[this]() {
|
|
|
|
auto progress = _mission->mission_progress();
|
|
|
|
return progress.current == progress.total;
|
2020-07-31 04:41:08 -03:00
|
|
|
}, std::chrono::seconds(90)));
|
2020-06-26 06:25:41 -03:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:43:51 -03:00
|
|
|
void AutopilotTester::execute_mission_and_get_mag_stuck()
|
|
|
|
{
|
|
|
|
CHECK(_param->set_param_int("SYS_FAILURE_EN", 1) == Param::Result::Success);
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
start_and_wait_for_mission_sequence(1);
|
2020-06-26 11:43:51 -03:00
|
|
|
|
2021-02-10 08:33:28 -04:00
|
|
|
CHECK(_failure->inject(Failure::FailureUnit::SensorMag, Failure::FailureType::Stuck, 0) == Failure::Result::Success);
|
2020-06-26 11:43:51 -03:00
|
|
|
|
|
|
|
// We except the mission to continue with a stuck mag just fine.
|
|
|
|
REQUIRE(poll_condition_with_timeout(
|
|
|
|
[this]() {
|
|
|
|
auto progress = _mission->mission_progress();
|
|
|
|
return progress.current == progress.total;
|
2020-07-31 04:41:08 -03:00
|
|
|
}, std::chrono::seconds(120)));
|
2020-06-26 11:43:51 -03:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:41:28 -04:00
|
|
|
CoordinateTransformation AutopilotTester::get_coordinate_transformation()
|
2019-10-02 15:14:18 -03:00
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
const auto home = _telemetry->home();
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(std::isfinite(home.latitude_deg));
|
|
|
|
CHECK(std::isfinite(home.longitude_deg));
|
|
|
|
return CoordinateTransformation({home.latitude_deg, home.longitude_deg});
|
2019-10-02 15:14:18 -03:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:26:11 -03:00
|
|
|
Mission::MissionItem AutopilotTester::create_mission_item(
|
2020-03-17 12:15:34 -03:00
|
|
|
const CoordinateTransformation::LocalCoordinate &local_coordinate,
|
|
|
|
const MissionOptions &mission_options,
|
|
|
|
const CoordinateTransformation &ct)
|
2019-10-02 15:14:18 -03:00
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
auto mission_item = Mission::MissionItem{};
|
2020-03-17 12:15:34 -03:00
|
|
|
const auto pos_north = ct.global_from_local(local_coordinate);
|
2020-06-10 14:26:11 -03:00
|
|
|
mission_item.latitude_deg = pos_north.latitude_deg;
|
|
|
|
mission_item.longitude_deg = pos_north.longitude_deg;
|
|
|
|
mission_item.relative_altitude_m = mission_options.relative_altitude_m;
|
2020-07-09 12:36:24 -03:00
|
|
|
mission_item.is_fly_through = mission_options.fly_through;
|
2020-03-17 12:15:34 -03:00
|
|
|
return mission_item;
|
2019-10-03 06:29:22 -03:00
|
|
|
}
|
|
|
|
|
2021-03-26 07:06:31 -03:00
|
|
|
void AutopilotTester::load_qgc_mission_raw_and_move_here(const std::string &plan_file)
|
2021-03-25 13:37:03 -03:00
|
|
|
{
|
2021-03-26 07:06:31 -03:00
|
|
|
auto import_result = _mission_raw->import_qgroundcontrol_mission(plan_file);
|
2021-03-25 13:37:03 -03:00
|
|
|
REQUIRE(import_result.first == MissionRaw::Result::Success);
|
|
|
|
|
2021-03-26 07:06:31 -03:00
|
|
|
move_mission_raw_here(import_result.second.mission_items);
|
|
|
|
|
2021-03-25 13:37:03 -03:00
|
|
|
REQUIRE(_mission_raw->upload_mission(import_result.second.mission_items) == MissionRaw::Result::Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::execute_mission_raw()
|
|
|
|
{
|
|
|
|
REQUIRE(_mission->start_mission() == Mission::Result::Success);
|
|
|
|
|
|
|
|
// TODO: Adapt time limit based on mission size, flight speed, sim speed factor, etc.
|
|
|
|
|
2021-04-06 03:08:29 -03:00
|
|
|
wait_for_mission_raw_finished(std::chrono::seconds(120));
|
2021-03-25 13:37:03 -03:00
|
|
|
}
|
|
|
|
|
2019-10-03 06:29:22 -03:00
|
|
|
void AutopilotTester::execute_rtl()
|
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(Action::Result::Success == _action->return_to_launch());
|
2019-10-07 10:04:30 -03:00
|
|
|
}
|
2020-01-30 06:57:48 -04:00
|
|
|
|
2021-11-26 11:52:14 -04:00
|
|
|
void AutopilotTester::execute_land()
|
|
|
|
{
|
|
|
|
REQUIRE(Action::Result::Success == _action->land());
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:26:11 -03:00
|
|
|
void AutopilotTester::offboard_goto(const Offboard::PositionNedYaw &target, float acceptance_radius_m,
|
2020-03-17 12:15:34 -03:00
|
|
|
std::chrono::seconds timeout_duration)
|
2020-01-30 06:57:48 -04:00
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
_offboard->set_position_ned(target);
|
2020-06-10 14:26:11 -03:00
|
|
|
REQUIRE(_offboard->start() == Offboard::Result::Success);
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(poll_condition_with_timeout(
|
|
|
|
[ = ]() { return estimated_position_close_to(target, acceptance_radius_m); }, timeout_duration));
|
2021-02-09 03:45:38 -04:00
|
|
|
std::cout << time_str() << "Target position reached" << std::endl;
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-10 07:57:31 -03:00
|
|
|
void AutopilotTester::check_mission_item_speed_above(int item_index, float min_speed_m_s)
|
2020-07-09 12:36:24 -03:00
|
|
|
{
|
2020-07-10 07:57:31 -03:00
|
|
|
|
|
|
|
_telemetry->set_rate_velocity_ned(10);
|
|
|
|
_telemetry->subscribe_velocity_ned([item_index, min_speed_m_s, this](Telemetry::VelocityNed velocity) {
|
2020-07-09 12:36:24 -03:00
|
|
|
float horizontal = std::hypot(velocity.north_m_s, velocity.east_m_s);
|
|
|
|
auto progress = _mission->mission_progress();
|
|
|
|
|
|
|
|
if (progress.current == item_index) {
|
2020-07-10 07:57:31 -03:00
|
|
|
CHECK(horizontal > min_speed_m_s);
|
2020-07-09 12:36:24 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-31 04:45:12 -03:00
|
|
|
void AutopilotTester::fly_forward_in_posctl()
|
|
|
|
{
|
2020-08-05 13:20:58 -03:00
|
|
|
const unsigned manual_control_rate_hz = 50;
|
2020-07-31 04:45:12 -03:00
|
|
|
|
2020-08-06 03:01:30 -03:00
|
|
|
// Send something to make sure RC is available.
|
2021-02-12 12:07:18 -04:00
|
|
|
for (unsigned i = 0; i < 1 * manual_control_rate_hz; ++i) {
|
2020-08-06 03:01:30 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.f, 0.f, 0.5f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-08-06 03:01:30 -03:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:36:00 -03:00
|
|
|
CHECK(_manual_control->start_position_control() == ManualControl::Result::Success);
|
|
|
|
|
2021-02-12 06:40:25 -04:00
|
|
|
// Climb up for 20 seconds
|
|
|
|
for (unsigned i = 0; i < 20 * manual_control_rate_hz; ++i) {
|
2020-07-31 04:45:12 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.f, 0.f, 1.f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-07-31 04:45:12 -03:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:01:03 -04:00
|
|
|
// Fly forward for 60 seconds
|
|
|
|
for (unsigned i = 0; i < 60 * manual_control_rate_hz; ++i) {
|
2020-07-31 04:45:12 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.5f, 0.f, 0.5f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-07-31 04:45:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Descend until disarmed
|
2020-11-11 17:01:03 -04:00
|
|
|
for (unsigned i = 0; i < 60 * manual_control_rate_hz; ++i) {
|
2020-07-31 04:45:12 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.f, 0.f, 0.0f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-07-31 04:45:12 -03:00
|
|
|
|
|
|
|
if (!_telemetry->in_air()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::fly_forward_in_altctl()
|
|
|
|
{
|
2020-08-06 03:01:30 -03:00
|
|
|
const unsigned manual_control_rate_hz = 50;
|
|
|
|
|
2020-07-31 04:45:12 -03:00
|
|
|
// Send something to make sure RC is available.
|
2021-02-12 12:07:18 -04:00
|
|
|
for (unsigned i = 0; i < 1 * manual_control_rate_hz; ++i) {
|
2020-08-06 03:01:30 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.f, 0.f, 0.5f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-08-06 03:01:30 -03:00
|
|
|
}
|
2020-07-31 04:45:12 -03:00
|
|
|
|
2020-08-06 03:01:30 -03:00
|
|
|
CHECK(_manual_control->start_altitude_control() == ManualControl::Result::Success);
|
2020-07-31 04:45:12 -03:00
|
|
|
|
2021-02-12 06:40:25 -04:00
|
|
|
// Climb up for 20 seconds
|
|
|
|
for (unsigned i = 0; i < 20 * manual_control_rate_hz; ++i) {
|
2020-07-31 04:45:12 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.f, 0.f, 1.f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-07-31 04:45:12 -03:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:01:03 -04:00
|
|
|
// Fly forward for 60 seconds
|
|
|
|
for (unsigned i = 0; i < 60 * manual_control_rate_hz; ++i) {
|
2020-07-31 04:45:12 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.5f, 0.f, 0.5f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-07-31 04:45:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Descend until disarmed
|
2020-11-11 17:01:03 -04:00
|
|
|
for (unsigned i = 0; i < 60 * manual_control_rate_hz; ++i) {
|
2020-07-31 04:45:12 -03:00
|
|
|
CHECK(_manual_control->set_manual_control_input(0.f, 0.f, 0.0f, 0.f) == ManualControl::Result::Success);
|
2021-02-15 11:17:39 -04:00
|
|
|
sleep_for(std::chrono::milliseconds(1000 / manual_control_rate_hz));
|
2020-07-31 04:45:12 -03:00
|
|
|
|
|
|
|
if (!_telemetry->in_air()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-04 03:22:30 -03:00
|
|
|
void AutopilotTester::fly_forward_in_offboard_attitude()
|
|
|
|
{
|
|
|
|
// This test does not depend on valid position estimate.
|
|
|
|
// Wait for raw gps & stable attitude estimate
|
|
|
|
CHECK(poll_condition_with_timeout(
|
|
|
|
[this]() {
|
|
|
|
auto attitude = _telemetry->attitude_euler();
|
|
|
|
return _telemetry->raw_gps().altitude_ellipsoid_m > 0.f && fabsf(attitude.roll_deg) < 5.f
|
|
|
|
&& fabsf(attitude.pitch_deg) < 5.f;
|
|
|
|
}, std::chrono::seconds(20)));
|
|
|
|
|
|
|
|
const float start_altitude_ellipsoid_m = _telemetry->raw_gps().altitude_ellipsoid_m;
|
|
|
|
|
|
|
|
Offboard::Attitude attitude{};
|
|
|
|
_offboard->set_attitude(attitude);
|
|
|
|
REQUIRE(_offboard->start() == Offboard::Result::Success);
|
|
|
|
|
|
|
|
// Wait until we can arm
|
|
|
|
CHECK(poll_condition_with_timeout(
|
|
|
|
[this]() { return _telemetry->health().is_armable; }, std::chrono::seconds(20)));
|
|
|
|
arm();
|
|
|
|
|
|
|
|
const unsigned offboard_rate_hz = 50;
|
|
|
|
|
|
|
|
// Climb
|
|
|
|
const float climb_altitude_m = 10.f;
|
|
|
|
attitude.thrust_value = 0.8f;
|
|
|
|
|
|
|
|
while (_telemetry->raw_gps().altitude_ellipsoid_m - start_altitude_ellipsoid_m < climb_altitude_m) {
|
|
|
|
CHECK(_offboard->set_attitude(attitude) == Offboard::Result::Success);
|
|
|
|
sleep_for(std::chrono::milliseconds(1000 / offboard_rate_hz));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fly forward for 3s
|
|
|
|
attitude.thrust_value = 0.8f;
|
|
|
|
attitude.pitch_deg = -20.f;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 3 * offboard_rate_hz; ++i) {
|
|
|
|
CHECK(_offboard->set_attitude(attitude) == Offboard::Result::Success);
|
|
|
|
sleep_for(std::chrono::milliseconds(1000 / offboard_rate_hz));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check attitude
|
|
|
|
auto attitude_estimate = _telemetry->attitude_euler();
|
|
|
|
CHECK(fabsf(attitude.roll_deg - attitude_estimate.roll_deg) < 5.f);
|
|
|
|
CHECK(fabsf(attitude.pitch_deg - attitude_estimate.pitch_deg) < 5.f);
|
|
|
|
|
|
|
|
// Descend
|
2023-08-08 06:30:12 -03:00
|
|
|
attitude.thrust_value = 0.4f;
|
2023-08-04 03:22:30 -03:00
|
|
|
attitude.pitch_deg = 0.f;
|
|
|
|
|
2023-08-08 06:30:12 -03:00
|
|
|
for (unsigned i = 0; i < 6 * offboard_rate_hz; ++i) {
|
2023-08-04 03:22:30 -03:00
|
|
|
CHECK(_offboard->set_attitude(attitude) == Offboard::Result::Success);
|
|
|
|
sleep_for(std::chrono::milliseconds(1000 / offboard_rate_hz));
|
|
|
|
}
|
|
|
|
|
|
|
|
attitude.thrust_value = 0.0f;
|
|
|
|
CHECK(_offboard->set_attitude(attitude) == Offboard::Result::Success);
|
|
|
|
}
|
2020-07-31 04:45:12 -03:00
|
|
|
|
2021-11-26 11:52:14 -04:00
|
|
|
void AutopilotTester::start_checking_altitude(const float max_deviation_m)
|
|
|
|
{
|
|
|
|
std::array<float, 3> initial_position = get_current_position_ned();
|
|
|
|
float target_altitude = initial_position[2];
|
|
|
|
|
|
|
|
_telemetry->subscribe_position([target_altitude, max_deviation_m, this](Telemetry::Position new_position) {
|
|
|
|
const float current_deviation = fabs((-target_altitude) - new_position.relative_altitude_m);
|
|
|
|
CHECK(current_deviation <= max_deviation_m);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::stop_checking_altitude()
|
|
|
|
{
|
|
|
|
_telemetry->subscribe_position(nullptr);
|
|
|
|
}
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
void AutopilotTester::check_tracks_mission_raw(float corridor_radius_m, bool reverse)
|
|
|
|
{
|
|
|
|
auto mission_raw = _mission_raw->download_mission();
|
|
|
|
CHECK(mission_raw.first == MissionRaw::Result::Success);
|
|
|
|
|
|
|
|
auto mission_items = mission_raw.second;
|
|
|
|
auto ct = get_coordinate_transformation();
|
|
|
|
|
|
|
|
_telemetry->set_rate_position_velocity_ned(5);
|
|
|
|
_telemetry->subscribe_position_velocity_ned([ct, mission_items, corridor_radius_m, reverse,
|
|
|
|
this](Telemetry::PositionVelocityNed position_velocity_ned) {
|
|
|
|
auto progress = _mission_raw->mission_progress();
|
|
|
|
|
|
|
|
|
|
|
|
std::function<std::array<float, 3>(std::vector<mavsdk::MissionRaw::MissionItem>, unsigned, mavsdk::geometry::CoordinateTransformation)>
|
|
|
|
get_waypoint_for_sequence = [](std::vector<mavsdk::MissionRaw::MissionItem> mission_items, int sequence, auto ct) {
|
|
|
|
for (auto waypoint : mission_items) {
|
|
|
|
|
|
|
|
if (waypoint.seq == (uint32_t)sequence) {
|
|
|
|
return get_local_mission_item_from_raw_item<float>(waypoint, ct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::array<float, 3>({0.0f, 0.0f, 0.0f});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (progress.current > 0 && progress.current < progress.total) {
|
|
|
|
// Get shortest distance of current position to 3D line between previous and next waypoint
|
|
|
|
|
|
|
|
std::array<float, 3> current { position_velocity_ned.position.north_m,
|
|
|
|
position_velocity_ned.position.east_m,
|
|
|
|
position_velocity_ned.position.down_m };
|
|
|
|
std::array<float, 3> wp_prev = get_waypoint_for_sequence(mission_items,
|
|
|
|
reverse ? progress.current + 1 : progress.current - 1, ct);
|
|
|
|
std::array<float, 3> wp_next = get_waypoint_for_sequence(mission_items, progress.current, ct);
|
|
|
|
|
|
|
|
float distance_to_trajectory = point_to_line_distance(current, wp_prev, wp_next);
|
|
|
|
|
|
|
|
CHECK(distance_to_trajectory < corridor_radius_m);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-23 04:34:26 -03:00
|
|
|
void AutopilotTester::check_mission_land_within(float acceptance_radius_m)
|
|
|
|
{
|
|
|
|
auto mission_raw = _mission_raw->download_mission();
|
|
|
|
CHECK(mission_raw.first == MissionRaw::Result::Success);
|
|
|
|
|
|
|
|
// Get last mission item
|
|
|
|
MissionRaw::MissionItem land_mission_item = mission_raw.second.back();
|
|
|
|
bool is_landing_item = (land_mission_item.command == 85) || (land_mission_item.command == 21);
|
|
|
|
CHECK(is_landing_item);
|
|
|
|
Telemetry::GroundTruth land_coord{};
|
|
|
|
land_coord.latitude_deg = static_cast<double>(land_mission_item.x) / 1E7;
|
|
|
|
land_coord.longitude_deg = static_cast<double>(land_mission_item.y) / 1E7;
|
|
|
|
|
|
|
|
CHECK(ground_truth_horizontal_position_close_to(land_coord, acceptance_radius_m));
|
|
|
|
}
|
|
|
|
|
2020-07-09 12:39:13 -03:00
|
|
|
void AutopilotTester::check_tracks_mission(float corridor_radius_m)
|
|
|
|
{
|
|
|
|
auto mission = _mission->download_mission();
|
|
|
|
CHECK(mission.first == Mission::Result::Success);
|
|
|
|
|
|
|
|
std::vector<Mission::MissionItem> mission_items = mission.second.mission_items;
|
|
|
|
auto ct = get_coordinate_transformation();
|
|
|
|
|
|
|
|
_telemetry->set_rate_position_velocity_ned(5);
|
2020-07-10 07:59:30 -03:00
|
|
|
_telemetry->subscribe_position_velocity_ned([ct, mission_items, corridor_radius_m,
|
|
|
|
this](Telemetry::PositionVelocityNed position_velocity_ned) {
|
2020-07-09 12:39:13 -03:00
|
|
|
auto progress = _mission->mission_progress();
|
|
|
|
|
|
|
|
if (progress.current > 0 && progress.current < progress.total) {
|
|
|
|
// Get shortest distance of current position to 3D line between previous and next waypoint
|
2020-07-10 07:59:30 -03:00
|
|
|
|
2020-07-09 12:39:13 -03:00
|
|
|
std::array<float, 3> current { position_velocity_ned.position.north_m,
|
|
|
|
position_velocity_ned.position.east_m,
|
|
|
|
position_velocity_ned.position.down_m };
|
2020-07-29 09:04:02 -03:00
|
|
|
std::array<float, 3> wp_prev = get_local_mission_item<float>(mission_items[progress.current - 1], ct);
|
|
|
|
std::array<float, 3> wp_next = get_local_mission_item<float>(mission_items[progress.current], ct);
|
2020-07-09 12:39:13 -03:00
|
|
|
|
2020-07-10 07:59:30 -03:00
|
|
|
float distance_to_trajectory = point_to_line_distance(current, wp_prev, wp_next);
|
2020-07-09 12:39:13 -03:00
|
|
|
|
|
|
|
CHECK(distance_to_trajectory < corridor_radius_m);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-20 04:11:17 -03:00
|
|
|
void AutopilotTester::check_current_altitude(float target_rel_altitude_m, float max_distance_m)
|
|
|
|
{
|
|
|
|
CHECK(std::abs(_telemetry->position().relative_altitude_m - target_rel_altitude_m) <= max_distance_m);
|
|
|
|
}
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
void AutopilotTester::execute_rtl_when_reaching_mission_sequence(int sequence_number)
|
|
|
|
{
|
|
|
|
start_and_wait_for_mission_sequence_raw(sequence_number);
|
|
|
|
execute_rtl();
|
|
|
|
}
|
|
|
|
|
2023-10-20 08:36:14 -03:00
|
|
|
void AutopilotTester::send_custom_mavlink_command(const MavlinkPassthrough::CommandInt &command)
|
|
|
|
{
|
|
|
|
_mavlink_passthrough->send_command_int(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::send_custom_mavlink_message(mavlink_message_t &message)
|
|
|
|
{
|
|
|
|
_mavlink_passthrough->send_message(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::add_mavlink_message_callback(uint16_t message_id,
|
|
|
|
std::function< void(const mavlink_message_t &)> callback)
|
|
|
|
{
|
|
|
|
_mavlink_passthrough->subscribe_message_async(message_id, std::move(callback));
|
|
|
|
}
|
|
|
|
|
2021-11-26 11:52:14 -04:00
|
|
|
std::array<float, 3> AutopilotTester::get_current_position_ned()
|
|
|
|
{
|
|
|
|
mavsdk::Telemetry::PositionVelocityNed position_velocity_ned = _telemetry->position_velocity_ned();
|
|
|
|
std::array<float, 3> position_ned{position_velocity_ned.position.north_m, position_velocity_ned.position.east_m, position_velocity_ned.position.down_m};
|
|
|
|
return position_ned;
|
|
|
|
}
|
2020-07-09 12:39:13 -03:00
|
|
|
|
2020-01-30 06:57:48 -04:00
|
|
|
void AutopilotTester::offboard_land()
|
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
Offboard::VelocityNedYaw land_velocity;
|
2020-03-17 12:15:34 -03:00
|
|
|
land_velocity.north_m_s = 0.0f;
|
|
|
|
land_velocity.east_m_s = 0.0f;
|
|
|
|
land_velocity.down_m_s = 1.0f;
|
|
|
|
land_velocity.yaw_deg = 0.0f;
|
|
|
|
_offboard->set_velocity_ned(land_velocity);
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:26:11 -03:00
|
|
|
bool AutopilotTester::estimated_position_close_to(const Offboard::PositionNedYaw &target_pos, float acceptance_radius_m)
|
2020-01-30 06:57:48 -04:00
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
Telemetry::PositionNed est_pos = _telemetry->position_velocity_ned().position;
|
2020-07-31 04:41:08 -03:00
|
|
|
const float distance_m = std::sqrt(sq(est_pos.north_m - target_pos.north_m) +
|
|
|
|
sq(est_pos.east_m - target_pos.east_m) +
|
|
|
|
sq(est_pos.down_m - target_pos.down_m));
|
|
|
|
const bool pass = distance_m < acceptance_radius_m;
|
|
|
|
|
|
|
|
if (!pass) {
|
2021-02-09 03:45:38 -04:00
|
|
|
std::cout << time_str() << "distance: " << distance_m << ", " << "acceptance: " << acceptance_radius_m << std::endl;
|
2020-07-31 04:41:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:26:11 -03:00
|
|
|
bool AutopilotTester::estimated_horizontal_position_close_to(const Offboard::PositionNedYaw &target_pos,
|
2020-03-17 12:15:34 -03:00
|
|
|
float acceptance_radius_m)
|
2020-01-30 06:57:48 -04:00
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
Telemetry::PositionNed est_pos = _telemetry->position_velocity_ned().position;
|
2020-03-17 12:15:34 -03:00
|
|
|
return sq(est_pos.north_m - target_pos.north_m) +
|
|
|
|
sq(est_pos.east_m - target_pos.east_m) < sq(acceptance_radius_m);
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::request_ground_truth()
|
|
|
|
{
|
2020-06-10 14:26:11 -03:00
|
|
|
CHECK(_telemetry->set_rate_ground_truth(15) == Telemetry::Result::Success);
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
|
|
|
|
2020-03-17 12:15:34 -03:00
|
|
|
bool AutopilotTester::ground_truth_horizontal_position_close_to(const Telemetry::GroundTruth &target_pos,
|
|
|
|
float acceptance_radius_m)
|
2020-01-30 06:57:48 -04:00
|
|
|
{
|
2020-03-17 12:15:34 -03:00
|
|
|
CHECK(std::isfinite(target_pos.latitude_deg));
|
|
|
|
CHECK(std::isfinite(target_pos.longitude_deg));
|
|
|
|
using GlobalCoordinate = CoordinateTransformation::GlobalCoordinate;
|
|
|
|
using LocalCoordinate = CoordinateTransformation::LocalCoordinate;
|
|
|
|
CoordinateTransformation ct(GlobalCoordinate{target_pos.latitude_deg, target_pos.longitude_deg});
|
|
|
|
|
|
|
|
Telemetry::GroundTruth current_pos = _telemetry->ground_truth();
|
|
|
|
CHECK(std::isfinite(current_pos.latitude_deg));
|
|
|
|
CHECK(std::isfinite(current_pos.longitude_deg));
|
2020-06-10 14:26:11 -03:00
|
|
|
GlobalCoordinate global_current;
|
|
|
|
global_current.latitude_deg = current_pos.latitude_deg;
|
|
|
|
global_current.longitude_deg = current_pos.longitude_deg;
|
|
|
|
LocalCoordinate local_pos = ct.local_from_global(global_current);
|
2020-07-31 04:45:12 -03:00
|
|
|
const double distance_m = sqrt(sq(local_pos.north_m) + sq(local_pos.east_m));
|
|
|
|
const bool pass = distance_m < acceptance_radius_m;
|
2020-05-26 05:41:40 -03:00
|
|
|
|
|
|
|
if (!pass) {
|
2021-02-09 03:45:38 -04:00
|
|
|
std::cout << time_str() << "target_pos.lat: " << target_pos.latitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "target_pos.lon: " << target_pos.longitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "current.lat: " << current_pos.latitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "current.lon: " << current_pos.longitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "Distance: " << distance_m << std::endl;
|
|
|
|
std::cout << time_str() << "Acceptance radius: " << acceptance_radius_m << std::endl;
|
2020-05-26 05:41:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
2020-01-30 06:57:48 -04:00
|
|
|
}
|
2020-07-09 11:42:13 -03:00
|
|
|
|
2020-07-31 04:45:12 -03:00
|
|
|
bool AutopilotTester::ground_truth_horizontal_position_far_from(const Telemetry::GroundTruth &target_pos,
|
|
|
|
float min_distance_m)
|
|
|
|
{
|
|
|
|
CHECK(std::isfinite(target_pos.latitude_deg));
|
|
|
|
CHECK(std::isfinite(target_pos.longitude_deg));
|
|
|
|
using GlobalCoordinate = CoordinateTransformation::GlobalCoordinate;
|
|
|
|
using LocalCoordinate = CoordinateTransformation::LocalCoordinate;
|
|
|
|
CoordinateTransformation ct(GlobalCoordinate{target_pos.latitude_deg, target_pos.longitude_deg});
|
|
|
|
|
|
|
|
Telemetry::GroundTruth current_pos = _telemetry->ground_truth();
|
|
|
|
CHECK(std::isfinite(current_pos.latitude_deg));
|
|
|
|
CHECK(std::isfinite(current_pos.longitude_deg));
|
|
|
|
GlobalCoordinate global_current;
|
|
|
|
global_current.latitude_deg = current_pos.latitude_deg;
|
|
|
|
global_current.longitude_deg = current_pos.longitude_deg;
|
|
|
|
LocalCoordinate local_pos = ct.local_from_global(global_current);
|
|
|
|
const double distance_m = sqrt(sq(local_pos.north_m) + sq(local_pos.east_m));
|
|
|
|
const bool pass = distance_m > min_distance_m;
|
|
|
|
|
|
|
|
if (!pass) {
|
2021-02-09 03:45:38 -04:00
|
|
|
std::cout << time_str() << "target_pos.lat: " << target_pos.latitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "target_pos.lon: " << target_pos.longitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "current.lat: " << current_pos.latitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "current.lon: " << current_pos.longitude_deg << std::endl;
|
|
|
|
std::cout << time_str() << "Distance: " << distance_m << std::endl;
|
|
|
|
std::cout << time_str() << "Min distance: " << min_distance_m << std::endl;
|
2020-07-31 04:45:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
void AutopilotTester::start_and_wait_for_mission_sequence(int sequence_number)
|
2021-02-10 08:33:28 -04:00
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
_mission->subscribe_mission_progress([&prom, this, sequence_number](Mission::MissionProgress progress) {
|
2021-02-10 08:33:28 -04:00
|
|
|
std::cout << time_str() << "Progress: " << progress.current << "/" << progress.total << std::endl;
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
if (progress.current >= sequence_number) {
|
2021-02-10 08:33:28 -04:00
|
|
|
_mission->subscribe_mission_progress(nullptr);
|
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(_mission->start_mission() == Mission::Result::Success);
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(std::chrono::seconds(60)) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
2023-01-18 04:34:56 -04:00
|
|
|
void AutopilotTester::start_and_wait_for_mission_sequence_raw(int sequence_number)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_mission_raw->subscribe_mission_progress([&prom, this, sequence_number](MissionRaw::MissionProgress progress) {
|
|
|
|
std::cout << time_str() << "Progress: " << progress.current << "/" << progress.total << std::endl;
|
|
|
|
|
|
|
|
if (progress.current >= sequence_number) {
|
|
|
|
_mission_raw->subscribe_mission_progress(nullptr);
|
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(_mission_raw->start_mission() == MissionRaw::Result::Success);
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(std::chrono::seconds(60)) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:33:28 -04:00
|
|
|
void AutopilotTester::wait_for_flight_mode(Telemetry::FlightMode flight_mode, std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_telemetry->subscribe_flight_mode([&prom, flight_mode, this](Telemetry::FlightMode new_flight_mode) {
|
|
|
|
if (new_flight_mode == flight_mode) {
|
|
|
|
_telemetry->subscribe_flight_mode(nullptr);
|
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(timeout) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::wait_for_landed_state(Telemetry::LandedState landed_state, std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_telemetry->subscribe_landed_state([&prom, landed_state, this](Telemetry::LandedState new_landed_state) {
|
|
|
|
if (new_landed_state == landed_state) {
|
|
|
|
_telemetry->subscribe_landed_state(nullptr);
|
2021-11-26 11:52:14 -04:00
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(timeout) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutopilotTester::wait_until_speed_lower_than(float speed, std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_telemetry->subscribe_position_velocity_ned([&prom, speed, this](Telemetry::PositionVelocityNed position_velocity_ned) {
|
|
|
|
std::array<float, 3> current_velocity;
|
|
|
|
current_velocity[0] = position_velocity_ned.velocity.north_m_s;
|
|
|
|
current_velocity[1] = position_velocity_ned.velocity.east_m_s;
|
|
|
|
current_velocity[2] = position_velocity_ned.velocity.down_m_s;
|
|
|
|
const float current_speed = norm(current_velocity);
|
|
|
|
|
|
|
|
if (current_speed <= speed) {
|
|
|
|
_telemetry->subscribe_position_velocity_ned(nullptr);
|
2021-02-10 08:33:28 -04:00
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(timeout) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
2021-02-11 05:00:09 -04:00
|
|
|
void AutopilotTester::wait_for_mission_finished(std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_mission->subscribe_mission_progress([&prom, this](Mission::MissionProgress progress) {
|
|
|
|
if (progress.current == progress.total) {
|
|
|
|
_mission->subscribe_mission_progress(nullptr);
|
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(timeout) == std::future_status::ready);
|
|
|
|
}
|
2021-03-17 05:38:37 -03:00
|
|
|
|
2021-03-25 13:37:03 -03:00
|
|
|
void AutopilotTester::wait_for_mission_raw_finished(std::chrono::seconds timeout)
|
|
|
|
{
|
|
|
|
auto prom = std::promise<void> {};
|
|
|
|
auto fut = prom.get_future();
|
|
|
|
|
|
|
|
_mission_raw->subscribe_mission_progress([&prom, this](MissionRaw::MissionProgress progress) {
|
|
|
|
if (progress.current == progress.total) {
|
|
|
|
_mission_raw->subscribe_mission_progress(nullptr);
|
|
|
|
prom.set_value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(fut.wait_for(timeout) == std::future_status::ready);
|
|
|
|
}
|
|
|
|
|
2021-03-26 07:06:31 -03:00
|
|
|
void AutopilotTester::move_mission_raw_here(std::vector<MissionRaw::MissionItem> &mission_items)
|
|
|
|
{
|
|
|
|
const auto position = _telemetry->position();
|
|
|
|
REQUIRE(std::isfinite(position.latitude_deg));
|
|
|
|
REQUIRE(std::isfinite(position.longitude_deg));
|
|
|
|
|
|
|
|
auto offset_x = mission_items[0].x - static_cast<int32_t>(1e7 * position.latitude_deg);
|
2021-04-22 08:39:08 -03:00
|
|
|
auto offset_y = mission_items[0].y - static_cast<int32_t>(1e7 * position.longitude_deg);
|
2021-03-26 07:06:31 -03:00
|
|
|
|
|
|
|
for (auto &item : mission_items) {
|
|
|
|
if (item.frame == 3) { // MAV_FRAME_GLOBAL_RELATIVE_ALT
|
|
|
|
item.x -= offset_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.y -= offset_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:38:37 -03:00
|
|
|
void AutopilotTester::report_speed_factor()
|
|
|
|
{
|
|
|
|
// We check the exit flag more often than the speed factor.
|
|
|
|
unsigned counter = 0;
|
|
|
|
|
|
|
|
while (!_should_exit) {
|
|
|
|
if (counter++ % 10 == 0) {
|
|
|
|
if (_info != nullptr) {
|
|
|
|
std::cout << "Current speed factor: " << _info->get_speed_factor().second ;
|
|
|
|
|
|
|
|
if (speed_factor.has_value()) {
|
|
|
|
std::cout << " (set: " << speed_factor.value() << ')';
|
|
|
|
}
|
|
|
|
|
2021-12-22 11:50:21 -04:00
|
|
|
std::cout << std::endl;
|
2021-03-17 05:38:37 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
}
|
|
|
|
}
|