-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautonomousController.js
More file actions
395 lines (314 loc) · 13.5 KB
/
autonomousController.js
File metadata and controls
395 lines (314 loc) · 13.5 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
let SkidpadStage = {
STARTING: 0,
FINISH: 5,
STOPPING: 6
};
class AutonomousController {
constructor(newcar, targetSpeed, newLookaheadDistance, newSteeringGain, newDGain, stanlyCrosstrackGain, stanlyHeadingGain, stanly_Ke, stanly_Kv, newDelayCompensation) {
this.controlledCar = newcar;
this.stearingAngle = 0;
this.force = 0;
this.stage = SkidpadStage.STARTING;
this.progress = 0;
this.speedTarget = targetSpeed;
this.lookaheadDistance = newLookaheadDistance;
this.steeringGain = newSteeringGain;
this.dGain = newDGain;
this.delayCompensation = newDelayCompensation;
this.stanlyCrosstrackGain = stanlyCrosstrackGain;
this.stanlyHeadingGain = stanlyHeadingGain;
this.stanly_Ke = stanly_Ke;
this.stanly_Kv = stanly_Kv;
this.veltot = 0;
this.worldAngle = 0;
this.path = this.getSkidpadPath();
this.pathPosition = 2;
this.lookaheadpoint = createVector(0, 0);
this.deviationFromPath = 0; // Distance from the path to the (expected) car cog
this.crossTrackError = 0; // Distance from the path to the (expected) front axcel
this.headingError = 0;
this.yaw_diff_crosstrack = 0;
this.expectedCarPos = createVector(0, 0);
this.expectedFrontAxcelPos = createVector(0, 0);
this.expectedHeading = createVector(0, 1);
this.deviationIntegral = 0;
this.pathPoint = createVector(0, 0);
this.frontAxcelPathPoint = createVector(0, 0);
this.frontAxcelPathHeading = createVector(0, 0);
this.oldSteeringP = 0;
this.drawController = false;
}
reset(){
this.stage = SkidpadStage.STARTING;
this.progress = 0;
this.pathPosition = 2;
this.deviationFromPath = 0;
this.deviationIntegral = 0;
this.oldSteeringP = 0;
this.lookaheadpoint = createVector(0, 0);
this.pathPoint = createVector(0, 0);
this.expectedCarPos = createVector(0, 0);
this.expectedFrontAxcelPos = createVector(0, 0);
this.expectedHeading = createVector(0, 1);
}
update(dt) {
this.veltot = this.controlledCar.velocity.x;
this.worldAngle = this.controlledCar.heading.heading() - PI / 2;
if (this.worldAngle < 0) {
this.worldAngle += 2 * PI;
}
// Go to traget speed during skidpad with fuzzy logic
if (this.stage !== SkidpadStage.STOPPING && this.stage !== SkidpadStage.FINISH) {
if (this.veltot > this.speedTarget + 0.2)
this.force = -100;
else if (this.veltot < this.speedTarget)
this.force = 3080;
else
this.force = 0;
} else {
this.force = -3080;
}
this.calculateExpectedCarPos(dt); // this.expectedCarPos
this.updateStateProgress(dt); // this.pathPosition, this.progress, this.deviationFromPath, this.pathPoint, this.stage
this.lookaheadpoint = this.calculateLookaheadPoint(this.lookaheadDistance);
let lookaheaddriection = this.lookaheadpoint.copy().sub(this.expectedCarPos);
// Proportional steering: angle between the car heading and the heading to the lookahead point (A poor mans Pure Pursuit)
let steeringProportional = -lookaheaddriection.cross(this.controlledCar.heading).z * this.steeringGain;
// Stanly controller
// http://robots.stanford.edu/papers/thrun.stanley05.pdf
// https://github.com/DongChen06/PathTrackingBicycle/blob/master/controller2d.py
// Stanly controller front axcel heading error
this.headingError = transform_angle(this.frontAxcelPathHeading.heading() - this.expectedHeading.heading());
// Stanly controller cross track error term
this.yaw_diff_crosstrack = atan(this.stanly_Ke * this.crossTrackError / (this.stanly_Kv + this.veltot));
let d = max(min((steeringProportional - this.oldSteeringP) / dt, 0.2), -0.2);
this.oldSteeringP = steeringProportional;
this.stearingAngle = steeringProportional
+ d * this.dGain
+ this.stanlyCrosstrackGain * this.yaw_diff_crosstrack
+ this.stanlyHeadingGain * this.headingError;
this.stearingAngle = min(max(this.stearingAngle, -this.controlledCar.maxStearingAngle), this.controlledCar.maxStearingAngle);
this.controlledCar.setSetpoints(this.stearingAngle, this.force);
}
/**
* Uses:
* - this.delayCompensation set by user
* - current position
* - current heading
* - current velocity
* - (current steering)
* Calculates:
* - this.expectedCarPos
* - this.expectedHeading
* - this.expectedFrontAxcelPos
*/
calculateExpectedCarPos(dt){
let localX = 0;
let localY = 0;
// Turning radius
let R = 1 / (tan(this.controlledCar.stearingAngle) * cos(this.controlledCar.sideslip) / this.controlledCar.wheelbase);
if(this.drawController){
fill(50, 50, 50);
circle(wm.tX(this.controlledCar.pos.x), wm.tY(this.controlledCar.pos.y), 10);
for(let i = 1; i < 10; i++){
let dist = this.controlledCar.velocity.x * this.delayCompensation * i;
if(Math.abs(R) < 0.0001)
R = 0.0001;
let theta = dist / R;
localX = R * sin(theta);
localY = R * (1 - cos(theta));
this.expectedCarPos = this.controlledCar.pos.copy();
let ofset = createVector(localX, localY);
ofset.rotate(this.controlledCar.heading.heading() + this.controlledCar.sideslip);
this.expectedCarPos.add(ofset)
fill(150, 50, 50);
circle(wm.tX(this.expectedCarPos.x), wm.tY(this.expectedCarPos.y), 10);
}
}
// Distance traveled
let dist = this.controlledCar.velocity.x * this.delayCompensation;
if(Math.abs(R) < 0.0001)
R = 0.0001;
// Angle traveled
let theta = dist / R;
// Travled distance in local coordinates
localX = R * sin(theta);
localY = R * (1 - cos(theta));
// Expected car position
this.expectedCarPos = this.controlledCar.pos.copy();
let ofset = createVector(localX, localY);
ofset.rotate(this.controlledCar.heading.heading() + this.controlledCar.sideslip);
this.expectedCarPos.add(ofset)
// Expected heading
this.expectedHeading = this.controlledCar.heading.copy();
this.expectedHeading.rotate(theta);
// Expected front axcel position
this.expectedFrontAxcelPos = this.expectedCarPos.copy();
let heading = this.expectedHeading.copy();
this.expectedFrontAxcelPos.add(heading.mult(this.controlledCar.wheelbase * this.controlledCar.cog));
/* Expected car position without steering */
// this.expectedCarPos = this.controlledCar.pos.copy();
// let carV = this.controlledCar.heading.copy();
// carV.mult(this.delayCompensation * this.controlledCar.velocity.x);
// this.expectedCarPos.add(carV);
}
/**
* Uses:
* - lookaheadDistance (method argument)
* - this.path
* - this.pathPosition
* - this.expectedCarPos
* Returns:
* - lookaheadpoint
*/
calculateLookaheadPoint(lookAheadDist) {
let currentPoint = this.path[this.pathPosition];
let nextPoint = this.path[this.pathPosition + 1];
let lineDirection = p5.Vector.sub(currentPoint, nextPoint);
let pointToLineStart = p5.Vector.sub(this.expectedCarPos, currentPoint);
let totalDist = pointToLineStart.dot(lineDirection) / lineDirection.mag();
for (let i = this.pathPosition; i < this.path.length - 1; i++) {
let point1 = this.path[i];
let point2 = this.path[i + 1];
let dist = point1.dist(point2);
if (totalDist + dist > lookAheadDist) {
let factor = (lookAheadDist - totalDist) / dist;
return p5.Vector.lerp(point1, point2, factor);
}
totalDist += dist;
}
return this.path[this.path.length - 1];
}
/**
* Uses:
* - this.path
* - this.expectedCarPos
* Calculates:
* - this.pathPosition
* - this.progress
* - this.deviationFromPath
* - this.pathPoint
* - this.stage
* - this.crossTrackError
*/
updateStateProgress(dt = 1/100) {
// Determin the point on the path closest to the (expected) car cog
for (let i = this.pathPosition - 1; i < this.path.length - 1; i++) {
let currentPoint = this.path[i];
let nextPoint = this.path[i + 1];
let lineDirection = p5.Vector.sub(nextPoint, currentPoint);
let pointToLineStart = p5.Vector.sub(this.expectedCarPos, currentPoint);
let t = pointToLineStart.dot(lineDirection) / lineDirection.mag();
if (t < 0) {
if(i > 2)
this.pathPosition = i - 1;
break;
}
this.pathPoint = currentPoint.copy();
this.pathPoint.add(lineDirection.mult(t / lineDirection.mag()));
this.deviationFromPath = this.pathPoint.dist(this.expectedCarPos) * Math.sign(pointToLineStart.dot(lineDirection.rotate(PI / 2)))
}
// Determin the point on the path closest to the front axcel and calculate the cross track error
for (let i = this.pathPosition - 1; i < this.path.length - 1; i++) {
let currentPoint = this.path[i];
let nextPoint = this.path[i + 1];
let lineDirection = p5.Vector.sub(nextPoint, currentPoint);
let pointToLineStart = p5.Vector.sub(this.expectedFrontAxcelPos, currentPoint);
let t = pointToLineStart.dot(lineDirection) / lineDirection.mag();
if (t < 0) {
// Calculate the heading of the path at the front axcel (use linear interpolation between the heading of the current point and the previous point)
let pathHeadingPrevious = p5.Vector.sub(currentPoint, this.path[i-1]);
let scaleFactor = -t / pathHeadingPrevious.mag();
this.frontAxcelPathHeading = lineDirection.copy();
this.frontAxcelPathHeading.normalize();
pathHeadingPrevious.normalize();
this.frontAxcelPathHeading.lerp(pathHeadingPrevious, scaleFactor);
break;
}
this.frontAxcelPathPoint = currentPoint.copy();
this.frontAxcelPathPoint.add(lineDirection.mult(t / lineDirection.mag()));
this.crossTrackError = -this.frontAxcelPathPoint.dist(this.expectedFrontAxcelPos) * Math.sign(pointToLineStart.dot(lineDirection.rotate(PI / 2)))
}
this.progress = this.pathPosition / this.path.length;
if (this.stage == SkidpadStage.STARTING && this.pathPosition + 60 >= this.path.length)
this.stage = SkidpadStage.STOPPING;
if(this.stage == SkidpadStage.STOPPING && this.veltot < 0.1)
this.stage = SkidpadStage.FINISH;
}
/** Draw everything related to visualizing the path */
draw() {
if(!this.drawController)
return;
noStroke();
fill(250, 0, 0);
circle(wm.tX(this.lookaheadpoint.x), wm.tY(this.lookaheadpoint.y), 30);
fill(0, 0, 250);
circle(wm.tX(this.expectedCarPos.x), wm.tY(this.expectedCarPos.y), 20);
fill(100, 100, 250);
circle(wm.tX(this.expectedFrontAxcelPos.x), wm.tY(this.expectedFrontAxcelPos.y), 15);
fill(0, 250, 0);
// circle(wm.tX(this.path[this.pathPosition].x), wm.tY(this.path[this.pathPosition].y), 20);
circle(wm.tX(this.pathPoint.x), wm.tY(this.pathPoint.y), 20);
circle(wm.tX(this.frontAxcelPathPoint.x), wm.tY(this.frontAxcelPathPoint.y), 20);
noFill();
stroke(0);
circle(wm.tX(this.pathPoint.x), wm.tY(this.pathPoint.y), 2 * wm.scaleW2S(this.deviationFromPath));
circle(wm.tX(this.frontAxcelPathPoint.x), wm.tY(this.frontAxcelPathPoint.y), 2 * wm.scaleW2S(this.crossTrackError));
let headingPoint = this.frontAxcelPathHeading.copy();
headingPoint.mult(5);
headingPoint.add(this.frontAxcelPathPoint)
strokeWeight(2);
line(wm.tX(this.frontAxcelPathPoint.x), wm.tY(this.frontAxcelPathPoint.y), wm.tX(headingPoint.x), wm.tY(headingPoint.y));
strokeWeight(1);
noStroke();
fill(250, 50, 250);
circle(wm.tX(this.path[this.pathPosition].x), wm.tY(this.path[this.pathPosition].y), 10);
circle(wm.tX(this.path[this.pathPosition + 1].x), wm.tY(this.path[this.pathPosition + 1].y), 10);
for (let i = 0; i < this.path.length; i++) {
let p = this.path[i];
if (this.pathPosition > i)
fill(0, 250, 0);
else
fill(250, 100, 0);
circle(wm.tX(p.x), wm.tY(p.y), 5);
}
noFill();
stroke(0);
beginShape();
for (let i = 0; i < this.path.length; i++) {
let p = this.path[i];
vertex(wm.tX(p.x), wm.tY(p.y));
}
endShape();
}
/**
* Returns the path of the skidpad
* @param { Number } skdipadRadius
* @returns { Array } Array of p5.Vector with the path of the skidpad
*/
getSkidpadPath(skdipadRadius = 9.125) {
let path = [];
let spCenterY = 15;
let pathStep = 0.3;
for (let i = -1; i < 15; i+=pathStep) {
path.push(createVector(0, i));
}
for (let i = 0; i < 4 * PI; i += pathStep * 2 * PI / 57.33) {
path.push(createVector(skdipadRadius - cos(i) * skdipadRadius, spCenterY + sin(i) * skdipadRadius));
}
for (let i = 0; i < 4 * PI; i += pathStep * 2 * PI / 57.33) {
path.push(createVector(-skdipadRadius + cos(i) * skdipadRadius, spCenterY + sin(i) * skdipadRadius));
}
for (let i = 15; i < 40; i += pathStep) {
path.push(createVector(0, i));
}
return path;
}
}
function transform_angle(angle){
if(angle > PI)
angle -= 2 * PI;
else if(angle < -PI)
angle += 2 * PI;
return angle;
}