test_iterative_torque_limit_parameterization.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2023, Re:Build AppliedLogix, LLC
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 are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 #include <gtest/gtest.h>
37 #include <urdf_parser/urdf_parser.h>
38 
39 TEST(time_optimal_trajectory_generation, test_totg_with_torque_limits)
40 {
41  // Request a trajectory. This will serve as the baseline.
42  // Then decrease the joint torque limits and re-parameterize. The trajectory duration should be shorter.
43 
44  const std::string urdf = R"(<?xml version="1.0" ?>
45  <robot name="one_robot">
46  <link name="base_link"/>
47  <joint name="joint_a" type="continuous">
48  <axis xyz="0.7071 0.7071 0"/>
49  <parent link="base_link"/>
50  <child link="link_a"/>
51  </joint>
52  <link name="link_a"/>
53  </robot>)";
54 
55  const std::string srdf = R"(<?xml version="1.0" ?>
56  <robot name="one_robot">
57  <virtual_joint name="base_joint" child_link="base_link" parent_frame="odom_combined" type="planar"/>
58  <group name="single_dof_group">
59  <joint name="joint_a"/>
60  </group>
61  "</robot>)";
62 
63  urdf::ModelInterfaceSharedPtr urdf_model = urdf::parseURDF(urdf);
64  srdf::ModelSharedPtr srdf_model = std::make_shared<srdf::Model>();
65  srdf_model->initString(*urdf_model, srdf);
66  auto robot_model = std::make_shared<moveit::core::RobotModel>(urdf_model, srdf_model);
67  ASSERT_TRUE((bool)robot_model) << "Failed to load robot model";
68 
69  auto group = robot_model->getJointModelGroup("single_dof_group");
70  ASSERT_TRUE((bool)group) << "Failed to load joint model group ";
71  moveit::core::RobotState waypoint_state(robot_model);
72  waypoint_state.setToDefaultValues();
73 
75  waypoint_state.setJointGroupPositions(group, std::vector<double>{ -0.5 });
76  trajectory.addSuffixWayPoint(waypoint_state, 0.1);
77  waypoint_state.setJointGroupPositions(group, std::vector<double>{ 0.5 });
78  trajectory.addSuffixWayPoint(waypoint_state, 0.1);
79 
80  const geometry_msgs::Vector3 gravity_vector = [] {
81  geometry_msgs::Vector3 gravity;
82  gravity.x = 0;
83  gravity.y = 0;
84  gravity.z = -9.81;
85  return gravity;
86  }();
87  const std::vector<double> joint_torque_limits{ 250 }; // in N*m
88  const double accel_limit_decrement_factor = 0.1;
89  const std::unordered_map<std::string, double> velocity_limits = { { "joint_a", 3 } };
90  const std::unordered_map<std::string, double> acceleration_limits = { { "joint_a", 3 } };
91 
92  trajectory_processing::IterativeTorqueLimitParameterization totg(0.1 /* path tolerance */, 0.01 /* resample dt */,
93  0.001 /* min angle change */);
94 
95  // Assume no external forces on the robot.
96  const std::vector<geometry_msgs::Wrench> external_link_wrenches = [&group] {
97  geometry_msgs::Wrench zero_wrench;
98  zero_wrench.force.x = 0;
99  zero_wrench.force.y = 0;
100  zero_wrench.force.z = 0;
101  zero_wrench.torque.x = 0;
102  zero_wrench.torque.y = 0;
103  zero_wrench.torque.z = 0;
104  // KDL (the dynamics solver) requires one wrench per link
105  std::vector<geometry_msgs::Wrench> vector_of_zero_wrenches(group->getLinkModels().size(), zero_wrench);
106  return vector_of_zero_wrenches;
107  }();
108 
109  bool totg_success =
110  totg.computeTimeStampsWithTorqueLimits(trajectory, gravity_vector, external_link_wrenches, joint_torque_limits,
111  accel_limit_decrement_factor, velocity_limits, acceleration_limits,
112  1.0 /* vel scaling */, 1.0 /* accel scaling */);
113  ASSERT_TRUE(totg_success) << "Failed to compute timestamps";
114  double first_duration = trajectory.getDuration();
115 
116  // Now decrease joint torque limits and re-time-parameterize. The trajectory duration should be longer.
117  const std::vector<double> lower_torque_limits{ 1 }; // in N*m
118  totg_success =
119  totg.computeTimeStampsWithTorqueLimits(trajectory, gravity_vector, external_link_wrenches, lower_torque_limits,
120  accel_limit_decrement_factor, velocity_limits, acceleration_limits,
121  1.0 /* accel scaling */, 1.0 /* vel scaling */);
122  ASSERT_TRUE(totg_success) << "Failed to compute timestamps";
123  double second_duration = trajectory.getDuration();
124  EXPECT_GT(second_duration, first_duration) << "The second time parameterization should result in a longer duration";
125 }
126 
127 int main(int argc, char** argv)
128 {
129  testing::InitGoogleTest(&argc, argv);
130  return RUN_ALL_TESTS();
131 }
moveit::core
Core components of MoveIt.
Definition: kinematics_base.h:83
trajectory_processing::IterativeTorqueLimitParameterization::computeTimeStampsWithTorqueLimits
bool computeTimeStampsWithTorqueLimits(robot_trajectory::RobotTrajectory &trajectory, const geometry_msgs::Vector3 &gravity_vector, const std::vector< geometry_msgs::Wrench > &external_link_wrenches, const std::vector< double > &joint_torque_limits, double accel_limit_decrement_factor, const std::unordered_map< std::string, double > &velocity_limits, const std::unordered_map< std::string, double > &acceleration_limits, const double max_velocity_scaling_factor, const double max_acceleration_scaling_factor) const
Compute a trajectory with waypoints spaced equally in time (according to resample_dt_)....
Definition: iterative_torque_limit_parameterization.cpp:84
srdf::ModelSharedPtr
std::shared_ptr< Model > ModelSharedPtr
robot_trajectory::RobotTrajectory::addSuffixWayPoint
RobotTrajectory & addSuffixWayPoint(const moveit::core::RobotState &state, double dt)
Add a point to the trajectory.
Definition: robot_trajectory.h:205
moveit::core::RobotState
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:155
robot_trajectory::RobotTrajectory::getDuration
double getDuration() const
Definition: robot_trajectory.cpp:114
robot_trajectory::RobotTrajectory
Maintain a sequence of waypoints and the time durations between these waypoints.
Definition: robot_trajectory.h:84
main
int main(int argc, char **argv)
Definition: test_all_valid.cpp:55
moveit::core::RobotState::setToDefaultValues
void setToDefaultValues()
Set all joints to their default positions. The default position is 0, or if that is not within bounds...
Definition: robot_state.cpp:434
robot_model_test_utils.h
urdf
TEST
TEST(time_optimal_trajectory_generation, test_totg_with_torque_limits)
Definition: test_iterative_torque_limit_parameterization.cpp:39
iterative_torque_limit_parameterization.h
srdf
moveit::core::RobotState::setJointGroupPositions
void setJointGroupPositions(const std::string &joint_group_name, const double *gstate)
Given positions for the variables that make up a group, in the order found in the group (including va...
Definition: robot_state.h:671
trajectory_processing::IterativeTorqueLimitParameterization
Definition: iterative_torque_limit_parameterization.h:74


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Wed Feb 21 2024 03:25:09