Skip to content

Commit f502c4b

Browse files
committed
nav: update and modernize API
1 parent 7b15a16 commit f502c4b

25 files changed

+189
-302
lines changed

apps/mrpt_apps_gui/ReactiveNav3D-Demo/ReactiveNav3D_demo.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,14 @@ class CMyReactInterface : public mrpt::nav::CRobot2NavInterfaceForSimulator_Diff
473473
gui::CDisplayWindow3D window;
474474
Scene::Ptr scene;
475475

476-
bool getCurrentPoseAndSpeeds(
477-
mrpt::math::TPose2D& curPose,
478-
mrpt::math::TTwist2D& curVel,
479-
mrpt::system::TTimeStamp& timestamp,
480-
mrpt::math::TPose2D& odomPose,
481-
std::string& pose_frame_id) override
476+
std::optional<CurrentPoseAndSpeeds> getCurrentPoseAndSpeeds() override
482477
{
483-
curPose = robotSim.getCurrentGTPose();
484-
curVel = robotSim.getCurrentGTVel();
485-
timestamp = mrpt::Clock::now();
486-
odomPose = robotSim.getCurrentOdometricPose();
487-
return true;
478+
return CurrentPoseAndSpeeds{
479+
.pose = robotSim.getCurrentGTPose(),
480+
.velGlobal = robotSim.getCurrentGTVel(),
481+
.timestamp = mrpt::Clock::now(),
482+
.odometry = robotSim.getCurrentOdometricPose(),
483+
};
488484
}
489485

