ForwardKinematics.cpp
Go to the documentation of this file.
1 
8 
9 ForwardKinematics::ForwardKinematics() : k(0.0), currentPosition(std::vector<double>(3, 0)) { init(); }
11  : k(other.k)
12 {
13  PI = other.PI;
14  initT = other.initT;
15  T = other.T;
16  prevT = other.prevT;
18  radius = 0;
19 }
20 
21 ForwardKinematics::ForwardKinematics(double k, std::vector<double> initialPose) : k(k), currentPosition(initialPose) { init(); }
22 
24 {
25  PI = std::acos(-1);
26  T.push_back(Eigen::Matrix4d::Identity());
27  Eigen::Matrix4d* rot = new Eigen::Matrix4d();
28  *rot = Eigen::Matrix4d::Identity();
29  Eigen::Matrix4d* trans = new Eigen::Matrix4d();
30  *trans = Eigen::Matrix4d::Identity();
31  double theta = currentPosition[2];
32  double x = currentPosition[0];
33  double y = currentPosition[1];
34  // init pose
35  (*rot)(0, 0) = flattenZeros(std::cos(theta));
36  (*rot)(0, 1) = flattenZeros(-std::sin(theta));
37  (*rot)(1, 0) = flattenZeros(std::sin(theta));
38  (*rot)(1, 1) = flattenZeros(std::cos(theta));
39  (*trans)(0, 3) = flattenZeros(x);
40  (*trans)(1, 3) = flattenZeros(y);
41  prevT = (*trans) * (*rot);
44  prevT(0,0) = flattenZeros(std::cos(currentPosition[2]));
45  prevT(1,0) = flattenZeros(std::sin(currentPosition[2]));
46  delete rot;
47  delete trans;
48 }
49 
50 const double ForwardKinematics::getRadius() const { return radius; }
51 
52 std::pair<double, double> ForwardKinematics::calcICC(double alpha)
53 {
54  double x = -k / std::tan(alpha);
55  double y = -k;
56  return std::make_pair(x, y);
57 }
58 double ForwardKinematics::flattenZeros(double value)
59 {
60  return (std::fabs(value) < 0.000001) ? 0 : value;
61 }
62 double ForwardKinematics::degToRad(double angle) { return angle / 180.0 * PI; }
63 double ForwardKinematics::radToDeg(double angle) { return angle / PI * 180.0; }
64 
65 double ForwardKinematics::calcRadius(std::pair<double, double> ICC)
66 {
67  radius = std::sqrt(ICC.first * ICC.first + ICC.second * ICC.second);
68  return radius;
69 }
70 double ForwardKinematics::calcTheta(double distance, double radius)
71 {
72  return distance / radius;
73 }
74 Eigen::Matrix4d* ForwardKinematics::calcRot(double theta, double alpha)
75 {
76  Eigen::Matrix4d* rot = new Eigen::Matrix4d();
77  *rot = Eigen::Matrix4d::Identity();
78 
79  if (alpha > 0)
80  {
81  (*rot)(0, 0) = flattenZeros(std::cos(theta));
82  (*rot)(0, 1) = flattenZeros(-std::sin(theta));
83  (*rot)(1, 0) = flattenZeros(std::sin(theta));
84  (*rot)(1, 1) = flattenZeros(std::cos(theta));
85  }
86  else
87  {
88  (*rot)(0, 0) = flattenZeros(std::cos(-theta));
89  (*rot)(0, 1) = flattenZeros(-std::sin(-theta));
90  (*rot)(1, 0) = flattenZeros(std::sin(-theta));
91  (*rot)(1, 1) = flattenZeros(std::cos(-theta));
92  }
93 
94  return rot;
95 }
96 Eigen::Matrix4d* ForwardKinematics::calcTrans(double radius, double distance,
97  double alpha,
98  std::pair<double, double> ICC)
99 {
100  Eigen::Matrix4d* trans = new Eigen::Matrix4d();
101  *trans = Eigen::Matrix4d::Identity();
102  double x;
103  double y;
104 
105  if (alpha == 0)
106  {
107  x = 0;
108  y = distance;
109  }
110  else
111  {
112  double theta = calcTheta(distance, radius);
113 
114  if (alpha > 0)
115  {
116  // Anmerkung: vorher stand alpha+theta
117  x = radius * std::cos(theta + alpha) + ICC.first;
118  y = radius * std::sin(theta + alpha) + ICC.second;
119  }
120  else
121  {
122  // Anmerkung: vorher stand PI+alpha-theta
123  x = radius * std::cos(PI - theta + alpha) + ICC.first;
124  y = radius * std::sin(PI - theta + alpha) + ICC.second;
125  }
126  }
127 
128  //(*trans)(0,3)= (std::fabs(x)<0.000001)?0:x;
129  //(*trans)(1,3)= (std::fabs(y)<0.000001)?0:y;
130  (*trans)(0, 3) = flattenZeros(x);
131  (*trans)(1, 3) = flattenZeros(y);
132 
133  return trans;
134 }
135 std::vector<double>& ForwardKinematics::getUpdate(double alpha, double distance)
136 {
137  Eigen::Matrix4d* Ti = new Eigen::Matrix4d();
138  *Ti = Eigen::Matrix4d::Identity();
139  alpha = flattenZeros(alpha);
140 
141  if (alpha == 0)
142  {
143  Ti = calcTrans(0, distance, alpha, std::make_pair(0, 0));
144  }
145  else
146  {
147  std::pair<double, double> ICC = calcICC(alpha);
148  double radius = calcRadius(ICC);
149  double theta = calcTheta(distance, radius);
150 
151  Ti = calcRot(theta, alpha);
152  Eigen::Matrix4d* trans = calcTrans(radius, distance, alpha, ICC);
153  *Ti = (*trans) * (*Ti);
154  delete trans;
155  }
156 
157  // Eigen::Matrix4d Tn = T.back()*Ti;
158  // T.push_back(Tn);
159 
160  Eigen::Matrix4d Tn = prevT * (*Ti);
161  delete Ti;
162 
163  prevT = Tn;
164 
165  double x = Tn(0, 3);
166  double y = Tn(1, 3);
167  double e_x1 = Tn(0, 0);
168  double e_x2 = Tn(1, 0);
169  // Angle in Degrees
170  // double angle = std::atan2(e_x2,e_x1)/(2*PI)*360
171  // Angle in rad
172  double angle = std::atan2(e_x2, e_x1);
173 
176  currentPosition[2] = angle;
177 
178  return currentPosition;
179 }
Header file for the ForwardKinematics class.
Eigen::Matrix4d * calcRot(double theta, double alpha)
Calculate the current rotation matrix.
Eigen::Matrix4d * calcTrans(double radius, double distance, double alpha, std::pair< double, double > ICC)
Calculate the current translation matrix.
double flattenZeros(double value)
Rounds values close to zero to exactly zero.
ForwardKinematics()
ForwardKinematics default constructor.
double radToDeg(double angle)
Conversion from radiants to degrees.
double calcTheta(double distance, double radius)
Calculate the current orientation of the robot with respect to the coordinate system of the previous ...
Eigen::Matrix4d initT
Eigen::Matrix4d prevT
std::pair< double, double > calcICC(double alpha)
Calculates the current instantaneous center of curvature given a steering angle.
std::vector< double > & getUpdate(double alpha, double distance)
Get the current position and orientation.
std::vector< Eigen::Matrix4d > T
double calcRadius(std::pair< double, double > ICC)
Calculate the radius of the current robot tracjectory around the instantaneous center of curvature (I...
void init()
Initialize simulation.
const double getRadius() const
Get the radius of the current robot tracjectory around the instantaneous center of curvature (ICC)...
The ForwardKinematics class simulates kinematic behavoir of a car like mobile platform given its curr...
std::vector< double > currentPosition
double degToRad(double angle)
Conversion from degrees to radiants.


pses_simulation
Author(s): Sebastian Ehmes
autogenerated on Wed Dec 13 2017 19:37:54