From: Erik Andresen Date: Wed, 6 May 2026 19:01:09 +0000 (+0200) Subject: Add script for auto brightness X-Git-Url: https://defiant.homedns.org/gitweb/?a=commitdiff_plain;h=6126eb1245ab76375d8684360e8f715539c075f3;p=a4wd3.git Add script for auto brightness --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 28f0b09..72c9340 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,10 +38,17 @@ add_executable(display src/display.cpp) ament_target_dependencies(display rclcpp sensor_msgs) target_compile_features(display PUBLIC cxx_std_20) +add_executable(light_brightness src/light_brightness.cpp) +target_link_libraries(light_brightness "${cpp_typesupport_target}") +ament_target_dependencies(light_brightness rclcpp sensor_msgs) +target_compile_features(light_brightness PUBLIC cxx_std_20) + install(TARGETS hw_node DESTINATION lib/${PROJECT_NAME}) install(TARGETS display DESTINATION lib/${PROJECT_NAME}) +install(TARGETS light_brightness + DESTINATION lib/${PROJECT_NAME}) install(DIRECTORY launch DESTINATION share/${PROJECT_NAME}) install(DIRECTORY params diff --git a/src/hw_node.cpp b/src/hw_node.cpp index bc0e2a2..7f3cfab 100644 --- a/src/hw_node.cpp +++ b/src/hw_node.cpp @@ -343,6 +343,9 @@ class A4wd3 : public rclcpp::Node { auto msg = sensor_msgs::msg::Illuminance(); msg.header.stamp = this->get_clock()->now(); msg.illuminance = lux*2.0183-5.2079; + if (msg.illuminance < 0) { + msg.illuminance = 0; + } pub_light->publish(msg); } diff --git a/src/light_brightness.cpp b/src/light_brightness.cpp new file mode 100644 index 0000000..cdcfdf9 --- /dev/null +++ b/src/light_brightness.cpp @@ -0,0 +1,50 @@ +#include +#include "sensor_msgs/msg/illuminance.hpp" +#include "a4wd3/msg/led_stripe.hpp" +#include "a4wd3/msg/led.hpp" + +#include "rclcpp/rclcpp.hpp" +using std::placeholders::_1; + +class LightBrightness : public rclcpp::Node +{ + public: + LightBrightness() : Node("light_brightness") { + sub_light = this->create_subscription("/light", 10, std::bind(&LightBrightness::light_callback, this, _1)); + pub_led = this->create_publisher("/led_stripe", 10); + } + + private: + void light_callback(const sensor_msgs::msg::Illuminance msg) { + bool changed=false; + if (msg.illuminance == 0 && val < 254) { + val++; + changed=true; + } else if (msg.illuminance > 0 && val > 0) { + val--; + changed=true; + } + RCLCPP_INFO(this->get_logger(), "'%.2lflx -> light=%d'", msg.illuminance, val); + if (changed) { + auto ledmsg = a4wd3::msg::LedStripe(); + auto led=a4wd3::msg::Led(); + led.num=0; + led.cw=val; + led.ww=val; + ledmsg.leds.push_back(led); + pub_led->publish(ledmsg); + } + } + + rclcpp::Subscription::SharedPtr sub_light; + rclcpp::Publisher::SharedPtr pub_led; + uint8_t val=0; +}; + +int main(int argc, char * argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +}