speed_observer_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <ros/ros.h>
19 #include <urdf/model.h>
20 
21 #include <pilz_utils/get_param.h>
23 
24 using namespace prbt_hardware_support;
25 
26 static const std::string ADDITIONAL_FRAMES_PARAM_NAME{ "additional_frames" };
27 static const std::string ROBOT_DESCRIPTION_PARAM_NAME{ "robot_description" };
28 static const std::string SET_SPEED_LIMIT_SERVICE{ "set_speed_limit" };
29 static const std::string OBSERVATION_FREQUENCY_PARAM_NAME{ "observation_frequency" };
30 
31 bool hasOnlyFixedParentJoints(const urdf::LinkSharedPtr &link)
32 {
33  auto parent_link {link};
34  while ( (parent_link->parent_joint != nullptr) && (parent_link->parent_joint->type == urdf::Joint::FIXED) )
35  {
36  parent_link = parent_link->getParent();
37  }
38  return parent_link->parent_joint == nullptr;
39 }
40 
45 int main(int argc, char** argv)
46 {
47  ros::init(argc, argv, "speed_observer");
48  ros::NodeHandle pnh{ "~" };
49  ros::NodeHandle nh;
50 
51  urdf::Model model;
53  ROS_DEBUG_STREAM("reference_frame: " << model.getRoot()->name);
54  std::string reference_frame = model.getRoot()->name;
55  std::vector<urdf::LinkSharedPtr> links;
56  std::vector<std::string> frames_to_observe;
57  model.getLinks(links);
58  ROS_DEBUG_STREAM("Received the following frames to observer from urdf:");
59  for (const auto& link : links)
60  {
61  // exclude frames which cannot move
62  if (!hasOnlyFixedParentJoints(link))
63  {
64  ROS_DEBUG_STREAM(" - " << link->name);
65  frames_to_observe.push_back(link->name);
66  }
67  }
68  std::vector<std::string> additional_frames;
69  pnh.getParam(ADDITIONAL_FRAMES_PARAM_NAME, additional_frames);
70  ROS_DEBUG_STREAM("Additional frames defined by user:");
71  for (const auto& frame : additional_frames)
72  {
73  ROS_DEBUG_STREAM(" - " << frame);
74  }
75  frames_to_observe.insert(frames_to_observe.end(), additional_frames.begin(), additional_frames.end());
76 
77  double frequency;
78  // LCOV_EXCL_START Simple parameter reading not analyzed
79  try
80  {
81  frequency = pilz_utils::getParam<double>(pnh, OBSERVATION_FREQUENCY_PARAM_NAME);
82  }
83  catch(const std::runtime_error& ex)
84  {
85  ROS_ERROR_STREAM(ex.what());
86  return EXIT_FAILURE;
87  }
88  // LCOV_EXCL_STOP
89 
90  SpeedObserver observer(nh, reference_frame, frames_to_observe);
91  ros::ServiceServer set_speed_limit_server =
93  observer.startObserving(frequency);
94 
95  return EXIT_SUCCESS;
96 }
static const std::string ADDITIONAL_FRAMES_PARAM_NAME
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
static const std::string OBSERVATION_FREQUENCY_PARAM_NAME
bool setSpeedLimitCb(SetSpeedLimit::Request &req, SetSpeedLimit::Response &res)
Callback for service to set the currently active speed limit.
URDF_EXPORT bool initParam(const std::string &param)
#define ROS_DEBUG_STREAM(args)
bool hasOnlyFixedParentJoints(const urdf::LinkSharedPtr &link)
#define ROS_ERROR_STREAM(args)
int main(int argc, char **argv)
Read requested parameters, start and initialize the prbt_hardware_support::SpeedObserver.
static const std::string SET_SPEED_LIMIT_SERVICE
void startObserving(const double frequency, const unsigned int allowed_missed_calculations=DEFAULT_ALLOWED_MISSED_CALCULATIONS)
Starts the observation cycle. The function blocks until ros shuts down.
static const std::string ROBOT_DESCRIPTION_PARAM_NAME


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17