diff --git a/Somfy.cpp b/Somfy.cpp index 3fdf593..b683dd5 100644 --- a/Somfy.cpp +++ b/Somfy.cpp @@ -1265,42 +1265,49 @@ void SomfyShade::triggerGPIOs(somfy_frame_t &frame) this->gpioDir = dir; } } +// Solves ln((α+1)/2) = r·ln(α) for α > 1, with 0.5 < r < 1: the ascent model, +// r being the fraction of the ascent time at which the shade is halfway. +// The descent model is the same equation for 1 - r. +// Bisection on x = ln(α): g(x) = ln(e^x + 1) - ln(2) - r·x is negative just above +// x = 0 and grows without bound, so a single root is bracketed for every r in +// (0.5, 1). Newton's method from α = 2 left its domain for r above ~0.66 (or +// below ~0.34 going down) and the shade silently fell back to linear. +// α is capped at 1e6 (r ≈ 0.95), beyond that float precision is lost. +static float solveTravelAlpha(float r) +{ + if (r <= 0.5f || r >= 1.0f) + return 0.0f; // no α > 1: keep the linear model + const float xMax = log(1e6f); + auto g = [r](float x) { return x + log1p(exp(-x)) - log(2.0f) - r * x; }; + if (g(xMax) < 0.0f) + return 1e6f; + float lo = 0.0f, hi = xMax; + for (uint8_t i = 0; i < 40; i++) + { + float mid = 0.5f * (lo + hi); + if (g(mid) < 0.0f) + lo = mid; + else + hi = mid; + } + float a = exp(0.5f * (lo + hi)); + return a > 1.001f ? a : 0.0f; +} void SomfyShade::computeAlphas() { - // Solve for alphaDown: given r = downMidTime/descendTime, find α > 1 such that - // ln(2α/(α+1)) = r * ln(α) - // i.e. f(α) = ln(2) + (1-r)*ln(α) - ln(α+1) = 0 - // Newton's method, 10 iterations from α₀ = 2.0 + // alphaDown: r = downMidTime/descendTime, ln(2α/(α+1)) = r·ln(α), i.e. the + // ascent equation for 1 - r. // descendTime = user-measured timestamp from fully open to floor touch (blades horizontal) // Tilt duration (blade closing) = downTime - descendTime (derived internally) uint32_t descentTime = (this->descendTime > 0 && this->descendTime < this->downTime) ? this->descendTime : this->downTime; if (this->downMidTime > 0 && this->downMidTime < descentTime) - { - float r = (float)this->downMidTime / (float)descentTime; - float a = 2.0f; - for (uint8_t i = 0; i < 10; i++) - { - float f = log(2.0f) + (1.0f - r) * log(a) - log(a + 1.0f); - float fp = (1.0f - r) / a - 1.0f / (a + 1.0f); - a -= f / fp; - if (a <= 1.001f) - { - a = 1.001f; - break; - } - } - this->alphaDown = a; - } + this->alphaDown = solveTravelAlpha(1.0f - (float)this->downMidTime / (float)descentTime); else - { this->alphaDown = 0.0f; - } - // Solve for alphaUp: given r = (upMidTime-upSlatTime)/ascentTime, find α > 1 such that - // ln((α+1)/2) = r * ln(α) - // i.e. f(α) = ln(α+1) - ln(2) - r*ln(α) = 0 + // alphaUp: r = (upMidTime-upSlatTime)/ascentTime, ln((α+1)/2) = r·ln(α) // upMidTime = timestamp from fully closed to 50% (user-measured from t=0) // upSlatTime = timestamp from fully closed to blades expanded (user-measured from t=0) // ascentTime = upTime - upSlatTime (pure ascent duration, excludes blade-open phase) @@ -1312,26 +1319,9 @@ void SomfyShade::computeAlphas() ? this->upMidTime - this->upSlatTime : this->upMidTime; if (upMidOffset > 0 && upMidOffset < ascentTime) - { - float r = (float)upMidOffset / (float)ascentTime; - float a = 2.0f; - for (uint8_t i = 0; i < 10; i++) - { - float f = log(a + 1.0f) - log(2.0f) - r * log(a); - float fp = 1.0f / (a + 1.0f) - r / a; - a -= f / fp; - if (a <= 1.001f) - { - a = 1.001f; - break; - } - } - this->alphaUp = a; - } + this->alphaUp = solveTravelAlpha((float)upMidOffset / (float)ascentTime); else - { this->alphaUp = 0.0f; - } } void SomfyShade::checkMovement() {