Skip to content

Commit

Permalink
RealtimePublisher only initialized if used.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmagyar committed May 3, 2016
1 parent de13f8e commit aec78d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/control_toolbox/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class Pid
// blocking the realtime update loop
realtime_tools::RealtimeBuffer<Gains> gains_buffer_;

realtime_tools::RealtimePublisher<control_msgs::PidState> state_publisher_;
boost::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::PidState> > state_publisher_;
bool publish_state_;

double p_error_last_; /**< _Save position state for derivative state calculation. */
Expand Down
39 changes: 22 additions & 17 deletions src/pid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ bool Pid::init(const ros::NodeHandle &node, const bool quiet)

nh.param("publish_state", publish_state_, false);

if(publish_state_){
state_publisher_.init(nh, "state", 1);
if(publish_state_)
{
state_publisher_.reset(new realtime_tools::RealtimePublisher<control_msgs::PidState>());
state_publisher_->init(nh, "state", 1);
}

setGains(gains);
Expand Down Expand Up @@ -341,22 +343,25 @@ double Pid::computeCommand(double error, double error_dot, ros::Duration dt)
cmd_ = p_term + i_term + d_term;

// Publish controller state if configured
if (publish_state_ && state_publisher_.trylock())
if (publish_state_ && state_publisher_)
{
state_publisher_.msg_.header.stamp = ros::Time::now();
state_publisher_.msg_.timestep = dt;
state_publisher_.msg_.error = error;
state_publisher_.msg_.error_dot = error_dot;
state_publisher_.msg_.p_error = p_error_;
state_publisher_.msg_.i_error = i_error_;
state_publisher_.msg_.d_error = d_error_;
state_publisher_.msg_.p_term = p_term;
state_publisher_.msg_.i_term = i_term;
state_publisher_.msg_.d_term = d_term;
state_publisher_.msg_.i_max = gains.i_max_;
state_publisher_.msg_.i_min = gains.i_min_;
state_publisher_.msg_.output = cmd_;
state_publisher_.unlockAndPublish();
if (state_publisher_->trylock())
{
state_publisher_->msg_.header.stamp = ros::Time::now();
state_publisher_->msg_.timestep = dt;
state_publisher_->msg_.error = error;
state_publisher_->msg_.error_dot = error_dot;
state_publisher_->msg_.p_error = p_error_;
state_publisher_->msg_.i_error = i_error_;
state_publisher_->msg_.d_error = d_error_;
state_publisher_->msg_.p_term = p_term;
state_publisher_->msg_.i_term = i_term;
state_publisher_->msg_.d_term = d_term;
state_publisher_->msg_.i_max = gains.i_max_;
state_publisher_->msg_.i_min = gains.i_min_;
state_publisher_->msg_.output = cmd_;
state_publisher_->unlockAndPublish();
}
}

return cmd_;
Expand Down

0 comments on commit aec78d1

Please sign in to comment.