integrationtest_execute_brake_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 <gtest/gtest.h>
19 
20 #include <ros/ros.h>
21 
22 #include <prbt_hardware_support/BrakeTest.h>
23 #include <prbt_hardware_support/WriteModbusRegister.h>
30 
31 static const std::string EXECUTE_BRAKE_TEST_SERVICE_NAME{"/prbt/execute_braketest"};
32 static const std::string MODBUS_WRITE_SERVICE_NAME{"/pilz_modbus_client_node/modbus_write"};
33 static const std::string CONTROLLER_HOLD_MODE_SERVICE_NAME{"/prbt/manipulator_joint_trajectory_controller/hold"};
34 static const std::string CONTROLLER_UNHOLD_MODE_SERVICE_NAME{"/prbt/manipulator_joint_trajectory_controller/unhold"};
35 
36 static constexpr double WAIT_FOR_BRAKE_TEST_SERVICE_TIMEOUT_S{5.0};
37 static constexpr uint16_t MODBUS_BRAKE_TEST_PREPARE_VALUE {0};
38 static constexpr uint16_t MODBUS_BRAKE_TEST_EXPECTED_VALUE {1};
39 
40 namespace prbt_hardware_support {
41 
43  PilzModbusServerMock &server,
44  unsigned short register_perf,
45  unsigned short register_res,
46  uint16_t expectation,
47  float sleep_per_try_s,
48  unsigned short retries)
49 {
50  RegCont content_perf, content_res;
51  for(int i = 0; i <= retries; i++){
52  ROS_INFO("Retry %d, waited for %.1fs so far", i, sleep_per_try_s * static_cast<float>(i));
53  content_perf = server.readHoldingRegister(register_perf, 1);
54  content_res = server.readHoldingRegister(register_res, 1);
55  if(
56  content_perf[0] == expectation &&
57  content_res[0] == expectation)
58  return true; // expected result
59  ros::Duration(sleep_per_try_s).sleep();
60  }
61  return false;
62 }
63 
85 TEST(IntegrationtestExecuteBrakeTest, testBrakeTestService)
86 {
87  ros::NodeHandle nh;
88  ros::NodeHandle nh_private("~");
89 
90  /**********
91  * Step 1 *
92  **********/
93  std::string ip;
94  int port;
95  ASSERT_TRUE(nh_private.getParam("modbus_server_ip", ip));
96  ASSERT_TRUE(nh_private.getParam("modbus_server_port", port));
97 
98  ModbusApiSpec write_api_spec {nh, "write_api_spec/"};
99  unsigned int modbus_register_size {write_api_spec.getMaxRegisterDefinition() + 1U};
100  prbt_hardware_support::PilzModbusServerMock modbus_server(modbus_register_size);
101  std::thread modbus_server_thread( &initalizeAndRun<prbt_hardware_support::PilzModbusServerMock>,
102  std::ref(modbus_server), ip.c_str(), static_cast<unsigned int>(port) );
103  ASSERT_TRUE(write_api_spec.hasRegisterDefinition(modbus_api_spec::BRAKETEST_PERFORMED));
104  unsigned short register_perf = write_api_spec.getRegisterDefinition(modbus_api_spec::BRAKETEST_PERFORMED);
105  ASSERT_TRUE(write_api_spec.hasRegisterDefinition(modbus_api_spec::BRAKETEST_RESULT));
106  unsigned short register_res = write_api_spec.getRegisterDefinition(modbus_api_spec::BRAKETEST_RESULT);
107 
108  modbus_server.setHoldingRegister({{register_perf, MODBUS_BRAKE_TEST_PREPARE_VALUE}});
109  modbus_server.setHoldingRegister({{register_res, MODBUS_BRAKE_TEST_PREPARE_VALUE}});
110 
111  /**********
112  * Step 2 *
113  **********/
114  CANOpenChainNodeMock canopen_mock;
115 
116  JointStatesPublisherMock joint_states_pub;
117  joint_states_pub.startAsync();
118 
119  ManipulatorMock manipulator;
122 
123  /**********
124  * Step 3 *
125  **********/
126  ros::ServiceClient brake_test_srv_client_ = nh.serviceClient<BrakeTest>(EXECUTE_BRAKE_TEST_SERVICE_NAME);
127  EXPECT_TRUE(brake_test_srv_client_.waitForExistence(ros::Duration(WAIT_FOR_BRAKE_TEST_SERVICE_TIMEOUT_S)));
128 
129  /**********
130  * Step 4 *
131  **********/
132  BrakeTest srv;
133  EXPECT_TRUE(brake_test_srv_client_.call(srv));
134 
135  /**********
136  * Step 5 *
137  **********/
138  EXPECT_TRUE(brakeTestRegisterSetOnServer(
139  modbus_server,
140  register_perf,
141  register_res,
143  1,
144  10
145  ));
146 
147  /**********
148  * Step 6 *
149  **********/
150  modbus_server.terminate();
151  modbus_server_thread.join();
152 }
153 
154 } // namespace prbt_hardware_support
155 
156 int main(int argc, char *argv[])
157 {
158  ros::init(argc, argv, "integrationtest_trigger_brake_test");
159  ros::NodeHandle nh;
160 
162  spinner.start();
163 
164  testing::InitGoogleTest(&argc, argv);
165  return RUN_ALL_TESTS();
166 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
static const std::string MODBUS_WRITE_SERVICE_NAME
static const std::string BRAKETEST_PERFORMED
static constexpr uint16_t MODBUS_BRAKE_TEST_PREPARE_VALUE
Offers a modbus server and read/write functionality via subscription/publication. ...
Mocks the ROS Api of the manipulator relevant for stopping and holding.
std::vector< uint16_t > RegCont
Convenience data type defining the data type for a collection of registers.
RegCont readHoldingRegister(const RegCont::size_type start_index, const RegCont::size_type num_reg_to_read)
Reads the specified number of registers, beginning at the specified start point from the holding regi...
static const std::string BRAKETEST_RESULT
static constexpr uint16_t MODBUS_BRAKE_TEST_EXPECTED_VALUE
void setHoldingRegister(std::initializer_list< std::pair< unsigned int, uint16_t > > reg_list)
bool brakeTestRegisterSetOnServer(PilzModbusServerMock &server, unsigned short register_perf, unsigned short register_res, uint16_t expectation, float sleep_per_try_s, unsigned short retries)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void startAsync(bool move=false)
Start a new thread publishing joint states.
Asynchronously publishes predefined messages on the /joint_states topic with rate ~100Hz...
void spinner()
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
#define ROS_INFO(...)
static const std::string CONTROLLER_HOLD_MODE_SERVICE_NAME
static const std::string EXECUTE_BRAKE_TEST_SERVICE_NAME
void terminate()
Terminate the Server. Reading or connecting to it will fail.
int main(int argc, char *argv[])
void advertiseHoldService(ros::NodeHandle nh, std::string hold_service_name)
void advertiseUnholdService(ros::NodeHandle nh, std::string unhold_service_name)
bool getParam(const std::string &key, std::string &s) const
TEST(IntegrationtestExecuteBrakeTest, testBrakeTestService)
static const std::string CONTROLLER_UNHOLD_MODE_SERVICE_NAME
Specifies the meaning of the holding registers.
static constexpr double WAIT_FOR_BRAKE_TEST_SERVICE_TIMEOUT_S


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17