ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
plot_trafficlights.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 implementation
13  * Thomas Lobig
14  ********************************************************************************/
15 
16 #pragma once
18 #include <adore/env/afactory.h>
19 #include <adore/mad/com_patterns.h>
23 #include <ros/console.h>
24 
25 namespace adore
26 {
27  namespace apps
28  {
34  {
35  private:
39  bool plot_connection_lines = true;
40 
41  // TODO consider not plotting controlled connections by distance, would maybe need those lines:
42  // std::unordered_set<std::string> plot_tags_old_;
43  // std::unordered_set<std::string> plot_tags_current_;
44 
45 
48  std::string prefix_;
49 
50  std::unordered_map<std::string, std::pair<double,double>> visible_TCD_controller_;
52 
53  public:
54  PlotTrafficLights(DLR_TS::PlotLab::AFigureStub* figure,std::string prefix, bool connection_lines = false)
55  {
59 
60  plot_connection_lines = connection_lines;
61  figure_ = figure;
62  prefix_ = prefix;
63  }
64 
66  {
67  delete positionReader_;
68  delete tl_controller_feed_;
69  delete borderfeed_;
70  delete figure_;
71  }
72 
73  void run()
74  {
75  // TODO consider not plotting controlled connections by distance, would maybe need those lines:
76  // plot_tags_old_.insert(plot_tags_current_.begin(),plot_tags_current_.end());
77  // plot_tags_current_.clear();
78 
80  {
82  }
83 
84  while (borderfeed_->hasNext())
85  {
86  auto border = new adore::env::BorderBased::Border();
87  borderfeed_->getNext(*border);
88  borderSet_.insert_border(border, true);
89  }
90 
92  }
93 
94  virtual void plotTLConnections()
95  {
96  auto time = position_.getTime();
97  int plot_distance = 400;
98 
100 
101  while(tl_controller_feed_->hasNext())
102  {
103  double r = 0,g = 0, b = 0;
104  std::stringstream ss_hashtag, ss_color, ss_tl_icon;
106 
108 
109  auto from = con.getID().getFrom();
110  double x_from = from.get<0>();
111  double y_from = from.get<1>();
112 
113  auto to = con.getID().getTo();
114  double x_to = to.get<0>();
115  double y_to = to.get<1>();
116 
117  std::pair<double,double> pos_coord = std::make_pair(x_from,y_from);
118  auto distance_to_object = getDistanceToPoint(pos_coord);
119 
120  if(distance_to_object > plot_distance)
121  continue;
122 
124  ss_hashtag <<"#con::"<< (int) x_from <<"::"<< (int) y_from<<"::" << (int) x_to<<"::" << (int)y_to;
125  ss_tl_icon << ss_hashtag.str() << "::icon";
126 
127  std::string tl_img;
128 
129  // if(state == adore::env::ConnectionState::DARK)
130  // {
131  // figure_->erase(ss_hashtag.str());
132  // figure_->erase(ss_tl_icon.str());
133  // visible_TCD_controller_.erase(ss_hashtag.str());
134  // visible_TCD_controller_.erase(ss_tl_icon.str());
135  // continue;
136  // }
137 
138  ROS_DEBUG_STREAM("Received new states.");
139  ROS_DEBUG_STREAM("x_from: " << x_from << "; y_from: " << y_from);
140  ROS_DEBUG_STREAM("state: " << state);
141 
142  // https://www.car-2-car.org/fileadmin/documents/Basic_System_Profile/Release_1.5.0/C2CCC_RS_2077_SPATMAP_AutomotiveRequirements.pdf
143  switch (state)
144  {
146  r = 0; g = 0; b = 0;
147  tl_img = "../images/tl_yellow.png";
148  break;
149  // 2 - green
151  g = 1; b = 1;
152  tl_img = "../images/tl_green.png";
153  break;
154  // 3 - red
156  r = 1;
157  tl_img = "../images/tl_red.png";
158  break;
159  // 4 - red-yellow
161  r = 0.5, g = 0.5;
162  tl_img = "../images/tl_red_yellow.png";
163  break;
164  // 5 - green with conflicts
166  tl_img = "../images/tl_green.png";
167  g = 0.5;
168  break;
169  // 6 - green
171  tl_img = "../images/tl_green.png";
172  g = 1;
173  break;
174  // 7 - yellow- conflicts possible
176  r = 0.5, g = 0.5;
177  tl_img = "../images/tl_yellow.png";
178  break;
179  // 8 - yellow- clear
181  r = 1, g = 1;
182  tl_img = "../images/tl_yellow.png";
183  break;
184  // 9 - green
186  b = 1;
187  tl_img = "../images/tl_yellow.png";
188  break;
189  default:
190  std::cout << "Unknown state: " << state << std::endl;
191  tl_img = "../images/tl_yellow.png";
192  b = 1;
193  break;
194  }
195 
196  ss_color << "LineColor=" << r << "," << g << "," << b;
197 
198  double heading = std::numeric_limits<double>::quiet_NaN();
199 
200  heading = getHeadingFromMap(x_from,y_from);
201 
202  if(std::isnan(heading))
203  heading = getHeadingOfLine(x_from,y_from,x_to,y_to);
204 
205  figure_->plotTexture(ss_tl_icon.str(), tl_img, x_from, y_from, 1, heading, 3.5, 1.5);
206  visible_TCD_controller_[ss_tl_icon.str()] = std::make_pair(x_from,y_from);
207 
209  {
210  adore::PLOT::plotLine(ss_hashtag.str(),x_from,y_from,x_to,y_to,2,ss_color.str(),figure_);
211  visible_TCD_controller_[ss_hashtag.str()] = std::make_pair(x_from,y_from);
212  }
213 
214  }
215  }
216 
217  double getHeadingOfLine(double x1,double y1,double x2,double y2)
218  {
219  return std::atan2(y2 - y1, x2 - x1) + (1.5 * M_PI);
220  }
221 
222  double getHeadingFromMap(double x, double y)
223  {
224  if (borderSet_.size() == 0)
225  return std::numeric_limits<double>::quiet_NaN();
226 
227  adore::env::BorderBased::BorderSubSet borders_near_connection_point =
229 
230  if (borders_near_connection_point.size() == 0)
231  return std::numeric_limits<double>::quiet_NaN();
232 
234  pred = borders_near_connection_point[0];
235 
236  double x1 = pred->m_path->getData()(1,0);
237  double y1 = pred->m_path->getData()(2,0);
238  double x2 = pred->m_path->getData()(1,1);
239  double y2 = pred->m_path->getData()(2,1);
240 
241  return getHeadingOfLine(x1,y1,x2,y2);
242  }
243  void removeDistanceBased(std::unordered_map<std::string, std::pair<double,double>> &visible_objects, int distance)
244  {
245  //remove tiles which are no longer visible
246  std::vector<std::string> remove;
247 
248  for(auto it = visible_objects.begin();it!=visible_objects.end();it++)
249  {
250  auto coord = (*it).second;
251  double distance_to_object = getDistanceToPoint(coord);
252  if(distance_to_object > distance) remove.push_back((*it).first);
253  }
254 
255  for(auto it = remove.begin();it!=remove.end();it++)
256  {
257  figure_->erase((*it));
258  visible_objects.erase((*it));
259  }
260 
261  }
262  double getDistanceToPoint(std::pair<double,double> &point)
263  {
264 
265  double v_pos_x = position_.getX();
266  double v_pos_y = position_.getY();
267 
268  double distance_x = v_pos_x - point.first;
269  double distance_y = v_pos_y - point.second;
270 
271  return (std::sqrt((distance_x * distance_x) + (distance_y*distance_y)));
272  }
273 
274  };
275  }
276 }
#define M_PI
Definition: arraymatrixtools.h:24
Definition: afigurestub.h:24
virtual void plotTexture(std::string hashtag, std::string url, double x, double y, double z, double psi, double w, double l)=0
virtual void erase(std::string hashtag)=0
a optimzed plotting application to plot map borders, vehicles and environment information and backgro...
Definition: plot_trafficlights.h:34
void removeDistanceBased(std::unordered_map< std::string, std::pair< double, double >> &visible_objects, int distance)
Definition: plot_trafficlights.h:243
bool plot_connection_lines
Definition: plot_trafficlights.h:39
virtual void plotTLConnections()
Definition: plot_trafficlights.h:94
PlotTrafficLights(DLR_TS::PlotLab::AFigureStub *figure, std::string prefix, bool connection_lines=false)
Definition: plot_trafficlights.h:54
DLR_TS::PlotLab::AFigureStub * figure_
Definition: plot_trafficlights.h:46
std::unordered_map< std::string, std::pair< double, double > > visible_TCD_controller_
Definition: plot_trafficlights.h:50
adore::mad::AFeed< adore::env::BorderBased::Border > * borderfeed_
Definition: plot_trafficlights.h:38
adore::mad::AReader< adore::env::VehicleMotionState9d > * positionReader_
Definition: plot_trafficlights.h:36
void run()
Definition: plot_trafficlights.h:73
double getDistanceToPoint(std::pair< double, double > &point)
Definition: plot_trafficlights.h:262
~PlotTrafficLights()
Definition: plot_trafficlights.h:65
adore::mad::AFeed< adore::env::ControlledConnection > * tl_controller_feed_
Definition: plot_trafficlights.h:37
adore::env::VehicleMotionState9d position_
Definition: plot_trafficlights.h:47
adore::env::BorderBased::BorderSet borderSet_
Definition: plot_trafficlights.h:51
double getHeadingFromMap(double x, double y)
Definition: plot_trafficlights.h:222
double getHeadingOfLine(double x1, double y1, double x2, double y2)
Definition: plot_trafficlights.h:217
std::string prefix_
Definition: plot_trafficlights.h:48
virtual TVehicleMotionStateReader * getVehicleMotionStateReader()=0
virtual TControlledConnectionFeed * getControlledConnectionFeed()=0
virtual TBorderFeed * getBorderFeed()=0
efficiently store borders in boost R-tree
Definition: borderset.h:99
BorderSubSet getBordersAtPoint(double x, double y, double max_lane_width)
get all borders at the given point
Definition: borderset.h:403
void insert_border(Border *b, bool force_insert=false)
insert new border into this
Definition: borderset.h:225
int size()
number of borders in this
Definition: borderset.h:1497
Definition: controlledconnection.h:81
const TID & getID() const
Definition: controlledconnection.h:100
ConnectionState::EConnectionState getState_byMinTime(double t) const
Definition: controlledconnection.h:118
static adore::env::AFactory * get()
Definition: afactory.h:236
virtual void getNext(T &value)=0
virtual bool hasNext() const =0
virtual void getData(T &value)=0
virtual bool hasUpdate() const =0
adoreMatrix< T, n+1, 0 > & getData()
Definition: llinearpiecewisefunction.h:147
void plotLine(std::string hashtag, double x0, double y0, double x1, double y1, double z, std::string options, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:209
std::vector< Border * > BorderSubSet
Definition: borderset.h:92
EConnectionState
Definition: controlledconnection.h:36
@ PROTECTED___MOVEMENT___ALLOWED
Definition: controlledconnection.h:43
@ STOP___AND___REMAIN
Definition: controlledconnection.h:40
@ PERMISSIVE___MOVEMENT___ALLOWED
Definition: controlledconnection.h:42
@ DARK
Definition: controlledconnection.h:38
@ STOP___THEN___PROCEED
Definition: controlledconnection.h:39
@ PERMISSIVE__CLEARANCE
Definition: controlledconnection.h:44
@ CAUTION___CONFLICTING___TRAFFIC
Definition: controlledconnection.h:46
@ PRE___MOVEMENT
Definition: controlledconnection.h:41
@ PROTECTED__CLEARANCE
Definition: controlledconnection.h:45
ALFunction< T, T > * heading(AScalarToN< T, 2 > *df)
Definition: heading.h:114
interval< T > atan2(interval< T > y, interval< T > x)
Definition: intervalarithmetic.h:234
x
Definition: adore_set_goal.py:30
g
Definition: adore_set_goal.py:29
y
Definition: adore_set_goal.py:31
y1
Definition: adore_set_pose.py:29
x1
Definition: adore_set_pose.py:28
r
Definition: adore_suppress_lanechanges.py:209
Definition: areaofeffectconverter.h:20
The border struct contains data of the smallest.
Definition: border.h:62
Tborderpath * m_path
Definition: border.h:70
Tboost_point getFrom() const
Definition: vectoridentifier.h:65
Tboost_point getTo() const
Definition: vectoridentifier.h:66
This struct holds the motion state of the vehicle in 9d.
Definition: vehiclemotionstate9d.h:39
double getX() const
Get the x-coordinate.
Definition: vehiclemotionstate9d.h:54
double getY() const
Get the y-coordinate.
Definition: vehiclemotionstate9d.h:60
double getTime() const
Get the time.
Definition: vehiclemotionstate9d.h:48