franka_hw_interfaces_test.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Franka Emika GmbH
2 // Use of this source code is governed by the Apache-2.0 license, see LICENSE
3 #include <array>
4 #include <random>
5 #include <string>
6 #include <vector>
7 
8 #include <gtest/gtest.h>
9 
13 #include <ros/ros.h>
14 #include <urdf/model.h>
15 
17 #include <franka_hw/franka_hw.h>
19 
20 extern std::string arm_id;
21 extern std::array<std::string, 7> joint_names;
22 
23 namespace franka_hw {
24 
25 TEST(FrankaHWTests, InterfacesWorkForReadAndCommand) {
26  auto robot_ptr = std::make_unique<FrankaHW>();
27  ros::NodeHandle private_nh("~");
28  ros::NodeHandle root_nh;
29  EXPECT_TRUE(robot_ptr->initParameters(root_nh, private_nh));
30  robot_ptr->initROSInterfaces(private_nh);
31 
40  FrankaPoseCartesianInterface* fpc_interface = robot_ptr->get<FrankaPoseCartesianInterface>();
41  FrankaVelocityCartesianInterface* fvc_interface =
42  robot_ptr->get<FrankaVelocityCartesianInterface>();
43  FrankaStateInterface* fs_interface = robot_ptr->get<FrankaStateInterface>();
44 
45  ASSERT_NE(nullptr, js_interface);
46  ASSERT_NE(nullptr, pj_interface);
47  ASSERT_NE(nullptr, vj_interface);
48  ASSERT_NE(nullptr, ej_interface);
49  ASSERT_NE(nullptr, fpc_interface);
50  ASSERT_NE(nullptr, fvc_interface);
51  ASSERT_NE(nullptr, fs_interface);
52 
53  // Model interface not available with this signature
54  FrankaModelInterface* fm_interface = robot_ptr->get<FrankaModelInterface>();
55  ASSERT_EQ(nullptr, fm_interface);
56 
57  EXPECT_NO_THROW(FrankaCartesianPoseHandle fpc_handle =
58  fpc_interface->getHandle(arm_id + "_robot"));
59  FrankaCartesianPoseHandle fpc_handle = fpc_interface->getHandle(arm_id + "_robot");
60  std::array<double, 16> pose_command = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
61  1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
62  fpc_handle.setCommand(pose_command);
63  EXPECT_EQ(pose_command, fpc_handle.getCommand());
64 
65  EXPECT_NO_THROW(FrankaCartesianVelocityHandle fvc_handle =
66  fvc_interface->getHandle(arm_id + "_robot"));
67  FrankaCartesianVelocityHandle fvc_handle = fvc_interface->getHandle(arm_id + "_robot");
68  std::array<double, 6> vel_command = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
69  fvc_handle.setCommand(vel_command);
70  EXPECT_EQ(vel_command, fvc_handle.getCommand());
71 
72  EXPECT_NO_THROW(fs_interface->getHandle(arm_id + "_robot"));
73 }
74 
75 TEST(FrankaHWTests, JointLimitInterfacesEnforceLimitsOnCommands) {
76  auto robot_ptr = std::make_unique<FrankaHW>();
77  ros::NodeHandle nh("~");
78  ASSERT_TRUE(robot_ptr->initParameters(nh, nh));
79  robot_ptr->initROSInterfaces(nh);
80 
87  ASSERT_NE(nullptr, pj_interface);
88  ASSERT_NE(nullptr, vj_interface);
89  ASSERT_NE(nullptr, ej_interface);
90 
91  std::vector<joint_limits_interface::JointLimits> joint_limits(7);
92  std::vector<hardware_interface::JointHandle> position_handles(7);
93  std::vector<hardware_interface::JointHandle> velocity_handles(7);
94  std::vector<hardware_interface::JointHandle> effort_handles(7);
95 
96  urdf::Model urdf_model;
97  ASSERT_TRUE(urdf_model.initParamWithNodeHandle("robot_description"));
98 
99  for (size_t i = 0; i < joint_names.size(); ++i) {
100  auto urdf_joint = urdf_model.getJoint(joint_names[i]);
101  ASSERT_TRUE(joint_limits_interface::getJointLimits(urdf_joint, joint_limits[i]));
102  ASSERT_NO_THROW(position_handles[i] = pj_interface->getHandle(joint_names[i]));
103  ASSERT_NO_THROW(velocity_handles[i] = vj_interface->getHandle(joint_names[i]));
104  ASSERT_NO_THROW(effort_handles[i] = ej_interface->getHandle(joint_names[i]));
105  }
106 
107  std::uniform_real_distribution<double> uniform_distribution(0.1, 3.0);
108  std::default_random_engine random_engine;
109 
110  for (size_t i = 0; i < joint_names.size(); ++i) {
111  position_handles[i].setCommand(joint_limits[i].max_position +
112  uniform_distribution(random_engine));
113  velocity_handles[i].setCommand(joint_limits[i].max_velocity +
114  uniform_distribution(random_engine));
115  effort_handles[i].setCommand(joint_limits[i].max_effort + uniform_distribution(random_engine));
116  }
117  robot_ptr->enforceLimits(ros::Duration(0.001));
118  for (size_t i = 0; i < joint_names.size(); ++i) {
119  EXPECT_LE(position_handles[i].getCommand(), joint_limits[i].max_position);
120  EXPECT_GE(position_handles[i].getCommand(), joint_limits[i].min_position);
121  EXPECT_LE(velocity_handles[i].getCommand(), joint_limits[i].max_velocity);
122  EXPECT_GE(velocity_handles[i].getCommand(), -joint_limits[i].max_velocity);
123  EXPECT_LE(effort_handles[i].getCommand(), joint_limits[i].max_effort);
124  EXPECT_GE(effort_handles[i].getCommand(), -joint_limits[i].max_effort);
125  }
126 
127  for (size_t i = 0; i < joint_names.size(); ++i) {
128  position_handles[i].setCommand(joint_limits[i].min_position -
129  uniform_distribution(random_engine));
130  velocity_handles[i].setCommand(-joint_limits[i].max_velocity -
131  uniform_distribution(random_engine));
132  effort_handles[i].setCommand(-joint_limits[i].max_effort - uniform_distribution(random_engine));
133  }
134  robot_ptr->enforceLimits(ros::Duration(0.001));
135  for (size_t i = 0; i < joint_names.size(); ++i) {
136  EXPECT_LE(position_handles[i].getCommand(), joint_limits[i].max_position);
137  EXPECT_GE(position_handles[i].getCommand(), joint_limits[i].min_position);
138  EXPECT_LE(velocity_handles[i].getCommand(), joint_limits[i].max_velocity);
139  EXPECT_GE(velocity_handles[i].getCommand(), -joint_limits[i].max_velocity);
140  EXPECT_LE(effort_handles[i].getCommand(), joint_limits[i].max_effort);
141  EXPECT_GE(effort_handles[i].getCommand(), -joint_limits[i].max_effort);
142  }
143 }
144 
145 } // namespace franka_hw
std::string arm_id
Hardware interface to command Cartesian poses.
URDF_EXPORT bool initParamWithNodeHandle(const std::string &param, const ros::NodeHandle &nh=ros::NodeHandle())
Hardware interface to read the complete robot state.
TEST(FrankaHWTests, InterfacesWorkForReadAndCommand)
std::array< std::string, 7 > joint_names
void setCommand(const std::array< double, 16 > &command) noexcept
Sets the given command.
Handle to read and command a Cartesian velocity.
Handle to read and command a Cartesian pose.
void setCommand(std::array< double, 6 > &command) noexcept
Sets the given command.
JointHandle getHandle(const std::string &name)
Hardware interface to command Cartesian velocities.
bool getJointLimits(urdf::JointConstSharedPtr urdf_joint, JointLimits &limits)
Hardware interface to perform calculations using the dynamic model of a robot.


franka_hw
Author(s): Franka Emika GmbH
autogenerated on Fri Oct 23 2020 03:47:05