Skip to content

Commit 85689c2

Browse files
committed
fixed a nasty bug relating to node regularization. had stored the references and it was a mess. not storing the values
1 parent 2e42ef4 commit 85689c2

8 files changed

Lines changed: 152 additions & 22 deletions

src/PoseGraphSLAM.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,8 @@ bool PoseGraphSLAM::saveAsJSON(const string base_path)
13541354
// #define __reint_allocation_cout(msg) msg;
13551355
#define __reint_allocation_cout(msg) ;
13561356

1357-
// #define __reint_odom_cout(msg) msg;
1358-
#define __reint_odom_cout(msg) ;
1357+
#define __reint_odom_cout(msg) msg;
1358+
// #define __reint_odom_cout(msg) ;
13591359

13601360

13611361

@@ -1382,7 +1382,7 @@ void PoseGraphSLAM::reinit_ceres_problem_onnewloopedge_optimize6DOF()
13821382
ceres::Solver::Summary reint_summary;
13831383
reint_options.linear_solver_type = ceres::SPARSE_NORMAL_CHOLESKY;
13841384
reint_options.minimizer_progress_to_stdout = false;
1385-
reint_options.max_num_iterations = 50;
1385+
reint_options.max_num_iterations = 10;
13861386
// reint_options.enable_fast_removal = true;
13871387
eigenquaternion_parameterization = new ceres::EigenQuaternionParameterization;
13881388
// robust_norm = new ceres::CauchyLoss(1.0);
@@ -1859,6 +1859,7 @@ void PoseGraphSLAM::reinit_ceres_problem_onnewloopedge_optimize6DOF()
18591859
//-----------------------------
18601860
// Mark nodes as constant. Possibly also need to mark starts of each worlds as constants. Can also try setting each sets 1st node as constant.
18611861
//------------------------------
1862+
#if 1
18621863
for( int v=0 ; v<regularization_terms_list.size() ; v++ ) {
18631864
reint_problem.RemoveResidualBlock( regularization_terms_list[v] );
18641865
cout << "Remove " << v << " th regularization term\n";
@@ -1868,30 +1869,36 @@ void PoseGraphSLAM::reinit_ceres_problem_onnewloopedge_optimize6DOF()
18681869
std::map< int, bool > mark_as_constant;
18691870
// mark_as_constant[0] = true;
18701871
mark_as_constant[1] = true;
1872+
cout << "There are " << manager->n_worlds() << " worlds. Loop through each world to get its start node\n";
1873+
cout << "Number of opt_nodes = " << this->n_opt_variables() << endl;
18711874
for( int ww=0 ; ww<manager->n_worlds(); ww++ ) {
18721875
// if( mark_as_constant.count(ww) ==0 )
18731876
// continue;
18741877
int ww_setid = manager->getWorldsConstPtr()->find_setID_of_world_i(ww);
1875-
cout << "&&&&&&&&&&&&world#" << ww << " is in setID=" << ww_setid << endl;
1878+
int ww_start = manager->nodeidx_of_world_i_started( ww );
1879+
cout << TermColor::CYAN() << "&&&&&&&&&&&&world#" << ww << " is in setID=" << ww_setid << " with ww_start=" << ww_start << TermColor::RESET() << endl;
18761880
if( ww_setid >= 0 && ww_setid==ww ) {
18771881

1882+
cout << "node#" << ww_start << " is in world#" << ww << " which has setID as " << ww_setid << endl;
1883+
cout << "Mark node#" << ww_start << " as constant. \n";
18781884

1879-
int ww_start = manager->nodeidx_of_world_i_started( ww );
18801885
// reint_problem.SetParameterBlockConstant( get_raw_ptr_to_opt_variable_q(ww_start) );
18811886
// reint_problem.SetParameterBlockConstant( get_raw_ptr_to_opt_variable_t(ww_start) );
18821887

1883-
ceres::CostFunction * regularixa_cost = NodePoseRegularization::Create( getNodePose(ww_start), 2.3 );
1888+
// Matrix4d ww_start_pose = manager->getNodePose(ww_start);
1889+
Matrix4d ww_start_pose = this->getNodePose(ww_start);
1890+
cout << "ww_start_pose : " << PoseManipUtils::prettyprintMatrix4d( ww_start_pose ) << endl;
1891+
ceres::CostFunction * regularixa_cost = NodePoseRegularization::Create( ww_start_pose, 3.3 );
18841892
ceres::ResidualBlockId resi_id = reint_problem.AddResidualBlock( regularixa_cost, NULL, get_raw_ptr_to_opt_variable_q(ww_start), get_raw_ptr_to_opt_variable_t(ww_start) );
18851893
regularization_terms_list.push_back( resi_id );
18861894

1887-
cout << "node#" << ww_start << " is in world#" << ww << " which has setID as " << ww_setid << endl;
1888-
cout << "Mark node#" << ww_start << " as constant. \n";
1895+
18891896
}
18901897
else
18911898
cout << "skip\n";
18921899
}
18931900
cout << "Total elements in `regularization_terms_list` = " << regularization_terms_list.size() << endl;
1894-
1901+
#endif
18951902

18961903

18971904

src/PoseGraphSLAM.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class SixDOFError
277277
class NodePoseRegularization
278278
{
279279
public:
280-
NodePoseRegularization( const Matrix4d& _nodepose, const double _weight = 1.0 ) : nodepose( _nodepose ), weight( _weight )
280+
NodePoseRegularization( const Matrix4d _nodepose, const double _weight = 1.0 ) : nodepose( _nodepose ), weight( _weight )
281281
{
282282

283283
}
@@ -303,11 +303,11 @@ class NodePoseRegularization
303303
Eigen::Map<Matrix<T,6,1> > residuals( residue_ptr );
304304
residuals.block(0,0, 3,1) = T(weight) * delta.col(3).topRows(3);
305305
residuals.block(3,0, 3,1) = T(weight) * T(2.0) * delta_q.vec();
306-
306+
307307
return true;
308308
}
309309

310-
static ceres::CostFunction* Create( const Matrix4d& _observed_node_pose, const double xweight )
310+
static ceres::CostFunction* Create( const Matrix4d _observed_node_pose, const double xweight )
311311
{
312312
return ( new ceres::AutoDiffCostFunction<NodePoseRegularization,6,4,3>
313313
(
@@ -317,7 +317,7 @@ class NodePoseRegularization
317317
}
318318

319319
private:
320-
const Matrix4d& nodepose;
320+
const Matrix4d nodepose;
321321
const double weight;
322322
};
323323

src/VizPoseGraph.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,38 @@ void VizPoseGraph::publishCameraVisualMarker( const Matrix4d& wTc, const string&
457457
pub_pgraph.publish( marker2 );
458458
}
459459

460+
void VizPoseGraph::publishXYZAxis( const Matrix4d& wT_axis, const string ns, int id )
461+
{
462+
visualization_msgs::Marker axis;
463+
RosMarkerUtils::init_line_marker( axis );
464+
axis.id = 0;
465+
axis.ns = ns.c_str();
466+
axis.scale.x = 0.2;
467+
468+
469+
// Add pts
470+
RosMarkerUtils::add_point_to_marker( 0,0,0, axis, true );
471+
RosMarkerUtils::add_point_to_marker( 1,0,0, axis, false );
472+
RosMarkerUtils::add_point_to_marker( 0,0,0, axis, false );
473+
RosMarkerUtils::add_point_to_marker( 0,1,0, axis, false );
474+
RosMarkerUtils::add_point_to_marker( 0,0,0, axis, false );
475+
RosMarkerUtils::add_point_to_marker( 0,0,1, axis, false );
476+
477+
// Add colors to each pt
478+
RosMarkerUtils::add_colors_to_marker( 1.0,0,0, axis, true );
479+
RosMarkerUtils::add_colors_to_marker( 1.0,0,0, axis, false );
480+
RosMarkerUtils::add_colors_to_marker( 0,1.0,0, axis, false );
481+
RosMarkerUtils::add_colors_to_marker( 0,1.0,0, axis, false );
482+
RosMarkerUtils::add_colors_to_marker( 0,0,1.0, axis, false );
483+
RosMarkerUtils::add_colors_to_marker( 0,0,1.0, axis, false );
484+
485+
486+
487+
RosMarkerUtils::setpose_to_marker( wT_axis, axis );
488+
pub_pgraph.publish( axis );
489+
490+
}
491+
460492
void VizPoseGraph::publishThisVisualMarker( const visualization_msgs::Marker& the_marker )
461493
{
462494
pub_pgraph.publish( the_marker );

src/VizPoseGraph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class VizPoseGraph
6262
void publishSlamResidueVisual( int n ) const;
6363
void publishCameraVisualMarker( const Matrix4d& wTc, const string& ns, float r, float g, float b );
6464

65+
void publishXYZAxis( const Matrix4d& wT_axis, const string ns, int id );
6566
void publishThisVisualMarker( const visualization_msgs::Marker& the_marker );
6667
void publishImage( const cv::Mat& im );
6768

src/Worlds.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,15 @@ void Worlds::disjoint_set_status_image(cv::Mat& im_disp, bool enable_bubbles, bo
310310
if( enable_bubbles ) {
311311
int uu = n_worlds(); //disjoint_set.element_count();
312312
// circles
313+
cv::putText( im_disp, "Worlds:", cv::Point(15, 15), cv::FONT_HERSHEY_SIMPLEX,
314+
0.4, cv::Scalar(255,255,255), 1.5 );
315+
cv::putText( im_disp, "SetIDs of Worlds:", cv::Point(15, 45), cv::FONT_HERSHEY_SIMPLEX,
316+
0.4, cv::Scalar(255,255,255), 1.5 );
313317
for( int i=0 ; i<uu; i++ ) {
314318
// World Colors
315319
cv::Scalar color = FalseColors::randomColor( i );
316320
// cout << color << endl;
317-
cv::Point pt = cv::Point(20*i+15, 15);
321+
cv::Point pt = cv::Point(20*i+15, 25);
318322
cv::circle( im_disp, pt, 10, color, -1 );
319323
cv::putText( im_disp, to_string(i), pt, cv::FONT_HERSHEY_SIMPLEX,
320324
0.4, cv::Scalar(255,255,255), 1.5 );
@@ -323,7 +327,7 @@ void Worlds::disjoint_set_status_image(cv::Mat& im_disp, bool enable_bubbles, bo
323327
// Set Colors
324328
int setId = find_setID_of_world_i( i );
325329
color = FalseColors::randomColor( setId );
326-
pt = cv::Point(20*i+15, 45);
330+
pt = cv::Point(20*i+15, 55);
327331
cv::circle( im_disp, pt, 10, color, -1 );
328332
cv::putText( im_disp, to_string(setId), pt, cv::FONT_HERSHEY_SIMPLEX,
329333
0.4, cv::Scalar(255,255,255), 1.5 );

src/keyframe_pose_graph_slam_node.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ void periodic_publish_odoms( const NodeDataManager * manager, const VizPoseGraph
7373
{
7474
cout << "Start `periodic_publish`\n";
7575
ros::Rate loop_rate(15);
76-
double offset_x = 30., offset_y=0., offset_z=0.;
76+
// double offset_x = 30., offset_y=0., offset_z=0.;
77+
double offset_x = 0., offset_y=0., offset_z=0.;
7778

7879
map<int, vector<Matrix4d> > jmb;
80+
bool published_axis = false;
7981
while( ros::ok() )
8082
{
8183
if( manager->getNodeLen() == 0 ) {
@@ -132,9 +134,9 @@ void periodic_publish_odoms( const NodeDataManager * manager, const VizPoseGraph
132134
#endif
133135

134136

135-
// only publish the latest odometry. Set this to zero if you dont need this.
136-
#if 1
137-
int to_publish_key = -1;
137+
138+
#if 1// only publish the latest odometry. Set this to zero if you dont need this.
139+
int to_publish_key = -1; //lets the largest key value
138140
for( auto it=jmb.begin() ; it!=jmb.end() ; it++ ) {
139141

140142
if( it->first > to_publish_key && it->first >=0 )
@@ -151,6 +153,16 @@ void periodic_publish_odoms( const NodeDataManager * manager, const VizPoseGraph
151153
#endif
152154

153155

156+
157+
if( published_axis==false || rand() % 100 == 0 ) {
158+
// Publish Axis - publish infrequently
159+
Matrix4d odm_axis_pose = Matrix4d::Identity();
160+
odm_axis_pose(0,3) += offset_x; odm_axis_pose(1,3) += offset_y; odm_axis_pose(2,3) += offset_z;
161+
viz->publishXYZAxis( odm_axis_pose, "odom-axis", 0 );
162+
published_axis = true;
163+
}
164+
165+
154166
loop_rate.sleep();
155167
}
156168
cout << "END `periodic_publish`\n";
@@ -334,7 +346,7 @@ void opt_traj_publisher_colored_by_world( const NodeDataManager * manager, const
334346

335347
ros::Rate loop_rate(20);
336348
map<int, vector<Matrix4d> > jmb;
337-
vector< Vector3d > lbm; // a corrected poses. Same index as the node.
349+
vector< Vector3d > lbm; // a corrected poses. Same index as the node. These are used for loopedges.
338350
while( ros::ok() )
339351
{
340352
// cout << "[opt_traj_publisher_colored_by_world]---\n";
@@ -361,7 +373,7 @@ void opt_traj_publisher_colored_by_world( const NodeDataManager * manager, const
361373
int world_id = manager->which_world_is_this( manager->getNodeTimestamp(i) );
362374

363375
// i>=0 and i<solvedUntil()
364-
if( i>=0 && i< ____solvedUntil ) {
376+
if( i>=0 && i<= ____solvedUntil ) {
365377
Matrix4d w_T_c_optimized, w_T_c;
366378

367379
// cerr << "world_id=" << world_id << " ";
@@ -393,20 +405,37 @@ void opt_traj_publisher_colored_by_world( const NodeDataManager * manager, const
393405

394406
// i>solvedUntil() < manager->getNodeLen() only odometry available here.
395407

396-
if( i>=(____solvedUntil) && manager->getNodeLen() ) {
408+
if( i>(____solvedUntil) ) {
397409
int last_idx=-1;
398410
Matrix4d w_TM_i;
399411

412+
/*
400413
if( ____solvedUntil == 0 ) {
401414
w_TM_i = manager->getNodePose( i );
402415
} else if( world_id < 0 ) {
403416
last_idx = manager->nodeidx_of_world_i_ended( -world_id-1 );
404417
} else if(world_id == ____solvedUntil_worldid && ____solvedUntil_worldid_is_neg==false) {
405418
last_idx = ____solvedUntil;
419+
}
420+
else if(world_id == ____solvedUntil_worldid && ____solvedUntil_worldid_is_neg==true) {
421+
last_idx = ____solvedUntil;
406422
} else {
407423
// last_idx = manager->nodeidx_of_world_i_started( world_id );
408424
w_TM_i = manager->getNodePose( i );
409425
}
426+
*/
427+
428+
if( ____solvedUntil == 0 ) {
429+
w_TM_i = manager->getNodePose( i );
430+
} else {
431+
if( world_id >= 0 && ____solvedUntil_worldid == world_id )
432+
last_idx = ____solvedUntil;
433+
else if( world_id >=0 && ____solvedUntil_worldid != world_id )
434+
w_TM_i = manager->getNodePose( i );
435+
else
436+
last_idx = ____solvedUntil;
437+
438+
}
410439

411440
if( last_idx >= 0 ) {
412441
Matrix4d w_T_last = slam->getNodePose(last_idx );

src/utils/RosMarkerUtils.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,54 @@ void RosMarkerUtils::add_points_to_marker( const MatrixXd& X, visualization_msgs
314314
marker.points.push_back( pt );
315315
}
316316
}
317+
318+
319+
void RosMarkerUtils::add_colors_to_marker( const Vector3d& color_rgb, visualization_msgs::Marker& marker, bool clear_prev_colors )
320+
{
321+
assert( (X.rows() == 3) && "[RosMarkerUtils::add_colors_to_marker] X need to of size 3xN representing rgb colors of the points\n" );
322+
// geometry_msgs::Point pt;
323+
std_msgs::ColorRGBA pt_color;
324+
325+
if( clear_prev_colors )
326+
marker.colors.clear();
327+
328+
329+
330+
pt_color.r = color_rgb(0); pt_color.g = color_rgb(1); pt_color.b = color_rgb(2); pt_color.a = 1.0;
331+
marker.colors.push_back( pt_color );
332+
333+
}
334+
335+
void RosMarkerUtils::add_colors_to_marker( float c_r, float c_g, float c_b, visualization_msgs::Marker& marker, bool clear_prev_colors )
336+
{
337+
assert( (X.rows() == 3) && "[RosMarkerUtils::add_colors_to_marker] X need to of size 3xN representing rgb colors of the points\n" );
338+
// geometry_msgs::Point pt;
339+
std_msgs::ColorRGBA pt_color;
340+
341+
if( clear_prev_colors )
342+
marker.colors.clear();
343+
344+
345+
346+
pt_color.r = c_r; pt_color.g = c_g; pt_color.b = c_b; pt_color.a = 1.0;
347+
marker.colors.push_back( pt_color );
348+
349+
}
350+
351+
352+
353+
void RosMarkerUtils::add_colors_to_marker( const MatrixXd& X, visualization_msgs::Marker& marker, bool clear_prev_colors )
354+
{
355+
assert( (X.rows() == 3) && "[RosMarkerUtils::add_colors_to_marker] X need to of size 3xN representing rgb colors of the points\n" );
356+
// geometry_msgs::Point pt;
357+
std_msgs::ColorRGBA pt_color;
358+
359+
if( clear_prev_colors )
360+
marker.colors.clear();
361+
362+
363+
for( int i=0 ; i<X.cols() ; i++ ) {
364+
pt_color.r = X(0,i); pt_color.g = X(1,i); pt_color.b = X(2,i); pt_color.a = 1.0;
365+
marker.colors.push_back( pt_color );
366+
}
367+
}

src/utils/RosMarkerUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ class RosMarkerUtils
5757
static void add_point_to_marker( const Vector4d& X, visualization_msgs::Marker& marker, bool clear_prev_points=true );
5858
static void add_points_to_marker( const MatrixXd& X, visualization_msgs::Marker& marker, bool clear_prev_points=true ); //X : 3xN or 4xN.
5959

60+
////////////// Add colors to individual points ///////////////
61+
static void add_colors_to_marker( const Vector3d& color_rgb, visualization_msgs::Marker& marker, bool clear_prev_colors );
62+
static void add_colors_to_marker( float c_r, float c_g, float c_b, visualization_msgs::Marker& marker, bool clear_prev_colors );
63+
static void add_colors_to_marker( const MatrixXd& X, visualization_msgs::Marker& marker, bool clear_prev_colors );
64+
65+
6066
};

0 commit comments

Comments
 (0)