SimDriver  0.1
VelocityHorizon.h
1 // Copyright (c) 2020 Institute for Automotive Engineering (ika), RWTH Aachen University. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 //
21 // Created by Jens Klimke on 2020-04-02.
22 // Contributors:
23 //
24 // VelocityHorizon.h
25 
26 
27 #ifndef SIMDRIVER_VELOCITYHORIZON_H
28 #define SIMDRIVER_VELOCITYHORIZON_H
29 
30 #include <cmath>
31 #include <deque>
32 #include <algorithm>
33 #include "model_collection.h"
34 
35 
36 namespace agent_model {
37 
38 
41 
42  protected:
43 
45  struct PredictionPoint {
46  size_t i;
47  double s;
48  double ds;
49  double vRule;
50  double vCont;
51  double sCont;
52  };
53 
54  double _offset;
55  double _vMax;
56 
57  std::deque <PredictionPoint> _elements{};
58 
59 
60 
61  public:
62 
63 
69  void init(double offset, unsigned int noOfElements) {
70 
71  // set offset
72  _offset = std::floor(offset);
73 
74  // reset elements
75  if (_elements.size() > 0) _elements.clear();
76 
77  // create points
78  for (size_t i = 0; i < noOfElements; ++i)
79  _elements.emplace_back(newPoint(i));
80 
81  }
82 
83 
89  void update(double s) {
90 
91  size_t i0 = 0; // first element with positive distance
92 
93  for (auto &e : _elements) {
94 
95  // recalculate distance
96  e.ds = e.s - s;
97 
98  // check distance and increment if <= 0
99  if(e.ds <= 0.0)
100  i0++;
101  }
102 
103  // get reference index of the last element
104  size_t ib = _elements.back().i;
105 
106  // remove old element
107  for (size_t i = 0; i + 1 < i0; ++i) {
108 
109  // new index
110  size_t i1 = ib + i + 1;
111 
112  // remove from front, add to the back
113  _elements.pop_front();
114  _elements.emplace_back(newPoint(i1));
115 
116  }
117 
118  }
119 
120 
126  unsigned int getIndexBefore(double s) {
127 
128  double s0 = _elements.at(0).s;
129 
130  if(s <= s0)
131  return 0.0;
132  if(s >= _elements.back().s)
133  return _elements.size() - 1;
134 
135  return (unsigned int) std::floor(s - s0);
136 
137  }
138 
139 
145  unsigned int getIndexAfter(double s) {
146 
147  double s0 = _elements.at(0).s;
148 
149  if(s <= s0)
150  return 0.0;
151  if(s >= _elements.back().s)
152  return _elements.size() - 1;
153 
154  return (unsigned int) std::ceil(s - s0);
155 
156  }
157 
158 
163  void setMaxVelocity(double v) {
164 
165  _vMax = v;
166 
167  }
168 
169 
173  void resetSpeedRule() {
174 
175  for(auto &e : _elements)
176  e.vRule = INFINITY;
177 
178  }
179 
180 
187  void updateSpeedRuleInInterval(double s0, double s1, double v) {
188 
189  auto i0 = getIndexBefore(s0);
190  auto i1 = getIndexAfter(s1);
191 
192  for (unsigned int i = i0; i <= i1; ++i) {
193 
194  // get element
195  auto &e = _elements.at(i);
196 
197  // set speed if speed is smaller and point in interval
198  if(e.vRule > v)
199  e.vRule = v;
200 
201  }
202 
203  }
204 
205 
211  void updateContinuousPoint(double s, double v) {
212 
213  // get index before position
214  auto i = getIndexAfter(s);
215  auto &e = _elements.at(i);
216 
217  if(s > e.sCont) {
218 
219  e.sCont = s;
220  e.vCont = v;
221 
222  }
223 
224  }
225 
226 
227 
235  double mean(double s0, double s1, double delta = 1.0) {
236 
237  // instantiate
238  double v = 0.0;
239  double vMin = INFINITY;
240  double j = 0;
241 
242  // get indexes
243  auto i0 = getIndexBefore(s0);
244  auto i1 = getIndexAfter(s1);
245 
246  for (unsigned int i = i0; i <= i1; ++i) {
247 
248  // get speed and s
249  auto v0 = (std::min)(vMin, getSpeedAt(i));
250  auto s = _elements.at(i).s;
251 
252  // sum up with scaled factor
253  auto f = agent_model::scale(s, s1, s0, delta);
254  v += f * v0;
255 
256  // set minimum for future
257  if(v0 < vMin)
258  vMin = v0;
259 
260  // divisor
261  j += f;
262 
263  }
264 
265  return v / (double) j;
266 
267  }
268 
269 
270 
271  protected:
272 
273 
279  double getSpeedAt(unsigned int i) {
280 
281  // get speed at index
282  auto &e = _elements.at(i);
283  return (std::min)((std::min)(e.vCont, e.vRule), _vMax);
284 
285  }
286 
287 
294 
295  double s = _offset + (double) i;
296  return PredictionPoint{i, s, INFINITY, INFINITY, INFINITY, s - 1.0};
297 
298  }
299 
300 
301  };
302 
303 
304 }
305 
306 
307 #endif //SIMDRIVER_VELOCITYHORIZON_H
A class to store the internal horizon.
Definition: VelocityHorizon.h:40
A class store a prediction point.
Definition: VelocityHorizon.h:45
double vCont
The continuous velocity (e.g. curve speed)
Definition: VelocityHorizon.h:50
double sCont
The continuous measure point.
Definition: VelocityHorizon.h:51
PredictionPoint newPoint(size_t i)
Definition: VelocityHorizon.h:293
unsigned int getIndexBefore(double s)
Definition: VelocityHorizon.h:126
unsigned int getIndexAfter(double s)
Definition: VelocityHorizon.h:145
double vRule
The planned velocity at the point.
Definition: VelocityHorizon.h:49
size_t i
Reference index of the point.
Definition: VelocityHorizon.h:46
void init(double offset, unsigned int noOfElements)
Definition: VelocityHorizon.h:69
Definition: DistanceTimeInterval.h:33
double mean(double s0, double s1, double delta=1.0)
Definition: VelocityHorizon.h:235
void update(double s)
Updates the horizon to the new reference position Removes all elements with a distance smaller than z...
Definition: VelocityHorizon.h:89
void resetSpeedRule()
Definition: VelocityHorizon.h:173
void updateContinuousPoint(double s, double v)
Definition: VelocityHorizon.h:211
double s
The longitudinal reference position of the point.
Definition: VelocityHorizon.h:47
double ds
The actual distance to the point.
Definition: VelocityHorizon.h:48
void updateSpeedRuleInInterval(double s0, double s1, double v)
Definition: VelocityHorizon.h:187
double getSpeedAt(unsigned int i)
Definition: VelocityHorizon.h:279
void setMaxVelocity(double v)
Definition: VelocityHorizon.h:163