-
Notifications
You must be signed in to change notification settings - Fork 0
/
c3-main.cpp
263 lines (215 loc) · 9.01 KB
/
c3-main.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
#include <carla/client/Client.h>
#include <carla/client/ActorBlueprint.h>
#include <carla/client/BlueprintLibrary.h>
#include <carla/client/Map.h>
#include <carla/geom/Location.h>
#include <carla/geom/Transform.h>
#include <carla/client/Sensor.h>
#include <carla/sensor/data/LidarMeasurement.h>
#include <thread>
#include <carla/client/Vehicle.h>
//pcl code
//#include "render/render.h"
namespace cc = carla::client;
namespace cg = carla::geom;
namespace csd = carla::sensor::data;
using namespace std::chrono_literals;
using namespace std::string_literals;
using namespace std;
#include <string>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/voxel_grid.h>
#include "helper.h"
#include <sstream>
#include <chrono>
#include <ctime>
#include <pcl/registration/icp.h>
#include <pcl/registration/ndt.h>
#include <pcl/console/time.h> // TicToc
PointCloudT pclCloud;
cc::Vehicle::Control control;
std::chrono::time_point<std::chrono::system_clock> currentTime;
vector<ControlState> cs;
bool refresh_view = false;
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent &event, void* viewer)
{
//boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = *static_cast<boost::shared_ptr<pcl::visualization::PCLVisualizer> *>(viewer_void);
if (event.getKeySym() == "Right" && event.keyDown()){
cs.push_back(ControlState(0, -0.02, 0));
}
else if (event.getKeySym() == "Left" && event.keyDown()){
cs.push_back(ControlState(0, 0.02, 0));
}
if (event.getKeySym() == "Up" && event.keyDown()){
cs.push_back(ControlState(0.1, 0, 0));
}
else if (event.getKeySym() == "Down" && event.keyDown()){
cs.push_back(ControlState(-0.1, 0, 0));
}
if(event.getKeySym() == "a" && event.keyDown()){
refresh_view = true;
}
}
void Accuate(ControlState response, cc::Vehicle::Control& state){
if(response.t > 0){
if(!state.reverse){
state.throttle = min(state.throttle+response.t, 1.0f);
}
else{
state.reverse = false;
state.throttle = min(response.t, 1.0f);
}
}
else if(response.t < 0){
response.t = -response.t;
if(state.reverse){
state.throttle = min(state.throttle+response.t, 1.0f);
}
else{
state.reverse = true;
state.throttle = min(response.t, 1.0f);
}
}
state.steer = min( max(state.steer+response.s, -1.0f), 1.0f);
state.brake = response.b;
}
void drawCar(Pose pose, int num, Color color, double alpha, pcl::visualization::PCLVisualizer::Ptr& viewer){
BoxQ box;
box.bboxTransform = Eigen::Vector3f(pose.position.x, pose.position.y, 0);
box.bboxQuaternion = getQuaternion(pose.rotation.yaw);
box.cube_length = 4;
box.cube_width = 2;
box.cube_height = 2;
renderBox(viewer, box, num, color, alpha);
}
int main(){
auto client = cc::Client("localhost", 2000);
client.SetTimeout(2s);
auto world = client.GetWorld();
auto blueprint_library = world.GetBlueprintLibrary();
auto vehicles = blueprint_library->Filter("vehicle");
auto map = world.GetMap();
auto transform = map->GetRecommendedSpawnPoints()[1];
auto ego_actor = world.SpawnActor((*vehicles)[12], transform);
//Create lidar
auto lidar_bp = *(blueprint_library->Find("sensor.lidar.ray_cast"));
// CANDO: Can modify lidar values to get different scan resolutions
lidar_bp.SetAttribute("upper_fov", "15");
lidar_bp.SetAttribute("lower_fov", "-25");
lidar_bp.SetAttribute("channels", "32");
lidar_bp.SetAttribute("range", "30");
lidar_bp.SetAttribute("rotation_frequency", "60");
lidar_bp.SetAttribute("points_per_second", "500000");
auto user_offset = cg::Location(0, 0, 0);
auto lidar_transform = cg::Transform(cg::Location(-0.5, 0, 1.8) + user_offset);
auto lidar_actor = world.SpawnActor(lidar_bp, lidar_transform, ego_actor.get());
auto lidar = boost::static_pointer_cast<cc::Sensor>(lidar_actor);
bool new_scan = true;
std::chrono::time_point<std::chrono::system_clock> lastScanTime, startTime;
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->registerKeyboardCallback(keyboardEventOccurred, (void*)&viewer);
auto vehicle = boost::static_pointer_cast<cc::Vehicle>(ego_actor);
Pose pose(Point(0,0,0), Rotate(0,0,0));
// Load map
PointCloudT::Ptr mapCloud(new PointCloudT);
pcl::io::loadPCDFile("map.pcd", *mapCloud);
cout << "Loaded " << mapCloud->points.size() << " data points from map.pcd" << endl;
renderPointCloud(viewer, mapCloud, "map", Color(0,0,1));
typename pcl::PointCloud<PointT>::Ptr filteredCloud (new pcl::PointCloud<PointT>);
typename pcl::PointCloud<PointT>::Ptr scanCloud (new pcl::PointCloud<PointT>);
typename pcl::PointCloud<PointT>::Ptr alignedCloud (new pcl::PointCloud<PointT>);
lidar->Listen([&new_scan, &lastScanTime, &scanCloud](auto data){
if(new_scan){
auto scan = boost::static_pointer_cast<csd::LidarMeasurement>(data);
for (auto detection : *scan){
if((detection.point.x*detection.point.x + detection.point.y*detection.point.y + detection.point.z*detection.point.z) > 8.0){ // Don't include points touching ego
pclCloud.points.push_back(PointT(detection.point.x, detection.point.y, detection.point.z));
}
}
if(pclCloud.points.size() > 5000){ // CANDO: Can modify this value to get different scan resolutions
lastScanTime = std::chrono::system_clock::now();
*scanCloud = pclCloud;
new_scan = false;
}
}
});
Pose poseRef(Point(vehicle->GetTransform().location.x, vehicle->GetTransform().location.y, vehicle->GetTransform().location.z), Rotate(vehicle->GetTransform().rotation.yaw * pi/180, vehicle->GetTransform().rotation.pitch * pi/180, vehicle->GetTransform().rotation.roll * pi/180));
double maxError = 0;
// Use NDT
pcl::NormalDistributionsTransform<PointT, PointT> ndt;
ndt.setInputTarget(mapCloud);
ndt.setMaximumIterations(50);
ndt.setStepSize(1);
ndt.setResolution(4);
Eigen::Matrix4f ndtTransform = Eigen::Matrix4f::Identity();
while (!viewer->wasStopped())
{
while(new_scan){
std::this_thread::sleep_for(0.1s);
world.Tick(1s);
}
if(refresh_view){
viewer->setCameraPosition(pose.position.x, pose.position.y, 60, pose.position.x+1, pose.position.y+1, 0, 0, 0, 1);
refresh_view = false;
}
viewer->removeShape("box0");
viewer->removeShape("boxFill0");
Pose truePose = Pose(Point(vehicle->GetTransform().location.x, vehicle->GetTransform().location.y, vehicle->GetTransform().location.z), Rotate(vehicle->GetTransform().rotation.yaw * pi/180, vehicle->GetTransform().rotation.pitch * pi/180, vehicle->GetTransform().rotation.roll * pi/180)) - poseRef;
drawCar(truePose, 0, Color(1,0,0), 0.7, viewer);
double theta = truePose.rotation.yaw;
double stheta = control.steer * pi/4 + theta;
viewer->removeShape("steer");
renderRay(viewer, Point(truePose.position.x+2*cos(theta), truePose.position.y+2*sin(theta),truePose.position.z), Point(truePose.position.x+4*cos(stheta), truePose.position.y+4*sin(stheta),truePose.position.z), "steer", Color(0,1,0));
ControlState accuate(0, 0, 1);
if(cs.size() > 0){
accuate = cs.back();
cs.clear();
Accuate(accuate, control);
vehicle->ApplyControl(control);
}
viewer->spinOnce ();
if(!new_scan){
new_scan = true;
// TODO: (Filter scan using voxel filter)
pcl::VoxelGrid<PointT> vg;
vg.setInputCloud(scanCloud);
vg.setLeafSize(1, 1, 1);
vg.filter(*filteredCloud);
// TODO: Find pose transform by using ICP or NDT matching
ndt.setInputCloud(filteredCloud);
ndt.align(*alignedCloud, ndtTransform);
ndtTransform << ndt.getFinalTransformation();
// TODO: Transform scan so it aligns with ego's actual pose and render that scan
pcl::transformPointCloud(*scanCloud, *alignedCloud, ndtTransform);
viewer->removePointCloud("scan");
// TODO: Change `scanCloud` below to your transformed scan
renderPointCloud(viewer, alignedCloud, "scan", Color(1,0,0) );
pose = getPose(ndtTransform.cast<double>());
viewer->removeAllShapes();
drawCar(pose, 1, Color(0,1,0), 0.35, viewer);
double poseError = sqrt( (truePose.position.x - pose.position.x) * (truePose.position.x - pose.position.x) + (truePose.position.y - pose.position.y) * (truePose.position.y - pose.position.y) );
if(poseError > maxError)
maxError = poseError;
double distDriven = sqrt( (truePose.position.x) * (truePose.position.x) + (truePose.position.y) * (truePose.position.y) );
viewer->removeShape("maxE");
viewer->addText("Max Error: "+to_string(maxError)+" m", 200, 100, 32, 1.0, 1.0, 1.0, "maxE",0);
viewer->removeShape("derror");
viewer->addText("Pose error: "+to_string(poseError)+" m", 200, 150, 32, 1.0, 1.0, 1.0, "derror",0);
viewer->removeShape("dist");
viewer->addText("Distance: "+to_string(distDriven)+" m", 200, 200, 32, 1.0, 1.0, 1.0, "dist",0);
if(maxError > 1.2 || distDriven >= 170.0 ){
viewer->removeShape("eval");
if(maxError > 1.2){
viewer->addText("Try Again", 200, 50, 32, 1.0, 0.0, 0.0, "eval",0);
}
else{
viewer->addText("Passed!", 200, 50, 32, 0.0, 1.0, 0.0, "eval",0);
}
}
pclCloud.points.clear();
}
}
return 0;
}