mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-16 06:13:34 +08:00
GTest functional tests that include parameters and uORB messaging (#12521)
* Add kdevelop to gitignore * Add test stubs * Rename px4_add_gtest to px4_add_unit_gtest * Add infrastructure to run functional tests * Add example tests with parameters and uorb messages * Fix memory issues in destructors in uORB manager and CDev * Add a more real-world test of the collision prevention
This commit is contained in:
@@ -33,3 +33,5 @@
|
||||
|
||||
px4_add_library(CollisionPrevention CollisionPrevention.cpp)
|
||||
target_compile_options(CollisionPrevention PRIVATE -Wno-cast-align) # TODO: fix and enable
|
||||
|
||||
px4_add_functional_gtest(SRC CollisionPreventionTest.cpp LINKLIBS CollisionPrevention )
|
||||
|
||||
@@ -53,6 +53,18 @@ CollisionPrevention::~CollisionPrevention()
|
||||
if (_mavlink_log_pub != nullptr) {
|
||||
orb_unadvertise(_mavlink_log_pub);
|
||||
}
|
||||
|
||||
if (_constraints_pub != nullptr) {
|
||||
orb_unadvertise(_constraints_pub);
|
||||
}
|
||||
|
||||
if (_obstacle_distance_pub != nullptr) {
|
||||
orb_unadvertise(_obstacle_distance_pub);
|
||||
}
|
||||
|
||||
if (_pub_vehicle_command != nullptr) {
|
||||
orb_unadvertise(_pub_vehicle_command);
|
||||
}
|
||||
}
|
||||
|
||||
void CollisionPrevention::_publishConstrainedSetpoint(const Vector2f &original_setpoint,
|
||||
@@ -102,7 +114,7 @@ void CollisionPrevention::_updateOffboardObstacleDistance(obstacle_distance_s &o
|
||||
void CollisionPrevention::_updateDistanceSensor(obstacle_distance_s &obstacle)
|
||||
{
|
||||
for (unsigned i = 0; i < ORB_MULTI_MAX_INSTANCES; i++) {
|
||||
distance_sensor_s distance_sensor;
|
||||
distance_sensor_s distance_sensor {};
|
||||
_sub_distance_sensor[i].copy(&distance_sensor);
|
||||
|
||||
// consider only instaces with updated, valid data and orientations useful for collision prevention
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2019 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 <gtest/gtest.h>
|
||||
#include <CollisionPrevention/CollisionPrevention.hpp>
|
||||
|
||||
// to run: make tests TESTFILTER=CollisionPrevention
|
||||
|
||||
class CollisionPreventionTest : public ::testing::Test
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
uORB::Manager::initialize();
|
||||
param_init();
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
param_reset_all();
|
||||
uORB::Manager::terminate();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(CollisionPreventionTest, instantiation) { CollisionPrevention cp(nullptr); }
|
||||
|
||||
TEST_F(CollisionPreventionTest, behaviorOff)
|
||||
{
|
||||
// GIVEN: a simple setup condition
|
||||
CollisionPrevention cp(nullptr);
|
||||
matrix::Vector2f original_setpoint(10, 0);
|
||||
float max_speed = 3.f;
|
||||
matrix::Vector2f curr_pos(0, 0);
|
||||
matrix::Vector2f curr_vel(2, 0);
|
||||
|
||||
// WHEN: we check if the setpoint should be modified
|
||||
matrix::Vector2f modified_setpoint = original_setpoint;
|
||||
cp.modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
|
||||
|
||||
// THEN: it should be the same
|
||||
EXPECT_EQ(original_setpoint, modified_setpoint);
|
||||
}
|
||||
|
||||
TEST_F(CollisionPreventionTest, withoutObstacleMessageNothing)
|
||||
{
|
||||
// GIVEN: a simple setup condition
|
||||
CollisionPrevention cp(nullptr);
|
||||
matrix::Vector2f original_setpoint(10, 0);
|
||||
float max_speed = 3.f;
|
||||
matrix::Vector2f curr_pos(0, 0);
|
||||
matrix::Vector2f curr_vel(2, 0);
|
||||
|
||||
// AND: a parameter handle
|
||||
param_t param = param_handle(px4::params::MPC_COL_PREV_D);
|
||||
|
||||
|
||||
// WHEN: we set the parameter check then apply the setpoint modification
|
||||
float value = 10; // try to keep 10m away from obstacles
|
||||
param_set(param, &value);
|
||||
|
||||
matrix::Vector2f modified_setpoint = original_setpoint;
|
||||
cp.modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
|
||||
|
||||
// THEN: it shouldn't interfere with the setpoint, because there isn't an obstacle
|
||||
EXPECT_EQ(original_setpoint, modified_setpoint);
|
||||
}
|
||||
|
||||
TEST_F(CollisionPreventionTest, testBehaviorOnWithAnObstacle)
|
||||
{
|
||||
// GIVEN: a simple setup condition
|
||||
CollisionPrevention cp(nullptr);
|
||||
matrix::Vector2f original_setpoint(10, 0);
|
||||
float max_speed = 3;
|
||||
matrix::Vector2f curr_pos(0, 0);
|
||||
matrix::Vector2f curr_vel(2, 0);
|
||||
|
||||
// AND: a parameter handle
|
||||
param_t param = param_handle(px4::params::MPC_COL_PREV_D);
|
||||
float value = 10; // try to keep 10m distance
|
||||
param_set(param, &value);
|
||||
|
||||
// AND: an obstacle message
|
||||
obstacle_distance_s message;
|
||||
memset(&message, 0xDEAD, sizeof(message));
|
||||
message.min_distance = 100;
|
||||
message.max_distance = 1000;
|
||||
message.timestamp = hrt_absolute_time();
|
||||
int distances_array_size = sizeof(message.distances) / sizeof(message.distances[0]);
|
||||
message.increment = 360 / distances_array_size;
|
||||
|
||||
for (int i = 0; i < distances_array_size; i++) {
|
||||
message.distances[i] = 101;
|
||||
}
|
||||
|
||||
orb_advert_t obstacle_distance_pub = orb_advertise(ORB_ID(obstacle_distance), &message);
|
||||
|
||||
|
||||
// WHEN: we publish the message and set the parameter and then run the setpoint modification
|
||||
matrix::Vector2f modified_setpoint = original_setpoint;
|
||||
cp.modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
|
||||
orb_unadvertise(obstacle_distance_pub);
|
||||
|
||||
// THEN: it should be cut down a lot
|
||||
EXPECT_GT(original_setpoint.norm() * 0.5f, modified_setpoint.norm()); //FIXME: this should actually be constrained to 0
|
||||
}
|
||||
@@ -395,4 +395,27 @@ CDev::remove_poll_waiter(px4_pollfd_struct_t *fds)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int CDev::unregister_driver_and_memory()
|
||||
{
|
||||
int retval = PX4_OK;
|
||||
|
||||
if (_registered) {
|
||||
unregister_driver(_devname);
|
||||
_registered = false;
|
||||
|
||||
} else {
|
||||
retval = -ENODEV;
|
||||
}
|
||||
|
||||
if (_devname != nullptr) {
|
||||
free((void *)_devname);
|
||||
_devname = nullptr;
|
||||
|
||||
} else {
|
||||
retval = -ENODEV;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
} // namespace cdev
|
||||
|
||||
@@ -264,6 +264,17 @@ protected:
|
||||
|
||||
px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */
|
||||
|
||||
|
||||
/**
|
||||
* First, unregisters the driver. Next, free the memory for the devname,
|
||||
* in case it was expected to have ownership. Sets devname to nullptr.
|
||||
*
|
||||
* This is only needed if the ownership of the devname was passed to the CDev, otherwise ~CDev handles it.
|
||||
*
|
||||
* @return PX4_OK on success, -ENODEV if the devname is already nullptr
|
||||
*/
|
||||
int unregister_driver_and_memory();
|
||||
|
||||
private:
|
||||
const char *_devname{nullptr}; /**< device node name */
|
||||
|
||||
|
||||
@@ -33,4 +33,4 @@
|
||||
|
||||
px4_add_library(hysteresis hysteresis.cpp)
|
||||
|
||||
px4_add_gtest(SRC HysteresisTest.cpp LINKLIBS hysteresis)
|
||||
px4_add_unit_gtest(SRC HysteresisTest.cpp LINKLIBS hysteresis)
|
||||
|
||||
@@ -162,3 +162,5 @@ add_dependencies(parameters prebuild_targets)
|
||||
if(${PX4_PLATFORM} STREQUAL "nuttx")
|
||||
target_link_libraries(parameters PRIVATE flashparams tinybson)
|
||||
endif()
|
||||
|
||||
px4_add_functional_gtest(SRC ParameterTest.cpp LINKLIBS parameters)
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2019 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 <px4_module_params.h>
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/topics/obstacle_distance.h>
|
||||
#include <uORB/uORBManager.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class ParameterTest : public ::testing::Test
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
uORB::Manager::initialize();
|
||||
param_init();
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
param_reset_all();
|
||||
uORB::Manager::terminate();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TEST_F(ParameterTest, testParamReadWrite)
|
||||
{
|
||||
// GIVEN a parameter handle
|
||||
param_t param = param_handle(px4::params::MPC_COL_PREV_D);
|
||||
|
||||
// WHEN: we get the parameter
|
||||
float value = -999.f;
|
||||
int status = param_get(param, &value);
|
||||
|
||||
// THEN it should be successful and have the default value
|
||||
EXPECT_EQ(0, status);
|
||||
EXPECT_EQ(-1, value);
|
||||
|
||||
// WHEN: we set the parameter
|
||||
value = 42.f;
|
||||
status = param_set(param, &value);
|
||||
|
||||
// THEN: it should be successful
|
||||
EXPECT_EQ(0, status);
|
||||
|
||||
// WHEN: we get the parameter again
|
||||
float value2 = -1999.f;
|
||||
status = param_get(param, &value2);
|
||||
|
||||
// THEN: it should be exactly the value we set
|
||||
EXPECT_EQ(0, status);
|
||||
EXPECT_EQ(42.f, value2);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(ParameterTest, testUorbSendReceive)
|
||||
{
|
||||
// GIVEN: a uOrb message
|
||||
obstacle_distance_s message;
|
||||
memset(&message, 0xDEAD, sizeof(message));
|
||||
message.min_distance = 1.f;
|
||||
message.max_distance = 10.f;
|
||||
|
||||
// AND: a subscriber
|
||||
uORB::SubscriptionData<obstacle_distance_s> sub_obstacle_distance{ORB_ID(obstacle_distance)};
|
||||
|
||||
// WHEN we send the message
|
||||
orb_advert_t obstacle_distance_pub = orb_advertise(ORB_ID(obstacle_distance), &message);
|
||||
ASSERT_TRUE(obstacle_distance_pub != nullptr);
|
||||
|
||||
// THEN: the subscriber should receive the message
|
||||
sub_obstacle_distance.update();
|
||||
const obstacle_distance_s &obstacle_distance = sub_obstacle_distance.get();
|
||||
|
||||
// AND: the values we got should be the same
|
||||
EXPECT_EQ(message.timestamp, obstacle_distance.timestamp);
|
||||
EXPECT_EQ(message.min_distance, obstacle_distance.min_distance);
|
||||
EXPECT_EQ(message.max_distance, obstacle_distance.max_distance);
|
||||
|
||||
// AND: all the bytes should be equal
|
||||
EXPECT_EQ(0, memcmp(&message, &obstacle_distance, sizeof(message)));
|
||||
}
|
||||
Reference in New Issue
Block a user