33
44#include <Arduino.h>
55
6+ /**
7+ * Throttle module (PWM-first pipeline)
8+ *
9+ * Responsibilities:
10+ * - Initialize throttle ADC input
11+ * - Read raw potentiometer value (0..4095)
12+ * - Convert raw value to PWM (ESC_MIN_PWM..ESC_MAX_PWM)
13+ * - Smooth PWM via an internal 8-sample ring buffer
14+ * - Apply mode-based ramp limiting and clamping in PWM domain
15+ */
16+
617// Constants related to throttle behavior
718#define DECEL_MULTIPLIER 2.0 // How much faster deceleration is vs acceleration
819
20+ // Throttle control (PWM-first) constants
21+ // Ramping in PWM microseconds per tick (~20ms per tick in throttle task)
22+ #define CHILL_MODE_MAX_PWM 1850 // 85% max power in chill mode
23+ #define CHILL_MODE_RAMP_RATE 10 // us/tick in chill mode (~1.6s 1035->1850)
24+ #define SPORT_MODE_RAMP_RATE 27 // us/tick in sport mode (~0.68s 1035->1950)
25+
926/**
1027 * Throttle easing function based on threshold/performance mode
1128 * Limits how quickly throttle can increase or decrease
1532 * @param threshold Maximum allowed change per cycle
1633 * @return Limited throttle value
1734 */
35+ /**
36+ * Limits how quickly a value may change between ticks.
37+ * - Caps acceleration to `threshold` per tick.
38+ * - Caps deceleration to `threshold * DECEL_MULTIPLIER` per tick.
39+ *
40+ * @param current Proposed value for this tick
41+ * @param last Value applied in the previous tick
42+ * @param threshold Max allowed increase per tick (units depend on caller)
43+ * @return Value adjusted to respect ramp limits
44+ */
1845int limitedThrottle (int current , int last , int threshold );
1946
47+ /** Initialize throttle input pin and ADC (12-bit on ESP32). */
48+ void initThrottleInput ();
49+
50+ /** Read raw throttle value from ADC (0..4095). */
51+ uint16_t readThrottleRaw ();
52+
53+ /** Convert raw pot reading (0..4095) to PWM microseconds. */
54+ int potRawToPwm (uint16_t raw );
55+
56+ /**
57+ * Apply mode-based ramp (us/tick) and clamp to the mode's max PWM.
58+ * Updates `prevPwm` with the final value.
59+ *
60+ * @param pwmAvg Smoothed PWM input (microseconds)
61+ * @param prevPwm Reference to previous PWM for ramping
62+ * @param performance_mode 0 = CHILL, 1 = SPORT
63+ * @return Final PWM to send to ESC (microseconds)
64+ */
65+ int applyModeRampClamp (int pwmAvg , int & prevPwm , uint8_t performance_mode );
66+
67+ // Throttle PWM filter (8-sample ring buffer)
68+ /** Clear the internal PWM smoothing buffer. */
69+ void throttleFilterClear ();
70+ /** Clear and pre-fill the buffer with `pwmValue` for a smooth restart. */
71+ void throttleFilterReset (int pwmValue );
72+ /** Push a new PWM sample into the smoothing buffer. */
73+ void throttleFilterPush (int pwmValue );
74+ /** Return the averaged PWM from the smoothing buffer. */
75+ int throttleFilterAverage ();
76+
2077/**
2178 * Checks if throttle is in safe position (below threshold)
2279 *
@@ -32,6 +89,22 @@ bool throttleSafe(int threshold);
3289 */
3390bool throttleEngaged ();
3491
92+ /**
93+ * Read throttle input and return smoothed PWM value.
94+ * This is the core throttle processing pipeline without any state logic.
95+ *
96+ * @return Smoothed PWM value from throttle input
97+ */
98+ int getSmoothedThrottlePwm ();
99+
100+ /**
101+ * Reset throttle state for clean startup/disarm.
102+ * Clears smoothing buffer and resets previous PWM tracking.
103+ *
104+ * @param prevPwm Reference to previous PWM variable to reset
105+ */
106+ void resetThrottleState (int & prevPwm );
107+
35108/**
36109 * Main throttle handling function - processes throttle input
37110 * and applies appropriate limits based on device state
0 commit comments