moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
test_check_start_state_bounds.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2024, Sebastian Castro
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 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: Sebastian Castro */
36
37#include <memory>
38#include <string>
39
40#include <gmock/gmock.h>
41#include <gtest/gtest.h>
42#include <rclcpp/rclcpp.hpp>
43
47
48class TestCheckStartStateBounds : public testing::Test
49{
50protected:
51 void SetUp() override
52 {
53 node_ = std::make_shared<rclcpp::Node>("test_check_start_state_bounds_adapter", "");
54
55 // Load a robot model and place it in a planning scene.
56 urdf::ModelInterfaceSharedPtr urdf_model = moveit::core::loadModelInterface("pr2");
57 srdf::ModelSharedPtr srdf_model = std::make_shared<srdf::Model>();
58 planning_scene_ = std::make_shared<planning_scene::PlanningScene>(urdf_model, srdf_model);
59
60 // Load the planning request adapter.
61 plugin_loader_ = std::make_unique<pluginlib::ClassLoader<planning_interface::PlanningRequestAdapter>>(
62 "moveit_core", "planning_interface::PlanningRequestAdapter");
63 adapter_ = plugin_loader_->createUniqueInstance("default_planning_request_adapters/CheckStartStateBounds");
64 adapter_->initialize(node_, "");
65 }
66
67 std::shared_ptr<rclcpp::Node> node_;
68 std::shared_ptr<planning_scene::PlanningScene> planning_scene_;
69 std::unique_ptr<pluginlib::ClassLoader<planning_interface::PlanningRequestAdapter>> plugin_loader_;
70 pluginlib::UniquePtr<planning_interface::PlanningRequestAdapter> adapter_;
71};
72
74{
76 request.group_name = "right_arm";
77 request.start_state.joint_state.name = {
78 "r_shoulder_pan_joint", "r_shoulder_lift_joint", "r_upper_arm_roll_joint", "r_forearm_roll_joint",
79 "r_elbow_flex_joint", "r_wrist_flex_joint", "r_wrist_roll_joint",
80 };
81 request.start_state.joint_state.position = {
82 0.0, 0.0, 0.0, 0.0, -0.5, -0.5, 0.0,
83 };
84
85 const auto result = adapter_->adapt(planning_scene_, request);
86 EXPECT_EQ(result.val, moveit_msgs::msg::MoveItErrorCodes::SUCCESS);
87 EXPECT_EQ(result.message, "");
88}
89
90TEST_F(TestCheckStartStateBounds, TestRevoluteJointOutOfBounds)
91{
93 request.group_name = "right_arm";
94 request.start_state.joint_state.name = {
95 "r_shoulder_pan_joint", "r_shoulder_lift_joint", "r_upper_arm_roll_joint", "r_forearm_roll_joint",
96 "r_elbow_flex_joint", "r_wrist_flex_joint", "r_wrist_roll_joint",
97 };
98 request.start_state.joint_state.position = {
99 1.0, // revolute joint out of bounds
100 0.0, 0.0, 0.0, -0.5, -0.5, 0.0,
101 };
102
103 const auto result = adapter_->adapt(planning_scene_, request);
104 EXPECT_EQ(result.val, moveit_msgs::msg::MoveItErrorCodes::START_STATE_INVALID);
105 EXPECT_EQ(result.message, "Start state out of bounds.");
106}
107
108TEST_F(TestCheckStartStateBounds, TestContinuousJointOutOfBounds)
109{
111 request.group_name = "right_arm";
112 request.start_state.joint_state.name = {
113 "r_shoulder_pan_joint", "r_shoulder_lift_joint", "r_upper_arm_roll_joint", "r_forearm_roll_joint",
114 "r_elbow_flex_joint", "r_wrist_flex_joint", "r_wrist_roll_joint",
115 };
116 request.start_state.joint_state.position = {
117 0.0, 0.0, 0.0, 100.0, // continuous joint out of bounds
118 -0.5, -0.5, 0.0,
119 };
120
121 const auto result = adapter_->adapt(planning_scene_, request);
122 EXPECT_EQ(result.val, moveit_msgs::msg::MoveItErrorCodes::START_STATE_INVALID);
123 EXPECT_EQ(result.message, "Start state out of bounds.");
124}
125
126TEST_F(TestCheckStartStateBounds, TestContinuousJointFixedBounds)
127{
129 request.group_name = "right_arm";
130 request.start_state.joint_state.name = {
131 "r_shoulder_pan_joint", "r_shoulder_lift_joint", "r_upper_arm_roll_joint", "r_forearm_roll_joint",
132 "r_elbow_flex_joint", "r_wrist_flex_joint", "r_wrist_roll_joint",
133 };
134 request.start_state.joint_state.position = {
135 0.0, 0.0, 0.0, 100.0, // continuous joint out of bounds
136 -0.5, -0.5, 0.0,
137 };
138
139 // Modify the start state. The adapter should succeed.
140 node_->set_parameter(rclcpp::Parameter("fix_start_state", true));
141
142 const auto result = adapter_->adapt(planning_scene_, request);
143 EXPECT_EQ(result.val, moveit_msgs::msg::MoveItErrorCodes::SUCCESS);
144 EXPECT_EQ(result.message, "Normalized start state.");
145
146 // Validate that the large continuous joint position value in the request start state was normalized.
147 const auto& joint_names = request.start_state.joint_state.name;
148 const size_t joint_idx =
149 std::find(joint_names.begin(), joint_names.end(), "r_forearm_roll_joint") - joint_names.begin();
150 EXPECT_NEAR(request.start_state.joint_state.position[joint_idx], -0.530965, 1.0e-4);
151}
152
153int main(int argc, char** argv)
154{
155 rclcpp::init(argc, argv);
156 ::testing::InitGoogleTest(&argc, argv);
157 const int result = RUN_ALL_TESTS();
158 rclcpp::shutdown();
159 return result;
160}
std::unique_ptr< pluginlib::ClassLoader< planning_interface::PlanningRequestAdapter > > plugin_loader_
std::shared_ptr< rclcpp::Node > node_
pluginlib::UniquePtr< planning_interface::PlanningRequestAdapter > adapter_
std::shared_ptr< planning_scene::PlanningScene > planning_scene_
urdf::ModelInterfaceSharedPtr loadModelInterface(const std::string &robot_name)
Loads a URDF Model Interface from moveit_resources.
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
int main(int argc, char **argv)
TEST_F(TestCheckStartStateBounds, TestWithinBounds)