server.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_SERVER_H_
31 #define EXOTICA_CORE_SERVER_H_
32 
33 #include <map>
34 #include <vector>
35 
37 #include <ros/ros.h>
39 
42 
43 namespace exotica
44 {
45 class RosNode
46 {
47 public:
48  RosNode() = delete;
49  RosNode(std::shared_ptr<ros::NodeHandle> nh, int numThreads = 2);
50  ~RosNode();
51  inline ros::NodeHandle &GetNodeHandle() { return *nh_; }
52  inline tf::TransformBroadcaster &GetTF() { return tf_; }
53 
54 protected:
55  std::shared_ptr<ros::NodeHandle> nh_;
58 };
59 
60 // Implementation of EXOTica Server class
61 class Server : public Uncopyable
62 {
63 public:
65  static std::shared_ptr<Server> Instance()
66  {
67  if (!singleton_server_) singleton_server_.reset(new Server);
68  return singleton_server_;
69  }
70  virtual ~Server();
71 
75  bool HasModel(const std::string &path);
76 
80  void GetModel(const std::string &path, robot_model::RobotModelPtr &model, const std::string &urdf = "", const std::string &srdf = "");
81 
85  robot_model::RobotModelConstPtr GetModel(const std::string &path, const std::string &urdf = "", const std::string &srdf = "");
86 
89  std::string GetName();
90 
91  inline static void InitRos(std::shared_ptr<ros::NodeHandle> nh, int numThreads = 2)
92  {
93  Instance()->node_.reset(new RosNode(nh, numThreads));
94  }
95 
96  inline static bool IsRos() { return Instance()->node_ != nullptr; }
97  inline static ros::NodeHandle &GetNodeHandle()
98  {
99  if (!IsRos()) ThrowPretty("EXOTica server not initialized as ROS node!");
100  return Instance()->node_->GetNodeHandle();
101  }
102 
103  template <typename T>
104  static bool GetParam(const std::string &name, T &param)
105  {
106  return Instance()->GetNodeHandle().getParam(name, param);
107  }
108 
109  template <typename T>
110  static void SetParam(const std::string &name, T &param)
111  {
112  Instance()->GetNodeHandle().setParam(name, param);
113  }
114 
115  inline bool static HasParam(const std::string &name)
116  {
117  if (IsRos())
118  {
119  return Instance()->GetNodeHandle().hasParam(name);
120  }
121  else
122  {
123  return false;
124  }
125  }
126 
127  template <typename T, typename... Args>
128  static ros::Publisher Advertise(Args &&... args)
129  {
130  return Instance()->GetNodeHandle().advertise<T>(std::forward<Args>(args)...);
131  }
132 
133  template <typename T, typename... Args>
134  static ros::Subscriber Subscribe(Args &&... args)
135  {
136  return Instance()->GetNodeHandle().subscribe<T>(std::forward<Args>(args)...);
137  }
138 
139  template <typename T>
140  static ros::ServiceClient ServiceClient(const std::string &service_name, bool persistent = false)
141  {
142  return Instance()->GetNodeHandle().serviceClient<T>(service_name, persistent);
143  }
144 
145  static void SendTransform(const tf::StampedTransform &transform)
146  {
147  if (!IsRos()) ThrowPretty("EXOTica server not initialized as ROS node!");
148  Instance()->node_->GetTF().sendTransform(transform);
149  }
150 
151  static void SendTransform(const std::vector<tf::StampedTransform> &transforms)
152  {
153  if (!IsRos()) ThrowPretty("EXOTica server not initialized as ROS node!");
154  Instance()->node_->GetTF().sendTransform(transforms);
155  }
156 
157  static void SendTransform(const geometry_msgs::TransformStamped &transform)
158  {
159  if (!IsRos()) ThrowPretty("EXOTica server not initialized as ROS node!");
160  Instance()->node_->GetTF().sendTransform(transform);
161  }
162 
163  static void SendTransform(const std::vector<geometry_msgs::TransformStamped> &transforms)
164  {
165  if (!IsRos()) ThrowPretty("EXOTica server not initialized as ROS node!");
166  Instance()->node_->GetTF().sendTransform(transforms);
167  }
168 
169  static void Destroy();
170 
171 private:
172  Server();
173  static std::shared_ptr<Server> singleton_server_;
175  Server(Server const &) = delete;
176  void operator=(Server const &) = delete;
177  robot_model::RobotModelPtr LoadModel(const std::string &name, const std::string &urdf = "", const std::string &srdf = "");
178 
180  std::string name_;
181 
182  std::shared_ptr<RosNode> node_;
183 
185  std::map<std::string, robot_model::RobotModelPtr> robot_models_;
186 };
187 
188 typedef std::shared_ptr<Server> ServerPtr;
189 } // namespace exotica
190 
191 #endif // EXOTICA_CORE_SERVER_H_
tf::TransformBroadcaster tf_
Definition: server.h:57
static void SendTransform(const geometry_msgs::TransformStamped &transform)
Definition: server.h:157
#define ThrowPretty(m)
Definition: exception.h:36
static bool GetParam(const std::string &name, T &param)
Definition: server.h:104
std::shared_ptr< ros::NodeHandle > nh_
Definition: server.h:55
static std::shared_ptr< Server > Instance()
Get the server.
Definition: server.h:65
static void InitRos(std::shared_ptr< ros::NodeHandle > nh, int numThreads=2)
Definition: server.h:91
std::map< std::string, robot_model::RobotModelPtr > robot_models_
Robot model cache.
Definition: server.h:185
static void SendTransform(const tf::StampedTransform &transform)
Definition: server.h:145
static void SendTransform(const std::vector< geometry_msgs::TransformStamped > &transforms)
Definition: server.h:163
static bool HasParam(const std::string &name)
Definition: server.h:115
static ros::Publisher Advertise(Args &&...args)
Definition: server.h:128
std::shared_ptr< RosNode > node_
Definition: server.h:182
static std::shared_ptr< Server > singleton_server_
Definition: server.h:173
static ros::ServiceClient ServiceClient(const std::string &service_name, bool persistent=false)
Definition: server.h:140
static void SendTransform(const std::vector< tf::StampedTransform > &transforms)
Definition: server.h:151
ros::AsyncSpinner sp_
Definition: server.h:56
static void SetParam(const std::string &name, T &param)
Definition: server.h:110
static ros::Subscriber Subscribe(Args &&...args)
Definition: server.h:134
std::shared_ptr< Server > ServerPtr
Definition: server.h:188
static bool IsRos()
Definition: server.h:96
ros::NodeHandle & GetNodeHandle()
Definition: server.h:51
static ros::NodeHandle & GetNodeHandle()
Definition: server.h:97
tf::TransformBroadcaster & GetTF()
Definition: server.h:52
std::string name_
The name of this server.
Definition: server.h:180


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49