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 <pilz_msgs/BrakeTest.h>
23 
24 #include <prbt_hardware_support/WriteModbusRegister.h>
31 
32 static const std::string EXECUTE_BRAKE_TEST_SERVICE_NAME{"/prbt/execute_braketest"};
33 static const std::string MODBUS_WRITE_SERVICE_NAME{"/pilz_modbus_client_node/modbus_write"};
34 static const std::string CONTROLLER_HOLD_MODE_SERVICE_NAME{"/prbt/manipulator_joint_trajectory_controller/hold"};
35 static const std::string CONTROLLER_UNHOLD_MODE_SERVICE_NAME{"/prbt/manipulator_joint_trajectory_controller/unhold"};
36 
37 static constexpr double WAIT_FOR_BRAKE_TEST_SERVICE_TIMEOUT_S{5.0};
38 static constexpr uint16_t MODBUS_BRAKE_TEST_PREPARE_VALUE {0};
39 static constexpr uint16_t MODBUS_BRAKE_TEST_EXPECTED_VALUE {1};
40 
41 namespace prbt_hardware_support {
42 
44  PilzModbusServerMock &server,
45  unsigned short register_perf,
46  unsigned short register_res,
47  uint16_t expectation,
48  float sleep_per_try_s,
49  unsigned short retries)
50 {
51  RegCont content_perf, content_res;
52  for(int i = 0; i <= retries; i++){
53  ROS_INFO("Retry %d, waited for %.1fs so far", i, sleep_per_try_s * static_cast<float>(i));
54  content_perf = server.readHoldingRegister(register_perf, 1);
55  content_res = server.readHoldingRegister(register_res, 1);
56  if(
57  content_perf[0] == expectation &&
58  content_res[0] == expectation)
59  return true; // expected result
60  ros::Duration(sleep_per_try_s).sleep();
61  }
62  return false;
63 }
64 
86 TEST(IntegrationtestExecuteBrakeTest, testBrakeTestService)
87 {
88  ros::NodeHandle nh;
89  ros::NodeHandle nh_private("~");
90 
91  /**********
92  * Step 1 *
93  **********/
94  std::string ip;
95  int port;
96  ASSERT_TRUE(nh_private.getParam("modbus_server_ip", ip));
97  ASSERT_TRUE(nh_private.getParam("modbus_server_port", port));
98 
99  ModbusApiSpec write_api_spec {nh, "write_api_spec/"};
100  unsigned int modbus_register_size {write_api_spec.getMaxRegisterDefinition() + 1U};
101  prbt_hardware_support::PilzModbusServerMock modbus_server(modbus_register_size);
102  std::thread modbus_server_thread( &initalizeAndRun<prbt_hardware_support::PilzModbusServerMock>,
103  std::ref(modbus_server), ip.c_str(), static_cast<unsigned int>(port) );
104  ASSERT_TRUE(write_api_spec.hasRegisterDefinition(modbus_api_spec::BRAKETEST_PERFORMED));
105  unsigned short register_perf = write_api_spec.getRegisterDefinition(modbus_api_spec::BRAKETEST_PERFORMED);
106  ASSERT_TRUE(write_api_spec.hasRegisterDefinition(modbus_api_spec::BRAKETEST_RESULT));
107  unsigned short register_res = write_api_spec.getRegisterDefinition(modbus_api_spec::BRAKETEST_RESULT);
108 
109  modbus_server.setHoldingRegister({{register_perf, MODBUS_BRAKE_TEST_PREPARE_VALUE}});
110  modbus_server.setHoldingRegister({{register_res, MODBUS_BRAKE_TEST_PREPARE_VALUE}});
111 
112  /**********
113  * Step 2 *
114  **********/
115  CANOpenChainNodeMock canopen_mock;
116 
117  JointStatesPublisherMock joint_states_pub;
118  joint_states_pub.startAsync();
119 
120  ManipulatorMock manipulator;
123 
124  /**********
125  * Step 3 *
126  **********/
127  ros::ServiceClient brake_test_srv_client = nh.serviceClient<pilz_msgs::BrakeTest>(EXECUTE_BRAKE_TEST_SERVICE_NAME);
128  EXPECT_TRUE(brake_test_srv_client.waitForExistence(ros::Duration(WAIT_FOR_BRAKE_TEST_SERVICE_TIMEOUT_S)));
129 
130  /**********
131  * Step 4 *
132  **********/
133  pilz_msgs::BrakeTest srv;
134  EXPECT_TRUE(brake_test_srv_client.call(srv));
135 
136  /**********
137  * Step 5 *
138  **********/
139  EXPECT_TRUE(brakeTestRegisterSetOnServer(
140  modbus_server,
141  register_perf,
142  register_res,
144  1,
145  10
146  ));
147 
148  /**********
149  * Step 6 *
150  **********/
151  modbus_server.terminate();
152  modbus_server_thread.join();
153 }
154 
155 } // namespace prbt_hardware_support
156 
157 int main(int argc, char *argv[])
158 {
159  ros::init(argc, argv, "integrationtest_trigger_brake_test");
160  ros::NodeHandle nh;
161 
163  spinner.start();
164 
165  testing::InitGoogleTest(&argc, argv);
166  return RUN_ALL_TESTS();
167 }
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)
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(...)
bool getParam(const std::string &key, std::string &s) const
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)
TEST(IntegrationtestExecuteBrakeTest, testBrakeTestService)
bool sleep() const
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 Thu Feb 13 2020 03:16:51