forked from shubhamgoyal/Motion-Planning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplePlanner.cpp
77 lines (67 loc) · 1.89 KB
/
SimplePlanner.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
#include "SimplePlanner.h"
#include <cstdio>
double min(double a, double b)
{
return a>b?b:a;
}
bool SimplePlanner::isDangerous(State astate)
{
double dangerZone = (car->getV()*3.0)+4.5;
if ( astate.y > car->getY() + car->getLength()/2 && astate.y < car->getY()+dangerZone) {
dd x = astate.x, y = astate.y, v = astate.v, theta= astate.theta;
//tt is the rough estimate on time needed for the car to
//reach the pedestrian y position
dd tt = (y - car->getY())/car->getV();
if (x < car->getX()+car->getWidth()/2 && v*cos(theta)>0.0001) {
//We times 1.5 to consider the car deceleration
if (x + tt*v*cos(theta)*1.5 > car->getX() - car->getWidth()/2)
return true;
}
if (x > car->getX()-car->getWidth()/2 && v*cos(theta)<-0.0001) {
if (x + tt*v*cos(theta)*1.5 < car->getX() + car->getWidth()/2)
return true;
}
}
return false;
}
bool SimplePlanner::existDangerous(std::vector<Pedestrian*> &apedestrians)
{
bool state=0;
for (int i=0;i<apedestrians.size();++i)
{
if (isDangerous((apedestrians[i])->getState()))
{
(apedestrians[i])->setColor(1);
state=1;
}
else
{
(apedestrians[i])->setColor(0);
}
}
return state;
}
void SimplePlanner::plan(std::vector<Pedestrian*> &apedestrians){
std::deque<Control>tempPath;
//pedestrians= apedestrians;
dd maxV = 15;
dd maxTheta = 0.1;
dd breakAccel = 0.9e-1;
dd speedAccel = 1e-2;
Control c;
c.h1 = 0;
c.h2 = 0;
if (existDangerous(apedestrians))
{
c.h1 = -1*min(breakAccel,car->getV());
/* debug */
//printf("-------DANGEROUS!!!--------## v: %lf, decel: %lf\n",car->getV(),c.h1);
}
else if (car->getV() < maxV) c.h1 = speedAccel;
else if (car->getV() >=maxV) c.h1=0;
tempPath.push_back(c);
pthread_mutex_lock(&car->mutex_path);
path->swap(tempPath);
pthread_mutex_unlock(&car->mutex_path);
//car->setPath(path);
}