Skip to content

Commit e5bf9e3

Browse files
committed
resolved the thread blocking issue when ceres::Solver is at work. The effect of not blocking when solver is at work is that viz part doesnt stall with large pose graphs
1 parent 54b2730 commit e5bf9e3

5 files changed

Lines changed: 490 additions & 22 deletions

File tree

src/PoseGraphSLAM.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ bool PoseGraphSLAM::nodePoseExists( int i ) const //< returns if ith node pose e
111111

112112
}
113113

114+
bool PoseGraphSLAM::nodePoseExists__nolock( int i ) const //< returns if ith node pose exist
115+
{
116+
#if defined(___OPT_AS_DOUBLE_STAR )
117+
// std::lock_guard<std::mutex> lk(mutex_opt_vars);
118+
if( i>= 0 && i<_opt_len_ )
119+
return true;
120+
return false;
121+
#else
122+
// std::lock_guard<std::mutex> lk(mutex_opt_vars);
123+
if( i>=0 && i <opt_quat.size() )
124+
return true;
125+
return false;
126+
#endif
127+
128+
}
114129

115130
void PoseGraphSLAM::allocate_and_append_new_opt_variable_withpose( const Matrix4d& pose )
116131
{
@@ -1671,7 +1686,7 @@ void PoseGraphSLAM::reinit_ceres_problem_onnewloopedge_optimize6DOF()
16711686
int setID_of__world_of_u = manager->getWorldsConstPtr()->find_setID_of_world_i( world_of_u );
16721687

16731688
// --------------> add odometry residue : u <---> u-f
1674-
for( int f=1 ; f<5 ; f++ ){
1689+
for( int f=1 ; f<6 ; f++ ){
16751690
int world_of_u_m_f=-1;
16761691
if( u-f >= 0 )
16771692
world_of_u_m_f = manager->which_world_is_this( manager->getNodeTimestamp(u-f) );
@@ -1899,7 +1914,8 @@ void PoseGraphSLAM::reinit_ceres_problem_onnewloopedge_optimize6DOF()
18991914
continue;
19001915
}
19011916
__reint_node_regularization_info( cout << TermColor::CYAN() << "&&&&&&&&&&&&world#" << ww << " is in setID=" << ww_setid << " with ww_start=" << ww_start << " ww_end=" << ww_end << TermColor::RESET() << endl; )
1902-
if( (ww_setid >= 0 && ww_setid==ww) )
1917+
if( (ww_setid >= 0 && ww_setid==ww) ) //< Mark only setIDs as constant,
1918+
// if( ww_setid >= 0 ) //< call all 0th nodes as constant
19031919
{
19041920
__reint_node_regularization_info( cout << "Mark node#" << ww_start << " as constant. \n"; )
19051921
reg_debug_info += "Mark node#" + to_string(ww_start) + " as constant \n";
@@ -1964,13 +1980,23 @@ void PoseGraphSLAM::reinit_ceres_problem_onnewloopedge_optimize6DOF()
19641980
reinit_ceres_problem_onnewloopedge_optimize6DOF_status = 2; // solving in progress.
19651981
eta.tic();
19661982
{
1967-
std::lock_guard<std::mutex> lk(mutex_opt_vars);
1983+
// note: ideally you want to acquire the lock before you Solve,
1984+
// so that other threads do not access the opt_vars through the slam->getPose().
1985+
// However, since ceres doesnot change the opt variable until the
1986+
// end it is usually alright to not acquire the block.
1987+
// By not locking here, the stall in the viz goes away.
1988+
// If someone has a better suggestion here, feel free to open a
1989+
// discussion on github-issues.
1990+
1991+
// std::lock_guard<std::mutex> lk(mutex_opt_vars);
19681992
ceres::Solve( reint_options, &reint_problem, &reint_summary );
19691993

19701994
__reint_ceres_solve_info( cout << "summary.termination_type = "<< (ceres::TerminationType::CONVERGENCE == reint_summary.termination_type) << endl; )
19711995
// if( reint_summary.termination_type == ceres::TerminationType::CONVERGENCE )
1996+
{
19721997
solved_until = node_len-1;
19731998
__reint_ceres_solve_info( cout << "solved_until:=" << node_len-1 << endl; )
1999+
}
19742000

19752001
if( reint_summary.termination_type == ceres::TerminationType::CONVERGENCE )
19762002
n_solve_convergences++;

src/PoseGraphSLAM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class PoseGraphSLAM
9797
// Get the optimized pose at node i. This function is thread-safe
9898
const Matrix4d getNodePose( int i ) const; //< this gives the pose from the optimization variable
9999
bool nodePoseExists( int i ) const; //< returns if ith node pose exist
100+
bool nodePoseExists__nolock( int i ) const; //< returns if ith node pose exist
100101
// bool getNodePose( int i, Matrix4d& ) const; //TODO: removal
101102
int nNodes() const;
102103
void getAllNodePose( vector<Matrix4d>& vec_w_T_ci ) const;

src/VizPoseGraph.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ void VizPoseGraph::publishLastNNodes( int n )
6565

6666

6767
void VizPoseGraph::publishNodesAsLineStrip( const vector<Matrix4d>& w_T_ci,
68-
const string& ns, float r, float g, float b ) const
68+
const string& ns, float r, float g, float b, float linewidth_multiplier ) const
6969
{
7070
// this is a cost effective way to visualize camera path
7171
visualization_msgs::Marker marker;
7272
RosMarkerUtils::init_line_marker( marker );
7373

7474
marker.type = visualization_msgs::Marker::LINE_STRIP;
75-
marker.scale.x = 0.08;
75+
marker.scale.x = 0.08*linewidth_multiplier;
7676
marker.ns = ns;
7777

7878

@@ -445,13 +445,13 @@ void VizPoseGraph::publishSlamResidueVisual( int n ) const
445445

446446

447447

448-
void VizPoseGraph::publishCameraVisualMarker( const Matrix4d& wTc, const string& ns, float r, float g, float b )
448+
void VizPoseGraph::publishCameraVisualMarker( const Matrix4d& wTc, const string& ns, float r, float g, float b, float linewidth_multiplier, float camera_size_multiplier )
449449
{
450450
visualization_msgs::Marker marker2 ;
451-
RosMarkerUtils::init_camera_marker( marker2, 10 );
451+
RosMarkerUtils::init_camera_marker( marker2, camera_size_multiplier );
452452
RosMarkerUtils::setpose_to_marker( wTc, marker2 );
453453
RosMarkerUtils::setcolor_to_marker( r,g,b, marker2 );
454-
marker2.scale.x = 0.08;
454+
marker2.scale.x = 0.08*linewidth_multiplier;
455455
marker2.id = 0;
456456
marker2.ns = ns+string("_cam_visual");
457457
pub_pgraph.publish( marker2 );

src/VizPoseGraph.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class VizPoseGraph
4646
void setOdometryPublisher( const ros::Publisher& pub );
4747

4848
// void publishLastNNodes( int n ); //< Removal
49-
void publishNodesAsLineStrip( const vector<Matrix4d>& w_T_ci, const string& ns, float r, float g, float b ) const;
49+
void publishNodesAsLineStrip( const vector<Matrix4d>& w_T_ci, const string& ns, float r, float g, float b, float linewidth_multiplier=1.0 ) const;
5050
void publishNodesAsLineStrip( const vector<Matrix4d>& w_T_ci, const string& ns,
5151
float r, float g, float b,
5252
int idx_partition,
@@ -60,7 +60,7 @@ class VizPoseGraph
6060
void publishLastNEdges( int n ) const;
6161

6262
void publishSlamResidueVisual( int n ) const;
63-
void publishCameraVisualMarker( const Matrix4d& wTc, const string& ns, float r, float g, float b );
63+
void publishCameraVisualMarker( const Matrix4d& wTc, const string& ns, float r, float g, float b, float linewidth_multiplier=1.0, float camera_size_multiplier=10. );
6464

6565
void publishXYZAxis( const Matrix4d& wT_axis, const string ns, int id );
6666
void publishThisVisualMarker( const visualization_msgs::Marker& the_marker );

0 commit comments

Comments
 (0)