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 {
64  rdf_loader::RDFLoader loader(urdf, srdf);
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  {
104  model = LoadModelImpl(LoadFile(urdf), LoadFile(srdf));
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
bool PathExists(const std::string &path)
Definition: tools.cpp:199
std::string GetName()
Get the name of ther server.
Definition: server.cpp:157
const srdf::ModelSharedPtr & getSRDF() const
static void Destroy()
Definition: server.cpp:57
#define ThrowPretty(m)
Definition: exception.h:36
bool HasModel(const std::string &path)
Check if a robot model exist.
Definition: server.cpp:152
std::string LoadFile(const std::string &path)
Definition: tools.cpp:184
static bool GetParam(const std::string &name, T &param)
Definition: server.h:104
std::map< std::string, robot_model::RobotModelPtr > robot_models_
Robot model cache.
Definition: server.h:185
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
static bool HasParam(const std::string &name)
Definition: server.h:115
static std::shared_ptr< Server > singleton_server_
Definition: server.h:173
ros::AsyncSpinner sp_
Definition: server.h:56
robot_model::RobotModelPtr LoadModelImpl(const std::string &urdf, const std::string &srdf)
Definition: server.cpp:62
#define ROS_INFO_STREAM(args)
virtual ~Server()
Definition: server.cpp:53
std::shared_ptr< Server > ServerPtr
Definition: server.h:188
robot_model::RobotModelPtr LoadModel(const std::string &name, const std::string &urdf="", const std::string &srdf="")
Definition: server.cpp:80
static bool IsRos()
Definition: server.h:96
const urdf::ModelInterfaceSharedPtr & getURDF() const
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