Skip to content

Commit

Permalink
refactor(goal_planner): separate thread safe data
Browse files Browse the repository at this point in the history
Signed-off-by: kosuke55 <[email protected]>
  • Loading branch information
kosuke55 committed Nov 6, 2023
1 parent ee20545 commit a6369db
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,30 @@ using behavior_path_planner::utils::path_safety_checker::SafetyCheckParams;
using behavior_path_planner::utils::path_safety_checker::TargetObjectsOnLane;
using tier4_autoware_utils::Polygon2d;

enum class PathType {
NONE = 0,
SHIFT,
ARC_FORWARD,
ARC_BACKWARD,
FREESPACE,
};
#define DEFINE_SETTER(TYPE, NAME) \
public: \
void set_##NAME(const TYPE & value) \
{ \
NAME##_ = value; \
}

#define DEFINE_SETTER_GETTER(TYPE, NAME) \
public: \
void set_##NAME(const TYPE & value) \
{ \
const std::lock_guard<std::recursive_mutex> lock(mutex_); \
NAME##_ = value; \
} \
\
TYPE get_##NAME() const \
{ \
const std::lock_guard<std::recursive_mutex> lock(mutex_); \
return NAME##_; \
#define DEFINE_GETTER(TYPE, NAME) \
public: \
TYPE get_##NAME() const \
{ \
return NAME##_; \
}

#define DEFINE_SETTER_GETTER(TYPE, NAME) \
DEFINE_SETTER(TYPE, NAME) \
DEFINE_GETTER(TYPE, NAME)

class PullOverStatus
{
public:
explicit PullOverStatus(std::recursive_mutex & mutex) : mutex_(mutex) {}

// Reset all data members to their initial states
void reset()
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
pull_over_path_ = nullptr;
lane_parking_pull_over_path_ = nullptr;
current_path_idx_ = 0;
require_increment_ = true;
Expand All @@ -111,14 +103,12 @@ class PullOverStatus
has_decided_path_ = false;
is_safe_static_objects_ = false;
is_safe_dynamic_objects_ = false;
prev_is_safe_ = false;
prev_found_path_ = false;
prev_is_safe_dynamic_objects_ = false;
has_decided_velocity_ = false;
has_requested_approval_ = false;
is_ready_ = false;
}

DEFINE_SETTER_GETTER(std::shared_ptr<PullOverPath>, pull_over_path)
DEFINE_SETTER_GETTER(std::shared_ptr<PullOverPath>, lane_parking_pull_over_path)
DEFINE_SETTER_GETTER(size_t, current_path_idx)
DEFINE_SETTER_GETTER(bool, require_increment)
Expand All @@ -130,22 +120,14 @@ class PullOverStatus
DEFINE_SETTER_GETTER(bool, has_decided_path)
DEFINE_SETTER_GETTER(bool, is_safe_static_objects)
DEFINE_SETTER_GETTER(bool, is_safe_dynamic_objects)
DEFINE_SETTER_GETTER(bool, prev_is_safe)
DEFINE_SETTER_GETTER(bool, prev_found_path)
DEFINE_SETTER_GETTER(bool, prev_is_safe_dynamic_objects)
DEFINE_SETTER_GETTER(bool, has_decided_velocity)
DEFINE_SETTER_GETTER(bool, has_requested_approval)
DEFINE_SETTER_GETTER(bool, is_ready)
DEFINE_SETTER_GETTER(std::shared_ptr<rclcpp::Time>, last_increment_time)
DEFINE_SETTER_GETTER(std::shared_ptr<rclcpp::Time>, last_path_update_time)
DEFINE_SETTER_GETTER(std::optional<GoalCandidate>, modified_goal_pose)
DEFINE_SETTER_GETTER(Pose, refined_goal_pose)
DEFINE_SETTER_GETTER(GoalCandidates, goal_candidates)
DEFINE_SETTER_GETTER(Pose, closest_goal_candidate_pose)
DEFINE_SETTER_GETTER(std::vector<PullOverPath>, pull_over_path_candidates)
DEFINE_SETTER_GETTER(std::optional<Pose>, closest_start_pose)

private:
std::shared_ptr<PullOverPath> pull_over_path_{nullptr};
std::shared_ptr<PullOverPath> lane_parking_pull_over_path_{nullptr};
size_t current_path_idx_{0};
bool require_increment_{true};
Expand All @@ -157,30 +139,124 @@ class PullOverStatus
bool has_decided_path_{false};
bool is_safe_static_objects_{false};
bool is_safe_dynamic_objects_{false};
bool prev_is_safe_{false};
bool prev_found_path_{false};
bool prev_is_safe_dynamic_objects_{false};
bool has_decided_velocity_{false};
bool has_requested_approval_{false};
bool is_ready_{false};

// save last time and pose
std::shared_ptr<rclcpp::Time> last_increment_time_;
std::shared_ptr<rclcpp::Time> last_path_update_time_;

// goal modification
std::optional<GoalCandidate> modified_goal_pose_;
Pose refined_goal_pose_{};
GoalCandidates goal_candidates_{};
Pose closest_goal_candidate_pose_{};
};

