minimum_jerk_trajectory.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2018 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
18 
19 using namespace robotis_framework;
20 
21 MinimumJerk::MinimumJerk(double ini_time, double fin_time,
22  std::vector<double_t> ini_pos, std::vector<double_t> ini_vel, std::vector<double_t> ini_acc,
23  std::vector<double_t> fin_pos, std::vector<double_t> fin_vel, std::vector<double_t> fin_acc)
24 {
25  ini_time_ = ini_time;
26  fin_time_ = fin_time;
27 
28  ini_pos_ = ini_pos;
29  ini_vel_ = ini_vel;
30  ini_acc_ = ini_acc;
31  fin_pos_ = fin_pos;
32  fin_vel_ = fin_vel;
33  fin_acc_ = fin_acc;
34 
35  number_of_joint_ = ini_pos.size();
36 
40  time_variables_.resize(1,6);
41 
42  position_coeff_.fill(0.0);
43  velocity_coeff_.fill(0.0);
44  acceleration_coeff_.fill(0.0);
45  time_variables_.fill(0.0);
46 
47  if(fin_time_ > ini_time_)
48  {
49  Eigen::MatrixXd time_mat;
50  Eigen::MatrixXd conditions_mat;
51 
52  time_mat.resize(6,6);
53  time_mat << powDI(ini_time_,5), powDI(ini_time_,4), powDI(ini_time_,3), powDI(ini_time_,2), ini_time_, 1.0,
54  5.0*powDI(ini_time_,4), 4.0*powDI(ini_time_,3), 3.0*powDI(ini_time_,2), 2.0*ini_time_, 1.0, 0.0,
55  20.0*powDI(ini_time_,3), 12.0*powDI(ini_time_,2), 6.0*ini_time_, 2.0, 0.0, 0.0,
57  5.0*powDI(fin_time_,4), 4.0*powDI(fin_time_,3), 3.0*powDI(fin_time_,2), 2.0*fin_time_, 1.0, 0.0,
58  20.0*powDI(fin_time_,3), 12.0*powDI(fin_time_,2), 6.0*fin_time_, 2.0, 0.0, 0.0;
59 
60  conditions_mat.resize(6,1);
61  conditions_mat.fill(0.0);
62 
63  for (int i = 0; i < number_of_joint_; i++)
64  {
65  conditions_mat.coeffRef(0,0) = ini_pos[i];
66  conditions_mat.coeffRef(1,0) = ini_vel[i];
67  conditions_mat.coeffRef(2,0) = ini_acc[i];
68  conditions_mat.coeffRef(3,0) = fin_pos[i];
69  conditions_mat.coeffRef(4,0) = fin_vel[i];
70  conditions_mat.coeffRef(5,0) = fin_acc[i];
71 
72  Eigen::MatrixXd position_coeff = time_mat.inverse() * conditions_mat;
73 
74  position_coeff_.block(0,i,6,1) = position_coeff;
75 
76  velocity_coeff_.coeffRef(0,i) = 0.0;
77  velocity_coeff_.coeffRef(1,i) = 5.0*position_coeff.coeff(0,0);
78  velocity_coeff_.coeffRef(2,i) = 4.0*position_coeff.coeff(1,0);
79  velocity_coeff_.coeffRef(3,i) = 3.0*position_coeff.coeff(2,0);
80  velocity_coeff_.coeffRef(4,i) = 2.0*position_coeff.coeff(3,0);
81  velocity_coeff_.coeffRef(5,i) = position_coeff.coeff(4,0);
82 
83  acceleration_coeff_.coeffRef(0,i) = 0.0;
84  acceleration_coeff_.coeffRef(1,i) = 0.0;
85  acceleration_coeff_.coeffRef(2,i) = 20.0*position_coeff.coeff(0,0);
86  acceleration_coeff_.coeffRef(3,i) = 12.0*position_coeff.coeff(1,0);
87  acceleration_coeff_.coeffRef(4,i) = 6.0*position_coeff.coeff(2,0);
88  acceleration_coeff_.coeffRef(5,i) = 2.0*position_coeff.coeff(3,0);
89  }
90  }
91 
92  cur_time_ = 0.0;
93  cur_pos_.resize(number_of_joint_);
94  cur_vel_.resize(number_of_joint_);
95  cur_acc_.resize(number_of_joint_);
96 }
97 
99 {
100 
101 }
102 
103 std::vector<double_t> MinimumJerk::getPosition(double time)
104 {
105  if(time >= fin_time_)
106  cur_pos_ = fin_pos_;
107  else if(time <= ini_time_)
108  cur_pos_ = ini_pos_;
109  else
110  {
111  cur_time_ = time;
112  time_variables_ << powDI(time, 5), powDI(time, 4), powDI(time, 3), powDI(time, 2), time, 1.0;
113 
114  for (int i = 0; i < number_of_joint_; i++)
115  {
116  Eigen::MatrixXd cur_pos = time_variables_ * position_coeff_.block(0,i,6,1);
117  cur_pos_[i] = cur_pos.coeff(0,0);
118  }
119  }
120  return cur_pos_;
121 }
122 
123 std::vector<double_t> MinimumJerk::getVelocity(double time)
124 {
125  if(time >= fin_time_)
126  cur_vel_ = fin_vel_;
127  else if(time <= ini_time_)
128  cur_vel_ = ini_vel_;
129  else
130  {
131  cur_time_ = time;
132  time_variables_ << powDI(time, 5), powDI(time, 4), powDI(time, 3), powDI(time, 2), time, 1.0;
133 
134  for (int i = 0; i < number_of_joint_; i++)
135  {
136  Eigen::MatrixXd cur_vel = time_variables_ * velocity_coeff_.block(0,i,6,1);
137  cur_vel_[i] = cur_vel.coeff(0,0);
138  }
139  }
140  return cur_vel_;
141 }
142 
143 std::vector<double_t> MinimumJerk::getAcceleration(double time)
144 {
145  if(time >= fin_time_)
146  cur_acc_ = fin_acc_;
147  else if(time <= ini_time_)
148  cur_acc_ = ini_acc_;
149  else
150  {
151  cur_time_ = time;
152  time_variables_ << powDI(time, 5), powDI(time, 4), powDI(time, 3), powDI(time, 2), time, 1.0;
153 
154  for (int i = 0; i < number_of_joint_; i++)
155  {
156  Eigen::MatrixXd cur_acc = time_variables_ * acceleration_coeff_.block(0,i,6,1);
157  cur_acc_[i] = cur_acc.coeff(0,0);
158  }
159  }
160  return cur_acc_;
161 }
MinimumJerk(double ini_time, double fin_time, std::vector< double_t > ini_pos, std::vector< double_t > ini_vel, std::vector< double_t > ini_acc, std::vector< double_t > fin_pos, std::vector< double_t > fin_vel, std::vector< double_t > fin_acc)
std::vector< double_t > getAcceleration(double time)
std::vector< double_t > getPosition(double time)
double powDI(double a, int b)
std::vector< double_t > getVelocity(double time)


robotis_math
Author(s): SCH , Kayman , Jay Song
autogenerated on Fri Jul 17 2020 03:17:50