Add power Modeling functions to all components

This commit is contained in:
2025-02-15 21:42:41 +01:00
parent 5cb25a412a
commit ff80ebe4db
13 changed files with 672 additions and 334 deletions

View File

@@ -4,6 +4,7 @@
#define IR_FRONT_PIN 14
#define IR_BOTTOM_PIN 13
#define DUTY_RESOLUTION LEDC_TIMER_10_BIT
#define DUTY_CYCLE_FREQUENCY 512
InfraredLED::InfraredLED(uint8_t pin,ledc_timer_t timer, ledc_channel_t channel){
this->ledPin = pin;
@@ -78,27 +79,53 @@ void InfraredLED::setState(bool state){
};
void InfraredLED::sendFrequency(uint16_t frequency){
constexpr uint32_t duty = 512;
constexpr uint32_t duty = DUTY_CYCLE_FREQUENCY;
// Float to force float division without casting
constexpr float resolution = 1 << DUTY_RESOLUTION;
if (this->ledPin == IR_BOTTOM_PIN) {
float currentConsumption =
(duty / resolution) *
PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM;
Power::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_IR_BOTTOM,
currentConsumption,
IR_LED_MAX_EXECUTION_DELAY_MS, NULL);
this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS,
NULL);
} else if (this->ledPin == IR_FRONT_PIN) {
float currentConsumption =
(duty / resolution) *
PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT;
Power::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_IR_FRONT,
currentConsumption,
IR_LED_MAX_EXECUTION_DELAY_MS, NULL);
this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS,
NULL);
}
ledc_set_freq(pwmSpeedMode,timer,frequency);
ledc_set_duty(pwmSpeedMode,channel,duty);
ledc_update_duty(pwmSpeedMode,channel);
};
};
float InfraredLED::modelCurrentConsumption(uint32_t duty){
// Float to force float division without casting
constexpr float max_value = 1 << DUTY_RESOLUTION;
const float duty_factor = duty / max_value;
if (this->ledPin == IR_BOTTOM_PIN) {
return duty_factor * PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM;
} else if (this->ledPin == IR_FRONT_PIN) {
return duty_factor * PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT;
}
return NAN;
};
float InfraredLED::modelChargeConsumptionOn(uint16_t durationMs) {
// Float to force float division without casting
constexpr float resolution = 1 << DUTY_RESOLUTION;
if (this->ledPin == IR_BOTTOM_PIN) {
return durationMs *
PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM * 10e6;
} else if (this->ledPin == IR_FRONT_PIN) {
return durationMs *
PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT * 10e6;
}
return NAN;
}
float InfraredLED::modelChargeConsumptionSendFrequency(uint16_t durationMs) {
// Float to force float division without casting
return durationMs * this->modelCurrentConsumption(DUTY_CYCLE_FREQUENCY) *
10e6;
}

View File

@@ -48,7 +48,32 @@ class InfraredLED{
* @param frequency
*/
void sendFrequency(uint16_t frequency);
protected:
/**
* @brief Estimate the current consumption of setting the specified led to the
* passed duty cycle
* @param duty the duty cycle of the led
* @return consumed current in milliamperes
*/
float modelCurrentConsumption(uint32_t duty);
/**
* @brief Estimate the energy consumption of turning the infrared led on
* @param durationMs time the led will be on
* @return consumed energy in coloumbs
*/
float modelChargeConsumptionOn(uint16_t durationMs);
/**
* @brief Estimate the energy consumption of sending a frequency on the
* infrared led
* @param durationMs time the led will be on
* @param frequency the frequency the led will be flashing
* @return consumed energy in coloumbs
*/
float modelChargeConsumptionSendFrequency(uint16_t durationMs);
protected:
uint8_t ledPin;
ledc_timer_t timer;
ledc_channel_t channel;