-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc3-main.cpp
298 lines (238 loc) · 10.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#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;
}
}
//zz_Sid : NDT function definition
Eigen::Matrix4d NDT(pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> ndt, PointCloudT::Ptr source, Pose startingPose, int iterations){
Eigen::Matrix4d transformation_matrix = Eigen::Matrix4d::Identity ();
// TODO: Implement the PCL NDT function and return the correct transformation matrix
// .....
pcl::console::TicToc time;
time.tic();
Eigen::Matrix4f init_guess = transform3D(startingPose.rotation.yaw, startingPose.rotation.pitch, startingPose.rotation.roll, startingPose.position.x, startingPose.position.y, startingPose.position.z).cast<float>();
// Seting ndt params
ndt.setMaximumIterations(iterations);
ndt.setInputSource(source);
PointCloudT::Ptr cloud_ndt(new PointCloudT);
ndt.align(*cloud_ndt, init_guess);
//cout <<"NDT has converged: " << ndt.hasConverged() <<" score: " << ndt.getFitnessScore() << " time: " << time.toc() << "ms" <<endl;
transformation_matrix = ndt.getFinalTransformation().cast<double>();
return transformation_matrix;
}
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 cloudFiltered (new pcl::PointCloud<PointT>);
typename pcl::PointCloud<PointT>::Ptr scanCloud (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.x*detection.x + detection.y*detection.y + detection.z*detection.z) > 8.0){
pclCloud.points.push_back(PointT(detection.x, detection.y, detection.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;
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);
int minPointsVoxel = 5; // zz_Sid
vg.setMinimumPointsNumberPerVoxel(minPointsVoxel); // zz_Sid
double filterRes = 0.5; // zz_Sid: change to 1.0
vg.setLeafSize(filterRes, filterRes, filterRes);
typename pcl::PointCloud<PointT>::Ptr cloudFiltered (new pcl::PointCloud<PointT>);
vg.filter(*cloudFiltered);
// TODO: Find pose transform by using ICP or NDT matching
//pose = ....
pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> ndt;
// setting min transformation difference for termination condition
ndt.setTransformationEpsilon(0.0001);
// Setting max step size for More-Thuente line search
ndt.setStepSize(1); // zz_Sid: try smalled step Size : 0.1
// Setting Resolution of NDT grid structure (VoxelGridCovariance)
ndt.setResolution(1);
// Setting Target Map Cloud for NDT object
ndt.setInputTarget(mapCloud);
Eigen::Matrix4d transform = transform3D(pose.rotation.yaw, pose.rotation.pitch, pose.rotation.roll, pose.position.x, pose.position.y, pose.position.z);
transform = NDT(ndt, cloudFiltered, pose, 3);
pose = getPose(transform);
// TODO: Transform scan so it aligns with ego's actual pose and render that scan
PointCloudT::Ptr transformed_scan (new PointCloudT);
pcl::transformPointCloud (*cloudFiltered, *transformed_scan, transform);
viewer->removePointCloud("scan");
// TODO: Change `scanCloud` below to your transformed scan
renderPointCloud(viewer, transformed_scan, "scan", Color(1,0,0) );
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;
}