Fabric 0.2.0
Loading...
Searching...
No Matches
bond_client.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <bondcpp/bond.hpp>
4#include <rclcpp/rclcpp.hpp>
5
7{
8public:
15 BondClient(rclcpp::Node::SharedPtr node, const std::string &bond_id)
16 {
17 bond_id_ = bond_id;
18 node_ = node;
19
20 //get the bond topic from parameter server
21 std::string bond_topic_param = "capability_client.bond_topic";
22 std::string bond_topic_value = "/capabilities/bond";
23
24 if (!node_->has_parameter(bond_topic_param))
25 node_->declare_parameter<std::string>(bond_topic_param, bond_topic_value);
26
27 if (!node_->get_parameter(bond_topic_param, topic_) || topic_.empty())
28 topic_ = bond_topic_value;
29 }
30
35 void start()
36 {
37 RCLCPP_INFO(node_->get_logger(), "Creating bond to capabilities server with id: %s", bond_id_.c_str());
38
39 bond_ = std::make_unique<bond::Bond>(topic_, bond_id_, node_, std::bind(&BondClient::on_broken, this), std::bind(&BondClient::on_formed, this));
40
41 bond_->setHeartbeatPeriod(0.10);
42 bond_->setHeartbeatTimeout(10.0);
43 bond_->start();
44
45 RCLCPP_INFO(node_->get_logger(), "Bond started with capabilities server with id: %s", bond_id_.c_str());
46 }
47
52 void stop()
53 {
54 RCLCPP_INFO(node_->get_logger(), "Destroying bond to capabilities server with id: %s", bond_id_.c_str());
55
56 if (bond_)
57 {
58 bond_.reset();
59 RCLCPP_INFO(node_->get_logger(), "Bond stopped with capabilities server with id: %s", bond_id_.c_str());
60 }
61 }
62
64 {
65 stop();
66 }
67
68private:
73 void on_formed()
74 {
75 // log bond established event
76 RCLCPP_INFO(node_->get_logger(), "Bond with capabilities server formed with id: %s", bond_id_.c_str());
77 }
78
83 void on_broken()
84 {
85 // log bond established event
86 RCLCPP_INFO(node_->get_logger(), "Bond with capabilities server broken with id: %s", bond_id_.c_str());
87 }
88
90 rclcpp::Node::SharedPtr node_;
91
93 std::string bond_id_;
94
96 std::string topic_;
97
99 std::shared_ptr<bond::Bond> bond_;
100};
Definition bond_client.hpp:7
void on_formed()
callback function for bond formed event
Definition bond_client.hpp:73
std::shared_ptr< bond::Bond > bond_
Definition bond_client.hpp:99
void start()
start the bond
Definition bond_client.hpp:35
void on_broken()
callback function for bond broken event
Definition bond_client.hpp:83
std::string topic_
Definition bond_client.hpp:96
BondClient(rclcpp::Node::SharedPtr node, const std::string &bond_id)
Construct a new Bond Client object.
Definition bond_client.hpp:15
~BondClient()
Definition bond_client.hpp:63
std::string bond_id_
Definition bond_client.hpp:93
rclcpp::Node::SharedPtr node_
Definition bond_client.hpp:90
void stop()
stop the bond
Definition bond_client.hpp:52