490486
bool senseObstacles(

doc/source/doxygen-docs/port_mrpt3.md

Lines changed: 13 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -338,90 +338,38 @@ increasing risk/effort. Each item is prefixed with a priority tag:
338338
339339
---
340340
341-
#### 13.6.1 Enum modernisation
342-
343-
**[DONE]** Converted all plain `enum` to `enum class`, with redundant prefixes removed from values:
344-
345-
| Old | New |
346-
|-----|-----|
347-
| `CAbstractNavigator::TState` — `IDLE`, `NAVIGATING`, `SUSPENDED`, `NAV_ERROR` | `enum class TState` — same values (`NAV_ERROR` kept to avoid clash with Windows `ERROR` macro) |
348-
| `CAbstractNavigator::TErrorCode` — `ERR_NONE`, `ERR_EMERGENCY_STOP`, `ERR_CANNOT_REACH_TARGET`, `ERR_OTHER` | `enum class TErrorCode` — `NONE`, `EMERGENCY_STOP`, `CANNOT_REACH_TARGET`, `OTHER` |
349-
| `CHolonomicND::TSituations` — `SITUATION_TARGET_DIRECTLY`, `SITUATION_SMALL_GAP`, `SITUATION_WIDE_GAP`, `SITUATION_NO_WAY_FOUND` | `enum class TSituations` — `TARGET_DIRECTLY`, `SMALL_GAP`, `WIDE_GAP`, `NO_WAY_FOUND` |
350-
| `PTG_collision_behavior_t` — `COLL_BEH_BACK_AWAY`, `COLL_BEH_STOP` | `enum class PTGCollisionBehavior` — `BACK_AWAY`, `STOP` |
351-
352-
---
353-
354-
#### 13.6.2 `[[nodiscard]]` annotations
355-
356-
**[DONE]** Added `[[nodiscard]]` to:
357-
358-
- `CAbstractNavigator`: `getCurrentState()`, `getErrorReason()`,
359-
`getFrameTF()`, `isRethrowNavExceptionsEnabled()`, `getDelaysTimeLogger()`
360-
- `CParameterizedTrajectoryGenerator`: `getDescription()`, `supportVelCmdNOP()`,
361-
`supportSpeedAtTarget()`, `isInitialized()`, `getAlphaValuesCount()`,
362-
`getPathCount()`, `getRefDistance()`, `getScorePriority()`,
363-
`getClearanceStepCount()`, `getClearanceDecimatedPaths()`, `getPathStepCount()`,
364-
`getPathPose()`, `getPathDist()`, `getPathStepDuration()`, `getMaxLinVel()`,
365-
`getMaxAngVel()`, `getMaxRobotRadius()`, `isPointInsideRobotShape()`
366-
- `CAbstractHolonomicReactiveMethod`: `getAssociatedPTG()`
367-
- `CWaypointsNavigator`: `getWaypointsAccessGuard()`
341+
#### 13.6.2 `[[nodiscard]]` annotations (remaining)
368342
369343
Still pending: `CAbstractPTGBasedReactive` getters, holonomic `TOptions` getters.
370344
371345
---
372346
373-
#### 13.6.3 Naming consistency
374-
375-
**[PARTIAL]** Done:
376-
- `CPTG_DiffDrive_CC/CCS/alpha`: replaced `os::sprintf` + `(int)K` casts with
377-
`mrpt::format()` + `static_cast<int>(K)`.
347+
#### 13.6.3 Naming consistency (remaining)
378348
379-
Remaining:
380-
- **Config structs**: Holonomic classes use `TOptions`; navigator classes use `TParams`.
381-
Pick one suffix uniformly.
382-
- **STEP-numbered methods**: `STEP1_InitPTGs()` … `STEP8_GenerateLogRecord()` —
383-
rename to descriptive names.
384-
- **`impl_` prefix**: Standardise virtual hook naming convention.
349+
- **Config structs**: Holonomic classes use `TOptions`; navigator/optimizer/graphslam
350+
classes use `TParams`. Low-value cosmetic change across multiple modules — deferred.
385351
386352
---
387353
388-
#### 13.6.4 Thread-safety fixes
389-
390-
**[DONE]**:
391-
- `CAbstractPTGBasedReactive::preDestructor()`: replaced bare
392-
`m_nav_cs.lock(); m_nav_cs.unlock()` with `{ std::scoped_lock lck(m_nav_cs); }`.
393-
- `CWaypointsNavigator`: replaced `beginWaypointsAccess()` / `endWaypointsAccess()`
394-
with `getWaypointsAccessGuard()` returning a `WaypointsAccessGuard` RAII object
395-
that holds `std::unique_lock<std::recursive_mutex>` and exposes `waypoints()`.
354+
#### 13.6.4 Thread-safety fixes (remaining)
396355
397-
Remaining: global mutable `OUTPUT_DEBUG_PATH_PREFIX` / `COLLISION_BEHAVIOR` statics
356+
Global mutable `OUTPUT_DEBUG_PATH_PREFIX` / `COLLISION_BEHAVIOR` statics
398357
have no synchronisation — consider moving to per-instance fields or guarding with mutex.
399358
400359
---
401360
402-
#### 13.6.5 Return-value modernisation (output-reference cleanup)
403-
404-
**[TODO — P1]** Key candidates:
361+
#### 13.6.5 Return-value modernisation (remaining)
405362
406363
| Method | Current | Proposed |
407364
|--------|---------|----------|
408365
| `CRobot2NavInterface::getCurrentPoseAndSpeeds()` | 4 output `&` params | Return `struct PoseAndSpeeds { TPose2D pose; TTwist2D vel; … };` |
409-
| `CAbstractHolonomicReactiveMethod::navigate()` | `NavOutput&` | Return `NavOutput` by value |
410-
| `CMultiObjectiveMotionOptimizerBase::decide()` | `int&` best index | Return `std::optional<size_t>` (nullopt = no good motion) |
411366
| `CParameterizedTrajectoryGenerator::inverseMap_WS2TP()` | `int& k, double& d` output | Return `std::optional<std::pair<uint16_t,double>>` |
412367
413368
---
414369
415-
#### 13.6.6 Raw-pointer cleanup
416-
417-
**[DONE — documented]**: Both `m_associatedPTG` in `CAbstractHolonomicReactiveMethod`
418-
and `TCandidateMovementPTG::PTG` are non-owning observer raw pointers. Both are now
419-
documented explicitly as such, with lifetime contract stated. Since `nullptr` is a valid
420-
state and the PTG lifetime is guaranteed by the owning reactive system, a raw pointer
421-
is the correct tool; `std::weak_ptr` would require the entire PTG ownership chain to
422-
be changed to shared ownership.
370+
#### 13.6.6 Raw-pointer cleanup (remaining)
423371
424-
Remaining: `CAbstractPTGBasedReactive::getHoloMethod()` returns a raw pointer;
372+
`CAbstractPTGBasedReactive::getHoloMethod()` returns a raw pointer;
425373
consider returning `CAbstractHolonomicReactiveMethod&` (with assert) for non-nullable
426374
use sites.
427375
@@ -435,10 +383,10 @@ logging, obstacle processing, velocity generation, and profiling) is the
435383
single largest maintenance burden. Proposed decomposition:
436384
437385
1. **Extract `PTGSetManager`** — owns the `vector<CParameterizedTrajectoryGenerator::Ptr>`,
438-
handles `STEP1_InitPTGs()`, `getPTG()`, `getPTG_count()`,
386+
handles `initPTGs()`, `getPTG()`, `getPTG_count()`,
439387
collision-grid builds.
440388
2. **Extract `NavigationLogger`** — owns the `CLogFileRecord`,
441-
`m_critZoneLastLog`, log-file path management, `STEP8_GenerateLogRecord()`.
389+
`m_critZoneLastLog`, log-file path management, `generateLogRecord()`.
442390
3. **Extract `VelocityFilter`** — owns `TSentVelCmd`, the speed-filter
443391
tau, and `filterVelocityCommand()`.
444392
4. Keep the main class as a thin orchestrator that wires the above
@@ -462,37 +410,9 @@ abstraction. Each should be broken into named helpers:
462410
463411
---
464412
465-
#### 13.6.9 Magic numbers → named constants
466-
467-
**[DONE]**
468-
- `CHolonomicVFF.cpp`: `1e6` → `MAX_FORCE_CAP`, `20.0` → `OBSTACLE_WEIGHT_FACTOR`,
469-
`6.0` → `OBSTACLE_NEARNESS_THRESHOLD`.
470-
- `CHolonomicND.cpp`: `0.02` → `DIRECT_PATH_SECTOR_FRACTION`,
471-
`1.05` → `DIRECT_PATH_DIST_MARGIN`, `0.95` → `DIRECT_PATH_RANGE_FRACTION`,
472-
`0.01` → `MIN_TARGET_DIST`.
473-
- `CHolonomicFullEval.cpp` (`evalSingleTarget`): `1.02` → `OBSTACLE_PAST_TARGET_MARGIN`,
474-
`0.95` → `TARGET_DIST_SAFETY_FRACTION`, `1.05` → `TARGET_CLEARANCE_MARGIN`,
475-
`1.01` → `SQRT_DOMAIN_EPS`, `0.1` → `CLEARANCE_WINDOW_HALF_FRACTION`,
476-
`0.99` → `FREE_SPACE_THRESHOLD`, `4.0` → `SECTOR_DIST_WEIGHT`,
477-
`0.1` → `BLOCKED_DIR_SCORE_PENALTY`.
478-
479-
---
480-
481-
#### 13.6.10 Test coverage
413+
#### 13.6.10 Test coverage (remaining)
482414
483-
**[PARTIAL]** Added `tests/holonomic_unittest.cpp` covering:
484-
485-
- `CHolonomicVFF` in isolation (5 tests: heading accuracy × 3, speed
486-
bounds, slow-down near target, behaviour with blocked obstacles).
487-
- `CHolonomicND` in isolation (4 tests: heading accuracy × 2, speed
488-
bounds, detour when target direction blocked).
489-
- `CHolonomicFullEval` in isolation (5 tests: heading accuracy × 3, speed
490-
bounds, repeated calls / hysteresis stability). Also fixed two latent
491-
bugs: default `TOptions` had 7 factorWeights for `NUM_FACTORS=8`, and
492-
factor[7] (heading) dereferenced `ptg` without a null guard.
493-
- `ClearanceDiagram` (4 tests: construction, resize, clear, resize-twice).
494-
495-
Still zero dedicated tests for:
415+
Zero dedicated unit tests for:
496416
497417
- `CWaypointsNavigator` / `TWaypointSequence` state machine.
498418
- `CNavigatorManualSequence`.
@@ -501,28 +421,6 @@ Still zero dedicated tests for:
501421
502422
---
503423
504-
#### 13.6.11 Documentation
505-
506-
**[DONE]** `lib_mrpt_nav.md` expanded to a full reference including:
507-
navigator state machine, waypoint sequencing (RAII guard API), TP-Space
508-
reactive core, holonomic methods table, PTG class table, robot interface
509-
guide, and path planning overview.
510-
511-
---
512-
513-
#### 13.6.12 Remaining code-quality items
514-
515-
**[DONE]** Quick wins completed:
516-
517-
- `MRPT_TODO("Optimize getNearestNode() with KD-tree!")` in
518-
`PlannerRRT_SE2_TPS.cpp` — converted to plain `// TODO:` comment.
519-
- `static size_t SAVE_LOG_SOLVE_COUNT` — kept (used to name debug log
520-
files across solve() calls, guarded by `params.save_3d_log_freq > 0`).
521-
- `CLogFileRecord::serializeFrom()` raw `new` — replaced with
522-
`std::make_shared` (three sites).
523-
- C-style casts `(int)K` — replaced with `static_cast<int>(K)` in
524-
`CPTG_DiffDrive_CC.cpp`, `CPTG_DiffDrive_CCS.cpp`, `CPTG_DiffDrive_alpha.cpp`.
525-
526424
### 13.7 `mrpt_maps` — Miscellaneous
527425
528426
- **`CObservationRotatingScan`**: `// TODO: populate organizedPoints?`

