Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fe9d1c8
Create template package
Vicente-III-H Jun 10, 2026
bfacc2d
Add logic for LED controller publisher
Vicente-III-H Jun 14, 2026
95c1c22
Fix dependencies
Vicente-III-H Jun 14, 2026
0dde467
Add main to run node
Vicente-III-H Jun 14, 2026
76a5e7c
Fix timer behaviour
Vicente-III-H Jun 14, 2026
41b3bf0
Add alternate CMake line for adding custom interface
Vicente-III-H Jun 14, 2026
2298676
Replace custom RGB msg with ROS2 standard RGBA msg
Vicente-III-H Jun 24, 2026
3bc0780
Add README for package
Vicente-III-H Jun 24, 2026
6a47962
Change formatting for clarity
Vicente-III-H Jun 24, 2026
175b0a1
Moved LED functionality to separate cpp file
Vicente-III-H Jun 24, 2026
ab68dac
Add functionality to change LED colour based on keyword
Vicente-III-H Jun 24, 2026
7d8ee5c
Add more LED colour keywords
Vicente-III-H Jun 25, 2026
12e84a1
Fix formatting
Vicente-III-H Jun 25, 2026
56fcb7d
Fix formatting
Vicente-III-H Jun 25, 2026
c898a8d
Add return values to LED class methods for debugging
Vicente-III-H Jun 25, 2026
b99363b
Change formatting and fix set_colour logger arguments
Vicente-III-H Jun 25, 2026
79b0a7b
Merge branch 'main' into feature/led-controller
Vicente-III-H Jun 25, 2026
d70581b
Remove unused msg files
Vicente-III-H Jun 25, 2026
4929ac1
Merge branch 'feature/led-controller' of github.com:uwrobotics/Sparky…
Vicente-III-H Jun 25, 2026
30e4360
Merge branch 'main' into feature/led-controller
Vicente-III-H Jul 3, 2026
1d7bff2
Add LED Lifecycle flowchart
Vicente-III-H Jul 3, 2026
87a7b6c
Fix mermaid diagram appearance
Vicente-III-H Jul 3, 2026
ea79207
Add on_cleanup behaviour to LED flowchart
Vicente-III-H Jul 3, 2026
059b085
Update README.md for clarity
Vicente-III-H Jul 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/led_controller/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.8)
project(led_controller)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(std_msgs REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

add_executable(led_colour_controller
src/led_colour_controller.cpp
src/led.cpp
)
ament_target_dependencies(led_colour_controller
rclcpp
rclcpp_lifecycle
std_msgs
)

install(TARGETS led_colour_controller
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
24 changes: 24 additions & 0 deletions src/led_controller/LED_LIFECYCLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
```mermaid
---
title: LED Lifecycle
---
flowchart TD
%% Nodes
Unconfigured["`**Unconfigured**<br>LED attributes (e.g. publisher, logger, etc.) not configured`"]
Standby["`**Standby**<br>LED is only YELLOW`"]
Active["`**Active**<br>LED can send colour data`"]
EStop["`**EStop**<br>LED is only RED`"]

%% Flowchart
Unconfigured -->|"on_configure()"| Standby
Standby -->|"on_cleanup()"| Unconfigured

Standby -->|"on_activate()"| Active
Active -->|"on_deactivate()"| Standby

%% EStop
Standby -->|"EStop engaged<br>&&<br>Node is inactive"| EStop
Active -->|"EStop engaged<br>&&<br>Node is active"| EStop
EStop -->|"EStop disengaged<br>&&<br>Node is active"| Active
EStop -->|"EStop disengaged<br>&&<br>Node is inactive"| Standby
```
17 changes: 17 additions & 0 deletions src/led_controller/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
34 changes: 34 additions & 0 deletions src/led_controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This package contains a lifecycle node that publishes RGB values to the `/led_colour` topic and changes behaviour depending on the node's current lifecycle state:

- **on_configure() -** Initializes node's publisher and timer, and publishes the colour yellow once to `/led_colour`
- **on_activate() -** Publishes a random colour every second to the `/led_colour`
- **on_deactivate() -** Publishes the colour red once to `/led_colour`
- **on_cleanup() and on_shutdown() -** Cleans up node's publisher and timer

## Instructions
### Build and Run Package
To build the package, run:
```bash
colcon build --packages-select led_controller
```
Then, source the workspace:
```bash
source install/setup.bash
```
Then, run the LED Colour Controller:
```bash
ros2 run led_controller led_colour_controller
```
### Node Commands
To change the state of the node, open a new terminal and run the commands below:
```bash
ros2 lifecycle set led_colour_controller_node configure
ros2 lifecycle set led_colour_controller_node activate
ros2 lifecycle set led_colour_controller_node deactivate
ros2 lifecycle set led_colour_controller_node cleanup
ros2 lifecycle set led_colour_controller_node shutdown
```
To view the output of the node, run:
```bash
ros2 topic echo /led_colour
```
22 changes: 22 additions & 0 deletions src/led_controller/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>led_controller</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="dev3@todo.todo">dev3</maintainer>
<license>MIT</license>

<depend>rclcpp</depend>
<depend>rclcpp_lifecycle</depend>
<depend>std_msgs</depend>

<buildtool_depend>ament_cmake</buildtool_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
65 changes: 65 additions & 0 deletions src/led_controller/src/led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "led.hpp"

LED::LED(std::shared_ptr<rclcpp::Publisher<std_msgs::msg::ColorRGBA>> pub_, rclcpp::Logger logger)
: pub_(pub_), logger(logger) {}

bool LED::set_colour(float r, float g, float b, float a)
{
auto colour = std::make_unique<std_msgs::msg::ColorRGBA>();
colour->r = r;
colour->g = g;
colour->b = b;
colour->a = a;

RCLCPP_INFO(
logger, "Publishing colour: R(%f), G(%f), B(%f), a(%f)",
colour->r,
colour->g,
colour->b,
colour->a
);

pub_->publish(std::move(colour));

return true;
}

bool LED::set_colour(LEDColours colour_name)
{
switch (colour_name) {
case LEDColours::RED:
set_colour(255, 0, 0);
break;
case LEDColours::ORANGE:
set_colour(255, 128, 0);
break;
case LEDColours::YELLOW:
set_colour(255, 255, 0);
break;
case LEDColours::GREEN:
set_colour(0, 255, 0);
break;
case LEDColours::BLUE:
set_colour(0, 0, 255);
break;
case LEDColours::PURPLE:
set_colour(128, 0, 255);
break;
case LEDColours::WHITE:
set_colour(255, 255, 255);
break;
default:
RCLCPP_INFO(
logger,
"LEDColours enum argument did not map to a colour value"
);
return false;
}

return true;
}

bool LED::set_random_colour()
{
return set_colour(rand() % 256, rand() % 256, rand() % 256);
}
35 changes: 35 additions & 0 deletions src/led_controller/src/led.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef LED_HPP
#define LED_HPP

#include <cstdlib>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/color_rgba.hpp"

enum class LEDColours
{
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
PURPLE,
WHITE
};

class LED
{
public:
LED(std::shared_ptr<rclcpp::Publisher<std_msgs::msg::ColorRGBA>> pub_, rclcpp::Logger logger);
bool set_colour(float r, float g, float b, float a = 0);
bool set_colour(LEDColours colour_name);
bool set_random_colour();

private:
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::ColorRGBA>> pub_;
rclcpp::Logger logger;
std::unique_ptr<std_msgs::msg::ColorRGBA> curr_colour_;
};

#endif
95 changes: 95 additions & 0 deletions src/led_controller/src/led_colour_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <chrono>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need header file for clean interface

#include <memory>

#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "led.hpp"

using namespace std::chrono_literals;

using LifecycleCallback = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

class LedColourControllerNode : public rclcpp_lifecycle::LifecycleNode
{
public:
explicit LedColourControllerNode(const std::string & name)
: rclcpp_lifecycle::LifecycleNode(name) {}

LifecycleCallback on_configure(const rclcpp_lifecycle::State &)
{
RCLCPP_INFO(this->get_logger(), "Configuring LED Colour Controller");

pub_ = this->create_publisher<std_msgs::msg::ColorRGBA>("led_colour", 10);

led_ = std::make_shared<LED>(pub_, this->get_logger());

timer_ = this->create_wall_timer(1s, std::bind(&LED::set_random_colour, led_.get()));
timer_->cancel(); // Start with the timer stopped

led_->set_colour(LEDColours::YELLOW);

return LifecycleCallback::SUCCESS;
}

LifecycleCallback on_activate(const rclcpp_lifecycle::State & state)
{
RCLCPP_INFO(this->get_logger(), "Activating LED Colour Controller");

LifecycleNode::on_activate(state);

timer_->reset(); // Resume timer on activate

return LifecycleCallback::SUCCESS;
}

LifecycleCallback on_deactivate(const rclcpp_lifecycle::State & state)
{
RCLCPP_INFO(this->get_logger(), "Deactivating LED Colour Controller");

LifecycleNode::on_deactivate(state);

timer_->cancel();

led_->set_colour(LEDColours::RED);

return LifecycleCallback::SUCCESS;
}

LifecycleCallback on_cleanup(const rclcpp_lifecycle::State & state)
{
RCLCPP_INFO(this->get_logger(), "Cleaning up LED Colour Controller");

timer_.reset();
pub_.reset();

return LifecycleCallback::SUCCESS;
}

LifecycleCallback on_shutdown(const rclcpp_lifecycle::State & state)
{
RCLCPP_INFO(this->get_logger(), "Shutting down LED Colour Controller");

timer_.reset();
pub_.reset();

return LifecycleCallback::SUCCESS;
}

private:
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::ColorRGBA>> pub_; // Using regular publisher to allow colour changing in inactive state
std::shared_ptr<rclcpp::TimerBase> timer_;
std::shared_ptr<LED> led_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);

rclcpp::executors::SingleThreadedExecutor executor;
auto node = std::make_shared<LedColourControllerNode>("led_colour_controller_node");

executor.add_node(node->get_node_base_interface());
executor.spin();

rclcpp::shutdown();
return 0;
}
Loading