-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_panel.py
executable file
·55 lines (41 loc) · 1.46 KB
/
led_panel.py
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
#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from my_interfaces.msg import LedStateArray
from my_interfaces.srv import SetLed
class LedPanelNode(Node):
def __init__(self):
super().__init__("led_panel")
self.led_states_ = [0, 0, 0]
self.led_states_publisher_ = self.create_publisher(
LedStateArray, "led_states", 10)
self.led_states_timer = self.create_timer(4, self.publish_led_states)
self.set_led_service_ = self.create_service(
SetLed, "set_led", self.callback_set_led)
self.get_logger().info("Led panel node has been started.")
def publish_led_states(self):
msg = LedStateArray()
msg.led_states = self.led_states_
self.led_states_publisher_.publish(msg)
def callback_set_led(self, request, response):
led_num = request.led_number
state = request.state
# The led exist
if led_num > len(self.led_states_)or led_num <= 0:
response.success = False
return response
# The state is False(0) or True(1)s
if state not in [0, 1]:
response.succes = False
return response
self.led_states_[led_num - 1] = state
response.success = True
self.publish_led_states()
return response
def main(args=None):
rclpy.init(args=args)
node = LedPanelNode()
rclpy.spin(node)
rclpy.shutdown()
if __name__ == "__main__":
main()