ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
platoonLogic.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  * Reza Dariani- initial API and implementation
13  ********************************************************************************/
14 #pragma once
21 #include <adore/fun/nlpqlp_planner/colors.h>
22 #include <adore/fun/nlpqlp_planner/planner_data.h>
23 //#include <adore/view/platoonview.h>
24 
25 namespace adore
26 {
27  namespace fun
28  {
32  using namespace adore::env;
33  using namespace adore::fun::logic;
35  {
36 
37  public:
39  enum CAR_FOLLOWING_TACTICS {IDM, CACC};
43 
44  enum LanePosition {offTheRoad = -1, hardShoulder=0, outermostDrivingLane = 1, secondLaneFromOutside = 2, thirdLaneFromOutside = 3, fourthLaneFromOutside = 4, fifthLaneFromOutside = 5 };
45 
51  Planner_data data;
52  // adore::view::platoonView* p_view;
53 
55  {
56  this->data = data;
57  platooningInformation_writer = FunFactory::get()->getPlatooningStateWriter();
58  this->three_lanes_ = three_lanes_;
59  this->v2xStationID = data.v2xStationID ;
60  this->CooperativeUsersProcess = CooperativeUsersProcess;
61  possiblePlatooningTau = 5.;
62  eps = 2.0;
63  communicationDelayThreshold = 0.5;
65 
66  //
67 
68  //p_view = new adore::view::platoonView(three_lanes_);
69  psm.initiate();
70  dsm.initiate();
71 
72 
73 
74  }
76  {
77  delete three_lanes_;
78  }
79  int update(adore::fun::VehicleMotionState9d& egoStates, int lanePosition=1)
80  {
82  NoCommunicationDelay_p = false;
83  sameLane_p = false;
84 
85 
86 
87  this->lanePosition = lanePosition;
89  {
90 
91  auto preceding = CooperativeUsersProcess->getPreceding();
92  if(preceding.MCM.communicationDelay < communicationDelayThreshold) NoCommunicationDelay_p= true;
93  if(preceding.MCM.lane_position == lanePosition ) sameLane_p = true;
94 
95  //transition [want to form --> joining]
96  if((preceding.PlatooningState == logic::WantToForm ||preceding.PlatooningState == logic::InPlatoon) &&
99  sameLane_p &&
100  preceding.inPlatooningRange &&
101  NoCommunicationDelay_p)
102  {
103  //std::cout<<"\nWF2J";
104  psm.process_event(WF2J());
105 
106  // psm.process_event(IP2J());
107  //transition [joining --> in platoon]
108  //std::cout<<"\n"<<CooperativeUsersProcess->getPrecedingTimeHeadway()<<"\t"<<psm.getInPlatoonTimeHeadway();
109 
110 
111  }
112  if((preceding.PlatooningState == logic::WantToForm ||preceding.PlatooningState == logic::InPlatoon) &&
113  (psm.getPlatooningState() == logic::Joining) &&
115  sameLane_p &&
116  preceding.inPlatooningRange &&
117  NoCommunicationDelay_p &&
119  {
120  //std::cout<<"\nJ2IP";
121  psm.process_event(J2IP());
122 
123  }
124  //[(joining || inplatoon) --> want to form]
125 
126  if( ((preceding.PlatooningState == logic::Leaving ||preceding.PlatooningState == logic::NotAble) &&
129  !sameLane_p ||
130  !NoCommunicationDelay_p)
131  {
132  //std::cout<<"\nJ2WF & IP2WF";
133  psm.process_event(J2WF());
134  psm.process_event(IP2WF());
135 
136 
137  }
138  }
139 
140  //I am the leading vehicle, no preceding exists
142  {
144  {
145  auto following = CooperativeUsersProcess->getFollowing();
146  //std::cout<<"\n"<<CooperativeUsersProcess->getDistanceToFollowing()<<"\t"<< CooperativeUsersProcess->getFollowingTimeHeadway() ;
147  if( (following.PlatooningState == logic::WantToForm || following.PlatooningState == logic::Joining || following.PlatooningState == logic::InPlatoon ) &&
149  following.MCM.lane_position == lanePosition &&
151  {
152  psm.process_event(WF2IP());
153  psm.process_event(J2IP());
154 
155  }
156  if( ((following.PlatooningState == logic::Leaving ||following.PlatooningState == logic::NotAble) &&
159  following.MCM.lane_position != lanePosition ||
160  following.MCM.communicationDelay > communicationDelayThreshold)
161  {
162  psm.process_event(J2WF());
163  psm.process_event(IP2WF());
164 
165 
166  }
167 
168  }
169 
170  }
171 
172 
173 
174 
175 
176 
177  // psm.process_event(adore::fun::logic::WF2J());
178  // psm.process_event(adore::fun::logic::J2NA());
179 
180 
181  // int a; std::cin>>a;
182  // //psm.process_event()
183  double platooningBasedDistance = std::max(egoStates.getvx(),3.)*psm.getTimeHeadway();
184  platooningInformation.setId(v2xStationID);
185  platooningInformation.setLanePosition(lanePosition); //TODO : must be calculated
186  platooningInformation.setTargetAutomationLevel(logic::SAE_LEVEL3);
187  platooningInformation.setToleratedDistanceAhead(platooningBasedDistance);//TODO : must be calculated
188  platooningInformation.setToleratedDistanceBehind(platooningBasedDistance);//TODO : must be calculated
189  // std::cout<<"\n"<<platooningInformation.getId();
190  // std::cout<<"\n"<<platooningInformation.getLanePosition();
191  // std::cout<<"\n"<<platooningInformation.getTargetAutomationLevel();
192  platooningInformation_writer->write(platooningInformation);
193  if(data.print_strategy) print();
195  //because I am leading, I am in platoon with following and not (yet) with preceding
197  {
198 
199  }
200  return CAR_FOLLOWING_TACTICS;
201 
202 
203  }
204  //adore::fun::VehicleMotionState9d &egoStates
205  private:
208  void print()
209  {
210  std::cout<<"\n--------------------------------";
211 
212  std::cout<<"\nEgo is: "<<v2xStationID;
213  printPlatooningState( psm.getPlatooningState() );
214  //std::cout<<"\n"
216  {
217  auto preceding = CooperativeUsersProcess->getPreceding();
218  std::cout<<"\nPreceding: "<<preceding.MCM.id;
219  printPlatooningState( preceding.PlatooningState );
220 
221  if(preceding.MCM.lane_position== lanePosition) std::cout<<" SAME LANE";
222  if(preceding.inPlatooningRange) std::cout<<" IN RANGE";
223  if(preceding.MCM.communicationDelay>communicationDelayThreshold) std::cout<< BOLD(FRED(" DELAY"));
224  if(!CooperativeUsersProcess->noVehicleBetweenEgoAndPreceding()) std::cout<< BOLD(FRED(" SomeoneELse"));
225  //std::cout<<"\n"<<CooperativeUsersProcess->getDistanceToPreceding()<<"\t"<<CooperativeUsersProcess->getPrecedingTimeHeadway();
226  }
228  {
229  auto following = CooperativeUsersProcess->getFollowing();
230  std::cout<<"\nFollowing: "<<following.MCM.id;
231  printPlatooningState( following.PlatooningState );
232 
233  if(following.MCM.lane_position== lanePosition) std::cout<<" SAME LANE";
234  if(following.inPlatooningRange) std::cout<<" IN RANGE";
235  if(following.MCM.communicationDelay>communicationDelayThreshold) std::cout<< BOLD(FRED(" DELAY"));
236  if(!CooperativeUsersProcess->noVehicleBetweenEgoAndFollowing()) std::cout<< BOLD(FRED(" SomeoneELse"));
237  //std::cout<<"\n"<<CooperativeUsersProcess->getDistanceToPreceding()<<"\t"<<CooperativeUsersProcess->getPrecedingTimeHeadway();
238  }
239 
240 
241  }
242  void printPlatooningState(int ps)
243  {
244  if(ps == 0) std::cout <<"\t"<< BOLD(FRED("NOT ABLE"));
245  if(ps== 1) std::cout <<"\t"<< BOLD(FYEL("WANT TO FORM"));
246  if(ps == 2) std::cout <<"\t"<< BOLD(FMAG("JOINING"));
247  if(ps == 3) std::cout <<"\t"<< BOLD(FGRN("IN PLATOON"));
248  if(ps== 4) std::cout <<"\t"<< BOLD(FRED("LEAVING"));
249  }
254  double eps;
255 
256  };
257  }
258 }
Definition: localroadmap.h:38
Definition: cooperativeusersprocess.h:27
bool precedingExists()
Definition: cooperativeusersprocess.h:68
CooperativeUser getFollowing()
Definition: cooperativeusersprocess.h:67
bool noVehicleBetweenEgoAndFollowing()
Definition: cooperativeusersprocess.h:81
bool leaderExists()
Definition: cooperativeusersprocess.h:69
double getFollowingTimeHeadway()
Definition: cooperativeusersprocess.h:79
double getPrecedingTimeHeadway()
Definition: cooperativeusersprocess.h:78
bool followingExists()
Definition: cooperativeusersprocess.h:70
CooperativeUser getPreceding()
Definition: cooperativeusersprocess.h:65
bool noVehicleBetweenEgoAndPreceding()
Definition: cooperativeusersprocess.h:80
Utility class to simplify factory access.
Definition: afactory.h:220
Definition: threelaneviewdecoupled.h:32
Utility class to simplify factory access.
Definition: afactory.h:154
static adore::fun::AFactory * get()
Definition: afactory.h:170
Definition: platooningInformation.h:28
void setId(int id)
Definition: platooningInformation.h:50
void setToleratedDistanceBehind(double tdb)
Definition: platooningInformation.h:47
void setLanePosition(int lp)
Definition: platooningInformation.h:49
void setTargetAutomationLevel(unsigned int tal)
Definition: platooningInformation.h:48
void setToleratedDistanceAhead(double tda)
Definition: platooningInformation.h:46
Definition: platoonLogic.h:35
platoonLogic(adore::env::ThreeLaneViewDecoupled *three_lanes_, adore::env::CooperativeUsersProcess *CooperativeUsersProcess, Planner_data data)
Definition: platoonLogic.h:54
adore::fun::logic::DistanceStateMachine dsm
Definition: platoonLogic.h:47
LanePosition
Definition: platoonLogic.h:44
adore::mad::AWriter< adore::fun::PlatooningInformation > * platooningInformation_writer
Definition: platoonLogic.h:41
int lanePosition
Definition: platoonLogic.h:49
void printPlatooningState(int ps)
Definition: platoonLogic.h:242
adore::env::CooperativeUsersProcess * CooperativeUsersProcess
Definition: platoonLogic.h:253
int v2xStationID
Definition: platoonLogic.h:48
int update(adore::fun::VehicleMotionState9d &egoStates, int lanePosition=1)
Definition: platoonLogic.h:79
bool sameLane_p
Definition: platoonLogic.h:207
int CAR_FOLLOWING_TACTICS
Definition: platoonLogic.h:38
adore::env::BorderBased::LocalRoadMap roadmap_
Definition: platoonLogic.h:40
~platoonLogic()
Definition: platoonLogic.h:75
double eps
Definition: platoonLogic.h:254
adore::env::ThreeLaneViewDecoupled * three_lanes_
Definition: platoonLogic.h:252
CAR_FOLLOWING_TACTICS
Definition: platoonLogic.h:39
bool NoCommunicationDelay_p
Definition: platoonLogic.h:206
double communicationDelayThreshold
Definition: platoonLogic.h:50
adore::fun::logic::PlatooningStateMachine psm
Definition: platoonLogic.h:46
double possiblePlatooningTau
if the time headway to the preceding vehicle is less than this value we consider it for platooning
Definition: platoonLogic.h:251
adore::fun::PlatooningInformation platooningInformation
Definition: platoonLogic.h:42
Planner_data data
Definition: platoonLogic.h:51
void print()
Definition: platoonLogic.h:208
virtual void write(const T &value)=0
Utility class to simplify factory access.
Definition: afactory.h:87
const double eps
Definition: cubic_piecewise_function.cpp:16
Definition: afactory.h:38
Definition: distanceStateMachine.h:29
@ Joining
Definition: platoonStateMachine.h:42
@ InPlatoon
Definition: platoonStateMachine.h:42
@ NotAble
Definition: platoonStateMachine.h:42
@ Leaving
Definition: platoonStateMachine.h:42
@ WantToForm
Definition: platoonStateMachine.h:42
@ SAE_LEVEL3
Definition: platoonStateMachine.h:43
adoreMatrix< T, N, M > max(adoreMatrix< T, N, M > a, const adoreMatrix< T, N, M > &b)
Definition: adoremath.h:686
Definition: areaofeffectconverter.h:20
int id
Definition: cooperativeusersprediction.h:27
CooperativeUserPrediction MCM
Definition: cooperativeusersprocess.h:33
This struct holds the motion state of the vehicle in 9d.
Definition: vehiclemotionstate9d.h:39
double getvx() const
Get the longitudinal velocity.
Definition: vehiclemotionstate9d.h:78
Definition: distanceStateMachine.h:42
Definition: platoonStateMachine.h:56
Definition: platoonStateMachine.h:49
Definition: platoonStateMachine.h:48
Definition: platoonStateMachine.h:72
double getTimeHeadway()
Definition: platoonStateMachine.h:76
int getPlatooningState()
Definition: platoonStateMachine.h:77
double getInPlatoonTimeHeadway()
Definition: platoonStateMachine.h:78
Definition: platoonStateMachine.h:53
Definition: platoonStateMachine.h:51