Files
mppt-testbench/code64/Core/Src/mppt.c
grabowski e7a23a3c7e Add LVSolarBuck64 firmware and debug console with uv support
STM32G474RB firmware for solar buck converter with MPPT, CC control,
Vfly compensation, and adaptive deadtime. Includes Textual TUI debug
console for real-time telemetry, parameter tuning, and SQLite logging.

Added pyproject.toml for uv: `cd code64 && uv run debug-console`

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 16:38:23 +07:00

65 lines
1.6 KiB
C

/*
* mppt.c
*
* Created on: Jun 11, 2025
* Author: janik
*/
#include "mppt.h"
#include <math.h> // for fabsf
void MPPT_IncCond_Init(MPPTController *mppt,
float initial_iref,
float step,
float iref_min,
float iref_max,
float dv_min)
{
mppt->last_vin = 0.0f;
mppt->last_iin = 0.0f;
mppt->iref = initial_iref;
mppt->step = step;
mppt->iref_min = iref_min;
mppt->iref_max = iref_max;
mppt->dv_min = dv_min;
mppt->deadband = 0.005f;
}
float MPPT_IncCond_Update(MPPTController *mppt, float vin, float iin)
{
float dv = vin - mppt->last_vin;
float di = iin - mppt->last_iin;
// Skip if input voltage is essentially zero
if (vin < 1e-2f)
return mppt->iref;
// Suppress update if both dv and di are below noise floor
if (fabsf(dv) < mppt->dv_min && fabsf(di) < mppt->dv_min)
return mppt->iref;
/* dP = vin*di + iin*dv (avoids dv division, works even when dv is small) */
float dp = vin * di + iin * dv;
float p = vin * iin; /* current power (mV*mA = nW scale) */
/* Relative deadband: |dP| < 0.5% of P => near MPP */
if (p > 1e3f && fabsf(dp) < mppt->deadband * fabsf(p)) {
// At or near MPP — don't step
} else if (dp > 0) {
mppt->iref -= mppt->step;
} else {
mppt->iref += mppt->step;
}
// Clamp
if (mppt->iref > mppt->iref_max) mppt->iref = mppt->iref_max;
if (mppt->iref < mppt->iref_min) mppt->iref = mppt->iref_min;
// Save state
mppt->last_vin = vin;
mppt->last_iin = iin;
return mppt->iref;
}