-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment.cpp
142 lines (109 loc) · 3.26 KB
/
environment.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
/* \author Aaron Brown */
// Create simple 3d highway enviroment using PCL
// for exploring self-driving car sensors
#include "render/render.h"
#include "trajectory_arc.h"
//#include "processPointClouds.h"
// using templates for processPointClouds so also include .cpp to help linker
//#include "processPointClouds.cpp"
class Player
{
public:
float x_pos, y_pos;
float angle, steering;
float velocity;
float Lf;
Player()
: x_pos(0), y_pos(0), angle(0), steering(0), velocity(0.2), Lf(2)
{}
Player(float xSet, float ySet, float angleSet)
: x_pos(xSet), y_pos(ySet), angle(angleSet), steering(0), velocity(0.2), Lf(2)
{}
void Forward()
{
x_pos+=velocity*cos(angle);
y_pos+=velocity*sin(angle);
angle += velocity * steering / Lf;
}
void Back()
{
x_pos-=0.2*cos(angle);
y_pos-=0.2*sin(angle);
angle += velocity * steering / Lf;
}
void setSteeringAngle(float setSteering)
{
steering = setSteering;
}
void TurnRight()
{
steering -= 0.01;
while (steering < -M_PI) { steering += 2 * M_PI; }
while (steering > M_PI) { steering -= 2 * M_PI; }
}
void TurnLeft()
{
steering += 0.01;
while (steering < -M_PI) { steering += 2 * M_PI; }
while (steering > M_PI) { steering -= 2 * M_PI; }
}
};
Player player(0,0,M_PI/2);
TrajectoryArc planner;
void setCamera(Vect3 pos, double angle, pcl::visualization::PCLVisualizer& viewer)
{
double viewDist = 10;
viewer.setCameraPosition(pos.x, pos.y, pos.z, pos.x + viewDist*cos(angle), pos.y + viewDist*sin(angle), 0, 0, 0, 1);
}
void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event, void* viewer_void)
{
pcl::visualization::PCLVisualizer* viewer = static_cast<pcl::visualization::PCLVisualizer*>(viewer_void);
if (event.getKeySym () == "Up" && event.keyDown ())
{
player.Forward();
}
else if(event.getKeySym () == "Down" && event.keyDown ())
{
player.Back();
}
else if (event.getKeySym() == "Right" && event.keyDown())
{
player.TurnRight();
}
else if (event.getKeySym() == "Left" && event.keyDown())
{
player.TurnLeft();
}
else if(event.getKeySym () == "t" && event.keyDown ())
{
saveScreenshot(viewer,"screenshot.png");
Mat image = imread("screenshot.png");
player.setSteeringAngle(-planner.process(image, true));
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", planner.cloned_image_);
waitKey(0);
}
else if (event.getKeySym() == "space" && event.keyDown())
{
saveScreenshot(viewer, "screenshot.png");
Mat image = imread("screenshot.png");
player.setSteeringAngle(-planner.process(image, true));
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", planner.cloned_image_);
player.Forward();
}
setCamera(Vect3(player.x_pos, player.y_pos, 1.5), player.angle, *viewer);
}
int main (int argc, char** argv)
{
std::cout << "starting enviroment" << std::endl;
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
setCamera(Vect3(player.x_pos, player.y_pos, 1.5), player.angle, *viewer);
renderHighway(viewer);
viewer->registerKeyboardCallback(keyboardEventOccurred, (void*)viewer.get());
while (!viewer->wasStopped ())
{
viewer->spinOnce ();
}
}