@@ -11,10 +11,13 @@ PID::PID(PIDConfig config)
1111float PID::PIDOut (float calculatedError, float setPoint)
1212{
1313
14+ // cout << "ErrorSize: " << Error.size() << endl;
15+
1416 if (Error.size () >= config.errorHistLength )
1517 {
1618 Error.pop_back ();
1719 }
20+
1821 Error.insert (Error.begin (), calculatedError); // insert new error into vector for history purposes.
1922
2023 float P = 0 ; // proportional yaw output
@@ -31,17 +34,48 @@ float PID::PIDOut(float calculatedError, float setPoint)
3134 }
3235
3336 // feed forward
34- float FF = config.feedForwardMultiplier * setPoint;
37+ // float FF = config.feedForwardMultiplier * setPoint;
38+ float FF = (pow (setPoint, 3 ) * config.feedForwardMultiplier ) + (setPoint * (config.feedForwardMultiplier / 4.6 ));
39+
40+ if (!config.alwaysIntegral && Error.size () > 1 )
41+ {
42+ // check the change of sign to see if the rover overshot its goal
43+ float sign_change = Error[0 ] / Error[1 ];
44+ // if the sign has changed between the previous error and the current error
45+ if (sign_change < 0 )
46+ {
47+ // reset the integral history and values
48+ integralErrorHistArray.clear ();
49+ integralErrorHistArray.resize (config.integralErrorHistoryLength , 0.0 );
50+ I = 0 ;
51+ step = 0 ;
52+
53+
54+ // Store error zero into temp variable
55+ float error_zero = Error[0 ];
56+
57+ // clear the error history in order to prevent movement in the incorrect direction because of the sign change
58+ Error.clear ();
59+
60+ // put back into vector
61+ Error.push_back (error_zero);
62+ }
63+ }
64+
65+
3566
3667 // error averager
3768 float avgError = 0 ;
38- if (Error.size () >= config.errorHistLength ) {
39- for (int i = 0 ; i < config.errorHistLength ; i++) {
69+ if (Error.size () >= config.errorHistLength )
70+ {
71+ for (int i = 0 ; i < config.errorHistLength ; i++)
72+ {
4073 avgError += Error[i];
4174 }
4275 avgError /= config.errorHistLength ;// config.errorHistLength;
4376 }
44- else {
77+ else
78+ {
4579 avgError = calculatedError;
4680 }
4781
@@ -105,7 +139,7 @@ float PID::PIDOut(float calculatedError, float setPoint)
105139 }
106140
107141 // Derivative
108- if (fabs (P) < config.antiWindup )
142+ if (Error. size () < 4 ) // ( fabs(P) < config.antiWindup)
109143 {
110144 float avgPrevError = 0 ;
111145 for (int i = 1 ; i < Error.size (); i++)
@@ -121,11 +155,17 @@ float PID::PIDOut(float calculatedError, float setPoint)
121155 avgPrevError = Error[0 ];
122156 }
123157
124- D = config.Kd * (Error[0 ] - Error[1 ]) * hz;
158+
159+ D = config.Kd * ((Error[0 ]+Error[1 ])/2 - (Error[2 ]+Error[3 ])/2 ) * hz;
160+
161+ // cout << "PID Error[0]: " << Error[0] << ", Error[1]: " << Error[1] << ", Error[2]: " << Error[2] << ", Error[3]: " << Error[3] << endl;
162+
125163 }
126164
127165 float PIDOut = P + I + D + FF;
128166
167+ // cout << "PID P: " << P << ", I: " << I << ", D: " << D << ", FF: " << FF << endl;
168+
129169 if (PIDOut > config.satUpper ) // cap vel command
130170 {
131171 PIDOut = config.satUpper ;
@@ -135,7 +175,5 @@ float PID::PIDOut(float calculatedError, float setPoint)
135175 PIDOut = config.satLower ;
136176 }
137177
138- cout << " PID OUTPUT: " << PIDOut << endl;
139-
140178 return PIDOut;
141179}
0 commit comments