@@ -15,7 +15,7 @@ namespace GridKit
1515 *
1616 * @note The sigmoid constant (mu) value is chosen to balance accuracy
1717 * and finite derivatives. Large values more closely approximate a step
18- * function, but lead to inf or NaN derivatives .
18+ * function, but can make the transition numerically stiff .
1919 *
2020 * @tparam ScalarT - scalar data type
2121 *
@@ -27,7 +27,7 @@ namespace GridKit
2727 {
2828 using RealT = typename GridKit::ScalarTraits<ScalarT>::RealT;
2929 static constexpr RealT MU = 240.0 ;
30- return ONE <RealT> / (ONE <RealT> + std::exp (- MU * x));
30+ return HALF <RealT> * (ONE <RealT> + std::tanh ( HALF <RealT> * MU * x));
3131 }
3232
3333 /* *
@@ -153,7 +153,32 @@ namespace GridKit
153153 }
154154
155155 /* *
156- * @brief Smooth two-sided deadband function
156+ * @brief Smooth Type 1 no-offset two-sided deadband function
157+ *
158+ * Smooth approximation to a deadband that returns zero inside the band and
159+ * passes the input through unchanged outside the band.
160+ *
161+ * @tparam ScalarT - scalar data type
162+ * @tparam LowerT - data type of the lower limit
163+ * @tparam UpperT - data type of the upper limit
164+ *
165+ * @param[in] x - Input signal
166+ * @param[in] lower - Lower breakpoint
167+ * @param[in] upper - Upper breakpoint
168+ * @return Smooth no-offset deadbanded value
169+ */
170+ template <class ScalarT , typename RealT>
171+ __attribute__ ((always_inline)) inline ScalarT deadband1 (
172+ const ScalarT x,
173+ const RealT lower,
174+ const RealT upper)
175+ {
176+ assert (lower <= upper);
177+ return x * (sigmoid (lower - x) + sigmoid (x - upper));
178+ }
179+
180+ /* *
181+ * @brief Smooth Type 2 offset two-sided deadband function
157182 *
158183 * Smooth approximation to x - min(max(x, lower), upper), composed from the
159184 * smooth ramp function.
@@ -164,10 +189,10 @@ namespace GridKit
164189 * @param[in] x - Input signal
165190 * @param[in] lower - Lower breakpoint
166191 * @param[in] upper - Upper breakpoint
167- * @return Smooth deadbanded value
192+ * @return Smooth offset deadbanded value
168193 */
169194 template <class ScalarT , typename RealT>
170- __attribute__ ((always_inline)) inline ScalarT deadband (
195+ __attribute__ ((always_inline)) inline ScalarT deadband2 (
171196 const ScalarT x,
172197 const RealT lower,
173198 const RealT upper)
@@ -331,13 +356,15 @@ namespace GridKit
331356 * @return Scalar value in [0, 1]: 1 when dynamics should pass through,
332357 * 0 when integration should be blocked.
333358 */
334- template <class ScalarT , typename RealT >
359+ template <class ScalarT , typename LowerT, typename UpperT >
335360 __attribute__ ((always_inline)) inline ScalarT indicator (
336361 const ScalarT x,
337362 const ScalarT f,
338- const RealT limit_min,
339- const RealT limit_max)
363+ const LowerT limit_min,
364+ const UpperT limit_max)
340365 {
366+ using RealT = typename GridKit::ScalarTraits<ScalarT>::RealT;
367+
341368 assert (limit_min <= limit_max);
342369
343370 ScalarT above_min = above (x, limit_min);
@@ -357,20 +384,21 @@ namespace GridKit
357384 * and blocks motion that would push further into saturation.
358385 *
359386 * @tparam ScalarT - Scalar data type
360- * @tparam RealT - Real data type (see GridKit::ScalarTraits<ScalarT>::RealT)
387+ * @tparam LowerT - data type of the lower limit
388+ * @tparam UpperT - data type of the upper limit
361389 *
362390 * @param[in] x - Limited state or limited output signal
363391 * @param[in] f - Pre-limit derivative
364392 * @param[in] limit_min - Minimum limit
365393 * @param[in] limit_max - Maximum limit
366394 * @return Smooth anti-windup limited derivative
367395 */
368- template <class ScalarT , typename RealT >
396+ template <class ScalarT , typename LowerT, typename UpperT >
369397 __attribute__ ((always_inline)) inline ScalarT antiwindup (
370398 const ScalarT x,
371399 const ScalarT f,
372- const RealT limit_min,
373- const RealT limit_max)
400+ const LowerT limit_min,
401+ const UpperT limit_max)
374402 {
375403 return indicator (x, f, limit_min, limit_max) * f;
376404 }
0 commit comments