Exotica
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 
36 #include <moveit/robot_model_loader/robot_model_loader.h>
37 #include <ros/ros.h>
38 #include <tf/transform_broadcaster.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_;
56  ros::AsyncSpinner sp_;
57  tf::TransformBroadcaster tf_;
58 };
59 
60 // Implementation of EXOTica Server class
61 class Server : public Uncopyable
62 {
63 public:
65  static std::shared_ptr<Server> Instance()
66  {
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_
exotica::Server::GetName
std::string GetName()
Get the name of ther server.
exotica::RosNode::nh_
std::shared_ptr< ros::NodeHandle > nh_
Definition: server.h:55
exotica::Uncopyable
Definition: uncopyable.h:35
exotica::Server::name_
std::string name_
The name of this server.
Definition: server.h:180
exotica::Server::GetParam
static bool GetParam(const std::string &name, T &param)
Definition: server.h:104
exotica::RosNode::RosNode
RosNode()=delete
exotica::Server::operator=
void operator=(Server const &)=delete
exotica::Server::Instance
static std::shared_ptr< Server > Instance()
Get the server.
Definition: server.h:65
exotica::Server::SendTransform
static void SendTransform(const tf::StampedTransform &transform)
Definition: server.h:145
exotica::Server::Server
Server()
exotica
Definition: cartpole_dynamics_solver.h:38
exotica::Server::~Server
virtual ~Server()
exotica::Server::Subscribe
static ros::Subscriber Subscribe(Args &&... args)
Definition: server.h:134
exotica::Server::SendTransform
static void SendTransform(const std::vector< geometry_msgs::TransformStamped > &transforms)
Definition: server.h:163
exotica::Server::HasModel
bool HasModel(const std::string &path)
Check if a robot model exist.
exotica::Server::robot_models_
std::map< std::string, robot_model::RobotModelPtr > robot_models_
Robot model cache.
Definition: server.h:185
exotica::Server
Definition: server.h:61
exotica::Server::InitRos
static void InitRos(std::shared_ptr< ros::NodeHandle > nh, int numThreads=2)
Definition: server.h:91
exotica::Server::GetModel
void GetModel(const std::string &path, robot_model::RobotModelPtr &model, const std::string &urdf="", const std::string &srdf="")
Get robot model.
exotica::Server::HasParam
static bool HasParam(const std::string &name)
Definition: server.h:115
exotica::Server::ServiceClient
static ros::ServiceClient ServiceClient(const std::string &service_name, bool persistent=false)
Definition: server.h:140
exotica::RosNode
Definition: server.h:45
exotica::Server::node_
std::shared_ptr< RosNode > node_
Definition: server.h:182
exotica::Server::SetParam
static void SetParam(const std::string &name, T &param)
Definition: server.h:110
exotica::Server::SendTransform
static void SendTransform(const std::vector< tf::StampedTransform > &transforms)
Definition: server.h:151
exotica::RosNode::GetNodeHandle
ros::NodeHandle & GetNodeHandle()
Definition: server.h:51
exotica::ServerPtr
std::shared_ptr< Server > ServerPtr
Definition: server.h:188
ThrowPretty
#define ThrowPretty(m)
Definition: exception.h:36
exotica::RosNode::sp_
ros::AsyncSpinner sp_
Definition: server.h:56
exotica::Server::Destroy
static void Destroy()
exotica::Server::LoadModel
robot_model::RobotModelPtr LoadModel(const std::string &name, const std::string &urdf="", const std::string &srdf="")
exotica::RosNode::GetTF
tf::TransformBroadcaster & GetTF()
Definition: server.h:52
exotica::Server::Advertise
static ros::Publisher Advertise(Args &&... args)
Definition: server.h:128
exotica::RosNode::tf_
tf::TransformBroadcaster tf_
Definition: server.h:57
exception.h
uncopyable.h
exotica::Server::IsRos
static bool IsRos()
Definition: server.h:96
exotica::RosNode::~RosNode
~RosNode()
exotica::Server::singleton_server_
static std::shared_ptr< Server > singleton_server_
Definition: server.h:173
exotica::Server::GetNodeHandle
static ros::NodeHandle & GetNodeHandle()
Definition: server.h:97
exotica::Server::SendTransform
static void SendTransform(const geometry_msgs::TransformStamped &transform)
Definition: server.h:157