Threads and call flow of mola::mapper::Mapper. Update this whenever methods
are added, renamed, or the call structure changes.
Styles: rounded boxes are public API, sharp boxes are internal helpers, and dashed boxes are background threads.
flowchart TD
subgraph front["Other modules"]
LIO(["LIO: requestInsertKeyframe()"])
RAW(["Raw data source: onNewObservation()"])
end
subgraph api["Mapper API"]
REQ(["requestInsertKeyframe()"])
OBS(["onNewObservation()"])
FIN(["finalize() / onQuit()"])
DIAG(["getDiagnostics()"])
end
subgraph state["Shared state (state_mtx_)"]
KFS[("keyframes_, loops_, georef_")]
PEND[("pending_gnss_, imu_samples_, gyro_samples_")]
end
subgraph attach["Raw data attachment (state_mtx_ held)"]
GNSS["attach_gnss_locked()"]
RETRY["retry_pending_gnss_locked()"]
GRAV["attach_gravity_locked()"]
GYRO["attach_gyro_rotation_locked()"]
end
subgraph opt["Optimizer thread"]
OLOOP["optimizer_thread_loop()"]
SOLVE["solve_global_graph()"]
GINPUT["graph_input_locked()"]
OGG["optimize_global_graph()"]
PUBGEO["publish_georeference()"]
end
subgraph lc["Loop closure thread"]
LLOOP["loop_closure_thread_loop()"]
LSCAN["run_loop_closure_scan()"]
ANALYZE["lc_engine_->analyze()"]
end
LIO --> REQ
RAW --> OBS
OBS --> PEND
OBS --> GNSS
GNSS --> KFS
REQ --> GRAV
REQ --> GYRO
REQ --> KFS
REQ --> RETRY
RETRY --> GNSS
GRAV --> PEND
GYRO --> PEND
REQ -. "optimizer_new_data_" .-> OLOOP
OLOOP --> SOLVE
SOLVE --> GINPUT
GINPUT --> KFS
SOLVE --> OGG
SOLVE -->|"poses, georef"| KFS
SOLVE --> PUBGEO
LLOOP --> LSCAN
LSCAN -->|"snapshot"| KFS
LSCAN --> ANALYZE
LSCAN -->|"new loop edges"| KFS
LSCAN -. "optimizer_new_data_" .-> OLOOP
FIN --> SOLVE
FIN --> LSCAN
DIAG --> KFS
classDef thread stroke-dasharray: 5 5
class OLOOP,LLOOP thread
requestInsertKeyframe() is called by the front end (LIO) and returns the new
keyframe index.
- Under
state_mtx_, a keyframe not newer than the last one is rejected (std::nullopt). - The initial pose in the map frame is the newest keyframe estimate plus the front end increment, if both keyframes share the source frame; the first keyframe defines the map frame.
attach_gravity_locked()averages the accelerometer readings of theimu_gravity_windowbefore the keyframe and stores them as aCObservationIMUin the keyframe auxiliary observations (needsimu_gravity_min_samples).attach_gyro_rotation_locked()integrates the gyroscope readings between the previous keyframe and this one (integrate_gyro()), with the bias from the firstmola::ImuBiasSourcethat has an estimate, else zero withgyro_bias_sigma. The rotation is rotated to the vehicle frame and stored with its sigma. With wheel odometry,attach_wheel_odom_locked()stores its increment since the previous keyframe (wheel_odometry_increment()), or queues the keyframe inpending_wheel_odom_until the readings reach its stamp.retry_pending_gnss_locked()re-attempts the GNSS readings that were waiting for a keyframe.- After releasing the lock,
optimizer_new_data_is set and the optimizer thread is woken.
onNewObservation() filters by sensor label regex (gnss_sensor_labels_re,
imu_sensor_labels_re, gyro_sensor_labels_re, image_sensor_labels_re,
wheel_odom_sensor_labels_re) and
stores under state_mtx_:
- GNSS:
attach_gnss_locked()keeps the reading pending until a keyframe at or after its stamp exists. Then it is attached to the nearest keyframe withingnss_max_time_offset; a keyframe keeps only its closest reading (attach_nearest_locked(), slot "gnss"). - Images: the same rule (slot "image:",
pending_images_). Once a later image of that camera is farther from the keyframe than the attached one (and a newer keyframe exists),finish_images_locked()swaps it for a copy referring to a file, written by thedisk_io_thread;finalize()waits for those writes before saving the outputs. Images are not recorded (record_input) nor passed to the graph input. - Accelerometer: queued in
imu_samples_(window plus a few seconds). - Gyroscope: the first IMU label matching
gyro_sensor_labels_reis queued ingyro_samples_(last 60 s). - Wheel odometry (
CObservationOdometry): the first matching label is queued inwheel_odom_samples_(last 60 s, and always back to the keyframe the next increment starts at); each reading retriespending_wheel_odom_(a throttled warning if many keyframes wait).
optimizer_thread_loop() waits for optimizer_new_data_ (new keyframe, new
GNSS reading, or new loop edges) and calls solve_global_graph(), at most once
every optimizer_min_period. With enable_optimizer_thread: false, only
optimize_now() solves.
solve_global_graph() (one solve at a time, solve_mtx_):
- Copies the graph input under
state_mtx_(graph_input_locked()), then releases it. - Runs
optimize_global_graph()(seeGlobalGraph.h): GTSAM batch Levenberg-Marquardt over the keyframe posesP(i)andT_enu_to_mapT(0), plus the marginal covariance ofT_enu_to_mapwhen it can be geo-referenced. - Under
state_mtx_, stores the optimized poses. Keyframes inserted while solving are re-chained onto the new estimates. Stores the datum and the geo-reference. - Publishes the geo-reference through
advertiseUpdatedMap()(publish_georeference()), if there is one. - If the solve added loop closures and
local_map_correctionsis true,advertise_local_map_correction()(mola::LocalMapCorrectionSource), still undersolve_mtx_but notstate_mtx_. Subscribers (LIO) then calllocal_map_keyframes(), which copies the keyframes near a reference one understate_mtx_.
Started by initialize() if loop_closure_enabled. It uses a
mola::FrameToFrameLoopClosure engine (pipeline in
loop_closure_pipeline_file) only as a detector (analyze()).
loop_closure_thread_loop() wakes every loop_closure_check_period_sec or on
request_loop_closure_scan(), and calls run_loop_closure_scan():
- Under
state_mtx_, builds aCSimpleMapsnapshot of the keyframe poses and front end observations, and the pairs already closed. Without a forced scan, it returns if fewer thanloop_closure_min_new_keyframeskeyframes are new. - The scan is incremental (only pairs with a new keyframe), or full if forced,
if
loop_closure_incrementalis false, or everyloop_closure_full_scan_every_nincremental scans. analyze()runs without the lock. The edges are sorted by keyframe pair, so the result does not depend on thread timing.merge_loop_edges(): understate_mtx_, the odometry check of the new pairs. Without the consistency check, they are appended toloops_. With it, they joinlc_gate_(all loops that passed the odometry check), which decides again, without the lock, the groups of neighbor loops they join; thenloops_is set to the accepted ones. Ifloops_changed (alsoloops_revision_, which triggers a local map correction after the next solve), the optimizer is woken.
wait_for_loop_closure_idle() blocks until no scan is running or requested.
finalize() runs once, from onQuit() or the destructor:
- Stops the loop closure and optimizer threads.
optimize_now().- If loop closure is enabled, up to
loop_closure_finalize_roundsrounds of a full scan (in the calling thread) followed byoptimize_now(), stopping at the first round without new loops. save_outputs(): graph input, TUM trajectory, geo-reference, state estimator trajectory and geo-reference, loop closures and simplemap, for theoutput_*parameters that are set.
state_mtx_guards keyframes, loops, georeference and pending data. Never call other modules (state estimator, loop closure engine, publishers) with it held.- The loop closure engine and the optimizer only work on copies taken under
state_mtx_. - The map frame is the one of the first keyframe pose in the front end source frame, which is also where the state estimator starts.