Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klaxalk committed Dec 5, 2023
1 parent d7af9ce commit b553ff4
Show file tree
Hide file tree
Showing 23 changed files with 723 additions and 33 deletions.
32 changes: 0 additions & 32 deletions src/automatic_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ class AutomaticStart : public nodelet::Nodelet {

bool takeoff();

bool elandImpl();
bool validateReference();

bool toggleControlOutput(const bool& value);
Expand Down Expand Up @@ -606,37 +605,6 @@ bool AutomaticStart::takeoff() {

//}

/* elandImpl() //{ */

bool AutomaticStart::elandImpl() {

ROS_INFO_THROTTLE(1.0, "[AutomaticStart]: elanding");

std_srvs::Trigger srv;

bool res = service_client_eland_.call(srv);

if (res) {

if (srv.response.success) {

return true;

} else {

ROS_ERROR_THROTTLE(1.0, "[AutomaticStart]: elanding failed: %s", srv.response.message.c_str());
}

} else {

ROS_ERROR_THROTTLE(1.0, "[AutomaticStart]: service call for elanding failed");
}

return false;
}

//}

/* validateReference() //{ */

bool AutomaticStart::validateReference() {
Expand Down
18 changes: 17 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
find_package(rostest REQUIRED)
find_package(mrs_uav_testing REQUIRED)
find_package(catkin REQUIRED COMPONENTS mrs_uav_testing)

# will test takeoff in mrs simulator
add_subdirectory(takeoff)

# will test takeoff in gazebo
add_subdirectory(gazebo_takeoff)

# will test if takeoff is not allowed outside of safety area
add_subdirectory(outside_safety_area)

# will test the "handle_takeoff: false" option
add_subdirectory(without_takeoff)

# will test if takeoff is not allowed when UAV is moving
add_subdirectory(moving_drone)

# will test if takeoff is not allowed if topic check fails
add_subdirectory(topic_check)
25 changes: 25 additions & 0 deletions test/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

PACKAGE_NAME=mrs_uav_autostart

while [ ! -e ".catkin_tools" ]; do
cd ..
if [[ `pwd` == "/" ]]; then
# we reached the root and didn't find the build/COLCON_IGNORE file - that's a fail!
echo "$0: could not find the root of the current workspace".
return 1
fi
done

cd build

lcov --capture --directory . --output-file coverage.info
lcov --remove coverage.info "*/test/*" --output-file coverage.info.removed
lcov --extract coverage.info.removed "*/mrs_uav_autostart/*" --output-file coverage.info.cleaned
genhtml -o coverage_html coverage.info.cleaned | tee /tmp/genhtml.log

COVERAGE_PCT=`cat /tmp/genhtml.log | tail -n 1 | awk '{print $2}'`

echo "Coverage: $COVERAGE_PCT"

xdg-open coverage_html/index.html
21 changes: 21 additions & 0 deletions test/gazebo_takeoff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
get_filename_component(TEST_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME)

catkin_add_executable_with_gtest(test_${TEST_NAME}
test.cpp
)

target_link_libraries(test_${TEST_NAME}
${catkin_LIBRARIES}
)

add_dependencies(test_${TEST_NAME}
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)

add_rostest(${TEST_NAME}.test
ARGS
test_path:=${CMAKE_CURRENT_SOURCE_DIR}
package_name:=${PROJECT_NAME}
test_name:=${TEST_NAME}
)
30 changes: 30 additions & 0 deletions test/gazebo_takeoff/gazebo_takeoff.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<launch>

<arg name="UAV_NAME" default="uav1" />
<arg name="UAV_TYPE" default="x500" />

<!-- these will be filled in from the CMakeLists -->
<arg name="test_path" default="" />
<arg name="test_name" default="" />
<arg name="package_name" default="" />

<include file="$(find mrs_uav_testing)/launch/gazebo_simulator.launch" />

<include file="$(find mrs_uav_px4_api)/launch/api.launch">
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
<arg name="RUN_TYPE" default="simulation" />
</include>

<include file="$(find mrs_uav_testing)/launch/mrs_uav_system.launch">
<arg name="automatic_start" default="true" />
<arg name="platform_config" default="$(find mrs_uav_gazebo_simulation)/config/mrs_uav_system/$(arg UAV_TYPE).yaml" />
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
</include>

<test pkg="$(arg package_name)" type="test_$(arg test_name)" test-name="$(arg test_name)" time-limit="60.0">
<param name="test_name" value="$(arg test_name)" />
<param name="uav_name" value="$(arg UAV_NAME)" />
<param name="gazebo_spawner_params" value="1 --$(arg UAV_TYPE) --enable-rangefinder" />
</test>

</launch>
62 changes: 62 additions & 0 deletions test/gazebo_takeoff/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <gtest/gtest.h>

#include <mrs_uav_testing/test_generic.h>

class Tester : public mrs_uav_testing::TestGeneric {

public:
bool test();
};

bool Tester::test() {

{
auto [success, message] = spawnGazeboUav();

if (!success) {
ROS_ERROR("[%s]: gazebo UAV spawning failed with message: '%s'", ros::this_node::getName().c_str(), message.c_str());
return false;
}
}

{
auto [success, message] = takeoff();

if (!success) {
ROS_ERROR("[%s]: midair activation failed with message: '%s'", ros::this_node::getName().c_str(), message.c_str());
return false;
}
}

this->sleep(5.0);

if (this->isFlyingNormally()) {
return true;
} else {
ROS_ERROR("[%s]: not flying normally", ros::this_node::getName().c_str());
return false;
}
}


TEST(TESTSuite, test) {

Tester tester;

bool result = tester.test();

if (result) {
GTEST_SUCCEED();
} else {
GTEST_FAIL();
}
}

int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) {

ros::init(argc, argv, "test");

testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
21 changes: 21 additions & 0 deletions test/moving_drone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
get_filename_component(TEST_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME)

catkin_add_executable_with_gtest(test_${TEST_NAME}
test.cpp
)

target_link_libraries(test_${TEST_NAME}
${catkin_LIBRARIES}
)

add_dependencies(test_${TEST_NAME}
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)

add_rostest(${TEST_NAME}.test
ARGS
test_path:=${CMAKE_CURRENT_SOURCE_DIR}
package_name:=${PROJECT_NAME}
test_name:=${TEST_NAME}
)
30 changes: 30 additions & 0 deletions test/moving_drone/moving_drone.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<launch>

<arg name="UAV_NAME" default="uav1" />
<arg name="UAV_TYPE" default="x500" />

<!-- these will be filled in from the CMakeLists -->
<arg name="test_path" default="" />
<arg name="test_name" default="" />
<arg name="package_name" default="" />

<include file="$(find mrs_uav_testing)/launch/mrs_simulator.launch">
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
</include>

<include file="$(find mrs_multirotor_simulator)/launch/hw_api.launch">
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
</include>

<include file="$(find mrs_uav_testing)/launch/mrs_uav_system.launch">
<arg name="automatic_start" default="true" />
<arg name="platform_config" default="$(find mrs_multirotor_simulator)/config/mrs_uav_system/$(arg UAV_TYPE).yaml" />
<arg name="UAV_NAME" default="$(arg UAV_NAME)" />
</include>

<test pkg="$(arg package_name)" type="test_$(arg test_name)" test-name="$(arg test_name)" time-limit="60.0">
<param name="test_name" value="$(arg test_name)" />
<param name="uav_name" value="$(arg UAV_NAME)" />
</test>

</launch>
70 changes: 70 additions & 0 deletions test/moving_drone/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <gtest/gtest.h>

#include <mrs_uav_testing/test_generic.h>

#include <mrs_msgs/HwApiVelocityHdgCmd.h>

class Tester : public mrs_uav_testing::TestGeneric {

public:
Tester();

bool test();

mrs_lib::PublisherHandler<mrs_msgs::HwApiVelocityHdgCmd> ph_velocity_;

ros::Timer timer_main_;
void timerMain(const ros::TimerEvent& event);
};

Tester::Tester() : mrs_uav_testing::TestGeneric() {

ph_velocity_ = mrs_lib::PublisherHandler<mrs_msgs::HwApiVelocityHdgCmd>(nh_, "/multirotor_simulator/" + _uav_name_ + "/velocity_hdg_cmd");

timer_main_ = nh_.createTimer(ros::Rate(100.0), &Tester::timerMain, this, false, true);
}

void Tester::timerMain(const ros::TimerEvent& event) {

mrs_msgs::HwApiVelocityHdgCmd msg;

msg.velocity.x = 0.5;
msg.velocity.z = 0.1;

ph_velocity_.publish(msg);
}

bool Tester::test() {

auto [success, message] = takeoff();

if (!success) {
return true;
} else {
ROS_ERROR("[%s]: takeoff initiated, this should not be possible", ros::this_node::getName().c_str());
return false;
}
}


TEST(TESTSuite, test) {

Tester tester;

bool result = tester.test();

if (result) {
GTEST_SUCCEED();
} else {
GTEST_FAIL();
}
}

int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) {

ros::init(argc, argv, "test");

testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
21 changes: 21 additions & 0 deletions test/outside_safety_area/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
get_filename_component(TEST_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME)

catkin_add_executable_with_gtest(test_${TEST_NAME}
test.cpp
)

target_link_libraries(test_${TEST_NAME}
${catkin_LIBRARIES}
)

add_dependencies(test_${TEST_NAME}
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)

add_rostest(${TEST_NAME}.test
ARGS
test_path:=${CMAKE_CURRENT_SOURCE_DIR}
package_name:=${PROJECT_NAME}
test_name:=${TEST_NAME}
)
14 changes: 14 additions & 0 deletions test/outside_safety_area/config/mrs_simulator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
individual_takeoff_platform:
enabled: false

uav_names: [
"uav1",
]

uav1:
type: "x500"
spawn:
x: 500.0
y: 500.0
z: 0.0
heading: 0
Loading

0 comments on commit b553ff4

Please sign in to comment.