modules/mrpt_nav/include/mrpt/nav/holonomic/CAbstractHolonomicReactiveMethod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class CAbstractHolonomicReactiveMethod : public mrpt::serialization::CSerializab
8787

8888
/** Invokes the holonomic navigation algorithm itself. See the description
8989
* of the input/output structures for details on each parameter. */
90-
virtual void navigate(const NavInput& ni, NavOutput& no) = 0;
90+
[[nodiscard]] virtual NavOutput navigate(const NavInput& ni) = 0;
9191

9292
/** ctor */
9393
CAbstractHolonomicReactiveMethod(const std::string& defaultCfgSectionName);

modules/mrpt_nav/include/mrpt/nav/holonomic/CHolonomicFullEval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CHolonomicFullEval : public CAbstractHolonomicReactiveMethod
4545
CHolonomicFullEval(const mrpt::config::CConfigFileBase* INI_FILE = nullptr);
4646

4747
// See base class docs
48-
void navigate(const NavInput& ni, NavOutput& no) override;
48+
[[nodiscard]] NavOutput navigate(const NavInput& ni) override;
4949

5050
void initialize(const mrpt::config::CConfigFileBase& INI_FILE) override; // See base class docs
5151
void saveConfigFile(mrpt::config::CConfigFileBase& c) const override; // See base class docs

