Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions trajectory_optimization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ target_link_libraries(${TARGET_NAME}
rclcpp_components_register_node(${TARGET_NAME}
PLUGIN "trajectory_optimization::TrajectoryOptimizationAckermannNode"
EXECUTABLE trajectory_optimization_ackermann_node
EXECUTOR MultiThreadedExecutor
)

rclcpp_components_register_node(${TARGET_NAME}
PLUGIN "trajectory_optimization::TrajectoryOptimizationRWSNode"
EXECUTABLE trajectory_optimization_rws_node
EXECUTOR MultiThreadedExecutor
)

target_include_directories(${TARGET_NAME} PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion trajectory_optimization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ flowchart LR
| `verbose` | `bool` | `false` | Print solver statistics |
| `performance_logging` | `bool` | `false` | Write one CSV record for every completed solver run |
| `debug_visualization` | `bool` | `false` | Publish debug visualization markers for ego, objects and boundaries |
| `run_as_callback` | `bool` | `false` | Run OCP once for each received reference trajectory (true) or on a timer (false) |
| `run_as_callback` | `bool` | `false` | Run OCP on reference updates, coalescing while busy (true), or on a timer (false) |
| `cost_weights` | `float[]` | `std::vector<double>(12, 1.0)` | Cost function weights |
| `dynamic_weight` | `float` | `1.0` | Dynamic weight alpha |
| `thw` | `float` | `2.0` | Desired time headway to front vehicle [s]; soft when object slacks are enabled |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <tracetools/tracetools.h>
#include <Eigen/Dense>
#include <mutex>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/int32.hpp>
#include <vector>
Expand Down Expand Up @@ -126,6 +127,26 @@ class TrajectoryOptimizationNode : public rclcpp::Node {
*/
void setup();

/**
* @brief Copies the latest buffered inputs into solver-owned planning-cycle state.
*
* Must run in the mutually exclusive solver callback group. Selects message pointers under the input mutex,
* then copies messages outside the lock so input callbacks can continue. Disabled object and boundary inputs
* are reset to empty. Inputs are selected together without synchronizing their timestamps and remain fixed
* for the active planning cycle.
*/
void takeInputSnapshot();

/**
* @brief Consumes a pending reference update and starts a cycle if reference-triggered mode is enabled.
*
* Runs in the mutually exclusive solver callback group. Cancels the one-shot timer and clears the pending
* flag under the input mutex so concurrent reference callbacks cannot lose a wakeup. Multiple updates coalesce
* into one request; updates received during planning can queue another cycle. Timer-driven mode consumes the
* request without starting an additional cycle.
*/
void processReferenceCycle();

/**
* @brief Resets the optimizer memory while retaining the generated solver instance.
*/
Expand Down Expand Up @@ -356,6 +377,18 @@ class TrajectoryOptimizationNode : public rclcpp::Node {

OnSetParametersCallbackHandle::SharedPtr parameters_callback_;

rclcpp::CallbackGroup::SharedPtr input_callback_group_;

struct BufferedInputs {
perception_msgs::msg::EgoData::ConstSharedPtr ego_data;
perception_msgs::msg::ObjectList::ConstSharedPtr object_list;
route_planning_msgs::msg::Route::ConstSharedPtr route;
trajectory_planning_msgs::msg::Trajectory::ConstSharedPtr reference_trajectory;
};
std::mutex input_mutex_;
BufferedInputs buffered_inputs_;
bool reference_cycle_pending_ = false;

rclcpp::Subscription<perception_msgs::msg::EgoData>::SharedPtr ego_data_sub_;
rclcpp::Subscription<perception_msgs::msg::ObjectList>::SharedPtr object_list_sub_;
rclcpp::Subscription<route_planning_msgs::msg::Route>::SharedPtr route_sub_;
Expand All @@ -367,6 +400,7 @@ class TrajectoryOptimizationNode : public rclcpp::Node {
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr boundary_pub_;

rclcpp::TimerBase::SharedPtr planning_timer_;
rclcpp::TimerBase::SharedPtr reference_cycle_timer_;

std::unique_ptr<tf2_ros::Buffer> tf2_buffer_;
std::shared_ptr<tf2_ros::TransformListener> tf2_listener_;
Expand Down
61 changes: 43 additions & 18 deletions trajectory_optimization/src/callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,60 @@
namespace trajectory_optimization {
void TrajectoryOptimizationNode::egoDataCallback(const perception_msgs::msg::EgoData::ConstSharedPtr msg) {
RCLCPP_DEBUG(this->get_logger(), "Received ego data");
ego_data_ = *msg;
std::lock_guard<std::mutex> lock(input_mutex_);
buffered_inputs_.ego_data = msg;
}

void TrajectoryOptimizationNode::objectListCallback(const perception_msgs::msg::ObjectList::ConstSharedPtr msg) {
if (consider_objects_ != CONSIDER_OBJECTS::NO_OBJECTS) {
RCLCPP_DEBUG(this->get_logger(), "Received object list");
object_list_ = *msg;
} else {
// reset object list to empty
object_list_ = perception_msgs::msg::ObjectList();
}
RCLCPP_DEBUG(this->get_logger(), "Received object list");
std::lock_guard<std::mutex> lock(input_mutex_);
buffered_inputs_.object_list = msg;
}

void TrajectoryOptimizationNode::referenceTrajectoryCallback(
const trajectory_planning_msgs::msg::Trajectory::ConstSharedPtr msg) {
RCLCPP_DEBUG(this->get_logger(), "Received reference trajectory");
reference_trajectory_ = *msg;
if (run_as_callback_) {
planningCycle();
}
std::lock_guard<std::mutex> lock(input_mutex_);
buffered_inputs_.reference_trajectory = msg;
reference_cycle_pending_ = true;
reference_cycle_timer_->reset();
}

void TrajectoryOptimizationNode::routeCallback(const route_planning_msgs::msg::Route::ConstSharedPtr msg) {
if (consider_boundaries_ != CONSIDER_BOUNDARIES::NO_BOUNDS) {
RCLCPP_DEBUG(this->get_logger(), "Received route");
route_ = *msg;
} else {
// reset route to empty
route_ = route_planning_msgs::msg::Route();
RCLCPP_DEBUG(this->get_logger(), "Received route");
std::lock_guard<std::mutex> lock(input_mutex_);
buffered_inputs_.route = msg;
}

void TrajectoryOptimizationNode::takeInputSnapshot() {
BufferedInputs inputs;
{
std::lock_guard<std::mutex> lock(input_mutex_);
inputs = buffered_inputs_;
}

// only the solver callback group reads or changes active cycle inputs and settings.
if (inputs.ego_data) {
ego_data_ = *inputs.ego_data;
}
// Reset the object list to empty when objects are disabled or no input has arrived.
object_list_ = inputs.object_list && consider_objects_ != CONSIDER_OBJECTS::NO_OBJECTS ? *inputs.object_list
: perception_msgs::msg::ObjectList();
// Reset the route to empty when boundaries are disabled or no input has arrived.
route_ =
inputs.route && consider_boundaries_ != CONSIDER_BOUNDARIES::NO_BOUNDS ? *inputs.route : route_planning_msgs::msg::Route();
if (inputs.reference_trajectory) reference_trajectory_ = *inputs.reference_trajectory;
}

void TrajectoryOptimizationNode::processReferenceCycle() {
bool pending = false;
{
std::lock_guard<std::mutex> lock(input_mutex_);
// serialize cancellation and input-side resets so a wakeup cannot be lost.
reference_cycle_timer_->cancel();
pending = reference_cycle_pending_;
reference_cycle_pending_ = false;
}
if (pending && run_as_callback_) planningCycle();
}
} // namespace trajectory_optimization
20 changes: 15 additions & 5 deletions trajectory_optimization/src/trajectory_optimization_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TrajectoryOptimizationNode::TrajectoryOptimizationNode(const std::string node_na
this->declareAndLoadParameter("debug_visualization", debug_viz_,
"Publish debug visualization markers for ego, objects and boundaries");
this->declareAndLoadParameter("run_as_callback", run_as_callback_,
"Run OCP once for each received reference trajectory (true) or on a timer (false)");
"Run OCP on reference updates, coalescing while busy (true), or on a timer (false)");
this->declareAndLoadParameter("cost_weights", cost_weights_, "Cost function weights");
this->declareAndLoadParameter("dynamic_weight", dynamic_weight_, "Dynamic weight alpha");
this->declareAndLoadParameter("thw", thw_, "Desired time headway to front vehicle [s]; soft when object slacks are enabled");
Expand Down Expand Up @@ -215,22 +215,31 @@ void TrajectoryOptimizationNode::setup() {
parameters_callback_ = this->add_on_set_parameters_callback(
std::bind(&TrajectoryOptimizationNode::parametersCallback, this, std::placeholders::_1));

// keep solver timers and parameter callbacks in the mutually exclusive default group.
input_callback_group_ = create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
rclcpp::SubscriptionOptions input_options;
input_options.callback_group = input_callback_group_;
// this cancelled timer acts as a one-shot wakeup for reference-triggered solves.
reference_cycle_timer_ =
create_wall_timer(std::chrono::nanoseconds(1), std::bind(&TrajectoryOptimizationNode::processReferenceCycle, this));
reference_cycle_timer_->cancel();

// set up subscriber for input topics
ego_data_sub_ = this->create_subscription<perception_msgs::msg::EgoData>(
"~/ego_data", 1, std::bind(&TrajectoryOptimizationNode::egoDataCallback, this, std::placeholders::_1));
"~/ego_data", 1, std::bind(&TrajectoryOptimizationNode::egoDataCallback, this, std::placeholders::_1), input_options);
RCLCPP_INFO(this->get_logger(), "Subscribed to '%s'", ego_data_sub_->get_topic_name());

object_list_sub_ = this->create_subscription<perception_msgs::msg::ObjectList>(
"~/object_list", 1, std::bind(&TrajectoryOptimizationNode::objectListCallback, this, std::placeholders::_1));
"~/object_list", 1, std::bind(&TrajectoryOptimizationNode::objectListCallback, this, std::placeholders::_1), input_options);
RCLCPP_INFO(this->get_logger(), "Subscribed to '%s'", object_list_sub_->get_topic_name());

route_sub_ = this->create_subscription<route_planning_msgs::msg::Route>(
"~/route", 1, std::bind(&TrajectoryOptimizationNode::routeCallback, this, std::placeholders::_1));
"~/route", 1, std::bind(&TrajectoryOptimizationNode::routeCallback, this, std::placeholders::_1), input_options);
RCLCPP_INFO(this->get_logger(), "Subscribed to '%s'", route_sub_->get_topic_name());

reference_trajectory_sub_ = this->create_subscription<trajectory_planning_msgs::msg::Trajectory>(
"~/reference_trajectory", 1,
std::bind(&TrajectoryOptimizationNode::referenceTrajectoryCallback, this, std::placeholders::_1));
std::bind(&TrajectoryOptimizationNode::referenceTrajectoryCallback, this, std::placeholders::_1), input_options);
RCLCPP_INFO(this->get_logger(), "Subscribed to '%s'", reference_trajectory_sub_->get_topic_name());

// set up publisher for output topics
Expand Down Expand Up @@ -401,6 +410,7 @@ bool TrajectoryOptimizationNode::setInitialGuess(const std::vector<double>& x_in
}

void TrajectoryOptimizationNode::planningCycle() {
takeInputSnapshot();
const auto cycle_start = SteadyClock::now();
if (debug_viz_) viz_object_boxes_.clear();
if (rclcpp::Time(this->now()) - rclcpp::Time(ego_data_.header.stamp) > rclcpp::Duration::from_seconds(ego_data_timeout_)) {
Expand Down
Loading