joint_limits.cpp
Go to the documentation of this file.
1 
26 #include <ros/console.h>
30 
32 
33 namespace stomp_moveit
34 {
35 namespace noisy_filters
36 {
37 
38 JointLimits::JointLimits():
39  lock_start_(true),
40  lock_goal_(true),
41  has_goal_constraints_(false)
42 {
43 
44 }
45 
46 JointLimits::~JointLimits()
47 {
48 
49 }
50 
51 bool JointLimits::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
52  const std::string& group_name,const XmlRpc::XmlRpcValue& config)
53 {
54  using namespace moveit::core;
55 
56  robot_model_ = robot_model_ptr;
57  group_name_ = group_name;
58 
59  // creating states
60  start_state_.reset(new RobotState(robot_model_));
61  goal_state_.reset(new RobotState(robot_model_));
62 
63  return configure(config);
64 }
65 
66 bool JointLimits::configure(const XmlRpc::XmlRpcValue& config)
67 {
68 
69  // check parameter presence
70  auto members = {"lock_start","lock_goal"};
71  for(auto& m : members)
72  {
73  if(!config.hasMember(m))
74  {
75  ROS_ERROR("%s failed to find one or more required parameters",getName().c_str());
76  return false;
77  }
78  }
79 
80  try
81  {
82  XmlRpc::XmlRpcValue c = config;
83  lock_start_ = static_cast<bool>(c["lock_start"]);
84  lock_goal_ = static_cast<bool>(c["lock_goal"]);
85  }
86  catch(XmlRpc::XmlRpcException& e)
87  {
88  ROS_ERROR("JointLimits plugin failed to load parameters %s",e.getMessage().c_str());
89  return false;
90  }
91 
92  return true;
93 }
94 
95 bool JointLimits::setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr& planning_scene,
96  const moveit_msgs::MotionPlanRequest &req,
97  const stomp_core::StompConfiguration &config,
98  moveit_msgs::MoveItErrorCodes& error_code)
99 {
100  using namespace moveit::core;
101 
102  error_code.val = error_code.val | moveit_msgs::MoveItErrorCodes::SUCCESS;
103 
104  // saving start state
105  if(!robotStateMsgToRobotState(req.start_state,*start_state_))
106  {
107  ROS_ERROR_STREAM("Failed to save start state");
108  return false;
109  }
110 
111  if(!start_state_->satisfiesBounds(robot_model_->getJointModelGroup(group_name_)))
112  {
113  ROS_WARN("%s Requested Start State is out of bounds",getName().c_str());
114  }
115 
116  // saving goal state
117  if(lock_goal_)
118  {
119  bool goal_state_saved = false;
120  has_goal_constraints_ = false;
121  for(auto& gc: req.goal_constraints)
122  {
123  has_goal_constraints_ = !gc.joint_constraints.empty();
124  for(auto& jc : gc.joint_constraints)
125  {
126  goal_state_->setVariablePosition(jc.joint_name,jc.position);
127  }
128 
129  if(!goal_state_->satisfiesBounds(robot_model_->getJointModelGroup(group_name_)))
130  {
131  ROS_WARN("%s Requested Goal State is out of bounds",getName().c_str());
132  continue;
133  }
134 
135  if(has_goal_constraints_)
136  {
137  goal_state_saved = true;
138  break;
139  }
140  }
141 
142  if(has_goal_constraints_ && !goal_state_saved)
143  {
144  ROS_ERROR_STREAM("Failed to save goal state");
145  return false;
146  }
147  }
148 
149  return true;
150 }
151 
152 bool JointLimits::filter(std::size_t start_timestep,std::size_t num_timesteps,
153  int iteration_number,int rollout_number,Eigen::MatrixXd& parameters,bool& filtered)
154 {
155  using namespace moveit::core;
156 
157  filtered = false;
158  const JointModelGroup* joint_group = robot_model_->getJointModelGroup(group_name_);
159  const std::vector<const JointModel*>& joint_models = joint_group->getActiveJointModels();
160  std::size_t num_joints = joint_group->getActiveJointModelNames().size();
161  if(parameters.rows() != num_joints)
162  {
163  ROS_ERROR("Incorrect number of joints in the 'parameters' matrix");
164  return false;
165  }
166 
167  if(lock_start_)
168  {
169  for(auto j = 0u; j < num_joints; j++)
170  {
171  parameters(j,0) = *start_state_->getJointPositions(joint_models[j]);
172  }
173 
174  filtered = true;
175  }
176 
177  if(has_goal_constraints_ && lock_goal_)
178  {
179  auto last_index = parameters.cols()-1;
180  for(auto j = 0u; j < num_joints; j++)
181  {
182  parameters(j,last_index) = *goal_state_->getJointPositions(joint_models[j]);
183  }
184 
185  filtered = true;
186  }
187 
188  double val;
189  for (auto j = 0u; j < num_joints; ++j)
190  {
191  for (auto t=0u; t< parameters.cols(); ++t)
192  {
193  val = parameters(j,t);
194  if(joint_models[j]->enforcePositionBounds(&val))
195  {
196  parameters(j,t) = val;
197  filtered = true;
198  }
199  }
200  }
201 
202  return true;
203 }
204 
205 } /* namespace filters */
206 } /* namespace stomp_moveit */
const std::string & getMessage() const
Interface class for filtering noisy trajectories.
std::string getName(void *handle)
#define ROS_WARN(...)
PLUGINLIB_EXPORT_CLASS(test_moveit_controller_manager::TestMoveItControllerManager, moveit_controller_manager::MoveItControllerManager)
const std::vector< std::string > & getActiveJointModelNames() const
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
virtual bool configure(const XmlRpc::XmlRpcValue &config) override
see base class for documentation
This defines a joint limit filter.
bool hasMember(const std::string &name) const
Checks that the joint values are within the limits as defined in the urdf file. It modifies the value...
Definition: joint_limits.h:46
const std::vector< const JointModel * > & getActiveJointModels() const
#define ROS_ERROR_STREAM(args)
#define ROS_ERROR(...)


stomp_moveit
Author(s): Jorge Nicho
autogenerated on Fri May 8 2020 03:35:47