-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: LED Colour Controller Package #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vicente-III-H
wants to merge
24
commits into
main
Choose a base branch
from
feature/led-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 bfacc2d
Add logic for LED controller publisher
Vicente-III-H 95c1c22
Fix dependencies
Vicente-III-H 0dde467
Add main to run node
Vicente-III-H 76a5e7c
Fix timer behaviour
Vicente-III-H 41b3bf0
Add alternate CMake line for adding custom interface
Vicente-III-H 2298676
Replace custom RGB msg with ROS2 standard RGBA msg
Vicente-III-H 3bc0780
Add README for package
Vicente-III-H 6a47962
Change formatting for clarity
Vicente-III-H 175b0a1
Moved LED functionality to separate cpp file
Vicente-III-H ab68dac
Add functionality to change LED colour based on keyword
Vicente-III-H 7d8ee5c
Add more LED colour keywords
Vicente-III-H 12e84a1
Fix formatting
Vicente-III-H 56fcb7d
Fix formatting
Vicente-III-H c898a8d
Add return values to LED class methods for debugging
Vicente-III-H b99363b
Change formatting and fix set_colour logger arguments
Vicente-III-H 79b0a7b
Merge branch 'main' into feature/led-controller
Vicente-III-H d70581b
Remove unused msg files
Vicente-III-H 4929ac1
Merge branch 'feature/led-controller' of github.com:uwrobotics/Sparky…
Vicente-III-H 30e4360
Merge branch 'main' into feature/led-controller
Vicente-III-H 1d7bff2
Add LED Lifecycle flowchart
Vicente-III-H 87a7b6c
Fix mermaid diagram appearance
Vicente-III-H ea79207
Add on_cleanup behaviour to LED flowchart
Vicente-III-H 059b085
Update README.md for clarity
Vicente-III-H File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #include <chrono> | ||
| #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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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