Manual
Toggle the lightring
You will notice that once you have set the lightrings LEDs they will remain like that forever. Lets
make the button toggle the light on or off each time we press it.
Add a boolean to keep track of the light state:
bool lights_on_;
Initialize the boolean in the class constructor:
TurtleBot4FirstNode()
: Node("turtlebot4_first_cpp_node"), lights_on_(false)
And modify button_1_function to toggle the light:
void button_1_function()
{
// Create a ROS2 message
auto lightring_msg = irobot_create_msgs::msg::LightringLeds();
// Stamp the message with the current time
lightring_msg.header.stamp = this->get_clock()->now();
// Lights are currently off
if (!lights_on_) {
// Override system lights
lightring_msg.override_system = true;
// LED 0
lightring_msg.leds[0].red = 255;
lightring_msg.leds[0].blue = 0;
lightring_msg.leds[0].green = 0;
// LED 1
lightring_msg.leds[1].red = 0;
lightring_msg.leds[1].blue = 255;
lightring_msg.leds[1].green = 0;
// LED 2
lightring_msg.leds[2].red = 0;
lightring_msg.leds[2].blue = 0;
lightring_msg.leds[2].green = 255;
// LED 3
lightring_msg.leds[3].red = 255;