server.cpp
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 #include <boost/any.hpp>
31 #include <typeinfo>
32 
33 #include <exotica_core/server.h>
34 #include <exotica_core/tools.h>
35 
37 namespace exotica
38 {
39 RosNode::RosNode(std::shared_ptr<ros::NodeHandle> nh, int numThreads) : nh_(nh), sp_(numThreads)
40 {
41  sp_.start();
42 }
43 
45 {
46  sp_.stop();
47 }
48 
49 Server::Server() : name_("EXOTicaServer"), node_(nullptr)
50 {
51 }
52 
54 {
55 }
56 
58 {
60 }
61 
62 robot_model::RobotModelPtr LoadModelImpl(const std::string& urdf, const std::string& srdf)
63 {
65 #if ROS_VERSION_MINIMUM(1, 14, 0) // if ROS version >= ROS_MELODIC
66  const std::shared_ptr<srdf::Model>& srdf_ = loader.getSRDF() ? loader.getSRDF() : std::shared_ptr<srdf::Model>(new srdf::Model());
67 #else
69 #endif
70  if (loader.getURDF())
71  {
72  return robot_model::RobotModelPtr(new robot_model::RobotModel(loader.getURDF(), srdf_));
73  }
74  else
75  {
76  ThrowPretty("Can't load robot model from URDF!");
77  }
78 }
79 
80 robot_model::RobotModelPtr Server::LoadModel(const std::string& name, const std::string& urdf, const std::string& srdf)
81 {
82  robot_model::RobotModelPtr model;
83  if (HasParam("RobotDescription"))
84  {
85  std::string robot_description_param;
86  GetParam("RobotDescription", robot_description_param);
87  ROS_INFO_STREAM("Using robot_description at " << robot_description_param);
88  model = robot_model_loader::RobotModelLoader(robot_description_param, false).getModel();
89  }
90  else if (HasParam(GetName() + "/RobotDescription"))
91  {
92  std::string robot_description_param;
93  GetParam(GetName() + "/RobotDescription", robot_description_param);
94  ROS_INFO_STREAM("Using robot_description at " << robot_description_param);
95  model = robot_model_loader::RobotModelLoader(robot_description_param, false).getModel();
96  }
97  else if ((urdf == "" && srdf == "") && IsRos())
98  {
99  model = robot_model_loader::RobotModelLoader(name, false).getModel();
100  }
101  // URDF and SRDF are meant to be read from files
102  else if (PathExists(urdf) && PathExists(srdf))
103  {
105  }
106  // URDF loaded from file, SRDF empty
107  else if (PathExists(urdf) && srdf == "")
108  {
109  model = LoadModelImpl(LoadFile(urdf), srdf);
110  }
111  // URDF and SRDF are passed in as strings
112  else if (urdf != "" && srdf != "")
113  {
114  model = LoadModelImpl(urdf, srdf);
115  }
116 
117  if (model)
118  {
119  robot_models_[name] = model;
120  }
121  else
122  {
123  ThrowPretty("Couldn't load the model at path " << name << "!");
124  }
125  return model;
126 }
127 
128 void Server::GetModel(const std::string& path, robot_model::RobotModelPtr& model, const std::string& urdf, const std::string& srdf)
129 {
130  if (robot_models_.find(path) != robot_models_.end())
131  {
132  model = robot_models_[path];
133  }
134  else
135  {
136  model = LoadModel(path, urdf, srdf);
137  }
138 }
139 
140 robot_model::RobotModelConstPtr Server::GetModel(const std::string& path, const std::string& urdf, const std::string& srdf)
141 {
142  if (robot_models_.count(path))
143  {
144  return robot_models_[path];
145  }
146  else
147  {
148  return LoadModel(path, urdf, srdf);
149  }
150 }
151 
152 bool Server::HasModel(const std::string& path)
153 {
154  return robot_models_.find(path) != robot_models_.end();
155 }
156 
157 std::string Server::GetName()
158 {
159  return name_;
160 }
161 } // namespace exotica
exotica::Server::GetName
std::string GetName()
Get the name of ther server.
Definition: server.cpp:157
boost::shared_ptr
exotica::Server::name_
std::string name_
The name of this server.
Definition: server.h:180
ros::AsyncSpinner::start
void start()
exotica::Server::GetParam
static bool GetParam(const std::string &name, T &param)
Definition: server.h:104
exotica::RosNode::RosNode
RosNode()=delete
exotica::Server::Destroy
static void Destroy()
Definition: server.cpp:57
exotica::Server::Server
Server()
Definition: server.cpp:49
exotica
Definition: collision_scene.h:46
rdf_loader::RDFLoader::getSRDF
const srdf::ModelSharedPtr & getSRDF() const
exotica::Server::singleton_server_
static std::shared_ptr< Server > singleton_server_
Definition: server.h:173
robot_model_loader::RobotModelLoader
exotica::Server::HasModel
bool HasModel(const std::string &path)
Check if a robot model exist.
Definition: server.cpp:152
name
std::string name
exotica::LoadFile
std::string LoadFile(const std::string &path)
Definition: tools.cpp:189
exotica::Server::robot_models_
std::map< std::string, robot_model::RobotModelPtr > robot_models_
Robot model cache.
Definition: server.h:185
exotica::Server::GetModel
void GetModel(const std::string &path, robot_model::RobotModelPtr &model, const std::string &urdf="", const std::string &srdf="")
Get robot model.
Definition: server.cpp:128
exotica::Server::HasParam
static bool HasParam(const std::string &name)
Definition: server.h:115
rdf_loader::RDFLoader
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
exotica::LoadModelImpl
robot_model::RobotModelPtr LoadModelImpl(const std::string &urdf, const std::string &srdf)
Definition: server.cpp:62
ros::AsyncSpinner::stop
void stop()
rdf_loader::RDFLoader::getURDF
const urdf::ModelInterfaceSharedPtr & getURDF() const
exotica::Server::~Server
virtual ~Server()
Definition: server.cpp:53
exotica::ServerPtr
std::shared_ptr< Server > ServerPtr
Definition: server.h:188
urdf
ThrowPretty
#define ThrowPretty(m)
Definition: exception.h:36
exotica::RosNode::sp_
ros::AsyncSpinner sp_
Definition: server.h:56
srdf
exotica::Server::LoadModel
robot_model::RobotModelPtr LoadModel(const std::string &name, const std::string &urdf="", const std::string &srdf="")
Definition: server.cpp:80
exotica::PathExists
bool PathExists(const std::string &path)
Definition: tools.cpp:204
tools.h
server.h
srdf::Model
exotica::Server::IsRos
static bool IsRos()
Definition: server.h:96
exotica::RosNode::~RosNode
~RosNode()
Definition: server.cpp:44


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Fri Aug 2 2024 08:43:02