|
| 1 | +#include <memory> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include "rclcpp/rclcpp.hpp" |
| 5 | +#include "ros2_contestant/msg/stamped_blob.hpp" |
| 6 | +using std::placeholders::_1; |
| 7 | + |
| 8 | +class ServerNode : public rclcpp::Node |
| 9 | +{ |
| 10 | +public: |
| 11 | + ros2_contestant::msg::StampedBlob tx_msg; |
| 12 | + |
| 13 | + rclcpp::Publisher<ros2_contestant::msg::StampedBlob>::SharedPtr pub; |
| 14 | + rclcpp::Subscription<ros2_contestant::msg::StampedBlob>::SharedPtr sub; |
| 15 | + |
| 16 | + rclcpp::TimerBase::SharedPtr timer; |
| 17 | + |
| 18 | + rclcpp::Time t_start, t_last_serve; |
| 19 | + |
| 20 | + uint64_t last_rx_counter = 0, last_tx_counter = 0; |
| 21 | + int max_message_count = 0; |
| 22 | + double startup_delay = 0; |
| 23 | + bool verbose = false; |
| 24 | + FILE *log = nullptr; |
| 25 | + const int MAX_BLOB_SIZE = 10000000; |
| 26 | + bool ball_returned = false; |
| 27 | + |
| 28 | + ServerNode(); |
| 29 | + void msg_cb(const ros2_contestant::msg::StampedBlob::SharedPtr msg); |
| 30 | + void timer_cb(); |
| 31 | + void serve(); |
| 32 | +}; |
| 33 | + |
| 34 | +ServerNode::ServerNode() |
| 35 | +: Node("table_tennis_server") |
| 36 | +{ |
| 37 | + declare_parameter<bool>("verbose", false); |
| 38 | + get_parameter("verbose", verbose); |
| 39 | + |
| 40 | + declare_parameter<int>("max_message_count", 0); |
| 41 | + get_parameter("max_message_count", max_message_count); |
| 42 | + |
| 43 | + srandom(0); |
| 44 | + for (int i = 0; i < MAX_BLOB_SIZE; i++) |
| 45 | + tx_msg.blob.push_back(random() % 256); |
| 46 | + |
| 47 | + sub = create_subscription<ros2_contestant::msg::StampedBlob>( |
| 48 | + "ball_return", |
| 49 | + 10, |
| 50 | + std::bind(&ServerNode::msg_cb, this, _1)); |
| 51 | + |
| 52 | + pub = create_publisher<ros2_contestant::msg::StampedBlob>( |
| 53 | + "ball_serve", |
| 54 | + 10); |
| 55 | + |
| 56 | + declare_parameter<double>("startup_delay", 1.0); |
| 57 | + get_parameter("startup_delay", startup_delay); |
| 58 | + |
| 59 | + t_start = get_clock()->now(); |
| 60 | + t_last_serve = get_clock()->now(); |
| 61 | + |
| 62 | + timer = create_wall_timer( |
| 63 | + std::chrono::milliseconds(1), |
| 64 | + std::bind(&ServerNode::timer_cb, this)); |
| 65 | +} |
| 66 | + |
| 67 | +void ServerNode::msg_cb( |
| 68 | + const ros2_contestant::msg::StampedBlob::SharedPtr rx_msg) |
| 69 | +{ |
| 70 | + const double latency = (get_clock()->now() - rx_msg->stamp).seconds(); |
| 71 | + |
| 72 | + if (rx_msg->counter == last_tx_counter) |
| 73 | + { |
| 74 | + fprintf(log, "%d,%.6f\n", (int)rx_msg->blob.size(), latency); |
| 75 | + ball_returned = true; |
| 76 | + } |
| 77 | + last_rx_counter = rx_msg->counter; |
| 78 | + |
| 79 | + if (verbose) |
| 80 | + RCLCPP_INFO(get_logger(), "latency: %.6f", latency); |
| 81 | +} |
| 82 | + |
| 83 | +void ServerNode::serve() |
| 84 | +{ |
| 85 | + tx_msg.counter++; |
| 86 | + tx_msg.stamp = get_clock()->now(); |
| 87 | + pub->publish(tx_msg); |
| 88 | + |
| 89 | + last_tx_counter = tx_msg.counter; |
| 90 | + t_last_serve = get_clock()->now(); |
| 91 | +} |
| 92 | + |
| 93 | +void ServerNode::timer_cb() |
| 94 | +{ |
| 95 | + const rclcpp::Time t(get_clock()->now()); |
| 96 | + if ((t - t_start).seconds() < startup_delay) |
| 97 | + return; |
| 98 | + |
| 99 | + // if we haven't received the ball back, assume it got lost |
| 100 | + if ((t - t_last_serve).seconds() > 1.0) |
| 101 | + { |
| 102 | + fprintf(log, "%d,10.0\n", (int)tx_msg.blob.size()); |
| 103 | + |
| 104 | + if (verbose) |
| 105 | + { |
| 106 | + RCLCPP_ERROR( |
| 107 | + get_logger(), |
| 108 | + "didn't get a response to %d-byte blob", |
| 109 | + (int)tx_msg.blob.size()); |
| 110 | + } |
| 111 | + |
| 112 | + serve(); |
| 113 | + } |
| 114 | + else if (ball_returned) |
| 115 | + { |
| 116 | + serve(); |
| 117 | + } |
| 118 | + |
| 119 | + if (max_message_count && tx_msg.counter > (uint64_t)max_message_count) |
| 120 | + { |
| 121 | + RCLCPP_INFO(get_logger(), "shutting down..."); |
| 122 | + rclcpp::shutdown(); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +int main(int argc, char **argv) |
| 127 | +{ |
| 128 | + rclcpp::init(argc, argv); |
| 129 | + rclcpp::spin(std::make_shared<ServerNode>()); |
| 130 | + rclcpp::shutdown(); |
| 131 | + return 0; |
| 132 | +} |
0 commit comments