-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_panel.cpp
72 lines (62 loc) · 2.11 KB
/
led_panel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <chrono>
#include <memory>
#include <vector>
#include "rclcpp/rclcpp.hpp"
#include "my_interfaces/msg/led_state_array.hpp"
#include "my_interfaces/srv/set_led.hpp"
using namespace std::chrono_literals;
class LedPanelNode : public rclcpp::Node
{
public:
LedPanelNode()
: Node("led_panel")
{
led_states_ = {0, 0, 0};
led_states_publisher_ = this->create_publisher<my_interfaces::msg::LedStateArray>("led_states", 10);
led_states_timer_ = this->create_wall_timer(4s, std::bind(&LedPanelNode::publish_led_states, this));
set_led_service_ = this->create_service<my_interfaces::srv::SetLed>(
"set_led",
std::bind(&LedPanelNode::callback_set_led, this, std::placeholders::_1, std::placeholders::_2)
);
RCLCPP_INFO(this->get_logger(), "Led panel node has been started.");
}
private:
void publish_led_states()
{
auto msg = my_interfaces::msg::LedStateArray();
msg.led_states = led_states_;
led_states_publisher_->publish(msg);
}
void callback_set_led(
const std::shared_ptr<my_interfaces::srv::SetLed::Request> request,
std::shared_ptr<my_interfaces::srv::SetLed::Response> response)
{
int64_t led_num = request->led_number;
int64_t state = request->state;
if (led_num > static_cast<int64_t>(led_states_.size()) || led_num <= 0)
{
response->success = false;
return;
}
if (state != 0 && state != 1)
{
response->success = false;
return;
}
led_states_[led_num - 1] = state;
response->success = true;
publish_led_states();
}
std::vector<int64_t> led_states_;
rclcpp::Publisher<my_interfaces::msg::LedStateArray>::SharedPtr led_states_publisher_;
rclcpp::TimerBase::SharedPtr led_states_timer_;
rclcpp::Service<my_interfaces::srv::SetLed>::SharedPtr set_led_service_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
auto node = std::make_shared<LedPanelNode>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}