normal_distribution_sampling.cpp
Go to the documentation of this file.
1 
27 #include <stomp_moveit/utils/multivariate_gaussian.h>
28 #include <XmlRpcException.h>
30 #include <ros/console.h>
31 
33 
34 /*
35  * These coefficients correspond to the five point stencil method
36  */
37 static const std::vector<double> ACC_MATRIX_DIAGONAL_VALUES = {-1.0/12.0, 16.0/12.0, -30.0/12.0, 16.0/12.0, -1.0/12.0};
38 static const std::vector<int> ACC_MATRIX_DIAGONAL_INDICES = {-2, -1, 0 ,1, 2};
39 
40 namespace stomp_moveit
41 {
42 
43 namespace noise_generators
44 {
45 
46 NormalDistributionSampling::NormalDistributionSampling():
47  name_("NormalDistributionSampling")
48 {
49  // TODO Auto-generated constructor stub
50 
51 }
52 
53 NormalDistributionSampling::~NormalDistributionSampling()
54 
55 {
56  // TODO Auto-generated destructor stub
57 }
58 
59 bool NormalDistributionSampling::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
60  const std::string& group_name,const XmlRpc::XmlRpcValue& config)
61 {
62  using namespace moveit::core;
63 
64  group_ = group_name;
65  const JointModelGroup* joint_group = robot_model_ptr->getJointModelGroup(group_name);
66  if(!joint_group)
67  {
68  ROS_ERROR("Invalid joint group %s",group_name.c_str());
69  return false;
70  }
71 
72  stddev_.resize(joint_group->getActiveJointModelNames().size());
73 
74 
75  return configure(config);
76 }
77 
78 bool NormalDistributionSampling::configure(const XmlRpc::XmlRpcValue& config)
79 {
80  using namespace XmlRpc;
81 
82  try
83  {
84  XmlRpcValue c = config;
85  XmlRpcValue stddev_param = c["stddev"];
86 
87  if(stddev_param.size() < stddev_.size())
88  {
89  ROS_ERROR("%s the 'stddev' parameter has fewer elements than the number of joints",getName().c_str());
90  return false;
91  }
92 
93  stddev_.resize(stddev_param.size());
94  for(auto i = 0u; i < stddev_param.size(); i++)
95  {
96  stddev_[i] = static_cast<double>(stddev_param[i]);
97  }
98  }
99  catch(XmlRpc::XmlRpcException& e)
100  {
101  ROS_ERROR("%s failed to load parameters",getName().c_str());
102  return false;
103  }
104 
105  return true;
106 }
107 
108 bool NormalDistributionSampling::setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr& planning_scene,
109  const moveit_msgs::MotionPlanRequest &req,
110  const stomp_core::StompConfiguration &config,
111  moveit_msgs::MoveItErrorCodes& error_code)
112 {
113  using namespace Eigen;
114 
115  auto fill_diagonal = [](Eigen::MatrixXd& m,double coeff,int diag_index)
116  {
117  std::size_t size = m.rows() - std::abs(diag_index);
118  m.diagonal(diag_index) = VectorXd::Constant(size,coeff);
119  };
120 
121  // creating finite difference acceleration matrix
122  std::size_t num_timesteps = config.num_timesteps;
123  Eigen::MatrixXd A = MatrixXd::Zero(num_timesteps,num_timesteps);
124  int num_elements = (int((ACC_MATRIX_DIAGONAL_INDICES.size() -1)/2.0) + 1)* num_timesteps ;
125  for(auto i = 0u; i < ACC_MATRIX_DIAGONAL_INDICES.size() ; i++)
126  {
127  fill_diagonal(A,ACC_MATRIX_DIAGONAL_VALUES[i],ACC_MATRIX_DIAGONAL_INDICES[i]);
128  }
129 
130  // create and scale covariance matrix
131  Eigen::MatrixXd covariance = MatrixXd::Identity(num_timesteps,num_timesteps);
132  covariance = A.transpose() * A;
133  covariance = covariance.fullPivLu().inverse();
134  double max_val = covariance.array().abs().matrix().maxCoeff();
135  covariance /= max_val;
136 
137  // create random generators
138  rand_generators_.resize(stddev_.size());
139  for(auto& r: rand_generators_)
140  {
141  r.reset(new utils::MultivariateGaussian(VectorXd::Zero(num_timesteps),covariance));
142  }
143 
144  // preallocating noise data
145  raw_noise_.resize(config.num_timesteps);
146  raw_noise_.setZero();
147 
148  return true;
149 }
150 
151 
152 bool NormalDistributionSampling::generateNoise(const Eigen::MatrixXd& parameters,
153  std::size_t start_timestep,
154  std::size_t num_timesteps,
155  int iteration_number,
156  int rollout_number,
157  Eigen::MatrixXd& parameters_noise,
158  Eigen::MatrixXd& noise)
159 {
160  if(parameters.rows() != stddev_.size())
161  {
162  ROS_ERROR("Number of parameters %i differs from what was preallocated ",int(parameters.rows()));
163  return false;
164  }
165 
166 
167  for(auto d = 0u; d < parameters.rows() ; d++)
168  {
169  rand_generators_[d]->sample(raw_noise_);
170  raw_noise_.head(1).setZero();
171  raw_noise_.tail(1).setZero(); // zeroing out the start and end noise values
172  noise.row(d).transpose() = stddev_[d] * raw_noise_;
173  parameters_noise.row(d) = parameters.row(d) + noise.row(d);
174  }
175 
176  return true;
177 }
178 
179 } /* namespace noise_generators */
180 } /* namespace stomp_moveit */
d
int size() const
Uses a normal distribution to apply noise onto the trajectory.
std::string getName(void *handle)
Generates samples from a multivariate gaussian distribution.
PLUGINLIB_EXPORT_CLASS(test_moveit_controller_manager::TestMoveItControllerManager, moveit_controller_manager::MoveItControllerManager)
const std::vector< std::string > & getActiveJointModelNames() const
This a normial distribution noisy trajectory update generator.
r
#define ROS_ERROR(...)


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