|
| 1 | +# ESP-Drone PID Configuration Analysis |
| 2 | + |
| 3 | +You are analyzing the **ESP-Drone firmware** to understand how PID gains are defined, scaled, and applied to determine the correct gain values for comparison with another firmware. |
| 4 | + |
| 5 | +## Your Task |
| 6 | + |
| 7 | +Extract and document the complete PID control pipeline from ESP-Drone, focusing on: |
| 8 | + |
| 9 | +1. **PID Gain Definitions** |
| 10 | +2. **Unit Scaling and Conversions** |
| 11 | +3. **Control Pipeline Flow** |
| 12 | +4. **Effective Gain Calculations** |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Section 1: Find PID Gain Definitions |
| 17 | + |
| 18 | +### Search Strategy |
| 19 | +```bash |
| 20 | +# Search for PID gain definitions (likely in config files) |
| 21 | +grep -r "pidAngleRoll\|pidRateRoll\|ANGLE.*KP\|RATE.*KP" --include="*.h" --include="*.c" |
| 22 | +grep -r "PID_ANGLE\|PID_RATE" --include="*.h" --include="*.c" |
| 23 | +grep -r "Kp.*=.*[0-9]\|Ki.*=.*[0-9]\|Kd.*=.*[0-9]" --include="*.h" --include="*.c" |
| 24 | +``` |
| 25 | + |
| 26 | +### Questions to Answer |
| 27 | + |
| 28 | +1. **Where are PID gains defined?** |
| 29 | + - File path and line numbers |
| 30 | + - Are they in a config file, header, or source file? |
| 31 | + |
| 32 | +2. **What are the EXACT gain values?** |
| 33 | + ```cpp |
| 34 | + // Find and report: |
| 35 | + PID_ANGLE_ROLL_KP = ? |
| 36 | + PID_ANGLE_ROLL_KI = ? |
| 37 | + PID_ANGLE_ROLL_KD = ? |
| 38 | + |
| 39 | + PID_RATE_ROLL_KP = ? |
| 40 | + PID_RATE_ROLL_KI = ? |
| 41 | + PID_RATE_ROLL_KD = ? |
| 42 | + |
| 43 | + // Same for PITCH and YAW |
| 44 | + ``` |
| 45 | + |
| 46 | +3. **What are the gain UNITS?** |
| 47 | + - Are gains defined as integers or floats? |
| 48 | + - Are there unit conversion macros (e.g., `#define KP(x) ((x) * 1000)`)? |
| 49 | + - Are gains stored as fixed-point (e.g., multiply by 1000 for precision)? |
| 50 | + |
| 51 | +4. **Are there multiple gain sets?** |
| 52 | + - Default vs tuned values? |
| 53 | + - Different modes (ANGLE vs ACRO)? |
| 54 | + - Flight mode dependent gains? |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Section 2: Trace PID Implementation |
| 59 | + |
| 60 | +### Find PID Controller Code |
| 61 | + |
| 62 | +```bash |
| 63 | +# Search for PID computation functions |
| 64 | +grep -r "pidUpdate\|pidCompute\|pid_update\|PID.*update" --include="*.c" --include="*.cpp" |
| 65 | +grep -r "error.*=.*setpoint.*-.*measurement" --include="*.c" |
| 66 | +``` |
| 67 | + |
| 68 | +### Questions to Answer |
| 69 | + |
| 70 | +1. **Where is the PID controller implemented?** |
| 71 | + - File path and function name |
| 72 | + - Is it a class, struct, or function? |
| 73 | + |
| 74 | +2. **What is the PID formula?** |
| 75 | + ```cpp |
| 76 | + // Find and report the exact computation: |
| 77 | + error = setpoint - measurement; |
| 78 | + iTerm += Ki * error * dt; |
| 79 | + dTerm = Kd * (error - lastError) / dt; |
| 80 | + output = Kp * error + iTerm + dTerm; |
| 81 | + ``` |
| 82 | + |
| 83 | +3. **Are gains scaled during computation?** |
| 84 | + - Are Kp/Ki/Kd divided or multiplied by constants? |
| 85 | + - Example: `output = (Kp * error) / 1000` (fixed-point scaling) |
| 86 | + |
| 87 | +4. **What are the PID output limits?** |
| 88 | + ```cpp |
| 89 | + // Find the saturation limits: |
| 90 | + Angle PID output limits: min = ?, max = ? |
| 91 | + Rate PID output limits: min = ?, max = ? |
| 92 | + ``` |
| 93 | + |
| 94 | +5. **What is the PID output data type?** |
| 95 | + - `float`, `int16_t`, `int32_t`? |
| 96 | + - This is CRITICAL for understanding magnitude! |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Section 3: Control Pipeline Flow |
| 101 | + |
| 102 | +### Find Cascaded PID Chain |
| 103 | + |
| 104 | +```bash |
| 105 | +# Search for control loop |
| 106 | +grep -r "controllerTask\|control_loop\|pidLoop" --include="*.c" |
| 107 | +grep -r "angle.*pid\|rate.*pid" --include="*.c" |
| 108 | +``` |
| 109 | + |
| 110 | +### Questions to Answer |
| 111 | + |
| 112 | +1. **Trace the control flow from RC input to motors:** |
| 113 | + |
| 114 | + ``` |
| 115 | + RC Input (roll/pitch/yaw setpoints) |
| 116 | + ↓ |
| 117 | + [Angle PID] → angle_output (what units? deg/s?) |
| 118 | + ↓ |
| 119 | + [Rate PID] → rate_output (what units? int16? float?) |
| 120 | + ↓ |
| 121 | + [Scaling/conversion?] → scaled_output |
| 122 | + ↓ |
| 123 | + [Mixer] → motor commands (what units? PWM duty? 0-65535?) |
| 124 | + ↓ |
| 125 | + [Motor output] → actual PWM |
| 126 | + ``` |
| 127 | + |
| 128 | +2. **What are the units at each stage?** |
| 129 | + - RC input: degrees? ±1000? ±32767? |
| 130 | + - Angle PID input: degrees |
| 131 | + - Angle PID output: deg/s (rate setpoint) |
| 132 | + - Rate PID input: deg/s |
| 133 | + - Rate PID output: **WHAT UNITS?** (int16? float? ±32767? ±100?) |
| 134 | + - Mixer input: **WHAT UNITS?** |
| 135 | + - Motor output: PWM duty cycle (0-65535? 0-1000?) |
| 136 | + |
| 137 | +3. **Are there scaling factors between stages?** |
| 138 | + ```cpp |
| 139 | + // Look for code like: |
| 140 | + rate_output = rate_pid.compute(...); |
| 141 | + rate_output_scaled = rate_output / SOME_CONSTANT; |
| 142 | + mixer_input = rate_output_scaled * ANOTHER_CONSTANT; |
| 143 | + ``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Section 4: Mixer Configuration |
| 148 | + |
| 149 | +### Find Mixer Code |
| 150 | + |
| 151 | +```bash |
| 152 | +# Search for mixer implementation |
| 153 | +grep -r "mixer\|motorMix\|powerDistribution" --include="*.c" --include="*.h" |
| 154 | +grep -r "motor\[0\]\|motor\[1\]\|motor\[2\]\|motor\[3\]" --include="*.c" |
| 155 | +``` |
| 156 | + |
| 157 | +### Questions to Answer |
| 158 | + |
| 159 | +1. **What are the mixer equations?** |
| 160 | + ```cpp |
| 161 | + // Find the exact formulas: |
| 162 | + motor[0] = thrust ± roll ± pitch ± yaw; |
| 163 | + motor[1] = thrust ± roll ± pitch ± yaw; |
| 164 | + motor[2] = thrust ± roll ± pitch ± yaw; |
| 165 | + motor[3] = thrust ± roll ± pitch ± yaw; |
| 166 | + ``` |
| 167 | + |
| 168 | +2. **Are PID outputs scaled before mixing?** |
| 169 | + ```cpp |
| 170 | + // Look for code like: |
| 171 | + int16_t r = control->roll / 2.0f; // ← CRITICAL! |
| 172 | + int16_t p = control->pitch / 2.0f; |
| 173 | + int16_t y = control->yaw; |
| 174 | + ``` |
| 175 | + |
| 176 | +3. **What is the thrust range?** |
| 177 | + - RC throttle input: 0-1000? 0-65535? |
| 178 | + - Mixer thrust input: same units or converted? |
| 179 | + |
| 180 | +4. **What is the motor output range?** |
| 181 | + - PWM duty cycle: 0-65535? 1000-2000? 0-100%? |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## Section 5: Calculate Effective Gains |
| 186 | + |
| 187 | +### Combine All Scaling Factors |
| 188 | + |
| 189 | +Once you have all the information above, calculate the **effective gain** from angle error to motor differential: |
| 190 | + |
| 191 | +``` |
| 192 | +Example calculation: |
| 193 | +
|
| 194 | +Given: Roll angle error = 10 degrees |
| 195 | +
|
| 196 | +Step 1: Angle PID |
| 197 | + output = Kp_angle * error = 6.0 * 10 = 60 deg/s (rate setpoint) |
| 198 | +
|
| 199 | +Step 2: Rate PID (assume measured rate = 0) |
| 200 | + error = 60 - 0 = 60 deg/s |
| 201 | + output = Kp_rate * error = 190.0 * 60 = 11,400 (int16) |
| 202 | +
|
| 203 | +Step 3: Mixer scaling (if roll/2 division exists) |
| 204 | + scaled_output = 11,400 / 2 = 5,700 |
| 205 | +
|
| 206 | +Step 4: Mixer differential (X-quad) |
| 207 | + Left motors = base + roll = base + 5,700 |
| 208 | + Right motors = base - roll = base - 5,700 |
| 209 | + Left-Right differential = 11,400 PWM units |
| 210 | +
|
| 211 | +Step 5: Convert to percentage of full range |
| 212 | + Differential = 11,400 / 65,535 ≈ 17.4% of full motor range |
| 213 | +``` |
| 214 | + |
| 215 | +### Report Format |
| 216 | + |
| 217 | +```markdown |
| 218 | +## Effective Gain Calculation |
| 219 | + |
| 220 | +**Angle-to-Rate Gain**: Kp_angle = X.X |
| 221 | +**Rate-to-Output Gain**: Kp_rate = X.X |
| 222 | +**Mixer Scaling Factor**: X.X (e.g., 0.5 if divide by 2) |
| 223 | +**Output Range**: 0 to XXXXX |
| 224 | + |
| 225 | +**Effective Gain** (10° angle error → motor differential): |
| 226 | + = Kp_angle * Kp_rate * mixer_scaling * 2 (differential multiplier) |
| 227 | + = X.X * X.X * X.X * 2 |
| 228 | + = XXXXX motor units per 10° error |
| 229 | + |
| 230 | +**As percentage of motor range**: |
| 231 | + = (XXXXX / MAX_MOTOR_VALUE) * 100% |
| 232 | + = XX.X% |
| 233 | +``` |
| 234 | + |
| 235 | +--- |
| 236 | + |
| 237 | +## Section 6: Find Any Additional Scaling |
| 238 | + |
| 239 | +### Search for Hidden Scaling Factors |
| 240 | + |
| 241 | +```bash |
| 242 | +# Look for authority scaling, throttle gating, etc. |
| 243 | +grep -r "authority\|stab.*gain\|throttle.*scale" --include="*.c" |
| 244 | +grep -r "MIN_THROTTLE\|MAX_THROTTLE\|IDLE" --include="*.h" |
| 245 | +``` |
| 246 | + |
| 247 | +### Questions to Answer |
| 248 | + |
| 249 | +1. **Is there dynamic authority scaling based on throttle?** |
| 250 | + - Does PID output get reduced at low/high throttle? |
| 251 | + |
| 252 | +2. **Is there a "stabilization gain" multiplier?** |
| 253 | + - Does PID output ramp up based on throttle level? |
| 254 | + |
| 255 | +3. **Are there output clamps/limits?** |
| 256 | + - Are PID outputs clamped before reaching the mixer? |
| 257 | + |
| 258 | +4. **Are there any unit conversion constants?** |
| 259 | + - Example: `#define ANGLE_TO_RATE_SCALE 1000` |
| 260 | + |
| 261 | +--- |
| 262 | + |
| 263 | +## Section 7: Compare to Reference Firmware |
| 264 | + |
| 265 | +The reference firmware has: |
| 266 | + |
| 267 | +```cpp |
| 268 | +// Reference firmware (drone-firmware): |
| 269 | +PID_ANGLE_ROLL_KP = 3.0 |
| 270 | +PID_ANGLE_PITCH_KP = 3.0 |
| 271 | +PID_RATE_ROLL_KP = 0.12 |
| 272 | +PID_RATE_PITCH_KP = 0.12 |
| 273 | + |
| 274 | +// PID outputs: float (±100 typical range) |
| 275 | +// Motor outputs: 0-1000 (PWM duty) |
| 276 | +// Mixer: NO scaling (direct application of roll/pitch/yaw) |
| 277 | +``` |
| 278 | + |
| 279 | +### Comparison Table |
| 280 | + |
| 281 | +Create a side-by-side comparison: |
| 282 | + |
| 283 | +| Parameter | ESP-Drone | Reference Firmware | Ratio | |
| 284 | +|-----------|-----------|-------------------|-------| |
| 285 | +| Angle Kp | X.X | 3.0 | X.Xx | |
| 286 | +| Rate Kp | X.X | 0.12 | X.Xx | |
| 287 | +| PID output type | int16/float? | float | - | |
| 288 | +| PID output range | ±XXXXX | ±100 | XXXx | |
| 289 | +| Mixer scaling | /2 or none? | none | X.Xx | |
| 290 | +| Motor range | 0-XXXXX | 0-1000 | XXx | |
| 291 | +| **Effective gain** | XXXXX | 12 | XXXXx | |
| 292 | + |
| 293 | +--- |
| 294 | + |
| 295 | +## Section 8: Critical Files to Check |
| 296 | + |
| 297 | +Based on typical ESP-IDF/Arduino drone firmware structure, prioritize these files: |
| 298 | + |
| 299 | +1. **`components/config/include/config.h`** - PID gain definitions |
| 300 | +2. **`components/core/crazyflie/modules/src/controller_pid.c`** - PID implementation |
| 301 | +3. **`components/core/crazyflie/modules/src/power_distribution_quadrotor.c`** - Mixer |
| 302 | +4. **`components/drivers/general/motors/src/motors.c`** - Motor output |
| 303 | +5. **`main/flight/flight_control.c`** or similar - Main control loop |
| 304 | + |
| 305 | +If files don't match these names, search for similar patterns. |
| 306 | + |
| 307 | +--- |
| 308 | + |
| 309 | +## Output Format |
| 310 | + |
| 311 | +Organize your findings as follows: |
| 312 | + |
| 313 | +### 1. PID Gain Definitions |
| 314 | +- File: `path/to/file.h:line` |
| 315 | +- Values: `Kp=X.X, Ki=X.X, Kd=X.X` |
| 316 | +- Units: int16/float, any scaling macros |
| 317 | + |
| 318 | +### 2. PID Implementation |
| 319 | +- File: `path/to/file.c:line` |
| 320 | +- Formula: [exact code snippet] |
| 321 | +- Output type: int16_t/float |
| 322 | +- Output limits: ±XXXXX |
| 323 | + |
| 324 | +### 3. Control Pipeline |
| 325 | +- [Diagram showing units at each stage] |
| 326 | +- Scaling factors between stages |
| 327 | + |
| 328 | +### 4. Mixer Configuration |
| 329 | +- Equations: [motor mixing formulas] |
| 330 | +- Scaling: [roll/pitch/yaw scaling before mixing] |
| 331 | +- Output range: 0-XXXXX |
| 332 | + |
| 333 | +### 5. Effective Gain Calculation |
| 334 | +- [Step-by-step calculation] |
| 335 | +- Final result: XXXXX motor units per 10° angle error |
| 336 | + |
| 337 | +### 6. Comparison Table |
| 338 | +- [Side-by-side with reference firmware] |
| 339 | + |
| 340 | +### 7. Key Findings |
| 341 | +- **Critical differences** that explain magnitude mismatch |
| 342 | +- **Recommended scaling factors** to port gains to reference firmware |
| 343 | + |
| 344 | +--- |
| 345 | + |
| 346 | +## Critical Questions |
| 347 | + |
| 348 | +Answer these YES/NO questions with file:line citations: |
| 349 | + |
| 350 | +1. **Are PID gains stored as integers multiplied by 1000?** YES/NO (`file:line`) |
| 351 | +2. **Are PID outputs int16_t (±32767 range)?** YES/NO (`file:line`) |
| 352 | +3. **Is roll/pitch divided by 2 before mixing?** YES/NO (`file:line`) |
| 353 | +4. **Is motor PWM range 0-65535 (16-bit)?** YES/NO (`file:line`) |
| 354 | +5. **Is there dynamic authority scaling?** YES/NO (`file:line`) |
| 355 | + |
| 356 | +--- |
| 357 | + |
| 358 | +## Begin Analysis |
| 359 | + |
| 360 | +Start by searching for PID gain definitions, then trace the entire control pipeline. Provide code snippets and exact line numbers for every claim. |
0 commit comments