Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralim committed Oct 15, 2023
1 parent 9052820 commit a9a190f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions source/Core/BSP/Pinecilv2/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@
#define NEEDS_VBUS_PROBE 0 // No vbus probe, its not connected in pcb
#define CANT_DIRECT_READ_SETTINGS // We cant memcpy settings due to flash cache
#define TIP_CONTROL_PID // We use PID rather than integrator
#define TIP_PID_KP 3000
#define TIP_PID_KI 5
#define TIP_PID_KD 300
#define TIP_PID_KP 35
#define TIP_PID_KI 8
#define TIP_PID_KD 200

#endif /* Pinecilv2 */

Expand Down
12 changes: 8 additions & 4 deletions source/Core/Threads/PIDThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,23 @@ template <class T, T Kp, T Ki, T Kd, T integral_limit_scale> struct PID {
const T target_delta = set_point - new_reading;

// Proportional term
const T kp_result = (Kp * target_delta) / 100;
const T kp_result = Kp * target_delta;

// Integral term
integration_running_sum += ((target_delta * interval_ms) / 3000);
// Integral term as we use mixed sampling rates, we cant assume a constant sample interval
// Thus we multiply this out by the interval time to ~= dv/dt
// Then the shift by 1000 is ms -> Seconds

integration_running_sum += (target_delta * interval_ms * Ki) / 1000;

// We constrain integration_running_sum to limit windup
// This is not overly required for most use cases but can prevent large overshoot in constrained implementations
if (integration_running_sum > integral_limit_scale * max_output) {
integration_running_sum = integral_limit_scale * max_output;
} else if (integration_running_sum < -integral_limit_scale * max_output) {
integration_running_sum = -integral_limit_scale * max_output;
}
// Calculate the integral term, we use a shift 100 to get precision in integral as we often need small amounts
T ki_result = (Ki * integration_running_sum) / 100;
T ki_result = integration_running_sum / 100;

// Derivative term
T derivative = (target_delta - previous_error_term);
Expand Down

0 comments on commit a9a190f

Please sign in to comment.