-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
visualization.cpp
414 lines (363 loc) · 15.9 KB
/
visualization.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*******************************************************
* Copyright (C) 2019, Aerial Robotics Group, Hong Kong University of Science and Technology
*
* This file is part of VINS.
*
* Licensed under the GNU General Public License v3.0;
* you may not use this file except in compliance with the License.
*******************************************************/
#include "visualization.h"
using namespace ros;
using namespace Eigen;
ros::Publisher pub_odometry, pub_latest_odometry;
ros::Publisher pub_path;
ros::Publisher pub_point_cloud, pub_margin_cloud;
ros::Publisher pub_key_poses;
ros::Publisher pub_camera_pose;
ros::Publisher pub_camera_pose_visual;
nav_msgs::Path path;
ros::Publisher pub_keyframe_pose;
ros::Publisher pub_keyframe_point;
ros::Publisher pub_extrinsic;
ros::Publisher pub_image_track;
CameraPoseVisualization cameraposevisual(1, 0, 0, 1);
static double sum_of_path = 0;
static Vector3d last_path(0.0, 0.0, 0.0);
size_t pub_counter = 0;
void registerPub(ros::NodeHandle &n)
{
pub_latest_odometry = n.advertise<nav_msgs::Odometry>("imu_propagate", 1000);
pub_path = n.advertise<nav_msgs::Path>("path", 1000);
pub_odometry = n.advertise<nav_msgs::Odometry>("odometry", 1000);
pub_point_cloud = n.advertise<sensor_msgs::PointCloud>("point_cloud", 1000);
pub_margin_cloud = n.advertise<sensor_msgs::PointCloud>("margin_cloud", 1000);
pub_key_poses = n.advertise<visualization_msgs::Marker>("key_poses", 1000);
pub_camera_pose = n.advertise<nav_msgs::Odometry>("camera_pose", 1000);
pub_camera_pose_visual = n.advertise<visualization_msgs::MarkerArray>("camera_pose_visual", 1000);
pub_keyframe_pose = n.advertise<nav_msgs::Odometry>("keyframe_pose", 1000);
pub_keyframe_point = n.advertise<sensor_msgs::PointCloud>("keyframe_point", 1000);
pub_extrinsic = n.advertise<nav_msgs::Odometry>("extrinsic", 1000);
pub_image_track = n.advertise<sensor_msgs::Image>("image_track", 1000);
cameraposevisual.setScale(0.1);
cameraposevisual.setLineWidth(0.01);
}
void pubLatestOdometry(const Eigen::Vector3d &P, const Eigen::Quaterniond &Q, const Eigen::Vector3d &V, double t)
{
nav_msgs::Odometry odometry;
odometry.header.stamp = ros::Time(t);
odometry.header.frame_id = "world";
odometry.pose.pose.position.x = P.x();
odometry.pose.pose.position.y = P.y();
odometry.pose.pose.position.z = P.z();
odometry.pose.pose.orientation.x = Q.x();
odometry.pose.pose.orientation.y = Q.y();
odometry.pose.pose.orientation.z = Q.z();
odometry.pose.pose.orientation.w = Q.w();
odometry.twist.twist.linear.x = V.x();
odometry.twist.twist.linear.y = V.y();
odometry.twist.twist.linear.z = V.z();
pub_latest_odometry.publish(odometry);
}
void pubTrackImage(const cv::Mat &imgTrack, const double t)
{
std_msgs::Header header;
header.frame_id = "world";
header.stamp = ros::Time(t);
sensor_msgs::ImagePtr imgTrackMsg = cv_bridge::CvImage(header, "bgr8", imgTrack).toImageMsg();
pub_image_track.publish(imgTrackMsg);
}
void printStatistics(const Estimator &estimator, double t)
{
if (estimator.solver_flag != Estimator::SolverFlag::NON_LINEAR)
return;
//printf("position: %f, %f, %f\r", estimator.Ps[WINDOW_SIZE].x(), estimator.Ps[WINDOW_SIZE].y(), estimator.Ps[WINDOW_SIZE].z());
ROS_DEBUG_STREAM("position: " << estimator.Ps[WINDOW_SIZE].transpose());
ROS_DEBUG_STREAM("orientation: " << estimator.Vs[WINDOW_SIZE].transpose());
if (ESTIMATE_EXTRINSIC)
{
cv::FileStorage fs(EX_CALIB_RESULT_PATH, cv::FileStorage::WRITE);
for (int i = 0; i < NUM_OF_CAM; i++)
{
//ROS_DEBUG("calibration result for camera %d", i);
ROS_DEBUG_STREAM("extirnsic tic: " << estimator.tic[i].transpose());
ROS_DEBUG_STREAM("extrinsic ric: " << Utility::R2ypr(estimator.ric[i]).transpose());
Eigen::Matrix4d eigen_T = Eigen::Matrix4d::Identity();
eigen_T.block<3, 3>(0, 0) = estimator.ric[i];
eigen_T.block<3, 1>(0, 3) = estimator.tic[i];
cv::Mat cv_T;
cv::eigen2cv(eigen_T, cv_T);
if(i == 0)
fs << "body_T_cam0" << cv_T ;
else
fs << "body_T_cam1" << cv_T ;
}
fs.release();
}
static double sum_of_time = 0;
static int sum_of_calculation = 0;
sum_of_time += t;
sum_of_calculation++;
ROS_DEBUG("vo solver costs: %f ms", t);
ROS_DEBUG("average of time %f ms", sum_of_time / sum_of_calculation);
sum_of_path += (estimator.Ps[WINDOW_SIZE] - last_path).norm();
last_path = estimator.Ps[WINDOW_SIZE];
ROS_DEBUG("sum of path %f", sum_of_path);
if (ESTIMATE_TD)
ROS_INFO("td %f", estimator.td);
}
void pubOdometry(const Estimator &estimator, const std_msgs::Header &header)
{
if (estimator.solver_flag == Estimator::SolverFlag::NON_LINEAR)
{
nav_msgs::Odometry odometry;
odometry.header = header;
odometry.header.frame_id = "world";
odometry.child_frame_id = "world";
Quaterniond tmp_Q;
tmp_Q = Quaterniond(estimator.Rs[WINDOW_SIZE]);
odometry.pose.pose.position.x = estimator.Ps[WINDOW_SIZE].x();
odometry.pose.pose.position.y = estimator.Ps[WINDOW_SIZE].y();
odometry.pose.pose.position.z = estimator.Ps[WINDOW_SIZE].z();
odometry.pose.pose.orientation.x = tmp_Q.x();
odometry.pose.pose.orientation.y = tmp_Q.y();
odometry.pose.pose.orientation.z = tmp_Q.z();
odometry.pose.pose.orientation.w = tmp_Q.w();
odometry.twist.twist.linear.x = estimator.Vs[WINDOW_SIZE].x();
odometry.twist.twist.linear.y = estimator.Vs[WINDOW_SIZE].y();
odometry.twist.twist.linear.z = estimator.Vs[WINDOW_SIZE].z();
pub_odometry.publish(odometry);
geometry_msgs::PoseStamped pose_stamped;
pose_stamped.header = header;
pose_stamped.header.frame_id = "world";
pose_stamped.pose = odometry.pose.pose;
path.header = header;
path.header.frame_id = "world";
path.poses.push_back(pose_stamped);
pub_path.publish(path);
// write result to file
ofstream foutC(VINS_RESULT_PATH, ios::app);
foutC.setf(ios::fixed, ios::floatfield);
foutC.precision(0);
foutC << header.stamp.toSec() * 1e9 << ",";
foutC.precision(5);
foutC << estimator.Ps[WINDOW_SIZE].x() << ","
<< estimator.Ps[WINDOW_SIZE].y() << ","
<< estimator.Ps[WINDOW_SIZE].z() << ","
<< tmp_Q.w() << ","
<< tmp_Q.x() << ","
<< tmp_Q.y() << ","
<< tmp_Q.z() << ","
<< estimator.Vs[WINDOW_SIZE].x() << ","
<< estimator.Vs[WINDOW_SIZE].y() << ","
<< estimator.Vs[WINDOW_SIZE].z() << "," << endl;
foutC.close();
Eigen::Vector3d tmp_T = estimator.Ps[WINDOW_SIZE];
printf("time: %f, t: %f %f %f q: %f %f %f %f \n", header.stamp.toSec(), tmp_T.x(), tmp_T.y(), tmp_T.z(),
tmp_Q.w(), tmp_Q.x(), tmp_Q.y(), tmp_Q.z());
}
}
void pubKeyPoses(const Estimator &estimator, const std_msgs::Header &header)
{
if (estimator.key_poses.size() == 0)
return;
visualization_msgs::Marker key_poses;
key_poses.header = header;
key_poses.header.frame_id = "world";
key_poses.ns = "key_poses";
key_poses.type = visualization_msgs::Marker::SPHERE_LIST;
key_poses.action = visualization_msgs::Marker::ADD;
key_poses.pose.orientation.w = 1.0;
key_poses.lifetime = ros::Duration();
//static int key_poses_id = 0;
key_poses.id = 0; //key_poses_id++;
key_poses.scale.x = 0.05;
key_poses.scale.y = 0.05;
key_poses.scale.z = 0.05;
key_poses.color.r = 1.0;
key_poses.color.a = 1.0;
for (int i = 0; i <= WINDOW_SIZE; i++)
{
geometry_msgs::Point pose_marker;
Vector3d correct_pose;
correct_pose = estimator.key_poses[i];
pose_marker.x = correct_pose.x();
pose_marker.y = correct_pose.y();
pose_marker.z = correct_pose.z();
key_poses.points.push_back(pose_marker);
}
pub_key_poses.publish(key_poses);
}
void pubCameraPose(const Estimator &estimator, const std_msgs::Header &header)
{
int idx2 = WINDOW_SIZE - 1;
if (estimator.solver_flag == Estimator::SolverFlag::NON_LINEAR)
{
int i = idx2;
Vector3d P = estimator.Ps[i] + estimator.Rs[i] * estimator.tic[0];
Quaterniond R = Quaterniond(estimator.Rs[i] * estimator.ric[0]);
nav_msgs::Odometry odometry;
odometry.header = header;
odometry.header.frame_id = "world";
odometry.pose.pose.position.x = P.x();
odometry.pose.pose.position.y = P.y();
odometry.pose.pose.position.z = P.z();
odometry.pose.pose.orientation.x = R.x();
odometry.pose.pose.orientation.y = R.y();
odometry.pose.pose.orientation.z = R.z();
odometry.pose.pose.orientation.w = R.w();
pub_camera_pose.publish(odometry);
cameraposevisual.reset();
cameraposevisual.add_pose(P, R);
if(STEREO)
{
Vector3d P = estimator.Ps[i] + estimator.Rs[i] * estimator.tic[1];
Quaterniond R = Quaterniond(estimator.Rs[i] * estimator.ric[1]);
cameraposevisual.add_pose(P, R);
}
cameraposevisual.publish_by(pub_camera_pose_visual, odometry.header);
}
}
void pubPointCloud(const Estimator &estimator, const std_msgs::Header &header)
{
sensor_msgs::PointCloud point_cloud, loop_point_cloud;
point_cloud.header = header;
loop_point_cloud.header = header;
for (auto &it_per_id : estimator.f_manager.feature)
{
int used_num;
used_num = it_per_id.feature_per_frame.size();
if (!(used_num >= 2 && it_per_id.start_frame < WINDOW_SIZE - 2))
continue;
if (it_per_id.start_frame > WINDOW_SIZE * 3.0 / 4.0 || it_per_id.solve_flag != 1)
continue;
int imu_i = it_per_id.start_frame;
Vector3d pts_i = it_per_id.feature_per_frame[0].point * it_per_id.estimated_depth;
Vector3d w_pts_i = estimator.Rs[imu_i] * (estimator.ric[0] * pts_i + estimator.tic[0]) + estimator.Ps[imu_i];
geometry_msgs::Point32 p;
p.x = w_pts_i(0);
p.y = w_pts_i(1);
p.z = w_pts_i(2);
point_cloud.points.push_back(p);
}
pub_point_cloud.publish(point_cloud);
// pub margined potin
sensor_msgs::PointCloud margin_cloud;
margin_cloud.header = header;
for (auto &it_per_id : estimator.f_manager.feature)
{
int used_num;
used_num = it_per_id.feature_per_frame.size();
if (!(used_num >= 2 && it_per_id.start_frame < WINDOW_SIZE - 2))
continue;
//if (it_per_id->start_frame > WINDOW_SIZE * 3.0 / 4.0 || it_per_id->solve_flag != 1)
// continue;
if (it_per_id.start_frame == 0 && it_per_id.feature_per_frame.size() <= 2
&& it_per_id.solve_flag == 1 )
{
int imu_i = it_per_id.start_frame;
Vector3d pts_i = it_per_id.feature_per_frame[0].point * it_per_id.estimated_depth;
Vector3d w_pts_i = estimator.Rs[imu_i] * (estimator.ric[0] * pts_i + estimator.tic[0]) + estimator.Ps[imu_i];
geometry_msgs::Point32 p;
p.x = w_pts_i(0);
p.y = w_pts_i(1);
p.z = w_pts_i(2);
margin_cloud.points.push_back(p);
}
}
pub_margin_cloud.publish(margin_cloud);
}
void pubTF(const Estimator &estimator, const std_msgs::Header &header)
{
if( estimator.solver_flag != Estimator::SolverFlag::NON_LINEAR)
return;
static tf::TransformBroadcaster br;
tf::Transform transform;
tf::Quaternion q;
// body frame
Vector3d correct_t;
Quaterniond correct_q;
correct_t = estimator.Ps[WINDOW_SIZE];
correct_q = estimator.Rs[WINDOW_SIZE];
transform.setOrigin(tf::Vector3(correct_t(0),
correct_t(1),
correct_t(2)));
q.setW(correct_q.w());
q.setX(correct_q.x());
q.setY(correct_q.y());
q.setZ(correct_q.z());
transform.setRotation(q);
br.sendTransform(tf::StampedTransform(transform, header.stamp, "world", "body"));
// camera frame
transform.setOrigin(tf::Vector3(estimator.tic[0].x(),
estimator.tic[0].y(),
estimator.tic[0].z()));
q.setW(Quaterniond(estimator.ric[0]).w());
q.setX(Quaterniond(estimator.ric[0]).x());
q.setY(Quaterniond(estimator.ric[0]).y());
q.setZ(Quaterniond(estimator.ric[0]).z());
transform.setRotation(q);
br.sendTransform(tf::StampedTransform(transform, header.stamp, "body", "camera"));
nav_msgs::Odometry odometry;
odometry.header = header;
odometry.header.frame_id = "world";
odometry.pose.pose.position.x = estimator.tic[0].x();
odometry.pose.pose.position.y = estimator.tic[0].y();
odometry.pose.pose.position.z = estimator.tic[0].z();
Quaterniond tmp_q{estimator.ric[0]};
odometry.pose.pose.orientation.x = tmp_q.x();
odometry.pose.pose.orientation.y = tmp_q.y();
odometry.pose.pose.orientation.z = tmp_q.z();
odometry.pose.pose.orientation.w = tmp_q.w();
pub_extrinsic.publish(odometry);
}
void pubKeyframe(const Estimator &estimator)
{
// pub camera pose, 2D-3D points of keyframe
if (estimator.solver_flag == Estimator::SolverFlag::NON_LINEAR && estimator.marginalization_flag == 0)
{
int i = WINDOW_SIZE - 2;
//Vector3d P = estimator.Ps[i] + estimator.Rs[i] * estimator.tic[0];
Vector3d P = estimator.Ps[i];
Quaterniond R = Quaterniond(estimator.Rs[i]);
nav_msgs::Odometry odometry;
odometry.header.stamp = ros::Time(estimator.Headers[WINDOW_SIZE - 2]);
odometry.header.frame_id = "world";
odometry.pose.pose.position.x = P.x();
odometry.pose.pose.position.y = P.y();
odometry.pose.pose.position.z = P.z();
odometry.pose.pose.orientation.x = R.x();
odometry.pose.pose.orientation.y = R.y();
odometry.pose.pose.orientation.z = R.z();
odometry.pose.pose.orientation.w = R.w();
//printf("time: %f t: %f %f %f r: %f %f %f %f\n", odometry.header.stamp.toSec(), P.x(), P.y(), P.z(), R.w(), R.x(), R.y(), R.z());
pub_keyframe_pose.publish(odometry);
sensor_msgs::PointCloud point_cloud;
point_cloud.header.stamp = ros::Time(estimator.Headers[WINDOW_SIZE - 2]);
point_cloud.header.frame_id = "world";
for (auto &it_per_id : estimator.f_manager.feature)
{
int frame_size = it_per_id.feature_per_frame.size();
if(it_per_id.start_frame < WINDOW_SIZE - 2 && it_per_id.start_frame + frame_size - 1 >= WINDOW_SIZE - 2 && it_per_id.solve_flag == 1)
{
int imu_i = it_per_id.start_frame;
Vector3d pts_i = it_per_id.feature_per_frame[0].point * it_per_id.estimated_depth;
Vector3d w_pts_i = estimator.Rs[imu_i] * (estimator.ric[0] * pts_i + estimator.tic[0])
+ estimator.Ps[imu_i];
geometry_msgs::Point32 p;
p.x = w_pts_i(0);
p.y = w_pts_i(1);
p.z = w_pts_i(2);
point_cloud.points.push_back(p);
int imu_j = WINDOW_SIZE - 2 - it_per_id.start_frame;
sensor_msgs::ChannelFloat32 p_2d;
p_2d.values.push_back(it_per_id.feature_per_frame[imu_j].point.x());
p_2d.values.push_back(it_per_id.feature_per_frame[imu_j].point.y());
p_2d.values.push_back(it_per_id.feature_per_frame[imu_j].uv.x());
p_2d.values.push_back(it_per_id.feature_per_frame[imu_j].uv.y());
p_2d.values.push_back(it_per_id.feature_id);
point_cloud.channels.push_back(p_2d);
}
}
pub_keyframe_point.publish(point_cloud);
}
}