26 #include <stomp_core/utils.h> 29 #include <Eigen/Dense> 36 void generateFiniteDifferenceMatrix(
int num_time_steps,
37 DerivativeOrders::DerivativeOrder order,
38 double dt, Eigen::MatrixXd& diff_matrix)
41 diff_matrix = Eigen::MatrixXd::Zero(num_time_steps, num_time_steps);
42 double multiplier = 1.0/pow(dt,(
int)order);
43 for (
int i=0; i<num_time_steps; ++i)
45 for (
int j=-FINITE_DIFF_RULE_LENGTH/2; j<=FINITE_DIFF_RULE_LENGTH/2; ++j)
53 if (index >= num_time_steps)
55 index = num_time_steps-1;
59 diff_matrix(i,index) = multiplier * FINITE_CENTRAL_DIFF_COEFFS[order][j+FINITE_DIFF_RULE_LENGTH/2];
64 void generateSmoothingMatrix(
int num_timesteps,
double dt, Eigen::MatrixXd& projection_matrix_M)
66 using namespace Eigen;
69 int start_index_padded = FINITE_DIFF_RULE_LENGTH-1;
70 int num_timesteps_padded = num_timesteps + 2*(FINITE_DIFF_RULE_LENGTH-1);
71 MatrixXd finite_diff_matrix_A_padded;
72 generateFiniteDifferenceMatrix(num_timesteps_padded,DerivativeOrders::STOMP_ACCELERATION,
73 dt,finite_diff_matrix_A_padded);
80 MatrixXd control_cost_matrix_R_padded = dt*finite_diff_matrix_A_padded.transpose() * finite_diff_matrix_A_padded;
81 MatrixXd control_cost_matrix_R = control_cost_matrix_R_padded.block(
82 start_index_padded,start_index_padded,num_timesteps,num_timesteps);
83 MatrixXd inv_control_cost_matrix_R = control_cost_matrix_R.fullPivLu().inverse();
86 projection_matrix_M = inv_control_cost_matrix_R;
88 for(
auto t = 0u; t < num_timesteps; t++)
90 max = projection_matrix_M(t,t);
91 projection_matrix_M.col(t)*= (1.0/(num_timesteps*max));
95 void differentiate(
const Eigen::VectorXd& parameters, DerivativeOrders::DerivativeOrder order,
96 double dt, Eigen::VectorXd& derivatives )
99 using namespace Eigen;
101 derivatives = Eigen::VectorXd::Zero(parameters.size());
105 VectorXd central_coeffs = VectorXd::Map(&FINITE_CENTRAL_DIFF_COEFFS[order][0],FINITE_DIFF_RULE_LENGTH);
106 VectorXd forward_coeffs = VectorXd::Map(&FINITE_FORWARD_DIFF_COEFFS[order][0],FINITE_DIFF_RULE_LENGTH);
107 VectorXd backward_coeffs = forward_coeffs.reverse();
112 backward_coeffs*= -1.0;
116 int rule_length = FINITE_DIFF_RULE_LENGTH;
117 int size = parameters.size();
118 int skip = FINITE_DIFF_RULE_LENGTH/2;
119 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(size,size);
120 int start_ind, end_ind;
121 for(
auto i = 0; i < size; i++)
126 A.row(i).segment(start_ind,rule_length) = forward_coeffs;
128 else if(i >= skip && i < size - skip)
130 start_ind = i - skip;
131 A.row(i).segment(start_ind,rule_length) = central_coeffs;
135 start_ind = i - rule_length+1;
136 A.row(i).segment(start_ind,rule_length) = backward_coeffs;
140 derivatives = A*parameters/std::pow(dt,2);
143 void toVector(
const Eigen::MatrixXd& m,std::vector<Eigen::VectorXd>& v)
145 v.resize(m.rows(),Eigen::VectorXd::Zero(m.cols()));
146 for(
auto d = 0u;
d < m.rows();
d++)
152 std::string toString(
const std::vector<Eigen::VectorXd>& data)
154 Eigen::IOFormat clean_format(4, 0,
", ",
"\n",
"[",
"]");
155 Eigen::MatrixXd m = Eigen::MatrixXd::Zero(data.size(),data.front().size());
156 std::stringstream ss;
157 for(
auto d = 0u;
d < data.size();
d++)
159 m.row(d) = data[
d].transpose();
162 ss<<m.format(clean_format);
166 std::string toString(
const Eigen::MatrixXd& data)
168 Eigen::IOFormat clean_format(4, 0,
", ",
"\n",
"[",
"]");
169 std::stringstream ss;
170 ss<<data.format(clean_format);
174 std::string toString(
const Eigen::VectorXd& data)
176 Eigen::IOFormat clean_format(4, 0,
", ",
"\n",
"[",
"]");
177 std::stringstream ss;
178 ss<<data.transpose().format(clean_format);