modules/mrpt_nav/include/mrpt/nav/holonomic/CHolonomicND.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CHolonomicND : public CAbstractHolonomicReactiveMethod
6464
CHolonomicND(const mrpt::config::CConfigFileBase* INI_FILE = nullptr);
6565

6666
// See base class docs
67-
void navigate(const NavInput& ni, NavOutput& no) override;
67+
[[nodiscard]] NavOutput navigate(const NavInput& ni) override;
6868

6969
/** The structure used to store a detected gap in obstacles. */
7070
struct TGap

modules/mrpt_nav/include/mrpt/nav/holonomic/CHolonomicVFF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CHolonomicVFF : public CAbstractHolonomicReactiveMethod
5959
CHolonomicVFF(const mrpt::config::CConfigFileBase* INI_FILE = nullptr);
6060

6161
// See base class docs
62-
void navigate(const NavInput& ni, NavOutput& no) override;
62+
[[nodiscard]] NavOutput navigate(const NavInput& ni) override;
6363

6464
void initialize(const mrpt::config::CConfigFileBase& INI_FILE) override; // See base class docs
6565
void saveConfigFile(mrpt::config::CConfigFileBase& c) const override; // See base class docs

modules/mrpt_nav/include/mrpt/nav/reactive/CAbstractPTGBasedReactive.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class CAbstractPTGBasedReactive : public CWaypointsNavigator
134134

135135
/** Must be called for loading collision grids, or the first navigation
136136
* command may last a long time to be executed.
137-
* Internally, it just calls STEP1_CollisionGridsBuilder().
137+
* Internally, it just calls initPTGs().
138138
*/
139139
void initialize() override;
140140

@@ -312,18 +312,18 @@ class CAbstractPTGBasedReactive : public CWaypointsNavigator
312312

313313
// Steps for the reactive navigation sytem.
314314
// ----------------------------------------------------------------------------
315-
virtual void STEP1_InitPTGs() = 0;
315+
virtual void initPTGs() = 0;
316316

317317
/** Return false on any fatal error */
318318
virtual bool implementSenseObstacles(mrpt::system::TTimeStamp& obs_timestamp) = 0;
319-
bool STEP2_SenseObstacles();
319+
bool senseObstacles();
320320

