ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
baseapp.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2023 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  * Matthias Nichting - initial API and implementation
13  * Thomas Lobig - initial API and implementation
14  ********************************************************************************/
15 
17 
18 #include <string>
19 #include <vector>
20 
22 {
23 
29  class Baseapp
30  {
31  public:
32  Baseapp() {}
33 
34  private:
35  inline static ros::NodeHandle *m_pN = 0;
36  std::vector<ros::Timer> timers_; // functions that are periodically called
38  0; // object to coordinate communication with the scheduler
39  bool useScheduler_; // true, if scheduler is used
40  double rate_; // main rate of calling the functions in timers_ are called
41 
42  public:
47  inline static void schedulerCallback(const ros::TimerEvent &e)
48  {
49  snm_->notifyScheduler(ros::Time::now().sec, ros::Time::now().nsec);
50  }
55  void init(int argc, char **argv, double rate, std::string nodename)
56  {
57  rate_ = rate;
58  ros::init(argc, argv, nodename);
59  ros::NodeHandle *n = new ros::NodeHandle();
60  m_pN = n;
62  {
63  ros::NodeHandle np("~");
64  np.getParam("rate", rate_);
65  }
66  if (rate == 0.0) useScheduler_ = false;
67  }
72  void initSim()
73  {
74  if (useScheduler_)
75  {
77  m_pN, std::hash<std::string>{}(ros::this_node::getNamespace() + ros::this_node::getName()),
78  (uint32_t)(1e9 / rate_));
79  timers_.push_back(m_pN->createTimer(ros::Duration(1 / rate_), schedulerCallback));
80  snm_->publishClientName(ros::this_node::getName());
81  }
82  }
86  static ros::NodeHandle *getRosNodeHandle() { return m_pN; }
90  virtual void resume() { snm_->resume(); }
94  virtual void pause() { snm_->pause(); }
98  virtual void run()
99  {
100  while (m_pN->ok())
101  {
102  ros::spin();
103  }
104  }
105  inline static void func(std::function<void()> &callback, const ros::TimerEvent &te) { callback(); }
109  virtual void addTimerCallback(std::function<void()> &callbackFcn, double rate_factor = 1.0)
110  {
111  timers_.push_back(m_pN->createTimer(ros::Duration(1 / rate_ / rate_factor),
112  std::bind(&func, callbackFcn, std::placeholders::_1)));
113  }
114 
118  template <typename T>
119  bool getParam(const std::string name, T &val)
120  {
121  if (name.compare(0, 1, "~") == 0)
122  {
123  ros::NodeHandle np("~");
124  return np.getParam(name.substr(1), val);
125  }
126  return m_pN->getParam(name, val);
127  }
131  template <typename T>
132  bool getParam(const std::string name, T &val, const T &default_val)
133  {
134  return m_pN->param<T>(name, val, default_val);
135  }
136  };
137 } // namespace adore_if_ros_scheduling
Definition: baseapp.h:30
virtual void addTimerCallback(std::function< void()> &callbackFcn, double rate_factor=1.0)
Definition: baseapp.h:109
void initSim()
Definition: baseapp.h:72
virtual void resume()
Definition: baseapp.h:90
virtual void run()
Definition: baseapp.h:98
bool getParam(const std::string name, T &val, const T &default_val)
Definition: baseapp.h:132
static adore_if_ros_scheduling::SchedulerNotificationManager * snm_
Definition: baseapp.h:37
bool getParam(const std::string name, T &val)
Definition: baseapp.h:119
double rate_
Definition: baseapp.h:40
static ros::NodeHandle * getRosNodeHandle()
Definition: baseapp.h:86
bool useScheduler_
Definition: baseapp.h:39
virtual void pause()
Definition: baseapp.h:94
std::vector< ros::Timer > timers_
Definition: baseapp.h:36
static void func(std::function< void()> &callback, const ros::TimerEvent &te)
Definition: baseapp.h:105
static void schedulerCallback(const ros::TimerEvent &e)
Definition: baseapp.h:47
Baseapp()
Definition: baseapp.h:32
void init(int argc, char **argv, double rate, std::string nodename)
Definition: baseapp.h:55
static ros::NodeHandle * m_pN
Definition: baseapp.h:35
Definition: schedulernotificationmanager.h:32
void resume() override
Definition: schedulernotificationmanager.h:75
void publishClientName(std::string name) override
Definition: schedulernotificationmanager.h:55
void notifyScheduler(uint32_t sec, uint32_t nsec) override
Definition: schedulernotificationmanager.h:66
void pause() override
Definition: schedulernotificationmanager.h:74
Definition: adore_if_ros_scheduling_constants.h:19
const char PARAM_NAME_USE_SCHEDULER[]
Definition: adore_if_ros_scheduling_constants.h:25