twist_mux.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (CC BY-NC-SA 4.0 License)
3  *
4  * Copyright (c) 2014, PAL Robotics, S.L.
5  * All rights reserved.
6  *
7  * This work is licensed under the Creative Commons
8  * Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  * To view a copy of this license, visit
11  * http://creativecommons.org/licenses/by-nc-sa/4.0/
12  * or send a letter to
13  * Creative Commons, 444 Castro Street, Suite 900,
14  * Mountain View, California, 94041, USA.
15  *********************************************************************/
16 
17 /*
18  * @author Enrique Fernandez
19  * @author Siegfried Gevatter
20  */
21 
22 #include <twist_mux/twist_mux.h>
23 #include <twist_mux/topic_handle.h>
26 #include <twist_mux/utils.h>
28 
36 bool hasIncreasedAbsVelocity(const geometry_msgs::Twist& old_twist, const geometry_msgs::Twist& new_twist)
37 {
38  const auto old_linear_x = std::abs(old_twist.linear.x);
39  const auto new_linear_x = std::abs(new_twist.linear.x);
40 
41  const auto old_angular_z = std::abs(old_twist.angular.z);
42  const auto new_angular_z = std::abs(new_twist.angular.z);
43 
44  return (old_linear_x < new_linear_x ) ||
45  (old_angular_z < new_angular_z);
46 }
47 
48 namespace twist_mux
49 {
50 
51 TwistMux::TwistMux(int window_size)
52 {
53  ros::NodeHandle nh;
54  ros::NodeHandle nh_priv("~");
55 
57  velocity_hs_ = boost::make_shared<velocity_topic_container>();
58  lock_hs_ = boost::make_shared<lock_topic_container>();
59  getTopicHandles(nh, nh_priv, "topics", *velocity_hs_);
60  getTopicHandles(nh, nh_priv, "locks" , *lock_hs_ );
61 
63  cmd_pub_ = nh.advertise<geometry_msgs::Twist>("cmd_vel_out", 1);
64 
66  diagnostics_ = boost::make_shared<diagnostics_type>();
67  status_ = boost::make_shared<status_type>();
68  status_->velocity_hs = velocity_hs_;
69  status_->lock_hs = lock_hs_;
70 
72 }
73 
75 {}
76 
78 {
79  status_->priority = getLockPriority();
80  diagnostics_->updateStatus(status_);
81 }
82 
83 void TwistMux::publishTwist(const geometry_msgs::TwistConstPtr& msg)
84 {
85  cmd_pub_.publish(*msg);
86 }
87 
88 template<typename T>
89 void TwistMux::getTopicHandles(ros::NodeHandle& nh, ros::NodeHandle& nh_priv, const std::string& param_name, std::list<T>& topic_hs)
90 {
91  try
92  {
93  xh::Array output;
94  xh::fetchParam(nh_priv, param_name, output);
95 
96  xh::Struct output_i;
97  std::string name, topic;
98  double timeout;
99  int priority;
100  for (int i = 0; i < output.size(); ++i)
101  {
102  xh::getArrayItem(output, i, output_i);
103 
104  xh::getStructMember(output_i, "name" , name );
105  xh::getStructMember(output_i, "topic" , topic );
106  xh::getStructMember(output_i, "timeout" , timeout );
107  xh::getStructMember(output_i, "priority", priority);
108 
109  topic_hs.emplace_back(nh, name, topic, timeout, priority, this);
110  }
111  }
112  catch (const xh::XmlrpcHelperException& e)
113  {
114  ROS_FATAL_STREAM("Error parsing params: " << e.what());
115  }
116 }
117 
119 {
120  LockTopicHandle::priority_type priority = 0;
121 
124  for (const auto& lock_h : *lock_hs_)
125  {
126  if (lock_h.isLocked())
127  {
128  auto tmp = lock_h.getPriority();
129  if (priority < tmp)
130  {
131  priority = tmp;
132  }
133  }
134  }
135 
136  ROS_DEBUG_STREAM("Priority = " << static_cast<int>(priority));
137 
138  return priority;
139 }
140 
142 {
143  const auto lock_priority = getLockPriority();
144 
145  LockTopicHandle::priority_type priority = 0;
146  std::string velocity_name = "NULL";
147 
150  for (const auto& velocity_h : *velocity_hs_)
151  {
152  if (!velocity_h.isMasked(lock_priority))
153  {
154  const auto velocity_priority = velocity_h.getPriority();
155  if (priority < velocity_priority)
156  {
157  priority = velocity_priority;
158  velocity_name = velocity_h.getName();
159  }
160  }
161  }
162 
163  return twist.getName() == velocity_name;
164 }
165 
166 } // namespace twist_mux
void updateDiagnostics(const ros::TimerEvent &event)
Definition: twist_mux.cpp:77
bool hasPriority(const VelocityTopicHandle &twist)
Definition: twist_mux.cpp:141
TwistMux(int window_size=10)
Definition: twist_mux.cpp:51
ros::Timer diagnostics_timer_
Definition: twist_mux.h:69
boost::shared_ptr< velocity_topic_container > velocity_hs_
velocity_hs_ Velocity topics&#39; handles. Note that if we use a vector, as a consequence of the re-alloc...
Definition: twist_mux.h:82
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
void getStructMember(Struct &col, const std::string &member, T &output)
def twist(x=0.0, r=0.0)
boost::shared_ptr< lock_topic_container > lock_hs_
Definition: twist_mux.h:83
void fetchParam(ros::NodeHandle nh, const std::string &param_name, T &output)
boost::shared_ptr< status_type > status_
Definition: twist_mux.h:95
ros::Publisher cmd_pub_
Definition: twist_mux.h:85
bool hasIncreasedAbsVelocity(const geometry_msgs::Twist &old_twist, const geometry_msgs::Twist &new_twist)
hasIncreasedAbsVelocity Check if the absolute velocity has increased in any of the components: linear...
Definition: twist_mux.cpp:36
void publish(const boost::shared_ptr< M > &message) const
#define ROS_FATAL_STREAM(args)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void getArrayItem(Array &col, int index, T &output)
boost::shared_ptr< diagnostics_type > diagnostics_
Definition: twist_mux.h:94
#define ROS_DEBUG_STREAM(args)
void getTopicHandles(ros::NodeHandle &nh, ros::NodeHandle &nh_priv, const std::string &param_name, std::list< T > &topic_hs)
Definition: twist_mux.cpp:89
static constexpr double DIAGNOSTICS_PERIOD
Definition: twist_mux.h:71
const std::string & getName() const
Definition: topic_handle.h:92
base_type::priority_type priority_type
Definition: topic_handle.h:183
void publishTwist(const geometry_msgs::TwistConstPtr &msg)
Definition: twist_mux.cpp:83


twist_mux
Author(s): Enrique Fernandez , Siegfried-A. Gevatter Pujals
autogenerated on Wed Oct 26 2022 02:14:17