From 566e015d016146a3e5a568823fa93cd45fe72836 Mon Sep 17 00:00:00 2001 From: PonomarevDA Date: Wed, 27 Dec 2023 01:47:37 +0300 Subject: [PATCH] add RGB LEDs init sequence, update submodule to handle new RGB LED for lights v2.2 --- Libs/Cyphal | 2 +- Libs/stm32-cube-project | 2 +- Src/cyphal_application/application.cpp | 2 + Src/cyphal_application/lights/lights.cpp | 49 +++++++++++++++--------- Src/cyphal_application/lights/lights.hpp | 33 +++++++++++++--- Src/periphery/adc/adc.hpp | 9 +++-- Src/periphery/led/led.cpp | 8 ++-- 7 files changed, 71 insertions(+), 34 deletions(-) diff --git a/Libs/Cyphal b/Libs/Cyphal index b512c61..11fc5d2 160000 --- a/Libs/Cyphal +++ b/Libs/Cyphal @@ -1 +1 @@ -Subproject commit b512c61c5b647580b5e70e057e8c06468ed6ef15 +Subproject commit 11fc5d2fa5f632a1137a82d1b99f0dd7ac0fc8e6 diff --git a/Libs/stm32-cube-project b/Libs/stm32-cube-project index 6f196b2..9418f4c 160000 --- a/Libs/stm32-cube-project +++ b/Libs/stm32-cube-project @@ -1 +1 @@ -Subproject commit 6f196b28431bcea6a690954cbd033b25b32bb962 +Subproject commit 9418f4c6ccb1b9377b6414c3558abcf4f31cbaf4 diff --git a/Src/cyphal_application/application.cpp b/Src/cyphal_application/application.cpp index 4b84d79..7a89aaa 100644 --- a/Src/cyphal_application/application.cpp +++ b/Src/cyphal_application/application.cpp @@ -10,6 +10,7 @@ #include "params.hpp" #include "lights/lights.hpp" #include "periphery/led/led.hpp" +#include "periphery/adc/adc.hpp" void init_persistent_storage() { @@ -21,6 +22,7 @@ void init_persistent_storage() { } void application_entry_point() { + AdcPeriphery::init(); LedPeriphery::reset(); init_persistent_storage(); cyphal::NodeGetInfoSubscriber::setHardwareVersion(2, 1); diff --git a/Src/cyphal_application/lights/lights.cpp b/Src/cyphal_application/lights/lights.cpp index be1d992..45f5a88 100644 --- a/Src/cyphal_application/lights/lights.cpp +++ b/Src/cyphal_application/lights/lights.cpp @@ -26,17 +26,24 @@ int8_t RgbLights::init() { return res; } - res = AdcPeriphery::init(); - if (res < 0) { - return res; - } - return 0; }; void RgbLights::update() { _process_crct_remperature(); - _process_lights(); + + if (HAL_GetTick() >= _next_light_process_time_ms) { + _next_light_process_time_ms = HAL_GetTick() + 40; + + if (_init_counter < WS2812_LEDS_AMOUNT) { + _init_lights(); + } else { + _process_last_received_command(); + } + + ws2812bSetColors(&_leds); + ws2812bStartOnce(); + } } /** @@ -63,20 +70,17 @@ void RgbLights::_process_crct_remperature() { } } -/** - * @note - * 25 Hz blinking is considered to be enough. - * Start decreasing the brighness when temperature is higher than 60 °C. - * Do not allow to reach 80 °C. - */ -void RgbLights::_process_lights() { - if (HAL_GetTick() < _next_light_process_time_ms) { - return; +void RgbLights::_init_lights() { + if (_init_counter < WS2812_LEDS_AMOUNT) { + _leds.colors[_init_counter].shades.red = 5; + _leds.colors[_init_counter].shades.green = 5; + _leds.colors[_init_counter].shades.blue = 5; } - _next_light_process_time_ms = HAL_GetTick() + 20; - auto color = _rgbled_sub.get(); + _init_counter++; +} +void RgbLights::_process_last_received_command() { float max_brightness; if (current_temperature <= CONTROL_MIN) { max_brightness = 1.0; @@ -86,6 +90,15 @@ void RgbLights::_process_lights() { max_brightness = 1.0f - (current_temperature - CONTROL_MIN) / (CONTROL_MAX - CONTROL_MIN); } + reg_udral_physics_optics_HighColor_0_1 color; + if (_rgbled_sub.get_last_recv_ts_ms() == 0) { + color.red = 1; + color.green = 2; + color.blue = 1; + } else { + color = _rgbled_sub.get(); + } + uint8_t red = (float)color.red / reg_udral_physics_optics_HighColor_0_1_MAX_RED * 255.0f * max_brightness; uint8_t green = (float)color.green / reg_udral_physics_optics_HighColor_0_1_MAX_GREEN * 255.0f * max_brightness; uint8_t blue = (float)color.blue / reg_udral_physics_optics_HighColor_0_1_MAX_BLUE * 255.0f * max_brightness; @@ -94,6 +107,4 @@ void RgbLights::_process_lights() { _leds.colors[led_idx].shades.green = green; _leds.colors[led_idx].shades.blue = blue; } - ws2812bSetColors(&_leds); - ws2812bStartOnce(); } diff --git a/Src/cyphal_application/lights/lights.hpp b/Src/cyphal_application/lights/lights.hpp index 1a491bb..1000116 100644 --- a/Src/cyphal_application/lights/lights.hpp +++ b/Src/cyphal_application/lights/lights.hpp @@ -10,29 +10,50 @@ #include "Udral/circuit_status.hpp" #include "periphery/ws2812/ws2812.h" + class RgbLights { public: - RgbLights(cyphal::Cyphal* driver) : _rgbled_sub(driver), _temp_pub(driver, 0) {}; + RgbLights(cyphal::Cyphal* driver) : _temp_pub(driver, 0), _rgbled_sub(driver) {}; int8_t init(); + + /** + * @note 25 Hz blinking is considered to be enough. + */ void update(); private: - void _process_lights(); - cyphal::HighColorSubscriber _rgbled_sub; - Leds_Color_t _leds; - uint32_t _next_light_process_time_ms{0}; + /** + * @brief When the node is enabled, the LEDs blink one after one with white color. + * This initialization sequence allows a user to verify that all LEDs are working properly. + */ + void _init_lights(); + uint8_t _init_counter{0}; + /** + * @brief Measure onboard temperature and publish to Cyphal/CAN network + * The temperature is used in the brightness controller + */ void _process_crct_remperature(); RaccoonLab::CircuitStatusTemperaturePublisher _temp_pub; float current_temperature; uint32_t _next_crct_process_time_ms{0}; /** - * @brief Controller logic + * @brief Brightness Controller * If the temperature goes above 60 °C, the controller will decrease the maximum brightness. * If the temperature goes above 80 °C, the controller will turn off the lights. */ static constexpr const float CONTROL_MIN = 273.15 + 60; static constexpr const float CONTROL_MAX = 273.15 + 80; + + /** + * @brief Just repeat the latest received command with respect to the brightness. + * By default, all LEDs should be slightly enabled with white color + * to be sure they are working properly. + */ + void _process_last_received_command(); + cyphal::HighColorSubscriber _rgbled_sub; + Leds_Color_t _leds{}; + uint32_t _next_light_process_time_ms{0}; }; #endif // SRC_CYPHAL_APPLICATION_LIGHTS_LIGHTS_HPP_ diff --git a/Src/periphery/adc/adc.hpp b/Src/periphery/adc/adc.hpp index 6c95dc3..db1df56 100644 --- a/Src/periphery/adc/adc.hpp +++ b/Src/periphery/adc/adc.hpp @@ -12,10 +12,11 @@ extern "C" { #endif enum class AdcChannel : uint8_t { - ADC_VIN, - ADC_5V, - ADC_TEMPERATURE, - ADC_NUMBER_OF_CNANNELS, + ADC_VIN = 0, + ADC_5V = 1, + ADC_VERSION = 2, + ADC_TEMPERATURE = 3, + ADC_NUMBER_OF_CNANNELS = 4, }; class AdcPeriphery { diff --git a/Src/periphery/led/led.cpp b/Src/periphery/led/led.cpp index 80fe016..5ff05ee 100644 --- a/Src/periphery/led/led.cpp +++ b/Src/periphery/led/led.cpp @@ -5,13 +5,15 @@ #include "led.hpp" #include "main.h" - void LedPeriphery::reset() { - HAL_GPIO_WritePin(INTERNAL_LED_GPIO_Port, INTERNAL_LED_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(INT_RGB_LED_GREEN_GPIO_Port, INT_RGB_LED_GREEN_Pin, GPIO_PIN_SET); } void LedPeriphery::toggle() { auto crnt_time_ms = HAL_GetTick(); GPIO_PinState state = (crnt_time_ms % 1000 > 500) ? GPIO_PIN_SET : GPIO_PIN_RESET; - HAL_GPIO_WritePin(INTERNAL_LED_GPIO_Port, INTERNAL_LED_Pin, state); + HAL_GPIO_WritePin(INT_RGB_LED_GREEN_GPIO_Port, INT_RGB_LED_GREEN_Pin, state); + + HAL_GPIO_WritePin(INT_RGB_LED_RED_GPIO_Port, INT_RGB_LED_RED_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(INT_RGB_LED_BLUE_GPIO_Port, INT_RGB_LED_BLUE_Pin, GPIO_PIN_SET); }