moveit2
The MoveIt Motion Planning Framework for ROS 2.
moveit_cpp_test.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, PickNik Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Jafar Abdi
36  Desc: Test the MoveItCpp and PlanningComponent interfaces
37 */
38 
39 // ROS
40 #include <rclcpp/rclcpp.hpp>
41 
42 // Testing
43 #include <gtest/gtest.h>
44 
45 // Main class
48 // Msgs
49 #include <geometry_msgs/PointStamped.h>
50 
51 namespace moveit_cpp
52 {
53 class MoveItCppTest : public ::testing::Test
54 {
55 public:
56  void SetUp() override
57  {
58  rclcpp::NodeOptions node_options;
59  // This enables loading arbitrary parameters
60  // However, best practice would be to declare parameters in the corresponding classes
61  // and provide descriptions about expected use
62  // TODO(henningkayser): remove once all parameters are declared inside the components
63  node_options.automatically_declare_parameters_from_overrides(true);
64  node_ = rclcpp::Node::make_shared("/moveit_cpp_test");
65  moveit_cpp_ptr = std::make_unique<MoveItCpp>(node_);
66  planning_component_ptr = std::make_unique<PlanningComponent>(PLANNING_GROUP, moveit_cpp_ptr);
67  robot_model_ptr = moveit_cpp_ptr->getRobotModel();
68  jmg_ptr = robot_model_ptr->getJointModelGroup(PLANNING_GROUP);
69 
70  target_pose1.header.frame_id = "panda_link0";
71  target_pose1.pose.orientation.w = 1.0;
72  target_pose1.pose.position.x = 0.28;
73  target_pose1.pose.position.y = -0.2;
74  target_pose1.pose.position.z = 0.5;
75 
76  start_pose.orientation.w = 1.0;
77  start_pose.position.x = 0.55;
78  start_pose.position.y = 0.0;
79  start_pose.position.z = 0.6;
80 
81  target_pose2.orientation.w = 1.0;
82  target_pose2.position.x = 0.55;
83  target_pose2.position.y = -0.05;
84  target_pose2.position.z = 0.8;
85  }
86 
87 protected:
88  rclcpp::Node node_;
89  MoveItCppPtr moveit_cpp_ptr;
90  PlanningComponentPtr planning_component_ptr;
91  moveit::core::RobotModelConstPtr robot_model_ptr;
93  const std::string PLANNING_GROUP = "panda_arm";
94  geometry_msgs::PoseStamped target_pose1;
95  geometry_msgs::Pose target_pose2;
96  geometry_msgs::Pose start_pose;
97 };
98 
99 // Test the current and the initial state of the Panda robot
100 TEST_F(MoveItCppTest, GetCurrentStateTest)
101 {
102  ros::Duration(1.0).sleep(); // Otherwise joint_states will result in an invalid robot state
103  auto robot_model = moveit_cpp_ptr->getRobotModel();
104  auto robot_state = std::make_shared<moveit::core::RobotState>(robot_model);
105  EXPECT_TRUE(moveit_cpp_ptr->getCurrentState(robot_state, 0.0));
106  // Make sure the Panda robot is in "ready" state which is loaded from yaml
107  std::vector<double> joints_vals;
108  robot_state->copyJointGroupPositions(PLANNING_GROUP, joints_vals);
109  EXPECT_NEAR(joints_vals[0], 0.0, 0.001); // panda_joint1
110  EXPECT_NEAR(joints_vals[1], -0.785, 0.001); // panda_joint2
111  EXPECT_NEAR(joints_vals[2], 0.0, 0.001); // panda_joint3
112  EXPECT_NEAR(joints_vals[3], -2.356, 0.001); // panda_joint4
113  EXPECT_NEAR(joints_vals[4], 0.0, 0.001); // panda_joint5
114  EXPECT_NEAR(joints_vals[5], 1.571, 0.001); // panda_joint6
115  EXPECT_NEAR(joints_vals[6], 0.785, 0.001); // panda_joint7
116 }
117 
118 // Test the name of the planning group used by PlanningComponent for the Panda robot
119 TEST_F(MoveItCppTest, NameOfPlanningGroupTest)
120 {
121  EXPECT_STREQ(planning_component_ptr->getPlanningGroupName().c_str(), "panda_arm");
122 }
123 
124 // Test setting the start state of the plan to the current state of the robot
125 TEST_F(MoveItCppTest, TestSetStartStateToCurrentState)
126 {
127  planning_component_ptr->setStartStateToCurrentState();
128  planning_component_ptr->setGoal(target_pose1, "panda_link8");
129 
130  ASSERT_TRUE(static_cast<bool>(planning_component_ptr->plan()));
131  // TODO(JafarAbdi) adding testing to the soln state
132 }
133 
134 // Test setting the goal using geometry_msgs::PoseStamped and a robot's link name
135 TEST_F(MoveItCppTest, TestSetGoalFromPoseStamped)
136 {
137  planning_component_ptr->setGoal(target_pose1, "panda_link8");
138 
139  ASSERT_TRUE(static_cast<bool>(planning_component_ptr->plan()));
140 }
141 
142 // Test setting the plan start state using moveit::core::RobotState
143 TEST_F(MoveItCppTest, TestSetStartStateFromRobotState)
144 {
145  auto start_state = *(moveit_cpp_ptr->getCurrentState());
146  start_state.setFromIK(jmg_ptr, start_pose);
147 
148  planning_component_ptr->setStartState(start_state);
149  planning_component_ptr->setGoal(target_pose1, "panda_link8");
150 
151  ASSERT_TRUE(static_cast<bool>(planning_component_ptr->plan()));
152 }
153 
154 // Test setting the goal of the plan using a moveit::core::RobotState
155 TEST_F(MoveItCppTest, TestSetGoalFromRobotState)
156 {
157  auto target_state = *(moveit_cpp_ptr->getCurrentState());
158 
159  target_state.setFromIK(jmg_ptr, target_pose2);
160 
161  planning_component_ptr->setGoal(target_state);
162 
163  ASSERT_TRUE(static_cast<bool>(planning_component_ptr->plan()));
164 }
165 } // namespace moveit_cpp
166 
167 int main(int argc, char** argv)
168 {
169  testing::InitGoogleTest(&argc, argv);
170  ros::init(argc, argv, "moveit_cpp_test");
171 
172  ros::AsyncSpinner spinner(4);
173  spinner.start();
174 
175  int result = RUN_ALL_TESTS();
176 
177  return result;
178 }
PlanningComponentPtr planning_component_ptr
const std::string PLANNING_GROUP
geometry_msgs::PoseStamped target_pose1
const moveit::core::JointModelGroup * jmg_ptr
moveit::core::RobotModelConstPtr robot_model_ptr
geometry_msgs::Pose start_pose
geometry_msgs::Pose target_pose2
int main(int argc, char **argv)
TEST_F(MoveItCppTest, GetCurrentStateTest)