15 #include <benchmark/benchmark.h>
17 #include <Eigen/Dense>
22 #include "gtest/gtest.h"
23 #include <geometry_msgs/msg/pose_stamped.hpp>
24 #include <geometry_msgs/msg/twist.hpp>
25 #include <nav_msgs/msg/path.hpp>
27 #include <nav2_costmap_2d/cost_values.hpp>
28 #include <nav2_costmap_2d/costmap_2d.hpp>
29 #include <nav2_costmap_2d/costmap_2d_ros.hpp>
30 #include <nav2_core/goal_checker.hpp>
31 #include "nav2_ros_common/tf2_factories.hpp"
33 #include "nav2_mppi_controller/optimizer.hpp"
34 #include "nav2_mppi_controller/motion_models.hpp"
36 #include "nav2_mppi_controller/tools/parameters_handler.hpp"
49 void prepareAndRunBenchmark(
50 bool consider_footprint, std::string motion_model,
51 std::vector<std::string> critics, benchmark::State & state)
53 int batch_size = 2000;
55 unsigned int path_points = 50u;
56 int iteration_count = 2;
57 double lookahead_distance = 10.0;
59 TestCostmapSettings costmap_settings{};
60 auto costmap_ros = getDummyCostmapRos(costmap_settings);
61 auto costmap = costmap_ros->getCostmap();
63 TestPose start_pose = costmap_settings.getCenterPose();
64 double path_step = costmap_settings.resolution;
66 TestPathSettings path_settings{start_pose, path_points, path_step, path_step};
67 TestOptimizerSettings optimizer_settings{batch_size, time_steps, iteration_count,
68 lookahead_distance, motion_model, consider_footprint};
70 unsigned int offset = 4;
71 unsigned int obstacle_size = offset * 2;
73 unsigned char obstacle_cost = 250;
75 auto [obst_x, obst_y] = costmap_settings.getCenterIJ();
77 obst_x = obst_x - offset;
78 obst_y = obst_y - offset;
79 addObstacle(costmap, {obst_x, obst_y, obstacle_size, obstacle_cost});
81 printInfo(optimizer_settings, path_settings, critics);
82 auto node = getDummyNode(optimizer_settings, critics);
83 std::string name =
"test";
84 auto parameters_handler = std::make_unique<mppi::ParametersHandler>(node, name);
85 auto tf_buffer = nav2::create_transform_buffer(node);
86 auto optimizer = getDummyOptimizer(node, costmap_ros, tf_buffer, parameters_handler.get());
89 auto pose = getDummyPointStamped(node, start_pose);
90 auto velocity = getDummyTwist();
91 auto path = getIncrementalDummyPath(node, path_settings);
94 for (
auto _ : state) {
95 auto [cmd, trajectory] = optimizer->evalControl(
96 pose, velocity, path, path.poses.back().pose,
101 static void BM_DiffDrivePointFootprint(benchmark::State & state)
103 bool consider_footprint =
true;
104 std::string motion_model =
"DiffDrive";
105 std::vector<std::string> critics = {{
"ConstraintCritic"}, {
"CostCritic"}, {
"GoalCritic"},
106 {
"GoalAngleCritic"}, {
"PathAlignCritic"}, {
"PathFollowCritic"}, {
"PathAngleCritic"},
107 {
"PreferForwardCritic"}};
109 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
112 static void BM_DiffDrive(benchmark::State & state)
114 bool consider_footprint =
true;
115 std::string motion_model =
"DiffDrive";
116 std::vector<std::string> critics = {{
"ConstraintCritic"}, {
"CostCritic"}, {
"GoalCritic"},
117 {
"GoalAngleCritic"}, {
"PathAlignCritic"}, {
"PathFollowCritic"}, {
"PathAngleCritic"},
118 {
"PreferForwardCritic"}};
120 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
123 static void BM_Omni(benchmark::State & state)
125 bool consider_footprint =
true;
126 std::string motion_model =
"Omni";
127 std::vector<std::string> critics = {{
"ConstraintCritic"}, {
"CostCritic"}, {
"GoalCritic"},
128 {
"GoalAngleCritic"}, {
"PathAlignCritic"}, {
"PathFollowCritic"}, {
"PathAngleCritic"},
129 {
"PreferForwardCritic"}};
131 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
134 static void BM_Ackermann(benchmark::State & state)
136 bool consider_footprint =
true;
137 std::string motion_model =
"Ackermann";
138 std::vector<std::string> critics = {{
"ConstraintCritic"}, {
"CostCritic"}, {
"GoalCritic"},
139 {
"GoalAngleCritic"}, {
"PathAlignCritic"}, {
"PathFollowCritic"}, {
"PathAngleCritic"},
140 {
"PreferForwardCritic"}};
142 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
145 static void BM_GoalCritic(benchmark::State & state)
147 bool consider_footprint =
true;
148 std::string motion_model =
"Ackermann";
149 std::vector<std::string> critics = {{
"GoalCritic"}};
151 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
154 static void BM_GoalAngleCritic(benchmark::State & state)
156 bool consider_footprint =
true;
157 std::string motion_model =
"Ackermann";
158 std::vector<std::string> critics = {{
"GoalAngleCritic"}};
160 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
163 static void BM_ObstaclesCritic(benchmark::State & state)
165 bool consider_footprint =
true;
166 std::string motion_model =
"Ackermann";
167 std::vector<std::string> critics = {{
"ObstaclesCritic"}};
169 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
172 static void BM_ObstaclesCriticPointFootprint(benchmark::State & state)
174 bool consider_footprint =
false;
175 std::string motion_model =
"Ackermann";
176 std::vector<std::string> critics = {{
"ObstaclesCritic"}};
178 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
181 static void BM_TwilringCritic(benchmark::State & state)
183 bool consider_footprint =
true;
184 std::string motion_model =
"Ackermann";
185 std::vector<std::string> critics = {{
"TwirlingCritic"}};
187 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
190 static void BM_PathFollowCritic(benchmark::State & state)
192 bool consider_footprint =
true;
193 std::string motion_model =
"Ackermann";
194 std::vector<std::string> critics = {{
"PathFollowCritic"}};
196 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
199 static void BM_PathAngleCritic(benchmark::State & state)
201 bool consider_footprint =
true;
202 std::string motion_model =
"Ackermann";
203 std::vector<std::string> critics = {{
"PathAngleCritic"}};
205 prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
208 BENCHMARK(BM_DiffDrivePointFootprint)->Unit(benchmark::kMillisecond);
209 BENCHMARK(BM_DiffDrive)->Unit(benchmark::kMillisecond);
210 BENCHMARK(BM_Omni)->Unit(benchmark::kMillisecond);
211 BENCHMARK(BM_Ackermann)->Unit(benchmark::kMillisecond);
213 BENCHMARK(BM_GoalCritic)->Unit(benchmark::kMillisecond);
214 BENCHMARK(BM_GoalAngleCritic)->Unit(benchmark::kMillisecond);
215 BENCHMARK(BM_PathAngleCritic)->Unit(benchmark::kMillisecond);
216 BENCHMARK(BM_PathFollowCritic)->Unit(benchmark::kMillisecond);
217 BENCHMARK(BM_ObstaclesCritic)->Unit(benchmark::kMillisecond);
218 BENCHMARK(BM_ObstaclesCriticPointFootprint)->Unit(benchmark::kMillisecond);
219 BENCHMARK(BM_TwilringCritic)->Unit(benchmark::kMillisecond);
Function-object for checking whether a goal has been reached.