ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to use WS2812B LED lights with ROS 2 node

asked 2023-04-13 08:54:21 -0500

Dragon_58 gravatar image

Hello everyone,

I am trying to control a WS2812B LED strip with a Raspberry Pi using ROS 2. I have looked for ROS 2 packages and Python libraries that can be used for this purpose, but I haven't been able to find anything that works well.

I came across the rpi_ws281x library, which seems to be a popular choice for controlling WS2812B LED strips with a Raspberry Pi. However, I am not sure how to integrate this library with a ROS 2 node.

Can anyone provide some guidance on how to use the rpi_ws281x library with a ROS 2 node? Specifically, how to subscribe to a ROS 2 topic for color messages and set the colors of the LEDs on the strip.

Thank you for your help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-13 09:40:29 -0500

Ranjit Kathiriya gravatar image

updated 2023-04-13 11:24:03 -0500

You need to Install the rpi_ws281x library on the Raspberry Pi.

pip install rpi-ws281x==1.1.1

And write a ROS 2 node in Python that imports the rpi_ws281x library and controls the WS2812B LED lights in response to ROS 2 messages.

import rclpy
from std_msgs.msg import ColorRGBA
from rpi_ws281x import Adafruit_NeoPixel, Color

# Define the number of WS2812B LED lights and the pin they are connected to
NUM_LEDS = 30  # Number of LED
LED_PIN = 18  # 0- number will form a color

# Initialize the WS2812B LED lights
strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN)
strip.begin()
strip.show()

# Callback function for the color message
def color_callback(msg):
    # Set the color of all the WS2812B LED lights
    for i in range(NUM_LEDS):
        strip.setPixelColor(i, Color(int(msg.r * 255), int(msg.g * 255), int(msg.b * 255)))
    strip.show()

def main(args=None):
    rclpy.init(args=args)
    node = rclpy.create_node('minimal_subscriber')
    subscription = node.create_subscription(ColorRGBA, 'color', color_callback, 10)
    subscription  # prevent unused variable warning
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

I hope it helps, fell free to drop a comment in case of questions.

edit flag offensive delete link more

Comments

rospy would be ROS 1, no?

gvdhoorn gravatar image gvdhoorn  ( 2023-04-13 11:13:01 -0500 )edit

@gvdhoorn , thanks for pointing that out. I will correct this code asap.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2023-04-13 11:16:13 -0500 )edit

Thank you so much for the suggestion.

Dragon_58 gravatar image Dragon_58  ( 2023-04-13 15:17:12 -0500 )edit

Tick the answer if you think it is relevant or correct.

Thanks,

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2023-04-14 06:40:48 -0500 )edit

Hello Whenever i use rpi_ws281x library with ros2 , then i'm not able to list out the node running on the system using 'ros2 node list' or even topic using 'ros2 topic list'. If you provide any input on this it would be great help. Thank you

Dragon_58 gravatar image Dragon_58  ( 2023-04-18 05:24:02 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2023-04-13 08:54:21 -0500

Seen: 369 times

Last updated: Apr 13 '23