2 #include <boost/shared_ptr.hpp> 4 #include <dynamic_reconfigure/server.h> 5 #include <cmd_vel_smoother/CmdVelSmootherConfig.h> 6 #include <geometry_msgs/Twist.h> 8 template<
class T>
inline T
sgn(T val) {
return val > 0 ? 1.0 : -1.0; }
10 namespace dyn = dynamic_reconfigure;
20 typedef cmd_vel_smoother::CmdVelSmootherConfig
Config;
36 boost::mutex::scoped_lock lock(cfg_mutex_);
37 x_acc_lim_ = cfg.x_acc_lim;
38 y_acc_lim_ = cfg.y_acc_lim;
39 yaw_acc_lim_ = cfg.yaw_acc_lim;
40 interpolate_max_frame_ = cfg.interpolate_max_frame;
42 if(desired_rate_ != cfg.desired_rate) {
43 desired_rate_ = cfg.desired_rate;
60 if (count_ > interpolate_max_frame_)
return;
61 if (latest_vel_ == NULL)
return;
63 double past_sec = (
event.current_real -
latest_time_).toSec();
64 if (past_sec < 1.0 / desired_rate_)
return;
66 double g_x = std::abs(latest_vel_->linear.x / past_sec);
67 double g_y = std::abs(latest_vel_->linear.y / past_sec);
68 double g_yaw = std::abs(latest_vel_->angular.z / past_sec);
70 ROS_DEBUG_STREAM(
"G: (" << g_x <<
", " << g_y <<
", " << g_yaw <<
") detected");
72 bool need_publish =
false;
73 if (g_x > x_acc_lim_) {
74 pub_vel_->linear.x = (g_x -
x_acc_lim_) * past_sec *
sgn(latest_vel_->linear.x);
78 if (g_y > y_acc_lim_) {
79 pub_vel_->linear.y = (g_y -
y_acc_lim_) * past_sec *
sgn(latest_vel_->linear.y);
83 if (g_yaw > yaw_acc_lim_) {
84 pub_vel_->angular.z = (g_yaw -
yaw_acc_lim_) * past_sec *
sgn(latest_vel_->angular.z);
96 dyn_srv_ = boost::make_shared<dyn::Server<Config> >(
pnh_);
98 dyn_srv_->setCallback(f);
101 pub_ = nh_.
advertise<geometry_msgs::Twist>(
"output", 100);
109 int main(
int argc,
char** argv) {
110 ros::init(argc, argv,
"cmd_vel_smoother");
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void setPeriod(const Duration &period, bool reset=true)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void timerCallback(const ros::TimerEvent &event)
geometry_msgs::Twist::ConstPtr latest_vel_
boost::shared_ptr< dyn::Server< Config > > dyn_srv_
ros::Duration timerDuration()
void velCallback(const geometry_msgs::Twist::ConstPtr &msg)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_DEBUG_STREAM(args)
geometry_msgs::Twist::Ptr pub_vel_
int main(int argc, char **argv)
#define ROS_INFO_STREAM(args)
cmd_vel_smoother::CmdVelSmootherConfig Config
int interpolate_max_frame_
void dynConfigCallback(Config &cfg, uint32_t level)