Skip to content

Commit fd515b7

Browse files
committed
[mppi] color trajectories by cost
Color trajectories by costs by adding a red component which is inversely proportional ti the costs of the visualized trajectories (the lower the cost the more red the trajectory) Signed-off-by: Adi Vardi <[email protected]>
1 parent 1779d92 commit fd515b7

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

nav2_mppi_controller/include/nav2_mppi_controller/models/trajectories.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct Trajectories
2929
Eigen::ArrayXXf x;
3030
Eigen::ArrayXXf y;
3131
Eigen::ArrayXXf yaws;
32+
Eigen::ArrayXf costs;
3233

3334
/**
3435
* @brief Reset state data

nav2_mppi_controller/src/optimizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ void Optimizer::optimize()
226226
generateNoisedTrajectories();
227227
critic_manager_.evalTrajectoriesScores(critics_data_);
228228
updateControlSequence();
229+
generated_trajectories_.costs = costs_;
229230
}
230231
}
231232

nav2_mppi_controller/src/trajectory_visualizer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ void TrajectoryVisualizer::add(
111111
const float shape_1 = static_cast<float>(n_cols);
112112
points_->markers.reserve(floor(n_rows / trajectory_step_) * floor(n_cols * time_step_));
113113

114+
std::vector<std::pair<size_t, float>> sorted_costs;
115+
sorted_costs.reserve(std::ceil(n_rows / trajectory_step_));
116+
117+
for (size_t i = 0; i < n_rows; i += trajectory_step_) {
118+
sorted_costs.emplace_back(i, trajectories.costs(i));
119+
}
120+
std::sort(sorted_costs.begin(), sorted_costs.end(),
121+
[](const auto & a, const auto & b) { return a.second < b.second; });
122+
123+
const float step = 1.0f / static_cast<float>(sorted_costs.size());
124+
float color_component = 1.0f;
125+
std::map<size_t, float> cost_color_map;
126+
for (const auto & pair : sorted_costs) {
127+
cost_color_map[pair.first] = color_component;
128+
color_component -= step;
129+
}
130+
114131
for (size_t i = 0; i < n_rows; i += trajectory_step_) {
115132
for (size_t j = 0; j < n_cols; j += time_step_) {
116133
const float j_flt = static_cast<float>(j);
@@ -119,7 +136,7 @@ void TrajectoryVisualizer::add(
119136

120137
auto pose = utils::createPose(trajectories.x(i, j), trajectories.y(i, j), 0.03);
121138
auto scale = utils::createScale(0.03, 0.03, 0.03);
122-
auto color = utils::createColor(0, green_component, blue_component, 1);
139+
auto color = utils::createColor(cost_color_map[i], green_component, blue_component, 1);
123140
auto marker = utils::createMarker(
124141
marker_id_++, pose, scale, color, frame_id_, marker_namespace);
125142

0 commit comments

Comments
 (0)