Skip to content

Commit 26f1376

Browse files
authored
Merge pull request #150 from BCLab-UNM/Drive_commenting
Drive commenting
2 parents f00fe31 + 0ef1bac commit 26f1376

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

src/behaviours/src/DriveController.cpp

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ void DriveController::Reset()
2828

2929
Result 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

228237
void 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()
272288
void 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

293309
void 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,
313329
void 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

src/behaviours/src/DriveController.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class DriveController : virtual Controller
2424

2525
Result result;
2626

27-
float left;
28-
float right;
27+
//MAX PWM is 255
28+
//abridge currently limits MAX to 120 to prevent overcurrent draw
29+
float left; //left wheels PWM value
30+
float right; //right wheels PWM value
2931

30-
bool interupt = false;
32+
bool interupt = false; //hold if interupt has occured yet
3133

3234
float rotateOnlyAngleTolerance = 0.05; //May be too low?
33-
float finalRotationTolerance = 0.1;
35+
float finalRotationTolerance = 0.1; //dead code not used
3436
const float waypointTolerance = 0.15; //15 cm tolerance.
3537

3638
//0.65 MAX value
@@ -62,6 +64,8 @@ class DriveController : virtual Controller
6264
void slowPID(float errorVel,float errorYaw, float setPointVel, float setPointYaw);
6365
void constPID(float erroVel,float constAngularError, float setPointVel, float setPointYaw);
6466

67+
//each PID movement paradigm needs at minimum two PIDs to acheive good robot motion.
68+
//one PID is for linear movement and the second for rotational movements
6569
PID fastVelPID;
6670
PID fastYawPID;
6771

src/behaviours/src/ObstacleController.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void ObstacleController::setTagData(vector<Tag> tags){
172172

173173
// this loop is to get the number of center tags
174174
if (!targetHeld) {
175-
for (int i = 0; i < tags.size(); i++) { //redudant for loop
175+
for (int i = 0; i < tags.size(); i++) { //redundant for loop
176176
if (tags[i].getID() == 256) {
177177

178178
collection_zone_seen = checkForCollectionZoneTags( tags );
@@ -264,7 +264,7 @@ void ObstacleController::setTargetHeld() {
264264

265265
void ObstacleController::setTargetHeldClear()
266266
{
267-
//adjust current state on transition from cube held to not cube helf
267+
//adjust current state on transition from cube held to cube not held
268268
if (targetHeld)
269269
{
270270
Reset();

0 commit comments

Comments
 (0)