321321
/** Builds TP-Obstacles from Workspace obstacles for the given PTG.
322322
* "out_TPObstacles" is already initialized to the proper length and
323323
* maximum collision-free distance for each "k" trajectory index.
324324
* Distances are in "pseudo-meters". They will be normalized automatically
325325
* to [0,1] upon return. */
326-
virtual void STEP3_WSpaceToTPSpace(
326+
virtual void transformToTPSpace(
327327
const size_t ptg_idx,
328328
std::vector<double>& out_TPObstacles,
329329
mrpt::nav::ClearanceDiagram& out_clearance,
@@ -370,7 +370,7 @@ class CAbstractPTGBasedReactive : public CWaypointsNavigator
370370
/** Return the [0,1] velocity scale of raw PTG cmd_vel */
371371
virtual double generate_vel_cmd(
372372
const TCandidateMovementPTG& in_movement, mrpt::kinematics::CVehicleVelCmd::Ptr& new_vel_cmd);
373-
void STEP8_GenerateLogRecord(
373+
void generateLogRecord(
374374
CLogFileRecord& newLogRec,
375375
const std::vector<mrpt::math::TPose2D>& relTargets,
376376
int nSelectedPTG,

modules/mrpt_nav/include/mrpt/nav/reactive/CMultiObjMotionOpt_Scalarization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class CMultiObjMotionOpt_Scalarization : public mrpt::nav::CMultiObjectiveMotion
6161
std::map<std::string, double> m_expr_scalar_vars;
6262

6363
// This virtual method is called by decide().
64-
int impl_decide(
64+
std::optional<size_t> impl_decide(
6565
const std::vector<mrpt::nav::TCandidateMovementPTG>& movs, TResultInfo& extra_info) override;
6666
};
6767
} // namespace mrpt::nav

modules/mrpt_nav/include/mrpt/nav/reactive/CMultiObjectiveMotionOptimizerBase.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <mrpt/nav/reactive/TCandidateMovementPTG.h>
2020
#include <mrpt/rtti/CObject.h>
2121

22+
#include <optional>
23+
2224
namespace mrpt::nav
2325
{
2426
/** Virtual base class for multi-objective motion choosers, as used for reactive
@@ -50,9 +52,10 @@ class CMultiObjectiveMotionOptimizerBase : public mrpt::rtti::CObject
5052

5153
/** The main entry point for the class: returns the 0-based index of the
5254
* best of the N motion candidates in `movs`.
53-
* If no valid one is found, `-1` will be returned.
55+
* If no valid one is found, `std::nullopt` will be returned.
5456
*/
55-
int decide(const std::vector<mrpt::nav::TCandidateMovementPTG>& movs, TResultInfo& extra_info);
57+
[[nodiscard]] std::optional<size_t> decide(
58+
const std::vector<mrpt::nav::TCandidateMovementPTG>& movs, TResultInfo& extra_info);
5659

5760
virtual void loadConfigFile(const mrpt::config::CConfigFileBase& c) = 0;
5861
virtual void saveConfigFile(mrpt::config::CConfigFileBase& c) const = 0;
@@ -97,7 +100,7 @@ class CMultiObjectiveMotionOptimizerBase : public mrpt::rtti::CObject
97100

98101
private:
99102
// This virtual method is called by decide().
100-
virtual int impl_decide(
103+
virtual std::optional<size_t> impl_decide(
101104
const std::vector<mrpt::nav::TCandidateMovementPTG>& movs, TResultInfo& extra_info) = 0;
102105

103106
TParamsBase& m_params_base;

modules/mrpt_nav/include/mrpt/nav/reactive/CReactiveNavigationSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class CReactiveNavigationSystem : public CAbstractPTGBasedReactive
119119

120120
// Steps for the reactive navigation sytem.
121121
// ----------------------------------------------------------------------------
122-
void STEP1_InitPTGs() override;
122+
void initPTGs() override;
123123

124124
// See docs in parent class
125125
bool implementSenseObstacles(mrpt::system::TTimeStamp& obs_timestamp) override;
@@ -142,7 +142,7 @@ class CReactiveNavigationSystem : public CAbstractPTGBasedReactive
142142
/** Obstacle points, before filtering (if filtering is enabled). */
143143
mrpt::maps::CSimplePointsMap m_WS_Obstacles_original;
144144
// See docs in parent class
145-
void STEP3_WSpaceToTPSpace(
145+
void transformToTPSpace(
146146
size_t ptg_idx,
147147
std::vector<double>& out_TPObstacles,
148148
mrpt::nav::ClearanceDiagram& out_clearance,

0 commit comments

Comments
 (0)