Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
poly.cpp
Go to the documentation of this file.
1 #include "utils/poly.hpp"
2 
3 #include "utils/logger.hpp"
4 #include <exception>
5 
6 using namespace ::std;
7 using namespace ::galaxis;
8 using namespace ::galaxis::utils;
9 using namespace ::galaxis::control;
10 
11 std::ostream& galaxis::operator<<(std::ostream& strm, polynom const& poly) {
12  std::string str = "";
13 
14  if(poly.size() >= 1) {
15  str = std::to_string(poly[0]);
16  str += "x^0";
17  }
18  for(unsigned int i = 1; i < poly.size(); ++i) {
19  str += " + ";
20  str += std::to_string(poly[i]);
21  str += "x^";
22  str += std::to_string(i);
23  }
24 
25  return strm << str;
26 }
27 
28 std::ostream& galaxis::operator<<(std::ostream& strm, interval const& interval) {
29  return strm << "[" << std::get<0>(interval) << ", " << std::get<1>(interval) << "]";
30 }
31 
32 double galaxis::utils::evaluate_polynom(std::vector<double> const& p_polynomRef, double p_x) {
33  float value = 0.0;
34 
35  float iValue;
36  for(size_t i = 0; i < p_polynomRef.size(); ++i) {
37  iValue = 1;
38 
39  // x^i
40  for(size_t j = 0; j < i; ++j) {
41  iValue *= p_x;
42  }
43 
44  value += p_polynomRef[i] * iValue;
45  }
46 
47  return value;
48 }
49 
50 float galaxis::utils::evaluate_polynom(polynom const& p_polynomRef, float p_x) {
51  std::vector<double> poly;
52  for(auto const& elt : p_polynomRef) {
53  poly.push_back(elt);
54  }
55 
56  return static_cast<float>(evaluate_polynom(poly,p_x));
57 }
58 
59 
60 float galaxis::utils::calculate_gradient(std::vector<float> const& p_polynomRef, float p_x) {
61  float value = 0.0;
62 
63  float iValue;
64  for(size_t i = 1; i < p_polynomRef.size(); ++i) {
65  iValue = 1;
66 
67  // i*x^(i-1)
68  for(size_t j = 0; j < i - 1; ++j) {
69  iValue *= p_x;
70  }
71 
72  value += p_polynomRef[i] * i * iValue;
73  }
74 
75  return value;
76 }
77 
78 double galaxis::utils::calculate_gradient(std::vector<double> const& p_polynomRef, double p_x) {
79  float value = 0.0;
80 
81  float iValue;
82  for(size_t i = 1; i < p_polynomRef.size(); ++i) {
83  iValue = 1;
84 
85  // i*x^(i-1)
86  for(size_t j = 0; j < i - 1; ++j) {
87  iValue *= p_x;
88  }
89 
90  value += p_polynomRef[i] * i * iValue;
91  }
92 
93  return value;
94 }
95 
96 double galaxis::utils::calculate_gradient(DynamicPolynom p_polynomVecSPtr, double p_x) {
97  float value = 0.0;
98 
99  float iValue;
100  for(size_t i = 1; i < p_polynomVecSPtr->size(); ++i) {
101  iValue = 1;
102 
103  // i*x^(i-1)
104  for(size_t j = 0; j < i - 1; ++j) {
105  iValue *= p_x;
106  }
107 
108  value += p_polynomVecSPtr->at(i) * i * iValue;
109  }
110 
111  return value;
112 }
113 
114 namespace galaxis {
115 
116  float calculate_error_between_polyspline_point_square(shared_ptr<PolySpline> p_polySpline, Eigen::Vector2f& p_point) {
117  // TODO: maxbe calc correct distance between polynom and
118  // log_warning("poly.hpp: galaxis::utils::calculate_error_between_polyspline_point - correct distance implementation missing");
119 
120  float splinevalue = p_polySpline->evaluate(static_cast<float>(p_point[0]));
121  float error = p_point[1] - splinevalue;
122 
123  return error * error;
124  }
125 
127  // TODO: maxbe calc correct distance between polynom and
128  // log_warning("poly.hpp: galaxis::utils::calculate_error_between_polyspline_point - correct distance implementation missing");
129 
130  float splinevalue = p_polySpline.evaluate(static_cast<float>(p_point[0]));
131  float error = p_point[1] - splinevalue;
132 
133  return error;
134  }
135 
136  float calculate_error_between_polyspline_poseseries(shared_ptr<PolySpline> p_polySplineSPtr, shared_ptr<galaxis::control::CarPoseSeries> p_poseSeriesSPtr,
137  shared_ptr<line_markings_reference_t> p_lineMarkings,
138  float cross_angle_weight, float cross_line_weight) {
139  float error = 0;
140  // Compute regular error function based on y difference between polynom and series
141  for(shared_ptr<CarPose> pose: *p_poseSeriesSPtr) {
142  error += calculate_error_between_polyspline_point_square(p_polySplineSPtr, pose->position);
143  }
144 
145 
146  // // Compute Angle at each planning point between polynom and series
147  // for(shared_ptr<CarPose> pose: *p_poseSeriesSPtr) {
148 
149  // // We dont use the spline part of polyspline
150  // float poly_gradient = calculate_gradient(p_polySplineSPtr->m_polynomVec.at(0), pose->position[0]);
151 
152  // // Convert gradient to orientation
153  // float poly_orientation = std::tanh(poly_gradient);
154  // float angle_error = poly_orientation - pose->orientation;
155 
156  // error += cross_angle_weight * (angle_error * angle_error);
157  // }
158 
159  if(p_lineMarkings != nullptr){
160  for(shared_ptr<CarPose> pose: *p_poseSeriesSPtr) {
161  //TODO: Check that the pose is in the well defined interval for the line polynoms (tend to be bad outside of the interval)
162  if(p_lineMarkings->isRightEnabled) {
163  float r_error = calculate_error_between_polyspline_point(p_lineMarkings->right, pose->position);
164 
165  if(r_error > 0){
166  error += (r_error * r_error) * cross_line_weight;
167  }
168  }
169 
170  if(p_lineMarkings->isLeftEnabled) {
171  float l_error = calculate_error_between_polyspline_point(p_lineMarkings->left, pose->position);
172 
173  if(l_error < 0){
174  error += (l_error * l_error) * cross_line_weight;
175  }
176  }
177  }
178  }
179 
180  return error;
181  }
182 
183 
184 } // namespace galaxis
185 
186 // --- START PolySpline ---
187 PolySpline::PolySpline() {}
188 
189 PolySpline::PolySpline(std::array<double,4> coeff, double max, double min) {
190  m_polynomVec.resize(2);
191  for(uint32_t i = 0; i < coeff.size(); i ++){
192  m_polynomVec.at(0).push_back(coeff.at(i));
193  }
194 
195  m_intervalVec.push_back(std::tuple<float, float>(min,max));
196 }
197 
198 void PolySpline::add_polynom(interval const& p_interval, polynom const& p_polynom) {
199  // TODO: add interval check
200  m_intervalVec.push_back(p_interval);
201  m_polynomVec.push_back(polynom(p_polynom));
202 }
203 
204 void PolySpline::add_polynom(float lowerBound, float upperBound, polynom const& polynom) {
205  interval interv = std::make_tuple(lowerBound, upperBound);
206  add_polynom(interv, polynom);
207 }
208 
209 float PolySpline::evaluate(float p_x) const {
210  for(size_t i = 0; i < m_intervalVec.size(); ++i) {
211  if(p_x >= std::get<0>(m_intervalVec[i]) && p_x <= std::get<1>(m_intervalVec[i])) {
212  return utils::evaluate_polynom(m_polynomVec[i], p_x);
213  }
214  }
215  // log_error("No polynominterval for x.");
216  return 0;
217 }
218 
219 void PolySpline::clear() {
220  m_intervalVec.clear();
221  m_polynomVec.clear();
222 }
223 // --- END PolySpline ---
galaxis::calculate_error_between_polyspline_poseseries
float calculate_error_between_polyspline_poseseries(shared_ptr< PolySpline > p_polySplineSPtr, shared_ptr< galaxis::control::CarPoseSeries > p_poseSeriesSPtr, shared_ptr< line_markings_reference_t > p_lineMarkings, float cross_angle_weight, float cross_line_weight)
Definition: poly.cpp:136
DynamicPolynom
std::shared_ptr< std::vector< double > > DynamicPolynom
Definition: poly.hpp:16
galaxis::PolySpline
Definition: poly.hpp:37
galaxis::utils::evaluate_polynom
float evaluate_polynom(polynom const &p_polynomRef, float p_x)
Definition: poly.cpp:50
galaxis::polynom
std::vector< float > polynom
Definition: poly.hpp:20
galaxis::PolySpline::evaluate
float evaluate(float p_x) const
Definition: poly.cpp:209
galaxis::interval
std::tuple< float, float > interval
Definition: poly.hpp:21
poly.hpp
galaxis::calculate_error_between_polyspline_point
float calculate_error_between_polyspline_point(shared_ptr< PolySpline > p_polySpline, Eigen::Vector2f &p_point)
galaxis
Definition: MapClasses.hpp:10
galaxis::calculate_error_between_polyspline_point_square
float calculate_error_between_polyspline_point_square(shared_ptr< PolySpline > p_polySpline, Eigen::Vector2f const &p_point)
galaxis::operator<<
std::ostream & operator<<(std::ostream &strm, polynom const &poly)
Definition: poly.cpp:11
galaxis::lina::Vector2f
Eigen::Vector2f Vector2f
Definition: linatypes.hpp:14
galaxis::control
Definition: control.hpp:24
logger.hpp
galaxis::utils
Definition: clock.hpp:5
galaxis::utils::calculate_gradient
float calculate_gradient(std::vector< float > const &p_polynomRef, float p_x)
Definition: poly.cpp:60