@@ -28,12 +28,14 @@ void DriveController::Reset()
2828
2929Result DriveController::DoWork ()
3030{
31+
32+ // /WARNING waypoint input must use FAST_PID at this point in time failure to set fast pid will result in no movment
3133
3234 if (result.type == behavior)
3335 {
3436 if (result.b == noChange)
3537 {
36- // if drive controller gets a no change command it is allowed to continue its previouse action
38+ // if drive controller gets a no change command it is allowed to continue its previous action
3739 // normally this will be to follow waypoints but it is not specified as such.
3840 }
3941
@@ -66,7 +68,7 @@ Result DriveController::DoWork()
6668 {
6769
6870 // Handlers and the final state of STATE_MACHINE are the only parts allowed to call INTERUPT
69- // This should be d one as little as possible. I Suggest to Use timeouts to set control bools false.
71+ // This should be d one as little as possible. I suggest using timeouts to set control bools to false.
7072 // Then only call INTERUPT if bool switches to true.
7173 case STATE_MACHINE_PRECISION_DRIVING:
7274 {
@@ -79,20 +81,26 @@ Result DriveController::DoWork()
7981 case STATE_MACHINE_WAYPOINTS:
8082 {
8183
82- // Handles route planning and navigation as well as makeing sure all waypoints are valid.
84+ // Handles route planning and navigation as well as making sure all waypoints are valid.
8385
8486 bool tooClose = true ;
87+ // while we have waypoints and they are tooClose to drive to
8588 while (!waypoints.empty () && tooClose)
8689 {
90+ // check next waypoint for distance
8791 if (hypot (waypoints.back ().x -currentLocation.x , waypoints.back ().y -currentLocation.y ) < waypointTolerance)
8892 {
93+ // if too close remove it
8994 waypoints.pop_back ();
9095 }
9196 else
9297 {
98+ // this waypoint is far enough to be worth driving to
9399 tooClose = false ;
94100 }
95101 }
102+
103+ // if we are out of waypoints then interupt and return to logic controller
96104 if (waypoints.empty ())
97105 {
98106 stateMachineState = STATE_MACHINE_WAITING;
@@ -102,12 +110,13 @@ Result DriveController::DoWork()
102110 }
103111 else
104112 {
113+ // select setpoint for heading and begin driving to the next waypoint
105114 stateMachineState = STATE_MACHINE_ROTATE;
106115 waypoints.back ().theta = atan2 (waypoints.back ().y - currentLocation.y , waypoints.back ().x - currentLocation.x );
107116 result.pd .setPointYaw = waypoints.back ().theta ;
108117
109- // cout << "**************************************************************************" << endl;
110- // cout << "Waypoint x : " << waypoints.back().x << " y : " << waypoints.back().y << endl;
118+ // cout << "**************************************************************************" << endl; //DEBUGGING CODE
119+ // cout << "Waypoint x : " << waypoints.back().x << " y : " << waypoints.back().y << endl; //DEBUGGING CODE
111120 // fall through on purpose
112121 }
113122 }
@@ -124,8 +133,8 @@ Result DriveController::DoWork()
124133 // Calculate the diffrence between current and desired heading in radians.
125134 float errorYaw = angles::shortest_angular_distance (currentLocation.theta , waypoints.back ().theta );
126135
127- // cout << "ROTATE Error yaw: " << errorYaw << " target heading : " << waypoints.back().theta << " current heading : " << currentLocation.theta << endl;
128- // cout << "Waypoint x : " << waypoints.back().x << " y : " << waypoints.back().y << " currentLoc x : " << currentLocation.x << " y : " << currentLocation.y << endl;
136+ // cout << "ROTATE Error yaw: " << errorYaw << " target heading : " << waypoints.back().theta << " current heading : " << currentLocation.theta << endl; //DEBUGGING CODE
137+ // cout << "Waypoint x : " << waypoints.back().x << " y : " << waypoints.back().y << " currentLoc x : " << currentLocation.x << " y : " << currentLocation.y << endl; //DEBUGGING CODE
129138
130139 result.pd .setPointVel = 0.0 ;
131140 // Calculate absolute value of angle
@@ -163,8 +172,8 @@ Result DriveController::DoWork()
163172 float errorYaw = angles::shortest_angular_distance (currentLocation.theta , waypoints.back ().theta );
164173 float distance = hypot (waypoints.back ().x - currentLocation.x , waypoints.back ().y - currentLocation.y );
165174
166- // cout << "Skid steer, Error yaw: " << errorYaw << " target heading : " << waypoints.back().theta << " current heading : " << currentLocation.theta << " error distance : " << distance << endl;
167- // cout << "Waypoint x : " << waypoints.back().x << " y : " << waypoints.back().y << " currentLoc x : " << currentLocation.x << " y : " << currentLocation.y << endl;
175+ // cout << "Skid steer, Error yaw: " << errorYaw << " target heading : " << waypoints.back().theta << " current heading : " << currentLocation.theta << " error distance : " << distance << endl; //DEBUGGING CODE
176+ // cout << "Waypoint x : " << waypoints.back().x << " y : " << waypoints.back().y << " currentLoc x : " << currentLocation.x << " y : " << currentLocation.y << endl; //DEBUGGING CODE
168177
169178
170179
@@ -175,7 +184,7 @@ Result DriveController::DoWork()
175184 result.pd .setPointVel = searchVelocity;
176185 if (result.PIDMode == FAST_PID)
177186 {
178- // cout << "linear velocity: " << linearVelocity << endl;
187+ // cout << "linear velocity: " << linearVelocity << endl; //DEBUGGING CODE
179188 fastPID ((searchVelocity-linearVelocity) ,errorYaw, result.pd .setPointVel , result.pd .setPointYaw );
180189 }
181190 }
@@ -227,14 +236,18 @@ bool DriveController::HasWork() { }
227236
228237void DriveController::ProcessData ()
229238{
239+ // determine if the drive commands are waypoint or precision driving
230240 if (result.type == waypoint) {
241+
242+ // sets logic controller into stand by mode while drive controller works
231243 result.type = behavior;
232244 result.b = noChange;
233245
234246 if (result.reset ) {
235247 waypoints.clear ();
236248 }
237249
250+ // add waypoints onto stack and change state to start following them
238251 if (!result.wpts .waypoints .empty ()) {
239252 waypoints.insert (waypoints.end (),result.wpts .waypoints .begin (), result.wpts .waypoints .end ());
240253 stateMachineState = STATE_MACHINE_WAYPOINTS;
@@ -243,6 +256,7 @@ void DriveController::ProcessData()
243256 else if (result.type == precisionDriving)
244257 {
245258
259+ // calculate inputs into the PIDS for precision driving
246260 if (result.PIDMode == FAST_PID)
247261 {
248262 float vel = result.pd .cmdVel -linearVelocity;
@@ -251,17 +265,19 @@ void DriveController::ProcessData()
251265 }
252266 else if (result.PIDMode == SLOW_PID)
253267 {
254- // will take longer to reach the setPoint but has less chanse of an overshoot
268+ // will take longer to reach the setPoint but has less chanse of an overshoot especially with slow feedback
255269 float vel = result.pd .cmdVel -linearVelocity;
256270 float setVel = result.pd .cmdVel ;
257271 slowPID (vel,result.pd .cmdAngularError , setVel, result.pd .setPointYaw );
258272 }
259273 else if (result.PIDMode == CONST_PID)
260274 {
275+ // vel is the same as fast PID however
276+ // this setup takes a target angular velocity to constantly turn at instead of a target heading
261277 float vel = result.pd .cmdVel - linearVelocity;
262278 float angular = result.pd .cmdAngular - angularVelocity;
263279
264- // cout << "Ang. Vel. " << angularVelocity << " Ang. Error" << angular << endl;
280+ // cout << "Ang. Vel. " << angularVelocity << " Ang. Error" << angular << endl; //DEBUGGING CODE
265281
266282 constPID (vel, angular ,result.pd .setPointVel , result.pd .setPointYaw );
267283 }
@@ -272,27 +288,27 @@ void DriveController::ProcessData()
272288void DriveController::fastPID (float errorVel, float errorYaw , float setPointVel, float setPointYaw)
273289{
274290
275- // cout << "PID FAST" << endl;
291+ // cout << "PID FAST" << endl; //DEBUGGING CODE
276292
277- float velOut = fastVelPID.PIDOut (errorVel, setPointVel);
278- float yawOut = fastYawPID.PIDOut (errorYaw, setPointYaw);
293+ float velOut = fastVelPID.PIDOut (errorVel, setPointVel); // returns PWM target to try and get error vel to 0
294+ float yawOut = fastYawPID.PIDOut (errorYaw, setPointYaw); // returns PWM target to try and get yaw error to 0
279295
280- int left = velOut - yawOut;
281- int right = velOut + yawOut;
296+ int left = velOut - yawOut; // combine yaw and vel PWM values
297+ int right = velOut + yawOut; // left and right are the same for vel output but opposite for yaw output
282298
283- int sat = 180 ;
299+ // prevent combine output from going over tihs value
300+ int sat = 180 ;
284301 if (left > sat) {left = sat;}
285302 if (left < -sat) {left = -sat;}
286303 if (right > sat) {right = sat;}
287304 if (right < -sat) {right = -sat;}
288-
289305 this ->left = left;
290306 this ->right = right;
291307}
292308
293309void DriveController::slowPID (float errorVel,float errorYaw, float setPointVel, float setPointYaw)
294310{
295- // cout << "PID SLOW" << endl;
311+ // cout << "PID SLOW" << endl; //DEBUGGING CODE
296312
297313 float velOut = slowVelPID.PIDOut (errorVel, setPointVel);
298314 float yawOut = slowYawPID.PIDOut (errorYaw, setPointYaw);
@@ -313,7 +329,7 @@ void DriveController::slowPID(float errorVel,float errorYaw, float setPointVel,
313329void DriveController::constPID (float erroVel,float constAngularError, float setPointVel, float setPointYaw)
314330{
315331
316- // cout << "PID CONST" << endl;
332+ // cout << "PID CONST" << endl; //DEBUGGING CODE
317333
318334 float velOut = constVelPID.PIDOut (erroVel, setPointVel);
319335 float yawOut = constYawPID.PIDOut (constAngularError, setPointYaw);
@@ -345,20 +361,20 @@ PIDConfig DriveController::fastVelConfig()
345361{
346362 PIDConfig config;
347363
348- config.Kp = 60 ;
349- config.Ki = 10 ;
350- config.Kd = 2 ;
351- config.satUpper = 255 ;
352- config.satLower = -255 ;
353- config.antiWindup = config.satUpper ;
354- config.errorHistLength = 4 ;
355- config.alwaysIntegral = true ;
356- config.resetOnSetpoint = true ;
364+ config.Kp = 60 ; // proportional constant
365+ config.Ki = 10 ; // integral constant
366+ config.Kd = 2 ; // derivative constant
367+ config.satUpper = 255 ; // upper limit for PID output
368+ config.satLower = -255 ; // lower limit for PID output
369+ config.antiWindup = config.satUpper ; // prevent integral from acruing error untill proportional output drops below a certain limit
370+ config.errorHistLength = 4 ; // how many time steps to average error over
371+ config.alwaysIntegral = true ; // should the integral alway be on or only when there is error
372+ config.resetOnSetpoint = true ; // reset the integral and error history whent he setpoint changes
357373 config.feedForwardMultiplier = 610 ; // gives 127 pwm at 0.4 commandedspeed ORIG:320
358- config.integralDeadZone = 0.01 ;
359- config.integralErrorHistoryLength = 10000 ;
360- config.integralMax = config.satUpper /2 ;
361- config.derivativeAlpha = 0.7 ;
374+ config.integralDeadZone = 0.01 ; // set the integral dead zone, prevented integral from growing or shrinking do to noise
375+ config.integralErrorHistoryLength = 10000 ; // how many time ticks should error history should be stored for integration
376+ config.integralMax = config.satUpper /2 ; // what is the limit of the integral output for the PID
377+ config.derivativeAlpha = 0.7 ; // dead code not used
362378
363379 return config;
364380
0 commit comments