This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pidTuner.hpp
73 lines (59 loc) · 1.77 KB
/
pidTuner.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <vector>
/**
Class contained by Pid to automatically tune PID Coefficients
*/
class PidTuner {
public:
/**
*@brief Constructs a new PidTuner
*@param ip The starting P value
*@param ii The starting I value
*@param id The starting D value
*@param Sp The step used for P
*@param Si The step used for I
*@param Sd The step used for D
*/
PidTuner(float ip, float ii, float id, float Sp = .1, float Si = .1, float Sd = .1);
/** @brief Starts a cycle to test a PID value
* @details Call Once at the beginning of a test cycle
*/
void startCycle();
/** @brief Adds the error to the total score of the current PID set
* @details Call Once each frame during a PID test
*/
void run(float err);
/** @brief Ends the test of a PID value, returns True if more tuning is needed
* @details Call Once at the end of a test cycle
*/
bool endCycle();
float getP() { return _currentPid.p; }
float getI() { return _currentPid.i; }
float getD() { return _currentPid.d; }
private:
//Struct to store P, I and D values and a score of the error
struct PidSet {
PidSet(float ip = 0, float ii = 0, float id = 0);
inline bool operator==(const PidSet& pidIn) {
return (p == pidIn.p) && (i == pidIn.i) && (d == pidIn.d);
}
inline bool operator<(const PidSet& pidIn) {
return (score < pidIn.score);
}
float p;
float i;
float d;
float score;
};
PidSet _initialPid;
PidSet _currentPid;
PidSet _bestPid;
int _cycles;
int _testNum;
float _pScale;
float _iScale;
float _dScale;
float _threshold;
float _overScale;
std::vector <PidSet> _testSets;
};