combined_robot_hw_test.cpp
Go to the documentation of this file.
1 // Copyright (C) 2015, Shadow Robot Company Ltd.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Shadow Robot Company Ltd. nor the names of its
12 // contributors may be used to endorse or promote products derived from
13 // this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
27 
28 #include <ros/ros.h>
29 #include <gtest/gtest.h>
30 
35 
37 
38 TEST(CombinedRobotHWTests, combinationOk)
39 {
40  ros::NodeHandle nh;
41 
42  CombinedRobotHW robot_hw;
43  bool init_success = robot_hw.init(nh, nh);
44  ASSERT_TRUE(init_success);
45 
52 
53  ASSERT_TRUE(js_interface != NULL);
54  ASSERT_TRUE(ej_interface != NULL);
55  ASSERT_TRUE(vj_interface != NULL);
56  ASSERT_TRUE(ft_interface != NULL);
57  ASSERT_TRUE(plain_hw_interface != NULL);
58 
59  // Test that no PositionJointInterface was found
60  ASSERT_EQ(NULL, pj_interface);
61 
62  // Test some handles from my_robot_hw_1
63  hardware_interface::JointStateHandle js_handle = js_interface->getHandle("test_joint1");
64  hardware_interface::JointHandle ej_handle = ej_interface->getHandle("test_joint1");
65  hardware_interface::JointHandle vj_handle = vj_interface->getHandle("test_joint1");
66  ASSERT_FLOAT_EQ(1.0, js_handle.getPosition());
67  ASSERT_FLOAT_EQ(0.0, ej_handle.getVelocity());
68  ASSERT_FLOAT_EQ(3.0, ej_handle.getCommand());
69 
70  // Test some handles from my_robot_hw_3
71  js_handle = js_interface->getHandle("right_arm_joint_1");
72  ej_handle = ej_interface->getHandle("right_arm_joint_1");
73  vj_handle = vj_interface->getHandle("right_arm_joint_1");
74  ASSERT_FLOAT_EQ(1.0, js_handle.getPosition());
75  ASSERT_FLOAT_EQ(0.0, ej_handle.getVelocity());
76  ASSERT_FLOAT_EQ(1.5, ej_handle.getCommand());
77 
78  // Test some handles from my_robot_hw_4
79  hardware_interface::ForceTorqueSensorHandle ft_handle = ft_interface->getHandle("ft_sensor_1");
80  ASSERT_FLOAT_EQ(0.2, ft_handle.getForce()[2]);
81 
82  // Test non-existent handle throws exception
83  ASSERT_ANY_THROW(ej_interface->getHandle("non_existent_joint"));
84 
85  // Test read and write functions
86  ros::Duration period(1.0);
87  robot_hw.read(ros::Time::now(), period);
88  js_handle = js_interface->getHandle("test_joint1");
89  ASSERT_FLOAT_EQ(2.7, js_handle.getPosition());
90  ASSERT_FLOAT_EQ(1.2, ft_handle.getForce()[2]);
91 
92  ej_handle = ej_interface->getHandle("test_joint1");
93  ej_handle.setCommand(3.5);
94  robot_hw.write(ros::Time::now(), period);
95  ej_handle = ej_interface->getHandle("test_joint2");
96  ASSERT_FLOAT_EQ(3.5, ej_handle.getCommand());
97 }
98 
99 TEST(CombinedRobotHWTests, switchOk)
100 {
101  ros::NodeHandle nh;
102 
103  CombinedRobotHW robot_hw;
104  bool init_success = robot_hw.init(nh, nh);
105  ASSERT_TRUE(init_success);
106 
107  // Test empty list (it is expected to work)
108  {
109  std::list<hardware_interface::ControllerInfo> start_list;
110  std::list<hardware_interface::ControllerInfo> stop_list;
111  ASSERT_TRUE(robot_hw.prepareSwitch(start_list, stop_list));
112  ASSERT_NO_THROW(robot_hw.doSwitch(start_list, stop_list));
113  }
114 
115  // Test failure
116  {
117  std::list<hardware_interface::ControllerInfo> start_list;
118  std::list<hardware_interface::ControllerInfo> stop_list;
120  controller_1.name = "ctrl_1";
121  controller_1.type = "some_type";
123  iface_res_1.hardware_interface = "hardware_interface::ForceTorqueSensorInterface";
124  iface_res_1.resources.insert("ft_sensor_1");
125  controller_1.claimed_resources.push_back(iface_res_1);
126  start_list.push_back(controller_1);
127  ASSERT_FALSE(robot_hw.prepareSwitch(start_list, stop_list));
128  ASSERT_ANY_THROW(robot_hw.doSwitch(start_list, stop_list));
129  }
130 
131  // Test existing interfaces and resources
132  {
133  std::list<hardware_interface::ControllerInfo> start_list;
134  std::list<hardware_interface::ControllerInfo> stop_list;
136  controller_1.name = "ctrl_1";
137  controller_1.type = "some_type";
139  iface_res_1.hardware_interface = "hardware_interface::EffortJointInterface";
140  iface_res_1.resources.insert("test_joint1");
141  iface_res_1.resources.insert("test_joint2");
142  iface_res_1.resources.insert("test_joint3");
143  iface_res_1.resources.insert("test_joint4");
144  controller_1.claimed_resources.push_back(iface_res_1);
146  iface_res_2.hardware_interface = "hardware_interface::VelocityJointInterface";
147  iface_res_2.resources.insert("test_joint1");
148  iface_res_2.resources.insert("test_joint4");
149  controller_1.claimed_resources.push_back(iface_res_1);
150  start_list.push_back(controller_1);
151 
154  iface_res_3.hardware_interface = "hardware_interface::VelocityJointInterface";
155  iface_res_3.resources.insert("test_joint3");
156  iface_res_3.resources.insert("test_joint5");
157  controller_2.claimed_resources.push_back(iface_res_3);
158  start_list.push_back(controller_2);
159  ASSERT_TRUE(robot_hw.prepareSwitch(start_list, stop_list));
160  ASSERT_NO_THROW(robot_hw.doSwitch(start_list, stop_list));
161  }
162 
163  // Test non-registered interfaces and resources
164  // (this should also work, as CombinedRobotHW will filter out the non-registered ones)
165  {
166  std::list<hardware_interface::ControllerInfo> start_list;
167  std::list<hardware_interface::ControllerInfo> stop_list;
169  controller_1.name = "ctrl_1";
170  controller_1.type = "some_type";
172  iface_res_1.hardware_interface = "hardware_interface::NonRegisteredInterface";
173  iface_res_1.resources.insert("test_joint1");
174  iface_res_1.resources.insert("test_joint2");
175  iface_res_1.resources.insert("test_joint3");
176  iface_res_1.resources.insert("test_joint4");
177  controller_1.claimed_resources.push_back(iface_res_1);
179  iface_res_2.hardware_interface = "hardware_interface::VelocityJointInterface";
180  iface_res_2.resources.insert("test_joint1");
181  iface_res_2.resources.insert("non_registered_joint1");
182  controller_1.claimed_resources.push_back(iface_res_1);
183  start_list.push_back(controller_1);
184 
187  iface_res_3.hardware_interface = "hardware_interface::VelocityJointInterface";
188  iface_res_3.resources.insert("test_joint3");
189  iface_res_3.resources.insert("non_registered_joint2");
190  controller_2.claimed_resources.push_back(iface_res_3);
191  start_list.push_back(controller_2);
192  ASSERT_TRUE(robot_hw.prepareSwitch(start_list, stop_list));
193  ASSERT_NO_THROW(robot_hw.doSwitch(start_list, stop_list));
194  }
195 
196  // Test resource and controller filtering
197  {
198  std::list<hardware_interface::ControllerInfo> start_list;
199  std::list<hardware_interface::ControllerInfo> stop_list;
201  controller_1.name = "ctrl_without_my_robot_hw_2_resources_in_one_of_two_ifaces";
202  controller_1.type = "some_type";
204  // iface_res_1 should be filtered out when controller_1 is passed to my_robot_hw_2
205  // as none of its resources belongs to my_robot_hw_2
206  iface_res_1.hardware_interface = "hardware_interface::EffortJointInterface";
207  iface_res_1.resources.insert("test_joint1");
208  iface_res_1.resources.insert("test_joint2");
209  iface_res_1.resources.insert("test_joint3");
210  controller_1.claimed_resources.push_back(iface_res_1);
212  iface_res_2.hardware_interface = "hardware_interface::VelocityJointInterface";
213  iface_res_2.resources.insert("test_joint1");
214  iface_res_2.resources.insert("test_joint4");
215  controller_1.claimed_resources.push_back(iface_res_1);
216  start_list.push_back(controller_1);
217 
219  // controller_2 should be filtered out when controller_1 is passed to my_robot_hw_2
220  // as none of its resources belongs to my_robot_hw_2
221  controller_2.name = "ctrl_without_my_robot_hw_2_resources";
222  controller_2.type = "some_type";
224  iface_res_3.hardware_interface = "hardware_interface::VelocityJointInterface";
225  iface_res_3.resources.insert("test_joint1");
226  iface_res_3.resources.insert("test_joint2");
227  controller_2.claimed_resources.push_back(iface_res_3);
228  start_list.push_back(controller_2);
229  ASSERT_TRUE(robot_hw.prepareSwitch(start_list, stop_list));
230  ASSERT_NO_THROW(robot_hw.doSwitch(start_list, stop_list));
231  }
232 }
233 
234 int main(int argc, char** argv)
235 {
236  testing::InitGoogleTest(&argc, argv);
237  ros::init(argc, argv, "CombinedRobotHWTestNode");
238 
239  int ret = RUN_ALL_TESTS();
240 
241  ros::shutdown();
242  return ret;
243 }
virtual bool prepareSwitch(const std::list< hardware_interface::ControllerInfo > &start_list, const std::list< hardware_interface::ControllerInfo > &stop_list)
std::set< std::string > resources
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
virtual bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh)
int main(int argc, char **argv)
std::vector< InterfaceResources > claimed_resources
virtual void read(const ros::Time &time, const ros::Duration &period)
JointStateHandle getHandle(const std::string &name)
virtual void doSwitch(const std::list< hardware_interface::ControllerInfo > &start_list, const std::list< hardware_interface::ControllerInfo > &stop_list)
void setCommand(double command)
static Time now()
TEST(CombinedRobotHWTests, combinationOk)
ROSCPP_DECL void shutdown()
virtual void write(const ros::Time &time, const ros::Duration &period)


combined_robot_hw_tests
Author(s): Toni Oliver
autogenerated on Mon Apr 20 2020 03:52:11