ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
ros_com_patterns.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/com_patterns.h>
18 #include <ros/ros.h>
19 #include <list>
20 #include <string>
21 
22 
23 namespace adore
24 {
25  namespace if_ROS
26  {
30  template<class T,class TMSG,class CONVERTER>
31  class Feed:public adore::mad::AFeed<T>
32  {
33  private:
34  std::list<T> data_;
35  ros::Subscriber subscriber_;
36  CONVERTER converter_;
37  public:
38  void receive(TMSG msg)
39  {
40  data_.emplace_back();
41  converter_(msg,data_.back());
42  }
43  Feed(ros::NodeHandle* n,const std::string& topic,int qsize)
44  {
45  bool no_delay;
46  n->param("/tcp_no_delay", no_delay, false);
47  subscriber_ = n->subscribe(topic,qsize,&Feed<T,TMSG,CONVERTER>::receive,this,ros::TransportHints().tcpNoDelay(no_delay));
48  }
49  virtual bool hasNext() const override
50  {
51  return data_.size()>0;
52  }
53  virtual void getNext(T& value) override
54  {
55  value = data_.front();
56  data_.pop_front();
57  }
58  virtual void getLatest(T& value) override
59  {
60  value = data_.back();
61  data_.clear();
62  }
63  };
64 
65  template<class T,class TMSG,class CONVERTER>
67  {
68  private:
69  std::list<T> data_;
70  ros::Subscriber subscriber_;
71  CONVERTER converter_;
72  std::function<void()> fcn_;
73  bool has_fcn_;
74  public:
75  void receive(TMSG msg)
76  {
77  data_.emplace_back();
78  converter_(msg,data_.back());
79  if (has_fcn_) fcn_();
80  }
81  FeedWithCallback(ros::NodeHandle* n,const std::string& topic,int qsize)
82  {
83  bool no_delay;
84  n->param("/tcp_no_delay", no_delay, false);
85  subscriber_ = n->subscribe(topic,qsize,&FeedWithCallback<T,TMSG,CONVERTER>::receive,this,ros::TransportHints().tcpNoDelay(no_delay));
86  has_fcn_ = false;
87  }
88  virtual void setCallback(std::function<void()> fcn) override
89  {
90  fcn_ = fcn;
91  has_fcn_ = true;
92  }
93  virtual bool hasNext() const override
94  {
95  return data_.size()>0;
96  }
97  virtual void getNext(T& value) override
98  {
99  value = data_.front();
100  data_.pop_front();
101  }
102  virtual void getLatest(T& value) override
103  {
104  value = data_.back();
105  data_.clear();
106  }
107  };
111  template<class T,class TMSG,class CONVERTER>
112  class Reader:public adore::mad::AReader<T>
113  {
114  private:
116  bool changed_;
117  T data_;
118  ros::Subscriber subscriber_;
119  void receive(TMSG msg)
120  {
121  CONVERTER()(msg,&data_);
122  initialized_=true;
123  changed_=true;
124  }
125  public:
126  Reader(ros::NodeHandle* n,const std::string& topic,int qsize)
127  :initialized_(false),changed_(false)
128  {
129  bool no_delay;
130  n->param("/tcp_no_delay", no_delay, false);
131  subscriber_ = n->subscribe(topic,qsize,&Reader<T,TMSG,CONVERTER>::receive,this,ros::TransportHints().tcpNoDelay(no_delay));
132  }
133  virtual bool hasData() const override
134  {
135  return initialized_;
136  }
137  virtual bool hasUpdate() const override
138  {
139  return changed_;
140  }
141  virtual void getData(T& value) override
142  {
143  value = data_;
144  changed_ = false;
145  }
146  };
147 
151  template<class T,class TMSG,class CONVERTER>
152  class Writer:public adore::mad::AWriter<T>
153  {
154  private:
155  ros::Publisher publisher_;
156  public:
157  Writer(ros::NodeHandle* n,const std::string& topic,int qsize)
158  {
159  publisher_ = n->advertise<TMSG>(topic,qsize);
160  }
161  virtual bool canWriteMore() const override
162  {
163  return true;//@TODO: check if this is a correct assumption
164  }
165  virtual void write(const T& value)
166  {
167  publisher_.publish(CONVERTER()(value));
168  }
169  virtual uint32_t getNumberOfSubscribers() const override
170  {
171  return publisher_.getNumSubscribers();
172  }
173  };
174 
178  class ROSParam
179  {
180  protected:
181  std::string prefix_;
182  ros::NodeHandle n_;
183  ROSParam(ros::NodeHandle n, std::string prefix):prefix_(prefix),n_(n){}
184  public:
185  template<typename T>
186  void get(const std::string & name,T& result)const
187  {
188  if(!n_.getParamCached(name,result))
189  {
190  ROS_INFO_STREAM("No parameter named " << name << " in launch-file. Hardcoded default value used.");
191  }
192  }
193  };
194  }
195 }
Definition: ros_com_patterns.h:67
virtual void getLatest(T &value) override
Definition: ros_com_patterns.h:102
virtual void getNext(T &value) override
Definition: ros_com_patterns.h:97
std::list< T > data_
Definition: ros_com_patterns.h:69
std::function< void()> fcn_
Definition: ros_com_patterns.h:72
virtual void setCallback(std::function< void()> fcn) override
Definition: ros_com_patterns.h:88
ros::Subscriber subscriber_
Definition: ros_com_patterns.h:70
FeedWithCallback(ros::NodeHandle *n, const std::string &topic, int qsize)
Definition: ros_com_patterns.h:81
CONVERTER converter_
Definition: ros_com_patterns.h:71
bool has_fcn_
Definition: ros_com_patterns.h:73
void receive(TMSG msg)
Definition: ros_com_patterns.h:75
virtual bool hasNext() const override
Definition: ros_com_patterns.h:93
Definition: ros_com_patterns.h:32
virtual void getLatest(T &value) override
Definition: ros_com_patterns.h:58
CONVERTER converter_
Definition: ros_com_patterns.h:36
void receive(TMSG msg)
Definition: ros_com_patterns.h:38
virtual void getNext(T &value) override
Definition: ros_com_patterns.h:53
virtual bool hasNext() const override
Definition: ros_com_patterns.h:49
std::list< T > data_
Definition: ros_com_patterns.h:34
ros::Subscriber subscriber_
Definition: ros_com_patterns.h:35
Feed(ros::NodeHandle *n, const std::string &topic, int qsize)
Definition: ros_com_patterns.h:43
Definition: ros_com_patterns.h:179
ros::NodeHandle n_
Definition: ros_com_patterns.h:182
std::string prefix_
Definition: ros_com_patterns.h:181
void get(const std::string &name, T &result) const
Definition: ros_com_patterns.h:186
ROSParam(ros::NodeHandle n, std::string prefix)
Definition: ros_com_patterns.h:183
Definition: ros_com_patterns.h:113
virtual bool hasUpdate() const override
Definition: ros_com_patterns.h:137
bool changed_
Definition: ros_com_patterns.h:116
Reader(ros::NodeHandle *n, const std::string &topic, int qsize)
Definition: ros_com_patterns.h:126
virtual bool hasData() const override
Definition: ros_com_patterns.h:133
ros::Subscriber subscriber_
Definition: ros_com_patterns.h:118
void receive(TMSG msg)
Definition: ros_com_patterns.h:119
T data_
Definition: ros_com_patterns.h:117
virtual void getData(T &value) override
Definition: ros_com_patterns.h:141
bool initialized_
Definition: ros_com_patterns.h:115
Definition: ros_com_patterns.h:153
virtual bool canWriteMore() const override
Definition: ros_com_patterns.h:161
virtual void write(const T &value)
Definition: ros_com_patterns.h:165
Writer(ros::NodeHandle *n, const std::string &topic, int qsize)
Definition: ros_com_patterns.h:157
ros::Publisher publisher_
Definition: ros_com_patterns.h:155
virtual uint32_t getNumberOfSubscribers() const override
Definition: ros_com_patterns.h:169
Definition: com_patterns.h:46
Definition: com_patterns.h:29
Definition: com_patterns.h:68
Definition: com_patterns.h:97
Definition: areaofeffectconverter.h:20