integrationtest_operation_mode_setup.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <utility>
19 
20 #include <gtest/gtest.h>
21 #include <gmock/gmock.h>
22 
23 #include <ros/ros.h>
24 
25 #include <std_srvs/SetBool.h>
26 
27 #include <pilz_msgs/GetSpeedOverride.h>
28 
29 #include <pilz_testutils/async_test.h>
30 
31 #include <pilz_msgs/OperationModes.h>
33 
34 namespace prbt_hardware_support
35 {
36 static const std::string OPERATON_MODE_SETUP_EXECUTOR_NODE_NAME{ "/operation_mode_setup_executor_node" };
37 static const std::string MONITOR_CARTESIAN_SPEED_SERVICE{ "manipulator_joint_trajectory_controller/"
38  "monitor_cartesian_speed" };
39 static const std::string OPERATION_MODE_TOPIC{ "operation_mode" };
40 static const std::string GET_SPEED_OVERRIDE_SERVICE{ "get_speed_override" };
41 static const std::string MONITOR_CARTESIAN_SPEED_CALLBACK_EVENT{ "monitor_cartesian_speed_callback_event" };
42 
43 static constexpr uint32_t DEFAULT_QUEUE_SIZE{ 10 };
44 
45 static constexpr double EXPECTED_SPEED_OVERRIDE_AUTO{ 1.0 };
46 static constexpr double EXPECTED_SPEED_OVERRIDE_T1{ 0.1 };
47 
48 struct TestData
49 {
50  constexpr TestData(const int8_t op_mode_, const bool expected_request_data_, const double expected_speed_override_)
51  : op_mode(op_mode_)
52  , expected_request_data(expected_request_data_)
53  , expected_speed_override(expected_speed_override_)
54  {
55  }
56 
57  const int8_t op_mode;
58 
61 };
62 
63 static constexpr TestData OP_MODE_T1_TEST_DATA{ pilz_msgs::OperationModes::T1, true, EXPECTED_SPEED_OVERRIDE_T1 };
64 static constexpr TestData OP_MODE_AUTO_TEST_DATA{ pilz_msgs::OperationModes::AUTO, false,
66 
69 {
70 public:
71  void advertiseService();
72  MOCK_METHOD2(monitor_cartesian_speed_callback_, bool(std_srvs::SetBoolRequest&, std_srvs::SetBoolResponse&));
73 
74 private:
77 };
78 
80 {
81  monitor_cartesian_speed_srv_ =
82  nh_.advertiseService(MONITOR_CARTESIAN_SPEED_SERVICE, &ControllerMock::monitor_cartesian_speed_callback_, this);
83 }
84 
90 class OperationModeSetupTest : public testing::Test,
91  public testing::AsyncTest,
92  public testing::WithParamInterface<TestData>
93 {
94 public:
95  void SetUp() override;
96 
97 protected:
98  using OperationModes = pilz_msgs::OperationModes;
99  using GetSpeedOverride = pilz_msgs::GetSpeedOverride;
100 
101 protected:
102  testing::AssertionResult isSpeedOverrideEqualTo(const double& expected_speed_override);
103 
104 protected:
109 };
110 
112 {
113  controller_mock_.advertiseService();
114  operation_mode_pub_ = nh_.advertise<OperationModes>(OPERATION_MODE_TOPIC, DEFAULT_QUEUE_SIZE, true);
115 
118  ASSERT_TRUE(waitForSubscriber(operation_mode_pub_));
119 }
120 
122 {
123  if (!get_speed_override_client_.isValid())
124  {
125  get_speed_override_client_ = nh_.serviceClient<GetSpeedOverride>(GET_SPEED_OVERRIDE_SERVICE);
126  }
127 
128  GetSpeedOverride get_speed_override;
129  if (!get_speed_override_client_.call(get_speed_override))
130  {
131  return testing::AssertionFailure() << "Service call failed: " << get_speed_override_client_.getService();
132  }
133  if (get_speed_override.response.speed_override != expected_speed_override)
134  {
135  return testing::AssertionFailure() << "Speed overrides do not match. Expected: " << expected_speed_override
136  << ", actual: " << get_speed_override.response.speed_override;
137  }
138  return testing::AssertionSuccess();
139 }
140 
141 using testing::_;
142 using testing::DoAll;
143 using testing::Field;
144 using testing::Return;
145 using testing::SetArgReferee;
146 
150 TEST_P(OperationModeSetupTest, testSpeedMonitoringActivation)
151 {
152  const auto test_data{ GetParam() };
153 
154  std_srvs::SetBoolResponse response;
155  response.success = true;
156  EXPECT_CALL(controller_mock_, monitor_cartesian_speed_callback_(
157  Field(&std_srvs::SetBoolRequest::data, test_data.expected_request_data), _))
158  .WillOnce(DoAll(SetArgReferee<1>(response), ACTION_OPEN_BARRIER(MONITOR_CARTESIAN_SPEED_CALLBACK_EVENT)));
159 
160  OperationModes msg;
161  msg.value = test_data.op_mode;
162  msg.time_stamp = ros::Time::now();
163  operation_mode_pub_.publish(msg);
164 
166 
167  EXPECT_TRUE(isSpeedOverrideEqualTo(test_data.expected_speed_override));
168 }
169 
170 INSTANTIATE_TEST_CASE_P(TestActivationOfSpeedMonitoring, OperationModeSetupTest,
171  testing::Values(OP_MODE_AUTO_TEST_DATA, OP_MODE_T1_TEST_DATA), );
172 
173 } // namespace prbt_hardware_support
174 
175 int main(int argc, char* argv[])
176 {
177  ros::init(argc, argv, "integrationtest_operation_mode_setup");
178  ros::NodeHandle nh;
179 
181  spinner.start();
182 
183  testing::InitGoogleTest(&argc, argv);
184  return RUN_ALL_TESTS();
185 }
msg
static constexpr TestData OP_MODE_T1_TEST_DATA
static const std::string MONITOR_CARTESIAN_SPEED_CALLBACK_EVENT
static const std::string OPERATON_MODE_SETUP_EXECUTOR_NODE_NAME
int main(int argc, char *argv[])
ServiceServer advertiseService(ros::NodeHandle n, std::string name, boost::function< bool(const typename ActionSpec::_action_goal_type::_goal_type &, typename ActionSpec::_action_result_type::_result_type &result)> service_cb)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
testing::AssertionResult isSpeedOverrideEqualTo(const double &expected_speed_override)
INSTANTIATE_TEST_CASE_P(TestActivationOfSpeedMonitoring, OperationModeSetupTest, testing::Values(OP_MODE_AUTO_TEST_DATA, OP_MODE_T1_TEST_DATA),)
Provides the monitor_cartesian_speed service.
void spinner()
static const std::string MONITOR_CARTESIAN_SPEED_SERVICE
static const std::string GET_SPEED_OVERRIDE_SERVICE
inline ::testing::AssertionResult waitForNode(const std::string node_name, const double loop_frequency=10.0, const ros::Duration timeout=ros::Duration(10.0))
Blocks until a node defined by node_name comes up.
static constexpr TestData OP_MODE_AUTO_TEST_DATA
static constexpr int DEFAULT_QUEUE_SIZE
static constexpr double EXPECTED_SPEED_OVERRIDE_AUTO
static Time now()
TEST_P(OperationModeSetupTest, testSpeedMonitoringActivation)
Tests that the Cartesian speed monitoring is activated or de-activated depending on the operation mod...
constexpr TestData(const int8_t op_mode_, const bool expected_request_data_, const double expected_speed_override_)
inline ::testing::AssertionResult waitForSubscriber(const ros::Publisher &publisher, const unsigned int subscriber_count=1, const double loop_frequency=10.0, const ros::Duration timeout=ros::Duration(10.0))
Blocks until the specified number of subscribers is registered on a topic.
static constexpr double EXPECTED_SPEED_OVERRIDE_T1
const std::string response
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


prbt_hardware_support
Author(s):
autogenerated on Mon Feb 28 2022 23:14:34