-
Notifications
You must be signed in to change notification settings - Fork 9
/
controllers.cpp
392 lines (365 loc) · 15.5 KB
/
controllers.cpp
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
#include "controllers.h"
#include <stdio.h>
#include <assert.h>
#include <algorithm>
#include <utility>
#include <math.h>
#include <QtCore/QString>
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include <QtCore/QTextStream>
using namespace std;
namespace Controllers {
MiscData kgpkubs(Pose initialPose, Pose finalPose, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
{
Q_UNUSED(prevSpeed);
int clearance = CLEARANCE_PATH_PLANNER;
static float prevVel = 0;
Vector2D<int> initial(initialPose.x(), initialPose.y());
Vector2D<int> final(finalPose.x(), finalPose.y());
double curSlope = initialPose.theta();
double finalSlope = finalPose.theta();
double theta = normalizeAngle(Vector2D<int>::angle(final, initial));
double phiStar = finalSlope;
double phi = curSlope;
//directionalAngle = (cos(atan2(final.y - initial.y, final.x - initial.x) - curSlope) >= 0) ? curSlope : normalizeAngle(curSlope - PI);
int dist = Vector2D<int>::dist(final, initial); // Distance of next waypoint from the bot
double alpha = normalizeAngle(theta - phiStar);
double beta = (fabs(alpha) < fabs(atan2(clearance, dist))) ? (alpha) : SGN(alpha) * atan2(clearance, dist);
double thetaD = normalizeAngle(theta + beta);
float delta = normalizeAngle(thetaD - phi);
double r = (sin(delta) * sin(delta)) * SGN(tan(delta));
double t = (cos(delta) * cos(delta)) * SGN(cos(delta));
if(!(t >= -1.0 && t <= 1.0 && r >= -1.0 && r <= 1.0)) {
printf("what? delta = %f, initial = (%lf, %lf, %lf)\n", delta, initialPose.x(), initialPose.y(), initialPose.theta());
assert(0);
}
float fTheta = asin(sqrt(fabs(r)));
fTheta = 1 - fTheta/(PI/2);
fTheta = pow(fTheta,2.2) ;
float fDistance = (dist > BOT_POINT_THRESH*3) ? 1 : dist / ((float) BOT_POINT_THRESH *3);
float fTot = fDistance * fTheta;
fTot = 0.2 + fTot*(1-0.2);
float profileFactor = MAX_BOT_SPEED * fTot;
if(fabs(r)<0.11)
profileFactor*=2;
{
if(fabs(profileFactor-prevVel)>MAX_BOT_LINEAR_VEL_CHANGE)
{
if(profileFactor>prevVel)
profileFactor=prevVel+MAX_BOT_LINEAR_VEL_CHANGE;
else
profileFactor=prevVel-MAX_BOT_LINEAR_VEL_CHANGE;
}
prevVel=profileFactor;
}
if(profileFactor>1.5*MAX_BOT_SPEED)
profileFactor = 1.5*MAX_BOT_SPEED;
else if(profileFactor <-1.5*MAX_BOT_SPEED)
profileFactor = -1.5*MAX_BOT_SPEED;
prevVel=profileFactor;
r *= 0.5*profileFactor;
t *= profileFactor;
vl = t-r;
vr = t+r;
return MiscData();
}
MiscData CMU(Pose s, Pose e, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
{
Q_UNUSED(prevSpeed);
int maxDis = 2*sqrt(HALF_FIELD_MAXX*HALF_FIELD_MAXX+HALF_FIELD_MAXY*HALF_FIELD_MAXY);
Vector2D<int> initial(s.x(), s.y());
Vector2D<int> final(e.x(), e.y());
int distance = Vector2D<int>::dist(initial, final);
double phi = s.theta();
double theta = Vector2D<int>::angle(final, initial);
double phiStar = e.theta();
double alpha = normalizeAngle(theta-phiStar);
double beta = atan2((maxDis/15.0), distance);
double thetaD = theta + min(alpha, beta);
thetaD = theta; // removing the phiStar part. wasnt able to get it to work right.
double delta = normalizeAngle(thetaD - phi);
double t = cos(delta)*cos(delta)*SGN(cos(delta));
double r = sin(delta)*sin(delta)*SGN(sin(delta));
vl = 100*(t-r);
vr = 100*(t+r);
return MiscData();
}
QString outputFilename = "/home/robocup/FileLog.txt STorage/FileLogQT6.txt";
QFile outputFile(outputFilename);
int va=-1;
MiscData DynamicWindow(Pose s, Pose e, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
{
// qDebug()<<"prevSpeed = "<<prevSpeed<<" prevOmega = "<<prevOmega;
// qDebug()<<"Inside function Dynamic Window";
// sprintf(buf, "in function Dynamic Window");
QTextStream outStream(&outputFile);
if(va==-1)
{
outputFile.open(QIODevice::WriteOnly);
outStream<<"\n\n\n\n \t\t\t\t The Log of QT Code\n";
va=0;
}
va=0;
outputFile.open(QIODevice::Append);
outStream<<" x = "<< s.x()<<" & y = "<< s.y();
const int del_v_max = 15; //ticks
const float step = 1; //ticks
const float max_vel = 100; //ticks
const float a_r_max = 380; //cm/s^2
// const float PI= 3.14159;
const float t=0.016;
const float k=5;
int count=0;
double arr[100000][3];
//float prevSpeed, prevOmega,alpha,acc,theta,x,y,reqtheta,dtheta;
//int i,j; //i=vr,,j=vl
for(float del_vr=-del_v_max;del_vr<=del_v_max;del_vr+=step)
{
for(float del_vl=-del_v_max;del_vl<=del_v_max;del_vl+=step)
{
float newSpeed= prevSpeed + Constants::ticksToCmS*(del_vr+del_vl)/2; // cm/s
float newOmega= prevOmega + Constants::ticksToCmS*(del_vr-del_vl)/Constants::d; // cm/s //D Is the constant breadth of the bot
// qDebug()<<"Trying newSpeed = "<<newSpeed<<" newOmega = "<<newOmega;
if(abs(newSpeed/Constants::ticksToCmS) >max_vel || abs(newOmega/Constants::ticksToCmS) >(2*max_vel)/Constants::d)
continue;
if(abs((newSpeed+(Constants::d*newOmega)/2)) > max_vel || abs((newSpeed-(Constants::d *newOmega)/2)) > max_vel)
continue;
if((newSpeed*newOmega)>=a_r_max*sqrt(1-pow((del_vr + del_vl)/(2*del_v_max),2)))
continue; // constraint from the equation of ellipse.
//if(count>400) break; //we would take only a fixed number of points under consideration
// qDebug()<<"Trying newSpeed = "<<newSpeed<<" newOmega = "<<newOmega;
// float alpha=(newOmega-prevOmega)/t; // rad/cm^2
float theta= s.theta() + (newOmega*t); //rad
theta = normalizeAngle(theta);
float x= s.x() + (newSpeed*cos(theta)*t);
float y= s.y() + (newSpeed*sin(theta)*t);
float reqtheta=atan2((e.y()-y),(e.x()-x));
float dtheta=firaNormalizeAngle(theta-reqtheta);
arr[count][0]= sqrt(pow((x - e.x()),2) + pow((y - e.y()),2)) + k*pow((dtheta),2) ; // our objective function
arr[count][1]= newSpeed;
arr[count][2]= newOmega;
count++;
}
}
float min= arr[0][0];
float best_v=arr[0][1];
float best_w=arr[0][2];
for(int i=0;i<count;i++)
{
if(arr[i][0]<min)
{
min=arr[i][0];
best_v= arr[i][1];
best_w = arr[i][2];
}
}
float theta= s.theta() + (best_w*t); //rad
theta = normalizeAngle(theta);
float x= s.x() + (best_v*cos(theta)*t);
float y= s.y() + (best_v*sin(theta)*t);
//outStream<<best_v<< '\t'<< best_w <<'\t'<< x <<'\t'<< y<<endl;
vr=(best_v)+(Constants::d *best_w)/2 ; // update velocity
vl=(2*best_v) - vr; // update omega
vr/=Constants::ticksToCmS;
vl/=Constants::ticksToCmS;
outStream<<" vl = "<<vl<<" & vr = "<<vr<<'\n';
return MiscData();
}
//MiscData DynamicWindow(Pose s, Pose e, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
//{
// const int del_v = 10;
// const float step = 1;
// int count=0;
// double arr[1000][3];
// //float oldvel, oldome,alpha,acc,theta,x,y,reqtheta,dtheta;
// float t=0.016;
// //int i,j; //i=vr,,j=vl
// int k=5;
// for(float del_vr=-del_v;i<=del_v;i+=step)
// {
// for(float j=-del_v;j<=del_v;j+=step)
// {
// //oldvel=prevSpeed;
// //oldome=prevOmega;
// newSpeed= prevSpeed + Constants::ticksToCmS*(i+j)/2;
// newOmega= prevOmega + Constants::ticksToCmS*(i-j)/Constants::d; //D Is the constant breadth of the bot
// if(abs(prevSpeed/Constants::ticksToCmS) >100 || abs(prevOmega/Constants::ticksToCmS) >200/Constants::d) continue;
// if((prevSpeed*prevOmega)>=380*sqrt(0.99)) continue; // constraint from the equation of ellipse.
// //if(count>400) break; //we would take only a fixed number of points under consideration
// count++;
// alpha=(prevOmega-oldome)/t;
// acc=(prevSpeed-oldvel)/t;
// theta= s.theta() + (oldome*t) + (0.5*alpha*t*t);
// if(theta>2*3.14){theta=theta - 2*3.14*static_cast<int>(theta/(2*3.14));}
// x= s.x() + (oldvel*cos(theta)*t) + (0.5*acc*cos(theta)*t*t);
// y= s.y() + (oldvel*sin(theta)*t) + (0.5*acc*sin(theta)*t*t);
// reqtheta=tanh((e.y()-y)/(e.x()-x));
// dtheta=abs(theta-reqtheta);
// if(dtheta>2*3.14){dtheta=dtheta-2*3.14*static_cast<int>(theta/(2*3.14));}
// if(dtheta>3.14)
// {dtheta=3.14*2-dtheta;}
// arr[count][0]= pow((x - e.x()),2) + pow((y - e.y()),2) + k*pow((dtheta),2) ; // our objective function
// arr[count][1]= prevSpeed;
// arr[count][2]= prevOmega;
// }
// }
// float min= arr[0][0];
// for(i=1;i<count;i++)
// {
// if(arr[i][0]<min) { min=arr[i][0]; j=i; }
// }
// vr=(arr[j][1])+(Constants::d * arr[j][2])/2 ; // update velocity
// vl=(2*arr[j][1]) - vr; // update omega
// vr/=Constants::ticksToCmS;
// vl/=Constants::ticksToCmS;
// return MiscData();
//}
MiscData PController(Pose s, Pose e, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
{
Q_UNUSED(prevSpeed);
Vector2D<int> initial(s.x(), s.y());
Vector2D<int> final(e.x(), e.y());
int distance = Vector2D<int>::dist(initial, final);
double angleError = normalizeAngle(s.theta() - Vector2D<int>::angle(final, initial));
double v = 0;
int maxDis = 2*sqrt(HALF_FIELD_MAXX*HALF_FIELD_MAXX+HALF_FIELD_MAXY*HALF_FIELD_MAXY);
if(angleError > PI/2) {
v = 0;
} else {
if(distance > maxDis/2) {
v = MAX_BOT_SPEED;
} else {
v = (distance/(double)maxDis)*90+10;
}
}
double w = -1.5*angleError;
v *= Constants::ticksToCmS;
vl = v - Constants::d*w/2;
vr = v + Constants::d*w/2;
if(abs(vl) > MAX_BOT_SPEED || abs(vr) > MAX_BOT_SPEED) {
double max = abs(vl)>abs(vr)?abs(vl):abs(vr);
vl = vl*MAX_BOT_SPEED/max;
vr = vr*MAX_BOT_SPEED/max;
}
return MiscData();
}
/* Aicardi M, Casalino G, Bicchi A, Balestrino A 1995 Closed loop steering of
unicycle-like vehicles via Lyapunov techniques. IEEE Robotics & Automation
Magazine 2(1):27–35
*/
MiscData PolarBased(Pose s, Pose e, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
{
// NOTE: its preferable to call x(), y(), and theta() of each object exactly once since they may return different
// values on each call (when simulating, gaussian errors non-zero).
Vector2D<int> initial(s.x()-e.x(), s.y()-e.y());
double etheta = e.theta();
double theta = normalizeAngle(s.theta() - etheta);
// rotate initial by -e.theta degrees;
double newx = initial.x * cos(-etheta) - initial.y * sin(-etheta);
double newy = initial.x * sin(-etheta) + initial.y * cos(-etheta);
initial = Vector2D<int>(newx, newy);
double rho = sqrt(initial.x*initial.x + initial.y*initial.y);
double gamma = normalizeAngle(atan2(initial.y, initial.x) - theta + PI);
double delta = normalizeAngle(gamma + theta);
double k1 = 0.05, k2 = 4, k3 = 20;
double v = k1*rho*cos(gamma);
double w;
if(gamma == 0) {
w = k2*gamma+k1*cos(gamma)*(gamma+k3*delta);
} else {
w = k2*gamma+k1*sin(gamma)*cos(gamma)/gamma*(gamma + k3*delta);
}
// velocity profiling based on curvature
double k = w/v; // k = curvature
// scale curvature by 50.
k *= 20;
double lambda = 2;
double beta = 0.7;
double v_curve = MAX_BOT_SPEED/(1+beta*pow(fabs(k),lambda));
if (v_curve < MIN_BOT_SPEED)
v_curve = MIN_BOT_SPEED;
v *= Constants::ticksToCmS;
vl = v - Constants::d*w/2;
vr = v + Constants::d*w/2;
double timeMs = 0.250*rho + 14.0 * sqrt(rho) + 100.0 * fabs(gamma);
double speed = timeMs/timeLCMs<(prevSpeed/MAX_BOT_LINEAR_VEL_CHANGE)?prevSpeed-MAX_BOT_LINEAR_VEL_CHANGE:prevSpeed+MAX_BOT_LINEAR_VEL_CHANGE;
// use vcurve as the velocity
// NOTE: adding vcurve and finalVel code
// critical condition: if bot close to final point, v_curve = MAX_BOT_SPEED
if (rho < BOT_POINT_THRESH && finalSpeed > MIN_BOT_SPEED) {
v_curve = MAX_BOT_SPEED;
vl = vr; // ? or vl = vr?
}
double rangeMin = max(prevSpeed - MAX_BOT_LINEAR_VEL_CHANGE, 0.0);
double rangeMax = min(prevSpeed + MAX_BOT_LINEAR_VEL_CHANGE, v_curve);
if (v_curve < prevSpeed - MAX_BOT_LINEAR_VEL_CHANGE) {
rangeMin = rangeMax = v_curve;
}
// only consider finalSpeed when d < BOT_FINALVEL_THRESH
if (rho <= BOT_FINALVEL_THRESH)
speed = finalSpeed < rangeMax? max(rangeMin, finalSpeed) : min(rangeMax, finalSpeed);
else
speed = rangeMax;
// critical condition : finalVel = 0, but bot not close to final pose
if (rho > BOT_POINT_THRESH && speed < MIN_BOT_SPEED)
speed = MIN_BOT_SPEED;
// end
if(speed > MAX_BOT_SPEED)
speed = MAX_BOT_SPEED;
else if (speed < 0)
speed = 0;
double max = fabs(vl)>fabs(vr)?fabs(vl):fabs(vr);
if(max > 0) {
vl = vl*speed/max;
vr = vr*speed/max;
}
return MiscData(k, v_curve, finalSpeed, rangeMin, rangeMax);
}
MiscData PolarBidirectional(Pose s, Pose e, int &vl, int &vr, double prevSpeed, double prevOmega, double finalSpeed)
{
MiscData m;
static bool wasInverted = false; // keeps track of whether in the previous call, the bot was inverted or not.
m = PolarBased(s, e, vl, vr, prevSpeed, finalSpeed);
double v = (vl+vr)/2.0;
wasInverted = false;
if(v < 0 || (v == 0 && wasInverted) ) {
s.setTheta(normalizeAngle(s.theta()+PI));
m = PolarBased(s, e, vl, vr, prevSpeed, finalSpeed);
swap(vl, vr);
vl = -vl;
vr = -vr;
wasInverted = true;
}
return m;
}
void PolarBasedGA(Pose s, Pose e, int &vl, int &vr, double k1, double k2, double k3) // this function is old now, do not use.
{
Vector2D<int> initial(s.x()-e.x(), s.y()-e.y());
double theta = normalizeAngle(s.theta() - e.theta());
// rotate initial by -e.theta degrees;
double newx = initial.x * cos(-e.theta()) - initial.y * sin(-e.theta());
double newy = initial.x * sin(-e.theta()) + initial.y * cos(-e.theta());
initial = Vector2D<int>(newx, newy);
double rho = sqrt(initial.x*initial.x + initial.y*initial.y);
double gamma = normalizeAngle(atan2(initial.y, initial.x) - theta + PI);
double delta = normalizeAngle(gamma + theta);
double v = k1*rho*cos(gamma);
double w;
if(gamma == 0) {
w = k2*gamma+k1*cos(gamma)*(gamma+k3*delta);
} else {
w = k2*gamma+k1*sin(gamma)*cos(gamma)/gamma*(gamma + k3*delta);
}
v *= Constants::ticksToCmS;
vl = v - Constants::d*w/2;
vr = v + Constants::d*w/2;
double timeMs = 23 * sqrt(rho); // empirical
double speed = timeMs<timeLCMs*5?timeMs/timeLCMs*(80/5):80;
double max = fabs(vl)>fabs(vr)?fabs(vl):fabs(vr);
if(max > 0) {
vl = vl*speed/max;
vr = vr*speed/max;
}
}
}