#undef DEFINE_SETTER
#undef DEFINE_GETTER
#undef DEFINE_SETTER_GETTER

#define DEFINE_SETTER_WITH_MUTEX(TYPE, NAME) \
public: \
void set_##NAME(const TYPE & value) \
{ \
const std::lock_guard<std::recursive_mutex> lock(mutex_); \
NAME##_ = value; \
}

#define DEFINE_GETTER_WITH_MUTEX(TYPE, NAME) \
public: \
TYPE get_##NAME() const \
{ \
const std::lock_guard<std::recursive_mutex> lock(mutex_); \
return NAME##_; \
}

#define DEFINE_SETTER_GETTER_WITH_MUTEX(TYPE, NAME) \
DEFINE_SETTER_WITH_MUTEX(TYPE, NAME) \
DEFINE_GETTER_WITH_MUTEX(TYPE, NAME)

// pull over path
class ThreadSafeData
{
public:
ThreadSafeData(std::recursive_mutex & mutex, rclcpp::Clock::SharedPtr clock)
: mutex_(mutex), clock_(clock)
{
}

bool incrementPathIndex()

Check warning on line 183 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L183

Added line #L183 was not covered by tests
{
const std::lock_guard<std::recursive_mutex> lock(mutex_);

Check warning on line 185 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L185

Added line #L185 was not covered by tests
if (pull_over_path_->incrementPathIndex()) {
last_path_idx_increment_time_ = clock_->now();
return true;

Check warning on line 188 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L188

Added line #L188 was not covered by tests
}
return false;
}

void set_pull_over_path(const PullOverPath & value)

Check warning on line 193 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L193

Added line #L193 was not covered by tests
{
const std::lock_guard<std::recursive_mutex> lock(mutex_);

Check warning on line 195 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L195

Added line #L195 was not covered by tests
pull_over_path_ = std::make_shared<PullOverPath>(value);
last_path_update_time_ = clock_->now();
}

Check warning on line 198 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L198

Added line #L198 was not covered by tests

void set_pull_over_path(const std::shared_ptr<PullOverPath> & value)

Check warning on line 200 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L200

Added line #L200 was not covered by tests
{
const std::lock_guard<std::recursive_mutex> lock(mutex_);

Check warning on line 202 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L202

Added line #L202 was not covered by tests
pull_over_path_ = value;
last_path_update_time_ = clock_->now();
}

Check warning on line 205 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L205

Added line #L205 was not covered by tests

void clearPullOverPath()

Check warning on line 207 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L207

Added line #L207 was not covered by tests
{
const std::lock_guard<std::recursive_mutex> lock(mutex_);

Check warning on line 209 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L209

Added line #L209 was not covered by tests
pull_over_path_ = nullptr;
}

Check warning on line 211 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L211

Added line #L211 was not covered by tests

bool foundPullOverPath() const

Check warning on line 213 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L213

Added line #L213 was not covered by tests
{
const std::lock_guard<std::recursive_mutex> lock(mutex_);

Check warning on line 215 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L215

Added line #L215 was not covered by tests
if (!pull_over_path_) {
return false;
}

return pull_over_path_->isValidPath();

Check warning on line 220 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L220

Added line #L220 was not covered by tests
}

void reset()
{
const std::lock_guard<std::recursive_mutex> lock(mutex_);
pull_over_path_ = nullptr;
pull_over_path_candidates_.clear();
goal_candidates_.clear();
modified_goal_pose_ = std::nullopt;
last_path_update_time_ = std::nullopt;
last_path_idx_increment_time_ = std::nullopt;
closest_start_pose_ = std::nullopt;
}

DEFINE_GETTER_WITH_MUTEX(std::shared_ptr<PullOverPath>, pull_over_path)
DEFINE_GETTER_WITH_MUTEX(std::optional<rclcpp::Time>, last_path_update_time)
DEFINE_GETTER_WITH_MUTEX(std::optional<rclcpp::Time>, last_path_idx_increment_time)

Check warning on line 237 in planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/scene_module/goal_planner/goal_planner_module.hpp#L235-L237

Added lines #L235 - L237 were not covered by tests

DEFINE_SETTER_GETTER_WITH_MUTEX(std::vector<PullOverPath>, pull_over_path_candidates)
DEFINE_SETTER_GETTER_WITH_MUTEX(GoalCandidates, goal_candidates)
DEFINE_SETTER_GETTER_WITH_MUTEX(std::optional<GoalCandidate>, modified_goal_pose)
DEFINE_SETTER_GETTER_WITH_MUTEX(std::optional<Pose>, closest_start_pose)

private:
std::shared_ptr<PullOverPath> pull_over_path_{nullptr};
std::vector<PullOverPath> pull_over_path_candidates_;
std::optional<Pose> closest_start_pose_;
GoalCandidates goal_candidates_{};
std::optional<GoalCandidate> modified_goal_pose_;
std::optional<rclcpp::Time> last_path_update_time_;
std::optional<rclcpp::Time> last_path_idx_increment_time_;
std::optional<Pose> closest_start_pose_{};

std::recursive_mutex & mutex_;
rclcpp::Clock::SharedPtr clock_;
};

