27 #include <stomp_moveit/utils/multivariate_gaussian.h> 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};
43 namespace noise_generators
46 NormalDistributionSampling::NormalDistributionSampling():
47 name_(
"NormalDistributionSampling")
53 NormalDistributionSampling::~NormalDistributionSampling()
59 bool NormalDistributionSampling::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
65 const JointModelGroup* joint_group = robot_model_ptr->getJointModelGroup(group_name);
68 ROS_ERROR(
"Invalid joint group %s",group_name.c_str());
75 return configure(config);
87 if(stddev_param.
size() < stddev_.size())
89 ROS_ERROR(
"%s the 'stddev' parameter has fewer elements than the number of joints",
getName().c_str());
93 stddev_.resize(stddev_param.
size());
94 for(
auto i = 0u; i < stddev_param.
size(); i++)
96 stddev_[i] =
static_cast<double>(stddev_param[i]);
108 bool NormalDistributionSampling::setMotionPlanRequest(
const planning_scene::PlanningSceneConstPtr&
planning_scene,
109 const moveit_msgs::MotionPlanRequest &req,
111 moveit_msgs::MoveItErrorCodes& error_code)
113 using namespace Eigen;
115 auto fill_diagonal = [](Eigen::MatrixXd& m,
double coeff,
int diag_index)
117 std::size_t size = m.rows() - std::abs(diag_index);
118 m.diagonal(diag_index) = VectorXd::Constant(size,coeff);
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++)
127 fill_diagonal(A,ACC_MATRIX_DIAGONAL_VALUES[i],ACC_MATRIX_DIAGONAL_INDICES[i]);
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;
138 rand_generators_.resize(stddev_.size());
139 for(
auto&
r: rand_generators_)
146 raw_noise_.setZero();
152 bool NormalDistributionSampling::generateNoise(
const Eigen::MatrixXd& parameters,
153 std::size_t start_timestep,
154 std::size_t num_timesteps,
155 int iteration_number,
157 Eigen::MatrixXd& parameters_noise,
158 Eigen::MatrixXd& noise)
160 if(parameters.rows() != stddev_.size())
162 ROS_ERROR(
"Number of parameters %i differs from what was preallocated ",
int(parameters.rows()));
167 for(
auto d = 0u;
d < parameters.rows() ;
d++)
169 rand_generators_[
d]->sample(raw_noise_);
170 raw_noise_.head(1).setZero();
171 raw_noise_.tail(1).setZero();
172 noise.row(
d).transpose() = stddev_[
d] * raw_noise_;
173 parameters_noise.row(
d) = parameters.row(
d) + noise.row(
d);
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.