Skip to content

Commit 16b95e2

Browse files
committed
Update RRT::plan to use tl
1 parent 55f9b1c commit 16b95e2

2 files changed

Lines changed: 16 additions & 26 deletions

File tree

roboplan_rrt/include/roboplan_rrt/rrt.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ class RRT {
4545
/// @param options A struct containing RRT options.
4646
RRT(const std::shared_ptr<Scene> scene, const RRTOptions& options = RRTOptions());
4747

48-
tl::expected<JointPath, std::string> plan_expected(const JointConfiguration& start,
49-
const JointConfiguration& goal);
50-
5148
/// @brief Plan a path from start to goal.
5249
/// @param start The starting joint configuration.
5350
/// @param goal The goal joint configuration.
54-
/// @return A joint-space path, if planning succeeds, else std::nullopt.
55-
std::optional<JointPath> plan(const JointConfiguration& start, const JointConfiguration& goal);
51+
/// @return A joint-space path, if planning succeeds, otherwise an error message.
52+
tl::expected<JointPath, std::string> plan(const JointConfiguration& start,
53+
const JointConfiguration& goal);
5654

5755
/// @brief Sets the seed for the random number generator (RNG).
5856
/// @details For reproducibility, this also seeds the underlying scene.

roboplan_rrt/src/rrt.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <chrono>
2+
#include <sstream>
23
#include <stdexcept>
34

45
#include <roboplan/core/path_utils.hpp>
@@ -32,28 +33,16 @@ RRT::RRT(const std::shared_ptr<Scene> scene, const RRTOptions& options)
3233
state_space_.set_bounds(lower_bounds, upper_bounds);
3334
};
3435

35-
tl::expected<JointPath, std::string> RRT::plan_expected(const JointConfiguration& start,
36-
const JointConfiguration& goal) {
37-
std::optional<JointPath> maybe_path = plan(start, goal);
38-
39-
if (maybe_path.has_value()) {
40-
return maybe_path.value();
41-
} else {
42-
return tl::make_unexpected("Failed to find path!");
43-
}
44-
}
45-
46-
std::optional<JointPath> RRT::plan(const JointConfiguration& start,
47-
const JointConfiguration& goal) {
36+
tl::expected<JointPath, std::string> RRT::plan(const JointConfiguration& start,
37+
const JointConfiguration& goal) {
4838
std::cout << "Planning...\n";
4939

5040
const auto& q_start = start.positions;
5141
const auto& q_goal = goal.positions;
5242

5343
// Ensure the start and goal poses are valid
5444
if (!scene_->isValidPose(q_start) || !scene_->isValidPose(q_goal)) {
55-
std::cout << "Invalid poses requested, cannot plan!\n";
56-
return std::nullopt;
45+
return tl::make_unexpected("Invalid poses requested, cannot plan!");
5746
}
5847

5948
// Check whether direct connection between the start and goal are possible.
@@ -83,14 +72,18 @@ std::optional<JointPath> RRT::plan(const JointConfiguration& start,
8372
auto elapsed =
8473
std::chrono::duration<double>(std::chrono::steady_clock::now() - start_time).count();
8574
if (options_.max_planning_time > 0 && options_.max_planning_time <= elapsed) {
86-
std::cout << "RRT timed out after " << options_.max_planning_time << " seconds.\n";
87-
break;
75+
std::stringstream ss;
76+
ss << "RRT timed out after " << options_.max_planning_time << " seconds.";
77+
std::cout << ss.str() << "\n";
78+
return tl::make_unexpected(ss.str());
8879
}
8980

9081
// Check loop termination criteria.
9182
if (start_nodes_.size() + goal_nodes_.size() >= options_.max_nodes) {
92-
std::cout << "Added maximum number of nodes (" << options_.max_nodes << ").\n";
93-
break;
83+
std::stringstream ss;
84+
ss << "Added maximum number of nodes (" << options_.max_nodes << ").";
85+
std::cout << ss.str() << "\n";
86+
return tl::make_unexpected(ss.str());
9487
}
9588

9689
// Set grow and target tree for this loop iteration.
@@ -126,8 +119,7 @@ std::optional<JointPath> RRT::plan(const JointConfiguration& start,
126119
}
127120
}
128121

129-
std::cout << "Unable to find a plan!\n";
130-
return std::nullopt;
122+
return tl::make_unexpected("Unable to find a path!");
131123
}
132124

133125
void RRT::initializeTree(KdTree& tree, std::vector<Node>& nodes, const Eigen::VectorXd& q_init,

0 commit comments

Comments
 (0)