Skip to content

Commit

Permalink
Only publish statistics if they are available.
Browse files Browse the repository at this point in the history
It is possible to try to collect statistics from the underlying
realtime_tools before they are available.  If we detect that is
the case, skip publishing the data and wait until the data
is available.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette committed Sep 20, 2019
1 parent d0cbff3 commit d6423ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pendulum_control/include/pendulum_control/rtt_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ class RttExecutor : public rclcpp::executor::Executor
return rclcpp::ok() && running;
}

void set_rtt_results_message(pendulum_msgs::msg::RttestResults & msg) const
bool set_rtt_results_message(pendulum_msgs::msg::RttestResults & msg) const
{
if (!results_available) {
return false;
}
msg.cur_latency = last_sample;
msg.mean_latency = results.mean_latency;
msg.min_latency = results.min_latency;
Expand All @@ -71,6 +74,8 @@ class RttExecutor : public rclcpp::executor::Executor
clock_gettime(CLOCK_MONOTONIC, &curtime);
msg.stamp.sec = curtime.tv_sec;
msg.stamp.nanosec = curtime.tv_nsec;

return true;
}

/// Wrap executor::spin into rttest_spin.
Expand Down Expand Up @@ -105,7 +110,9 @@ class RttExecutor : public rclcpp::executor::Executor
executor->spin_some();

// Retrieve rttest statistics accumulated so far and store them in the executor.
rttest_get_statistics(&executor->results);
if (rttest_get_statistics(&executor->results) >= 0) {
executor->results_available = true;
}
rttest_get_sample_at(executor->results.iteration, &executor->last_sample);
// In case this boolean wasn't set, notify that we've recently run the callback.
executor->running = true;
Expand All @@ -114,6 +121,7 @@ class RttExecutor : public rclcpp::executor::Executor

// For storing accumulated performance statistics.
rttest_results results;
bool results_available{false};
// True if the executor is spinning.
bool running;
// True if rttest has initialized and hasn't been stopped yet.
Expand Down
5 changes: 4 additions & 1 deletion pendulum_control/src/pendulum_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ int main(int argc, char * argv[])
auto logger_publish_callback =
[&logger_pub, &executor, &pendulum_motor, &pendulum_controller]() {
pendulum_msgs::msg::RttestResults results_msg;
if (!executor->set_rtt_results_message(results_msg)) {
// No data is available, just get out instead of publishing bogus data.
return;
}
results_msg.command = pendulum_controller->get_next_command_message();
results_msg.state = pendulum_motor->get_next_sensor_message();
executor->set_rtt_results_message(results_msg);
logger_pub->publish(results_msg);
};

Expand Down

0 comments on commit d6423ac

Please sign in to comment.