-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgcartrack.cc
214 lines (159 loc) · 4.48 KB
/
gcartrack.cc
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
#include <iomanip>
#include <iostream>
#include "ddp.h"
#include "viewer.h"
#include "gcarview.h"
#include "utils.h"
#include "se2.h"
#include "lqcost.h"
#include "constraintcost.h"
#include "multicost.h"
#include "diskconstraint.h"
#include "diskview.h"
#include "unistd.h"
using namespace std;
using namespace Eigen;
using namespace gcop;
typedef Ddp<GcarState, 4, 2> GcarDdp;
typedef DiskConstraint<GcarState, 4, 2> GcarDiskConstraint;
typedef ConstraintCost<GcarState, 4, 2, Dynamic, 1> DiskConstraintCost;
int GcarStateToVector2d(Vector2d &p, const GcarState &x)
{
p[0] = x.g(0,2); // x.g is the car SE(2) pose matrix
p[1] = x.g(1,2);
return 1; // index where position coordinates start (the car model coordinates are (theta,x,y,v)
}
bool DesiredState(GcarState &x, double t, double R, double v)
{
if (t < 0)
return false;
double w = v/R; // angular velocity around circle
double a = w*t; // angle around track
Vector3d q(a + M_PI/2, cos(a)*R, sin(a)*R);
SE2::Instance().q2g(x.g, q); // set initial pose
x.v = v;
return true;
}
bool MakeObstacles(vector<Disk> &disks, int N, double R, double dr)
{
if (R <= 0 || dr <=0)
return false;
for (int i = 0; i <N; ++i) {
double a = RND*2*M_PI;
disks.push_back(Disk(Vector2d(cos(a)*R, sin(a)*R), dr));
}
return true;
}
void solver_process(Viewer* viewer)
{
if (viewer)
viewer->SetCamera(44.875, 46.875, -0.4, 0.050001, -16);
double Rd = 5; // desired circle radius
double vd = 2; // desired forward speed
int N = 32;
double tf = 3;
double h = tf/N;
SE2 &se2 = SE2::Instance();
Gcar sys;
GcarState xf; // final state
// times
vector<double> ts(N+1);
for (int k = 0; k <=N; ++k)
ts[k] = k*h;
// states
vector<GcarState> xs(N+1);
Vector3d q0(0.1, .1, .1);
se2.q2g(xs[0].g, q0); // set initial pose
xs[0].v = vd;
// controls
vector<Vector2d> us(N);
for (int i = 0; i < N; ++i) {
us[i].setZero();
// us[i] = Vector2d(0.01,.1);
// us[N/2+i] = Vector2d(0.01,-.1);
}
LqCost< GcarState, 4, 2> lqcost(sys, tf, xf);
lqcost.Q.setZero();
lqcost.R(0,0) = .005;
lqcost.R(1,1) = .001;
MultiCost<GcarState, 4, 2> mcost(sys, tf);
mcost.costs.push_back(&lqcost);
// make obstacles and add as constarints
vector<Disk> disks;
MakeObstacles(disks, 6, Rd, .3);
GcarDiskConstraint* constraints[disks.size()];
DiskConstraintCost* dcosts[disks.size()];
DiskView* dviews[disks.size()];
for (int i = 0; i < disks.size(); ++i) {
constraints[i] = new GcarDiskConstraint(disks[i], .3);
constraints[i]->func = GcarStateToVector2d;
dcosts[i] = new DiskConstraintCost(sys, tf, *constraints[i]);
dcosts[i]->b = 1000;
mcost.costs.push_back(dcosts[i]);
dviews[i] = new DiskView(disks[i]);
viewer->Add(*dviews[i]);
}
GcarView view(sys, &xs, &us);
viewer->Add(view);
double t = h;
vector<GcarState> xfs;
DesiredState(xf, t, Rd, vd);
xfs.push_back(xf);
GcarView gview(sys, &xfs);
gview.rgba[0] = 1; gview.rgba[1] = 0; gview.rgba[2] =0;
viewer->Add(gview);
gview.renderSystem = false;
// current (i.e. the system at the current time) view
GcarView cview(sys);
cview.x = &xs[0];
cview.rgba[0] = 0; cview.rgba[1] = 0; cview.rgba[2] =1;
viewer->Add(cview);
getchar();
struct timeval timer;
while (t < 1000) {
GcarDdp ddp(sys, mcost, ts, xs, us);
// ddp.mu = 1;
ddp.debug = false; // turn off debug for speed
for (int i = 0; i < 50; ++i) {
timer_start(timer);
ddp.Iterate();
long te = timer_us(timer);
// cout << "Iteration #" << i << " took: " << te << " us." << endl;
}
getchar();
t = t + h;
xs[0] = xs[1];
rotate(us.begin(),us.begin()+1,us.end());
us.back().setZero();
// for (int i = 0; i < N; ++i)
// us[i].setZero();
// update desired state
DesiredState(xf, t, Rd, vd);
gview.Lock();
xfs.push_back(xf);
// inefficient way to cut down the reference traj
if (xfs.size() > xs.size())
xfs.erase(xfs.begin());
gview.Unlock();
}
cout << "done!" << endl;
while(1)
usleep(10);
}
#define DISP
int main(int argc, char** argv)
{
#ifdef DISP
Viewer *viewer = new Viewer;
viewer->Init(&argc, argv);
viewer->frameName = "videos/sys";
pthread_t dummy;
pthread_create( &dummy, NULL, (void *(*) (void *)) solver_process, viewer);
#else
solver_process(0);
#endif
#ifdef DISP
viewer->Start();
#endif
return 0;
}