#undef DEFINE_SETTER_GETTER
#undef DEFINE_SETTER_WITH_MUTEX
#undef DEFINE_GETTER_WITH_MUTEX
#undef DEFINE_SETTER_GETTER_WITH_MUTEX

struct FreespacePlannerDebugData
{
Expand Down Expand Up @@ -276,6 +352,7 @@ class GoalPlannerModule : public SceneModuleInterface

std::recursive_mutex mutex_;
PullOverStatus status_;
ThreadSafeData thread_safe_data_;

std::unique_ptr<LastApprovalData> last_approval_data_{nullptr};

Expand Down Expand Up @@ -329,6 +406,7 @@ class GoalPlannerModule : public SceneModuleInterface
bool isStuck();
bool hasDecidedPath() const;
void decideVelocity();
bool foundPullOverPath() const;

// validation
bool hasEnoughDistance(const PullOverPath & pull_over_path) const;
Expand All @@ -352,8 +430,6 @@ class GoalPlannerModule : public SceneModuleInterface
const GoalCandidates & goal_candidates) const;

// deal with pull over partial paths
PathWithLaneId getCurrentPath() const;
bool incrementPathIndex();
void transitionToNextPathIfFinishingCurrentPath();

// lanes and drivable area
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct PullOverPath
{
PullOverPlannerType type{PullOverPlannerType::NONE};
std::vector<PathWithLaneId> partial_paths{};
size_t path_idx{0};
// accelerate with constant acceleration to the target velocity
std::vector<std::pair<double, double>> pairs_terminal_velocity_and_accel{};
Pose start_pose{};
Expand Down Expand Up @@ -84,6 +85,27 @@ struct PullOverPath

return parking_path;
}

PathWithLaneId getCurrentPath() const
{
if (partial_paths.empty()) {
return PathWithLaneId{};
} else if (partial_paths.size() <= path_idx) {
return partial_paths.back();

Check warning on line 94 in planning/behavior_path_planner/include/behavior_path_planner/utils/goal_planner/pull_over_planner_base.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/utils/goal_planner/pull_over_planner_base.hpp#L94

Added line #L94 was not covered by tests
}
return partial_paths.at(path_idx);

Check warning on line 96 in planning/behavior_path_planner/include/behavior_path_planner/utils/goal_planner/pull_over_planner_base.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/utils/goal_planner/pull_over_planner_base.hpp#L96

Added line #L96 was not covered by tests
}

bool incrementPathIndex()
{
if (partial_paths.size() - 1 <= path_idx) {
return false;
}
path_idx += 1;
return true;
}

bool isValidPath() const { return type != PullOverPlannerType::NONE; }

Check warning on line 108 in planning/behavior_path_planner/include/behavior_path_planner/utils/goal_planner/pull_over_planner_base.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/utils/goal_planner/pull_over_planner_base.hpp#L108

Added line #L108 was not covered by tests
};

class PullOverPlannerBase
Expand Down
Loading

0 comments on commit a6369db

Please sign in to comment.