Skip to content

Commit

Permalink
add RGB LEDs init sequence, update submodule to handle new RGB LED fo…
Browse files Browse the repository at this point in the history
…r lights v2.2
  • Loading branch information
PonomarevDA committed Dec 26, 2023
1 parent 3d3dbd1 commit 566e015
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Libs/Cyphal
2 changes: 1 addition & 1 deletion Libs/stm32-cube-project
2 changes: 2 additions & 0 deletions Src/cyphal_application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -21,6 +22,7 @@ void init_persistent_storage() {
}

void application_entry_point() {
AdcPeriphery::init();
LedPeriphery::reset();
init_persistent_storage();
cyphal::NodeGetInfoSubscriber::setHardwareVersion(2, 1);
Expand Down
49 changes: 30 additions & 19 deletions Src/cyphal_application/lights/lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}
33 changes: 27 additions & 6 deletions Src/cyphal_application/lights/lights.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_
9 changes: 5 additions & 4 deletions Src/periphery/adc/adc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions Src/periphery/led/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 566e015

Please sign in to comment.