speed_scaling_state_controller.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2019 FZI Forschungszentrum Informatik
5 // Created on behalf of Universal Robots A/S
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
30 
32 
33 namespace scaled_controllers
34 {
36  ros::NodeHandle& controller_nh)
37 {
38  // get all joint states from the hardware interface
39  const std::vector<std::string>& sensor_names = hw->getNames();
40  for (unsigned i = 0; i < sensor_names.size(); i++)
41  ROS_DEBUG("Got sensor %s", sensor_names[i].c_str());
42 
43  // get publishing period
44  if (!controller_nh.getParam("publish_rate", publish_rate_))
45  {
46  ROS_ERROR("Parameter 'publish_rate' not set");
47  return false;
48  }
49 
50  for (unsigned i = 0; i < sensor_names.size(); i++)
51  {
52  // sensor handle
53  sensors_.push_back(hw->getHandle(sensor_names[i]));
54 
55  // realtime publisher
56  RtPublisherPtr rt_pub(new realtime_tools::RealtimePublisher<std_msgs::Float64>(root_nh, sensor_names[i], 4));
57  realtime_pubs_.push_back(rt_pub);
58  }
59 
60  // Last published times
61  last_publish_times_.resize(sensor_names.size());
62  return true;
63 }
64 
66 {
67  // initialize time
68  for (unsigned i = 0; i < last_publish_times_.size(); i++)
69  {
70  last_publish_times_[i] = time;
71  }
72 }
73 
74 void SpeedScalingStateController::update(const ros::Time& time, const ros::Duration& /*period*/)
75 {
76  // limit rate of publishing
77  for (unsigned i = 0; i < realtime_pubs_.size(); i++)
78  {
79  if (publish_rate_ > 0.0 && last_publish_times_[i] + ros::Duration(1.0 / publish_rate_) < time)
80  {
81  // try to publish
82  if (realtime_pubs_[i]->trylock())
83  {
84  // we're actually publishing, so increment time
86 
87  // populate message
88  // realtime_pubs_[i]->msg_.header.stamp = time;
89  realtime_pubs_[i]->msg_.data = *sensors_[i].getScalingFactor();
90 
91  realtime_pubs_[i]->unlockAndPublish();
92  }
93  }
94  }
95 }
96 
98 {
99 }
100 } // namespace scaled_controllers
101 
virtual void starting(const ros::Time &time) override
std::vector< scaled_controllers::SpeedScalingHandle > sensors_
virtual void update(const ros::Time &time, const ros::Duration &) override
bool getParam(const std::string &key, std::string &s) const
std::shared_ptr< realtime_tools::RealtimePublisher< std_msgs::Float64 > > RtPublisherPtr
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)
virtual bool init(scaled_controllers::SpeedScalingInterface *hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh) override
#define ROS_DEBUG(...)


speed_scaling_state_controller
Author(s):
autogenerated on Wed May 18 2022 02:43:20