-
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
fe9d1c8
bfacc2d
95c1c22
0dde467
76a5e7c
41b3bf0
2298676
3bc0780
6a47962
175b0a1
ab68dac
7d8ee5c
12e84a1
56fcb7d
c898a8d
b99363b
79b0a7b
d70581b
4929ac1
30e4360
1d7bff2
87a7b6c
ea79207
059b085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| 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(rosidl_default_generators REQUIRED) | ||
| find_package(rclcpp REQUIRED) | ||
| find_package(rclcpp_lifecycle REQUIRED) | ||
| # uncomment the following section in order to fill in | ||
| # further dependencies manually. | ||
| # find_package(<dependency> REQUIRED) | ||
|
|
||
| set(msg_files | ||
| "msg/RGB.msg" | ||
| ) | ||
|
|
||
| rosidl_generate_interfaces(${PROJECT_NAME} | ||
| ${msg_files} | ||
| ) | ||
|
|
||
| ament_export_dependencies(rosidl_default_runtime) | ||
|
|
||
| add_executable(led_colour_controller src/led_colour_controller.cpp) | ||
| ament_target_dependencies(led_colour_controller | ||
| rclcpp | ||
| rclcpp_lifecycle | ||
| ) | ||
|
|
||
| rosidl_get_typesupport_target(cpp_typesupport_target | ||
| ${PROJECT_NAME} rosidl_typesupport_cpp) | ||
|
|
||
| target_link_libraries(led_colour_controller "${cpp_typesupport_target}") | ||
|
|
||
| # Replacing the rosidl_get_typesupport_target and target_link_libraries lines above with the one below also works | ||
| # target_link_libraries(led_colour_controller ${PROJECT_NAME}__rosidl_typesupport_cpp) | ||
|
|
||
| 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() |
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| uint8 red_val | ||
| uint8 green_val | ||
| uint8 blue_val | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?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> | ||
|
|
||
| <buildtool_depend>ament_cmake</buildtool_depend> | ||
| <buildtool_depend>rosidl_default_generators</buildtool_depend> | ||
|
|
||
| <exec_depend>rosidl_default_runtime</exec_depend> | ||
|
|
||
| <member_of_group>rosidl_interface_packages</member_of_group> | ||
|
|
||
| <test_depend>ament_lint_auto</test_depend> | ||
| <test_depend>ament_lint_common</test_depend> | ||
|
|
||
| <export> | ||
| <build_type>ament_cmake</build_type> | ||
| </export> | ||
| </package> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| #include <chrono> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need header file for clean interface |
||
| #include <cstdlib> | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "rclcpp_lifecycle/lifecycle_node.hpp" | ||
| #include "led_controller/msg/rgb.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<led_controller::msg::RGB>("led_colour", 10); | ||
|
|
||
| timer_ = this->create_wall_timer(1s, std::bind(&LedColourControllerNode::publish_random_colour, this)); | ||
| timer_->cancel(); // Start with the timer stopped | ||
|
|
||
| publish_colour(255, 255, 0); // Turn to yellow after configuring | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. color should not be magic numbers |
||
|
|
||
| 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(); | ||
|
|
||
| publish_colour(255, 0, 0); // Turn to red on deactivate | ||
|
|
||
| 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(), "Shuting down LED Colour Controller"); | ||
|
|
||
| timer_.reset(); | ||
| pub_.reset(); | ||
|
|
||
| return LifecycleCallback::SUCCESS; | ||
| } | ||
|
|
||
| private: | ||
| void publish_colour(std::uint8_t r, std::uint8_t g, std::uint8_t b) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to return operation success or fail for debugging |
||
| auto colour = std::make_unique<led_controller::msg::RGB>(); | ||
| colour->red_val = r; | ||
| colour->green_val = g; | ||
| colour->blue_val = b; | ||
|
|
||
| RCLCPP_INFO(this->get_logger(), "Publishing colour: R(%d), G(%d), B(%d)", colour->red_val, colour->green_val, colour->blue_val); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two line for clarity |
||
| pub_->publish(std::move(colour)); | ||
| }; | ||
| void publish_random_colour() { | ||
| publish_colour(rand() % 256, rand() % 256, rand() % 256); | ||
| }; | ||
|
|
||
| // Using regular publisher to allow colour changing in inactive state | ||
| std::shared_ptr<rclcpp::Publisher<led_controller::msg::RGB>> pub_; | ||
| std::shared_ptr<rclcpp::TimerBase> timer_; | ||
| }; | ||
|
|
||
| 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; | ||
| } | ||
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.
https://docs.ros.org/en/noetic/api/std_msgs/html/msg/ColorRGBA.html
This is the ros official message for RGB i think for compatibility we should use the standard message.