ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
vehiclemotionstatebuffer.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2021 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/fun/afactory.h>
18 
19 namespace adore
20 {
21 namespace fun
22 {
23 
28  {
29  private:
31  std::list<VehicleMotionState9d> buffer_;
32  double interp(double t,double ta,double a,double tb,double b)
33  {
34  return a + (b-a)*(t-ta)/(tb-ta);
35  }
36  double dt_max_;
37  public:
39  :feed_(feed), dt_max_(dt_max){}
44  void update()
45  {
46  while(feed_->hasNext())
47  {
49  feed_->getNext(x);
50  buffer_.push_back(x);
51  }
52  while(buffer_.size()>0 &&
53  buffer_.begin()->getTime()+dt_max_<buffer_.rbegin()->getTime())
54  {
55  buffer_.pop_front();
56  }
57  }
58  double size()
59  {
60  return buffer_.size();
61  }
62  double getTmax()
63  {
64  if(size()==0)return 0.0;
65  return buffer_.rbegin()->getTime();
66  }
72  {
73  if(buffer_.size()==0)return false;
74  if(t<buffer_.begin()->getTime())return false;
75  if(t>buffer_.rbegin()->getTime())
76  {
77  std::cout<<"vehiclemotionstatebuffer: retrieving latest with dt="<<(t-buffer_.rbegin()->getTime())<<std::endl;
78  result = *buffer_.rbegin();
79  return true;
80  }
81  return interpolate(t,result);
82  }
87  bool interpolate(double t, VehicleMotionState9d& result)
88  {
89  if(buffer_.size()==0)return false;
90  if(buffer_.size()==1 && t==buffer_.begin()->getTime())
91  {
92  result = *buffer_.begin();
93  return true;
94  }
95  if(buffer_.size()<2||t<buffer_.begin()->getTime()||t>buffer_.rbegin()->getTime())return false;
96  for(auto a=buffer_.begin();a!=buffer_.end();a++)
97  {
98  auto b = a;
99  b++;
100  if(b==buffer_.end())break;
101  double ta = a->getTime();
102  double tb = b->getTime();
103  if(t==ta)
104  {
105  result = *a;
106  return true;
107  }
108  if(t==tb)
109  {
110  result = *b;
111  return true;
112  }
113  if(ta<=t && t<tb)
114  {
115  result.setTime(t);
116  result.setX(interp(t,ta,a->getX(),tb,b->getX()));
117  result.setY(interp(t,ta,a->getY(),tb,b->getY()));
118  result.setZ(interp(t,ta,a->getZ(),tb,b->getZ()));
119  const double ca = std::cos(a->getPSI());
120  const double sa = std::sin(a->getPSI());
121  const double cb = std::cos(b->getPSI());
122  const double sb = std::sin(b->getPSI());
123  const double dpsi = std::atan2(-sa*cb+ca*sb,ca*cb+sa*sb);
124  result.setPSI(a->getPSI()+interp(t,ta,0,tb,dpsi));
125  result.setvx(interp(t,ta,a->getvx(),tb,b->getvx()));
126  result.setvy(interp(t,ta,a->getvy(),tb,b->getvy()));
127  result.setOmega(interp(t,ta,a->getOmega(),tb,b->getOmega()));
128  result.setAx(interp(t,ta,a->getAx(),tb,b->getAx()));
129  result.setDelta(interp(t,ta,a->getDelta(),tb,b->getDelta()));
130  return true;
131  }
132  }
133  return false;
134  }
135  };
136 
137 }
138 }
Buffers and interpolates vehicle positions.
Definition: vehiclemotionstatebuffer.h:28
double getTmax()
Definition: vehiclemotionstatebuffer.h:62
std::list< VehicleMotionState9d > buffer_
Definition: vehiclemotionstatebuffer.h:31
adore::fun::AFactory::TMotionStateFeed * feed_
Definition: vehiclemotionstatebuffer.h:30
~VehicleMotionStateBuffer()
Definition: vehiclemotionstatebuffer.h:40
void update()
call update to check for new data
Definition: vehiclemotionstatebuffer.h:44
VehicleMotionStateBuffer(adore::fun::AFactory::TMotionStateFeed *feed, double dt_max=1.0)
Definition: vehiclemotionstatebuffer.h:38
bool interpolate(double t, VehicleMotionState9d &result)
interpolate a value for time t, if t is in buffered time range
Definition: vehiclemotionstatebuffer.h:87
bool interpolate_or_latest(double t, VehicleMotionState9d &result)
get the latest, if
Definition: vehiclemotionstatebuffer.h:71
double size()
Definition: vehiclemotionstatebuffer.h:58
double interp(double t, double ta, double a, double tb, double b)
Definition: vehiclemotionstatebuffer.h:32
double dt_max_
Definition: vehiclemotionstatebuffer.h:36
Definition: com_patterns.h:29
virtual void getNext(T &value)=0
virtual bool hasNext() const =0
interval< T > atan2(interval< T > y, interval< T > x)
Definition: intervalarithmetic.h:234
interval< T > cos(interval< T > x)
Definition: intervalarithmetic.h:225
interval< T > sin(interval< T > x)
Definition: intervalarithmetic.h:204
x
Definition: adore_set_goal.py:30
Definition: areaofeffectconverter.h:20
This struct holds the motion state of the vehicle in 9d.
Definition: vehiclemotionstate9d.h:39
void setZ(double value)
Set the z-coordinate.
Definition: vehiclemotionstate9d.h:127
void setY(double value)
Set the y-coordinate.
Definition: vehiclemotionstate9d.h:121
void setAx(double value)
Set the longitudinal acceleration.
Definition: vehiclemotionstate9d.h:157
void setvx(double value)
set the longitudinal velocity
Definition: vehiclemotionstate9d.h:139
void setOmega(double value)
Set the yaw rate.
Definition: vehiclemotionstate9d.h:151
void setX(double value)
Set the x-coordinate.
Definition: vehiclemotionstate9d.h:115
void setPSI(double value)
set the heading
Definition: vehiclemotionstate9d.h:133
void setvy(double value)
set the lateral velocity
Definition: vehiclemotionstate9d.h:145
void setDelta(double value)
Set the steering angle.
Definition: vehiclemotionstate9d.h:163
void setTime(double value)
Set the time.
Definition: vehiclemotionstate9d.h:109