-
Notifications
You must be signed in to change notification settings - Fork 9
/
trajectory-generators.hpp
169 lines (155 loc) · 6.65 KB
/
trajectory-generators.hpp
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
#ifndef TRAJECTORYGENERATORS_HPP
#define TRAJECTORYGENERATORS_HPP
#include <tracking.hpp>
#include <functional>
#include "geometry.h"
#include <QDebug>
#include <math.h>
#include "pose.h"
#include "velocity-profile.hpp"
#include "trajectory.hpp"
#include "splines.hpp"
#include "controlpoint-optimization.hpp"
#include "drawable.h"
#include "collision-checking.h"
using namespace std;
extern RenderArea *gRenderArea;
namespace TrajectoryGenerators {
Trajectory* circleGenerator(double x, double y, double r, double startTheta, double f) {
function<double(double)> xfunc = [=](double t)->double {
return (r*sin(2*PI*f*t + startTheta)+x)/fieldXConvert;
};
function<double(double)> yfunc = [=](double t)->double {
return (r*cos(2*PI*f*t + startTheta)+y)/fieldXConvert;
};
return new Trajectory(xfunc, yfunc);
}
Trajectory *quinticBezierSplineGenerator(Pose start, Pose end, double vls, double vrs, double vle, double vre) {
QuinticBezierSpline *p = new QuinticBezierSpline(start, end, vls, vrs, vle, vre);
SplineTrajectory *st = new SplineTrajectory(p, vls, vrs, vle, vre);
return st;
//// double k = 1/3.;
// function<double(double)> xfunc = [=](double t)->double {
// return st->x(t);
// };
// function<double(double)> yfunc = [=](double t)->double {
// return st->y(t);
// };
// return new Trajectory(xfunc, yfunc);
}
// doesn't care about units, returns the answer in the same units of the input.
/*
Vector2D<double> predictBallPose(Vector2D<double> ballPos, Vector2D<double> ballVel, double timeOfPrediction){
Vector2D<double> finalBallPos;
finalBallPos.x = ballPos.x + timeOfPrediction*ballVel.x;
finalBallPos.y = ballPos.y + timeOfPrediction*ballVel.y;
return finalBallPos;
}
*/
Trajectory *ellipseGen(double x, double y, double a, double b, double startTheta, double f) {
function<double(double)> xfunc = [=](double t)->double {
return (a*sin(2*PI*f*t + startTheta)+x)/fieldXConvert;
};
function<double(double)> yfunc = [=](double t)->double {
return (b*cos(2*PI*f*t + startTheta)+y)/fieldXConvert;
};
return new Trajectory(xfunc, yfunc);
}
SplineTrajectory *cubic(Pose start, Pose end, double vls, double vrs, double vle, double vre, vector<Pose> midPoints = vector<Pose>()) {
CubicSpline *p = new CubicSpline(start, end, midPoints);
// p->maxk();
SplineTrajectory *st = new SplineTrajectory(p, vls, vrs, vle, vre);
return st;
}
Trajectory *cubicnCP(Pose start, Pose end, double vls, double vrs, double vle, double vre, int n) {
// Pose cp1((start.x()*2+end.x())*1/3., (start.y()*2+end.y())*1/3., 0);
// Pose cp2((start.x()+2*end.x())*1/3., (start.y()+2*end.y())*1/3., 0);
// vector<Pose> midPoints;
// midPoints.push_back(cp1);
// midPoints.push_back(cp2);
// static PointDrawable *pt1 = NULL, *pt2 = NULL;
// if (pt1)
// delete pt1;
// if (pt2)
// delete pt2;
// pt1 = new PointDrawable(QPointF(cp1.x(), cp1.y()), gRenderArea);
// pt2 = new PointDrawable(QPointF(cp2.x(), cp2.y()), gRenderArea);
// CubicSpline *p = new CubicSpline(start, end, midPoints);
// SplineTrajectory *st = new SplineTrajectory(p, vls, vrs, vle, vre);
Trajectory *st = Optimization::cubicSplinenCPOptimization(start, end, vls, vrs, vle, vre, n);
return st;
}
SplineTrajectory *cubic_drawCollisions(Pose start, Pose end, double vls, double vrs, double vle, double vre, vector<Pose> midPoints = vector<Pose>()) {
// line segments for walls
// currently just adding 4 walls that are the coordinate limits. However we need 12, and they
// will all be slightly inside the HALF_FIELD coordinates.
// Their coordinates will depend significantly on the camera/arena setup.
using CollisionChecking::LineSegment;
vector<LineSegment> ls;
ls.push_back(LineSegment(-HALF_FIELD_MAXX/fieldXConvert, -HALF_FIELD_MAXY/fieldXConvert, HALF_FIELD_MAXX/fieldXConvert, -HALF_FIELD_MAXY/fieldXConvert));
ls.push_back(LineSegment(-HALF_FIELD_MAXX/fieldXConvert, HALF_FIELD_MAXY/fieldXConvert, HALF_FIELD_MAXX/fieldXConvert, HALF_FIELD_MAXY/fieldXConvert));
ls.push_back(LineSegment(-HALF_FIELD_MAXX/fieldXConvert, -HALF_FIELD_MAXY/fieldXConvert, -HALF_FIELD_MAXX/fieldXConvert, +HALF_FIELD_MAXY/fieldXConvert));
ls.push_back(LineSegment(HALF_FIELD_MAXX/fieldXConvert, -HALF_FIELD_MAXY/fieldXConvert, HALF_FIELD_MAXX/fieldXConvert, +HALF_FIELD_MAXY/fieldXConvert));
CubicSpline *p = new CubicSpline(start, end, midPoints);
vector<Pose> collisions;
for (int i = 0; i < ls.size(); i++) {
vector<Pose> results = CollisionChecking::cubicSpline_LineSegmentIntersection(*p, ls[i]);
collisions.insert(collisions.end(), results.begin(), results.end());
}
static vector<PointDrawable*> pts;
for (int i = 0; i < pts.size(); i++) {
if (pts[i])
delete pts[i];
}
pts.clear();
for (int i = 0; i < collisions.size(); i++) {
PointDrawable *pt = new PointDrawable(QPointF(collisions[i].x(), collisions[i].y()), gRenderArea);
pts.push_back(pt);
}
// qDebug() << "num collisions = " << collisions.size();
SplineTrajectory *st = new SplineTrajectory(p, vls, vrs, vle, vre);
return st;
}
// ballPos: strategy coordinates
// ballVel: strategy coordinates per second
/*
SplineTrajectory* ballInterception(Pose botPosStart, Vector2D<double> ballPos, Vector2D<double> ballVel){
Vector2D<double> predictedBallPos;
double error = 0.1;
double T2 = 6.0;
double T1 = 0.0;
double d = 0.0;
Vector2D<double> goalCentre(-HALF_FIELD_MAXX, 0);
SplineTrajectory *st = NULL;
while (1) {
// predictedBallPos: strategy coordinates
double mid = (T1+T2)/2;
predictedBallPos = predictBallPose(ballPos, ballVel, mid);
double endTheta = atan2(goalCentre.y - predictedBallPos.y, goalCentre.x - predictedBallPos.x);
Pose endPose(predictedBallPos.x, predictedBallPos.y, endTheta);
if (st)
delete st;
// add a cp behind the ball pos, distance of 500
Pose cp1(predictedBallPos.x+500*cos(endTheta+M_PI), predictedBallPos.y+500*sin(endTheta+M_PI), 0);
vector<Pose> midPoints;
midPoints.push_back(cp1);
st = cubic(botPosStart, endPose, 0, 0, 50, 50, midPoints);
double t = st->totalTime();
qDebug() << "mid = " << mid << ", t = " << t;
if (fabs(t-mid) < error)
break;
if (t > mid) {
T1 = mid;
} else if (t < mid) {
T2 = mid;
}
if (fabs(T2-T1) < error) {
qDebug() << "T2, T1 almost same = " << T1 <<", t = " << t;
break;
}
}
return st;
}
*/
}
#endif // TRAJECTORYGENERATORS_HPP