Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klaxalk committed Nov 22, 2023
1 parent e794a38 commit 92e6a42
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/control_manager/control_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7869,6 +7869,7 @@ void ControlManager::ungripSrv(void) {
/* toggleOutput() //{ */

void ControlManager::toggleOutput(const bool& input) {

if (input == output_enabled_) {
ROS_WARN_THROTTLE(0.1, "[ControlManager]: output is already %s", input ? "ON" : "OFF");
return;
Expand Down
18 changes: 18 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ add_dependencies(
${catkin_EXPORTED_TARGETS}
)

# landing test

add_rostest_gtest(MrsUavManagers_UavManagerLandingTest
uav_manager/landing/uav_manager_landing.test
uav_manager/landing/test.cpp
)

target_link_libraries(
MrsUavManagers_UavManagerLandingTest
${catkin_LIBRARIES}
)

add_dependencies(
MrsUavManagers_UavManagerLandingTest
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)

# goto test

add_rostest_gtest(MrsUavManagers_ControlManagerGotoTest
Expand Down
53 changes: 36 additions & 17 deletions test/control_manager/goto/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
#include <ros/console.h>
#include <log4cxx/logger.h>

/* TEST(TESTSuite, goto) //{ */

TEST(TESTSuite, goto) {

int result = 0;

// | ------------------ initialize test node ------------------ |

ros::NodeHandle nh = ros::NodeHandle("~");
Expand All @@ -30,6 +26,9 @@ TEST(TESTSuite, goto) {

ros::Time::waitForValid();

ros::AsyncSpinner spinner(2);
spinner.start();

std::string uav_name = "uav1";

// | ----------------------- subscribers ---------------------- |
Expand Down Expand Up @@ -70,8 +69,7 @@ TEST(TESTSuite, goto) {
break;
}

ros::spinOnce();
ros::Duration(0.1).sleep();
ros::Duration(0.01).sleep();
}

ROS_INFO("[%s]: MRS UAV System is ready", ros::this_node::getName().c_str());
Expand All @@ -80,12 +78,19 @@ TEST(TESTSuite, goto) {

// | ---------------------- arm the drone --------------------- |

ROS_INFO("[%s]: arming th edrone", ros::this_node::getName().c_str());
ROS_INFO("[%s]: arming the edrone", ros::this_node::getName().c_str());

std_srvs::SetBool arming;
arming.request.data = true;

sch_arming.call(arming);
{
bool service_call = sch_arming.call(arming);

if (!service_call || !arming.response.success) {
ROS_ERROR("[%s]: arming service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

// | -------------------------- wait -------------------------- |

Expand All @@ -97,7 +102,14 @@ TEST(TESTSuite, goto) {

std_srvs::Trigger midair;

sch_midair.call(midair);
{
bool service_call = sch_midair.call(midair);

if (!service_call || !midair.response.success) {
ROS_ERROR("[%s]: midair activation service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

// | ----------------- sleep before the action ---------------- |

Expand All @@ -116,7 +128,14 @@ TEST(TESTSuite, goto) {

ROS_INFO("[%s]: calling goto", ros::this_node::getName().c_str());

sch_goto.call(goto_cmd);
{
bool service_call = sch_goto.call(goto_cmd);

if (!service_call || !goto_cmd.response.success) {
ROS_ERROR("[%s]: goto service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

// | ------------------ check for the result ------------------ |

Expand All @@ -132,21 +151,21 @@ TEST(TESTSuite, goto) {

if (std::abs(goto_cmd.request.goal[0] - diag->pose.position.x) < 1e-1 && std::abs(goto_cmd.request.goal[1] - diag->pose.position.y) < 1e-1 &&
std::abs(goto_cmd.request.goal[2] - diag->pose.position.z) < 1e-1 && std::abs(goto_cmd.request.goal[3] - hdg) < 1e-1 && flying_normally) {
result = 1;

ROS_INFO("[%s]: finished", ros::this_node::getName().c_str());

GTEST_SUCCEED();
return;

break;
}

ros::spinOnce();
ros::Duration(0.01).sleep();
}

ROS_INFO("[%s]: finished", ros::this_node::getName().c_str());

EXPECT_TRUE(result);
GTEST_FAIL();
}

//}

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

ros::init(argc, argv, "goto_test");
Expand Down
27 changes: 27 additions & 0 deletions test/run_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "$0: \"${last_command}\" command failed with exit code $?"' ERR

PACKAGE="mrs_uav_managers"
VERBOSE=1

[ "$VERBOSE" = "0" ] && TEXT_OUTPUT=""
[ "$VERBOSE" = "1" ] && TEXT_OUTPUT="-t"

# build the package
catkin build $PACKAGE # it has to be fully built normally before building with --catkin-make-args tests
catkin build $PACKAGE --catkin-make-args tests

# folder for test results
TEST_RESULT_PATH=$(realpath /tmp/$RANDOM)
mkdir -p $TEST_RESULT_PATH

# run the test
rostest $PACKAGE uav_manager_landing.test $TEXT_OUTPUT --results-filename=$PACKAGE.test --results-base-dir="$TEST_RESULT_PATH"

# evaluate the test results
echo test result path is $TEST_RESULT_PATH
catkin_test_results "$TEST_RESULT_PATH"
198 changes: 198 additions & 0 deletions test/uav_manager/landing/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include <gtest/gtest.h>
#include <ros/console.h>
#include <log4cxx/logger.h>

#include <mrs_lib/subscribe_handler.h>
#include <mrs_lib/service_client_handler.h>

#include <std_msgs/Bool.h>
#include <mrs_msgs/ControlManagerDiagnostics.h>
#include <mrs_msgs/EstimationDiagnostics.h>
#include <mrs_msgs/Vec4.h>

#include <std_srvs/SetBool.h>
#include <std_srvs/Trigger.h>

TEST(TESTSuite, landing) {

// | ------------------ initialize test node ------------------ |

ros::NodeHandle nh = ros::NodeHandle("~");

ROS_INFO("[%s]: ROS node initialized", ros::this_node::getName().c_str());

ros::Time::waitForValid();

ros::AsyncSpinner spinner(2);
spinner.start();

std::string uav_name = "uav1";

// | ----------------------- subscribers ---------------------- |

mrs_lib::SubscribeHandlerOptions shopts;
shopts.nh = nh;
shopts.node_name = "Test";
shopts.no_message_timeout = mrs_lib::no_timeout;
shopts.threadsafe = true;
shopts.autostart = true;
shopts.queue_size = 10;
shopts.transport_hints = ros::TransportHints().tcpNoDelay();

mrs_lib::SubscribeHandler<mrs_msgs::ControlManagerDiagnostics> sh_control_manager_diag =
mrs_lib::SubscribeHandler<mrs_msgs::ControlManagerDiagnostics>(shopts, "/" + uav_name + "/control_manager/diagnostics");

mrs_lib::SubscribeHandler<mrs_msgs::EstimationDiagnostics> sh_estim_manager_diag =
mrs_lib::SubscribeHandler<mrs_msgs::EstimationDiagnostics>(shopts, "/" + uav_name + "/estimation_manager/diagnostics");

ROS_INFO("[%s]: subscribers initialized", ros::this_node::getName().c_str());

// | --------------------- service clients -------------------- |

mrs_lib::ServiceClientHandler<std_srvs::SetBool> sch_arming = mrs_lib::ServiceClientHandler<std_srvs::SetBool>(nh, "/" + uav_name + "/hw_api/arming");

mrs_lib::ServiceClientHandler<std_srvs::Trigger> sch_midair =
mrs_lib::ServiceClientHandler<std_srvs::Trigger>(nh, "/" + uav_name + "/uav_manager/midair_activation");

mrs_lib::ServiceClientHandler<std_srvs::Trigger> sch_land = mrs_lib::ServiceClientHandler<std_srvs::Trigger>(nh, "/" + uav_name + "/uav_manager/land");

mrs_lib::ServiceClientHandler<mrs_msgs::Vec4> sch_goto = mrs_lib::ServiceClientHandler<mrs_msgs::Vec4>(nh, "/" + uav_name + "/control_manager/goto");

ROS_INFO("[%s]: service client initialized", ros::this_node::getName().c_str());

// | ---------------- wait for ready to takeoff --------------- |

while (ros::ok()) {

ROS_INFO_THROTTLE(1.0, "[%s]: waiting for the MRS UAV System", ros::this_node::getName().c_str());

if (sh_control_manager_diag.hasMsg() && sh_estim_manager_diag.hasMsg()) {
break;
}

ros::Duration(0.01).sleep();
}

ROS_INFO("[%s]: MRS UAV System is ready", ros::this_node::getName().c_str());

ros::Duration(1.0).sleep();

// | ---------------------- arm the drone --------------------- |

ROS_INFO("[%s]: arming the edrone", ros::this_node::getName().c_str());

std_srvs::SetBool arming;
arming.request.data = true;

{
bool service_call = sch_arming.call(arming);

if (!service_call || !arming.response.success) {
ROS_ERROR("[%s]: arming service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

// | -------------------------- wait -------------------------- |

ros::Duration(0.2).sleep();

// | -------------------- midair activation ------------------- |

ROS_INFO("[%s]: activating the drone 'in mid air'", ros::this_node::getName().c_str());

std_srvs::Trigger midair;

{
bool service_call = sch_midair.call(midair);

if (!service_call || !midair.response.success) {
ROS_ERROR("[%s]: midair activation service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

// | ----------------- sleep before the action ---------------- |

ros::Duration(1.0).sleep();

// | -------------------------- goto -------------------------- |

mrs_msgs::Vec4 goto_cmd;

goto_cmd.request.goal[0] = 5;
goto_cmd.request.goal[1] = 5;
goto_cmd.request.goal[2] = 8;
goto_cmd.request.goal[3] = 1;

ROS_INFO("[%s]: calling goto", ros::this_node::getName().c_str());

{
bool service_call = sch_goto.call(goto_cmd);

if (!service_call || !goto_cmd.response.success) {
ROS_ERROR("[%s]: goto service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

ros::Duration(1.0).sleep();

// | ------------ check for the result of the goto ------------ |

while (ros::ok()) {

ROS_INFO_THROTTLE(1.0, "[%s]: waiting for goto", ros::this_node::getName().c_str());

if (!sh_control_manager_diag.getMsg()->tracker_status.have_goal) {

ROS_INFO("[%s]: goto finished", ros::this_node::getName().c_str());
break;
}

ros::Duration(0.01).sleep();
}

// | ------------------ initiate the landing ------------------ |

ROS_INFO("[%s]: calling for landing", ros::this_node::getName().c_str());

std_srvs::Trigger landing;

{
bool service_call = sch_land.call(landing);

if (!service_call || !landing.response.success) {
ROS_ERROR("[%s]: landing service call failed", ros::this_node::getName().c_str());
GTEST_FAIL();
}
}

// | ------------ waiting for the landing to finish ----------- |

while (ros::ok()) {

ROS_INFO_THROTTLE(1.0, "[%s]: waiting for the landing to finish", ros::this_node::getName().c_str());

if (!sh_control_manager_diag.getMsg()->output_enabled) {

ROS_INFO("[%s]: finished", ros::this_node::getName().c_str());

GTEST_SUCCEED();
return;
}

ros::Duration(0.01).sleep();
}

GTEST_FAIL();
}

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

testing::InitGoogleTest(&argc, argv);

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

return RUN_ALL_TESTS();
}
7 changes: 7 additions & 0 deletions test/uav_manager/landing/uav_manager_landing.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<launch>

<include file="$(find mrs_uav_managers)/test/sessions/x500_in_mid_air/launch/mrs_uav_system.launch" />

<test pkg="mrs_uav_managers" type="MrsUavManagers_UavManagerLandingTest" test-name="landing_test" time-limit="60.0" />

</launch>
Loading

0 comments on commit 92e6a42

Please sign in to comment.