ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
lspiralfunction.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Daniel Heß - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 
17 #include <adore/mad/alfunction.h>
18 #include <adore/mad/lpolynomial.h>
20 #include <adore/mad/oderk4.h>
21 #include <adore/mad/adoremath.h>
22 #include <adore/mad/rotations.h>
24 #include <algorithm>
25 #define _USE_MATH_DEFINES //for cmath
26 #include <math.h>
27 #include <iostream>
28 
29 namespace adore
30 {
31  namespace mad
32  {
36  template<typename T, int M> //M is degree of spiral - 0 for circle, 1 for clothoid, etc.
37  class LSpiralFunction : public AScalarToN<T, 2>// ALFunction<T,adoreMatrix<T,2,1>>, f: s->X,Y
38  {
39  public:
41  class diffspiral :public AOdeModel<T>
42  {
43  public:
45  virtual void f(T t, const adoreMatrix<T, 0, 1>& x_in, adoreMatrix<T, 0, 1>& dx_out)
46  {
47  dlib::set_all_elements(dx_out, (T)0);
48  dx_out(0) = (T)1;//ds
49  dx_out(1) = (std::cos)(x_in(3));//dX
50  dx_out(2) = (std::sin)(x_in(3));//dY
51  for (int i = 0; i <= M; i++)dx_out(3 + i) = x_in(3 + i + 1);//integrator chain, d/ds kappa^(i)=kappa^(i+1)
52  //dx_out(3+0) = dpsi, always1
53  //dx_out(3+M+1) = 0 //the last one is always 0
54  }
55  };
58  virtual ~LSpiralFunction()
59  {
60  delete m_linear_data;
61  }
66  LSpiralFunction(T S, T X0, T Y0, T ds, const adoreMatrix<T, 1, M + 2>& p0)
67  {
68  adoreMatrix<T, 1, 0> time = adore::mad::sequence<T>(0, ds, S);
69  OdeRK4<T> rk4;
70  adoreMatrix<T, 3 + M + 2, 1> x0;
71  x0(0) = 0;//s
72  x0(1) = X0;
73  x0(2) = Y0;
74  for (int i = 0; i <= M + 1; i++)x0(3 + i) = p0(i);//psi0,kappa0,dkappa0,ddkappa0...
75  diffspiral myspiral;
77  }
82  LSpiralFunction(T S, T X0, T Y0, T kappa_max, T emax, const adoreMatrix<T, 1, M + 2>& p0)
83  {
84  T ds;
85  if (kappa_max == 0)
86  {
87  ds = S;
88  }
89  else
90  {
91  //chose a stepsize in order to control deviation between path and linear path approximation
92  ds = acos((T)1 - emax*kappa_max) / (kappa_max*(T)M_PI);
93  }
94  adoreMatrix<T, 1, 0> time = adore::mad::sequence<T>(0, ds, S);
95  OdeRK4<T> rk4;
96  adoreMatrix<T, 3 + M + 2, 1> x0;
97  x0(0) = 0;//s
98  x0(1) = X0;
99  x0(2) = Y0;
100  for (int i = 0; i <= M + 1; i++)x0(3 + i) = p0(i);//psi0,kappa0,dkappa0,ddkappa0...
101  diffspiral myspiral;
102  m_linear_data = new LLinearPiecewiseFunctionM<T, 3 + M + 1>(rk4.solve(&myspiral, time, x0));
103  }
107  LSpiralFunction(T S, T X0, T Y0, T PSI0, T kappa0, T kappa1, T emax)
108  {
109  assert(M == 1);
110  T kappa_max = (std::max)((std::abs)(kappa0), (std::abs)(kappa1));//TODO improve min/max estimate, implement lagrange remainder for poly in bounds
111  T ds;
112  if (kappa_max == 0)
113  {
114  ds = S;
115  }
116  else
117  {
118  //chose a stepsize in order to control deviation between path and linear path approximation
119  ds = acos((T)1 - emax*kappa_max) / (kappa_max*(T)M_PI);
120  }
121  adoreMatrix<T, 1, 0> time = adore::mad::sequence<T>(0, ds, S);
122  OdeRK4<T> rk4;
123  adoreMatrix<T, 3 + M + 2, 1> x0;
124  x0(0) = 0;//s
125  x0(1) = X0;
126  x0(2) = Y0;
127  x0(3) = PSI0;
128  x0(4) = kappa0;
129  x0(5) = (kappa1 - kappa0) / S;
130  diffspiral myspiral;
131  m_linear_data = new LLinearPiecewiseFunctionM<T, 3 + M + 1>(rk4.solve(&myspiral, time, x0));
132  }
133 
139  {
140  adoreMatrix<T, M + 2, 1> ic = dlib::subm(m_linear_data->getData(), dlib::range(3, M + 4), dlib::range(0, 0));
141  return new LPolynomialS<T, M + 1>(
142  poly_parameter_for_initial_condition<T, M + 1>(ic),
145  );
146  }
147  public://from ALFunction
148  virtual adoreMatrix<T, 2, 1> f(T x) const override
149  {
150  adoreMatrix<T, 2, 1> y;
151  y(0) = fi(x, 0);
152  y(1) = fi(x, 1);
153  return y;
154  }
155 
156  virtual T limitHi() const override
157  {
158  return m_linear_data->limitHi();
159  }
160 
161  virtual T limitLo() const override
162  {
163  return m_linear_data->limitLo();
164  }
165 
166  virtual void setLimits(T lo, T hi) override
167  {
168  m_linear_data->setLimits(lo, hi);
169  }
171  {
173  }
175  {
176  adoreMatrix<T, 2, 1> v;
177  v = 1.0, 0.0;
178  return funop::rotate<T>(create_heading(), new LConstFun<T, adoreMatrix<T, 2, 1>>(v,limitLo(),limitHi()));
179  }
180  virtual void bound(const T& xmin, const T& xmax, adoreMatrix<T, 2, 1> & ymin, adoreMatrix<T, 2, 1> & ymax)override
181  {
182  adoreMatrix<T, 3 + M + 1, 1> lymin, lymax;
183  m_linear_data->bound(xmin, xmax, lymin, lymax);
184  ymin = rowm(lymin, dlib::range(0, 1));
185  ymax = rowm(lymax, dlib::range(0, 1));
186  }
187  public://from AScalarToN
188  virtual ALFunction<T, T>* dimension(int i) override
189  {
190  return m_linear_data->dimension(i);
191  }
192  virtual T fi(T x, int dim) const override
193  {
194  return m_linear_data->fi(x, dim);
195  }
196  virtual void multiply(adoreMatrix<T, 0, 0> A, int rowi, int rowj)
197  {
198  m_linear_data->multiply(A, rowi, rowj);
199  }
200  virtual void add(adoreMatrix<T, 0, 1> b, int rowi, int rowj)
201  {
202  m_linear_data->add(b, rowi, rowj);
203  }
204  public:
205 
206  //virtual SUBFUN* getCurvature()
207  //{
208  // return m_linear_data->dimension(3);
209  //}
210  //virtual SUBFUN* getCurvatureDerivative(int i)
211  //{
212  // assert(i<=M);
213  // return m_linear_data->dimension(3+i);
214  //}
216  // * create a copy function which contains Nnew=M-many dimensions, indices specified after M
217  // */
218  //template<int Nnew>
219  //LLinearPiecewiseFunctionM<T,Nnew>* clone(int first,...)
220  //{
221  // va_list args;
222  // va_start(args,first);
223  // return m_linear_data->clone<Nnew>(first,args);
224  //}
225  //LLinearPiecewiseFunctionM<T,2>* cloneXY()
226  //{
227  // return this->clone<2>(0,1);
228  //}
229 
231  // * createNormalOffset creates an s->X,Y function, which has an offset in normal direction of this spiral, where the length of the normal offset is defined by the parameter
232  // * create a function according to the pattern formerly known as "parallelTO"
233  // */
234  //AScalarToN<T,2>* createNormalOffsetFunction(ALFunction<T,T>* normal_distance)
235  //{
236  // return funop::add(
237  // cloneXY(),
238  // funop::multiply(
239  // funop::chain(
240  // new RotationFunction<T,2,1>(),
241  // getOrientation()->clone()
242  // ),
243  // funop::stack(
244  // new LConstFunS((T)0),
245  // normal_distance
246  // )
247  // )
248  // );
249  //}
250  };
251  }
252 }
#define M_PI
Definition: arraymatrixtools.h:24
Definition: aodemodel.h:30
Definition: alfunction.h:214
Definition: alfunction.h:287
virtual ALFunction< DT, CT > * clone() override
Definition: llinearpiecewisefunction.h:548
virtual void bound(const DT &xmin, const DT &xmax, CT &ymin, CT &ymax) override
Definition: llinearpiecewisefunction.h:278
virtual ALFunction< DT, T > * dimension(int i) override
Definition: llinearpiecewisefunction.h:470
virtual T fi(DT x, int row) const override
Definition: llinearpiecewisefunction.h:391
virtual DT limitLo() const override
Definition: llinearpiecewisefunction.h:264
virtual void add(adoreMatrix< T, 0, 1 > b, int rowi, int rowj) override
Definition: llinearpiecewisefunction.h:601
virtual void setLimits(DT lo, DT hi) override
Definition: llinearpiecewisefunction.h:270
adoreMatrix< T, n+1, 0 > & getData()
Definition: llinearpiecewisefunction.h:147
virtual DT limitHi() const override
Definition: llinearpiecewisefunction.h:259
virtual void multiply(adoreMatrix< T, 0, 0 > A, int rowi, int rowj) override
Definition: llinearpiecewisefunction.h:589
Definition: lpolynomial.h:140
Definition: lspiralfunction.h:42
virtual void f(T t, const adoreMatrix< T, 0, 1 > &x_in, adoreMatrix< T, 0, 1 > &dx_out)
Definition: lspiralfunction.h:45
diffspiral()
Definition: lspiralfunction.h:44
Definition: lspiralfunction.h:38
LSpiralFunction(T S, T X0, T Y0, T ds, const adoreMatrix< T, 1, M+2 > &p0)
Definition: lspiralfunction.h:66
virtual T limitHi() const override
Definition: lspiralfunction.h:156
LSpiralFunction(LLinearPiecewiseFunctionM< T, 3+M+1 > *linear_data)
Definition: lspiralfunction.h:57
virtual ALFunction< T, T > * create_heading()
Definition: lspiralfunction.h:138
LSpiralFunction(T S, T X0, T Y0, T kappa_max, T emax, const adoreMatrix< T, 1, M+2 > &p0)
Definition: lspiralfunction.h:82
virtual void setLimits(T lo, T hi) override
Definition: lspiralfunction.h:166
virtual void multiply(adoreMatrix< T, 0, 0 > A, int rowi, int rowj)
Definition: lspiralfunction.h:196
virtual ALFunction< T, T > * dimension(int i) override
Definition: lspiralfunction.h:188
virtual T limitLo() const override
Definition: lspiralfunction.h:161
virtual ~LSpiralFunction()
Definition: lspiralfunction.h:58
virtual ALFunction< T, adoreMatrix< T, 2, 1 > > * create_derivative() override
Definition: lspiralfunction.h:174
LSpiralFunction(T S, T X0, T Y0, T PSI0, T kappa0, T kappa1, T emax)
Definition: lspiralfunction.h:107
virtual ALFunction< T, adoreMatrix< T, 2, 1 > > * clone() override
Definition: lspiralfunction.h:170
LSpiralFunction()
Definition: lspiralfunction.h:56
virtual T fi(T x, int dim) const override
Definition: lspiralfunction.h:192
virtual adoreMatrix< T, 2, 1 > f(T x) const override
Definition: lspiralfunction.h:148
virtual void add(adoreMatrix< T, 0, 1 > b, int rowi, int rowj)
Definition: lspiralfunction.h:200
virtual void bound(const T &xmin, const T &xmax, adoreMatrix< T, 2, 1 > &ymin, adoreMatrix< T, 2, 1 > &ymax) override
Definition: lspiralfunction.h:180
LLinearPiecewiseFunctionM< T, 3+M+1 > * m_linear_data
Definition: lspiralfunction.h:40
Definition: oderk4.h:29
virtual adoreMatrix< T > solve(AOdeModel< T > *model, const adoreMatrix< double, 1, 0 > &time, const adoreMatrix< double, 0, 1 > &x0) override
Definition: oderk4.h:37
interval< T > cos(interval< T > x)
Definition: intervalarithmetic.h:225
interval< T > sin(interval< T > x)
Definition: intervalarithmetic.h:204
adoreMatrix< T, N, M > max(adoreMatrix< T, N, M > a, const adoreMatrix< T, N, M > &b)
Definition: adoremath.h:686
x0
Definition: adore_set_goal.py:25
x
Definition: adore_set_goal.py:30
y
Definition: adore_set_goal.py:31
p0
Definition: adore_set_pose.py:32
Definition: areaofeffectconverter.h:20