LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
pid.hpp
Go to the documentation of this file.
1/*
2This Source Code Form is subject to the terms of the Mozilla Public
3License, v. 2.0. If a copy of the MPL was not distributed with this
4file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7#pragma once
8
9#include "LightLib/api.h"
11
22namespace light {
30class PID {
31 public:
35 PID();
36
51 PID(double p, double i = 0, double d = 0, double start_i = 0, std::string name = "");
52
65 void constants_set(double p, double i = 0, double d = 0, double p_start_i = 0);
66
70 struct Constants {
71 double kp;
72 double ki;
73 double kd;
74 double start_i;
75 };
76
82 double small_error = 0;
83 int big_exit_time = 0;
84 double big_error = 0;
86 int mA_timeout = 0;
87 };
88
103 void exit_condition_set(int p_small_exit_time, double p_small_error, int p_big_exit_time = 0, double p_big_error = 0, int p_velocity_exit_time = 0, int p_mA_timeout = 0);
104
111 void target_set(double input);
112
119 double compute(double current);
120
131 double compute_error(double err, double current);
132
136 double target_get();
137
142
147
152
155
158
165 void velocity_sensor_secondary_set(double secondary_sensor);
166
171
179
184
192
197
205
210
218
227 light::exit_output exit_condition(pros::Motor sensor, bool print = false);
228
237 light::exit_output exit_condition(std::vector<pros::Motor> sensor, bool print = false);
238
247 light::exit_output exit_condition(pros::MotorGroup sensor, bool print = false);
248
255 void name_set(std::string name);
256
260 std::string name_get();
261
270 void i_reset_toggle(bool toggle);
271
278
283
289 double output = 0.0;
290 double cur = 0.0;
291 double error = 0.0;
292 double target = 0.0;
293 double prev_error = 0.0;
294 double prev_current = 0.0;
295 double integral = 0.0;
296 double derivative = 0.0;
297 long time = 0;
298 long prev_time = 0;
301 private:
302 double velocity_zero_main = 0.05;
303 double velocity_zero_secondary = 0.075;
304 int i = 0, j = 0, k = 0, l = 0, m = 0;
305 bool is_mA = false;
306 double second_sensor = 0.0;
307
308 std::string name;
309 bool name_active = false;
310 void exit_condition_print(light::exit_output exit_type);
311 bool reset_i_sgn = true;
312 double raw_compute();
313 bool use_second_sensor = false;
314};
315}; // namespace light
PROS API header provides high-level user functionality.
Discrete PID controller with rich exit-condition support.
Definition pid.hpp:30
bool i_reset_get()
Returns if i will reset when sgn of error changes.
void variables_reset()
Resets all variables to 0.
PID()
Default constructor.
void timers_reset()
Resets all timers for exit conditions.
double velocity_sensor_main_exit_get()
Returns the threshold that the main sensor will return 0 velocity within.
double target_get()
Returns target value.
void constants_set(double p, double i=0, double d=0, double p_start_i=0)
Set constants for PID.
void i_reset_toggle(bool toggle)
Enables / disables i resetting when sgn of error changes.
double derivative
Last derivative term.
Definition pid.hpp:296
light::exit_output exit_condition(std::vector< pros::Motor > sensor, bool print=false)
Iterative exit condition for PID.
Constants constants_get()
Returns constants.
long time
Timestamp of last compute (ms).
Definition pid.hpp:297
double output
Last computed control output.
Definition pid.hpp:289
double integral
Accumulated integral term.
Definition pid.hpp:295
exit_condition_ exit
Active exit-condition thresholds (read/write).
Definition pid.hpp:157
void velocity_sensor_secondary_set(double secondary_sensor)
Updates a secondary sensor for velocity exiting.
std::string name_get()
Returns the name of the PID that prints during exit conditions.
double error
Current error (target − cur).
Definition pid.hpp:291
Constants constants
Active PID gain constants (read/write).
Definition pid.hpp:154
bool constants_set_check()
Returns true if PID constants are set, returns false if they're all 0.
PID(double p, double i=0, double d=0, double start_i=0, std::string name="")
Constructor with constants.
void name_set(std::string name)
Sets the name of the PID that prints during exit conditions.
light::exit_output exit_condition(pros::MotorGroup sensor, bool print=false)
Iterative exit condition for PID.
light::exit_output exit_condition(pros::Motor sensor, bool print=false)
Iterative exit condition for PID.
double velocity_sensor_secondary_get()
Returns the updated secondary sensor for velocity exiting.
double prev_error
Previous-cycle error.
Definition pid.hpp:293
double target
Current target.
Definition pid.hpp:292
void velocity_sensor_main_exit_set(double zero)
Sets the threshold that the main sensor will return 0 velocity within.
void velocity_sensor_secondary_exit_set(double zero)
Sets the threshold that the secondary sensor will return 0 velocity within.
bool velocity_sensor_secondary_toggle_get()
Returns the boolean for if the secondary sensor will be updated or not.
long prev_time
Timestamp of previous compute (ms).
Definition pid.hpp:298
double compute(double current)
Computes PID.
void velocity_sensor_secondary_toggle_set(bool toggle)
Boolean for if the secondary sensor will be updated or not.
double velocity_sensor_secondary_exit_get()
Returns the threshold that the secondary sensor will return 0 velocity within.
void exit_condition_set(int p_small_exit_time, double p_small_error, int p_big_exit_time=0, double p_big_error=0, int p_velocity_exit_time=0, int p_mA_timeout=0)
Set's constants for exit conditions.
double prev_current
Previous-cycle sensor value.
Definition pid.hpp:294
void target_set(double input)
Sets PID target.
double compute_error(double err, double current)
Computes PID, but you compute the error yourself.
light::exit_output exit_condition(bool print=false)
Iterative exit condition for PID.
double cur
Last sensor value seen.
Definition pid.hpp:290
Public LightLib odometry / pose-estimation API.
Definition pid.hpp:22
exit_output
Result codes returned by PID::exit_condition.
Definition util.hpp:71
PID gain constants.
Definition pid.hpp:70
double kd
Derivative gain.
Definition pid.hpp:73
double start_i
Error threshold below which integral begins accumulating.
Definition pid.hpp:74
double ki
Integral gain.
Definition pid.hpp:72
double kp
Proportional gain.
Definition pid.hpp:71
Exit condition timing and thresholds.
Definition pid.hpp:80
int small_exit_time
ms to exit when error stays within small_error.
Definition pid.hpp:81
int big_exit_time
ms to exit when error stays within big_error.
Definition pid.hpp:83
double small_error
Error band that starts the small-exit timer.
Definition pid.hpp:82
int mA_timeout
ms above current limit before exiting.
Definition pid.hpp:86
double big_error
Error band that starts the big-exit timer.
Definition pid.hpp:84
int velocity_exit_time
ms of zero velocity before exiting.
Definition pid.hpp:85
Shared types, enums, math helpers, and unit conversions used across LightLib.