Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
controller_benchmark.cpp
1 // Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <benchmark/benchmark.h>
16 
17 #include <Eigen/Dense>
18 
19 #include <string>
20 
21 #include <geometry_msgs/msg/pose_stamped.hpp>
22 #include <geometry_msgs/msg/twist.hpp>
23 #include <nav_msgs/msg/path.hpp>
24 
25 #include <nav2_costmap_2d/cost_values.hpp>
26 #include <nav2_costmap_2d/costmap_2d.hpp>
27 #include <nav2_costmap_2d/costmap_2d_ros.hpp>
28 #include <nav2_core/goal_checker.hpp>
29 
30 #include "nav2_mppi_controller/motion_models.hpp"
31 #include "nav2_mppi_controller/controller.hpp"
32 
33 #include "utils.hpp"
34 #include "nav2_ros_common/tf2_factories.hpp"
35 
37 {
38 public:
39  RosLockGuard() {rclcpp::init(0, nullptr);}
40  ~RosLockGuard() {rclcpp::shutdown();}
41 };
42 
43 RosLockGuard g_rclcpp;
44 
45 void prepareAndRunBenchmark(
46  bool consider_footprint, std::string motion_model,
47  std::vector<std::string> critics, benchmark::State & state)
48 {
49  bool visualize = false;
50 
51  int batch_size = 300;
52  int time_steps = 12;
53  unsigned int path_points = 50u;
54  int iteration_count = 2;
55  double lookahead_distance = 10.0;
56 
57  TestCostmapSettings costmap_settings{};
58  auto costmap_ros = getDummyCostmapRos(costmap_settings);
59  auto costmap = costmap_ros->getCostmap();
60 
61  TestPose start_pose = costmap_settings.getCenterPose();
62  double path_step = costmap_settings.resolution;
63 
64  TestPathSettings path_settings{start_pose, path_points, path_step, path_step};
65  TestOptimizerSettings optimizer_settings{batch_size, time_steps, iteration_count,
66  lookahead_distance, motion_model, consider_footprint};
67 
68  unsigned int offset = 4;
69  unsigned int obstacle_size = offset * 2;
70 
71  unsigned char obstacle_cost = 250;
72 
73  auto [obst_x, obst_y] = costmap_settings.getCenterIJ();
74 
75  obst_x = obst_x - offset;
76  obst_y = obst_y - offset;
77  addObstacle(costmap, {obst_x, obst_y, obstacle_size, obstacle_cost});
78 
79  printInfo(optimizer_settings, path_settings, critics);
80 
81  rclcpp::NodeOptions options;
82  std::vector<rclcpp::Parameter> params;
83  setUpControllerParams(visualize, params);
84  setUpOptimizerParams(optimizer_settings, critics, params);
85  options.parameter_overrides(params);
86  auto node = getDummyNode(options);
87 
88  auto tf_buffer = nav2::create_transform_buffer(node);
89  tf_buffer->setUsingDedicatedThread(true); // One-thread broadcasting-listening model
90 
91  auto broadcaster = nav2::create_transform_broadcaster(node);
92  auto tf_listener = nav2::create_transform_listener(*tf_buffer, node);
93 
94  auto map_odom_broadcaster = std::async(
95  std::launch::async, sendTf, "map", "odom", broadcaster, node,
96  20);
97 
98  auto odom_base_link_broadcaster = std::async(
99  std::launch::async, sendTf, "odom", "base_link", broadcaster, node,
100  20);
101 
102  auto controller = getDummyController(node, tf_buffer, costmap_ros);
103 
104  // evalControl args
105  auto pose = getDummyPointStamped(node, start_pose);
106  auto velocity = getDummyTwist();
107  auto path = getIncrementalDummyPath(node, path_settings);
108 
109  controller->newPathReceived(path);
110 
111  nav2_core::GoalChecker * dummy_goal_checker{nullptr};
112  nav_msgs::msg::Path transformed_global_plan;
113  geometry_msgs::msg::PoseStamped goal;
114  for (auto _ : state) {
115  controller->computeVelocityCommands(pose, velocity, dummy_goal_checker, transformed_global_plan,
116  goal);
117  }
118  map_odom_broadcaster.wait();
119  odom_base_link_broadcaster.wait();
120 }
121 
122 static void BM_DiffDrivePointFootprint(benchmark::State & state)
123 {
124  bool consider_footprint = true;
125  std::string motion_model = "DiffDrive";
126  std::vector<std::string> critics = {{"GoalCritic"}, {"GoalAngleCritic"}, {"ObstaclesCritic"},
127  {"PathAngleCritic"}, {"PathFollowCritic"}, {"PreferForwardCritic"}};
128 
129  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
130 }
131 
132 static void BM_DiffDrive(benchmark::State & state)
133 {
134  bool consider_footprint = true;
135  std::string motion_model = "DiffDrive";
136  std::vector<std::string> critics = {{"GoalCritic"}, {"GoalAngleCritic"}, {"ObstaclesCritic"},
137  {"PathAngleCritic"}, {"PathFollowCritic"}, {"PreferForwardCritic"}};
138 
139  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
140 }
141 
142 
143 static void BM_Omni(benchmark::State & state)
144 {
145  bool consider_footprint = true;
146  std::string motion_model = "Omni";
147  std::vector<std::string> critics = {{"GoalCritic"}, {"GoalAngleCritic"}, {"ObstaclesCritic"},
148  {"TwirlingCritic"}, {"PathFollowCritic"}, {"PreferForwardCritic"}};
149 
150  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
151 }
152 
153 static void BM_Ackermann(benchmark::State & state)
154 {
155  bool consider_footprint = true;
156  std::string motion_model = "Ackermann";
157  std::vector<std::string> critics = {{"GoalCritic"}, {"GoalAngleCritic"}, {"ObstaclesCritic"},
158  {"PathAngleCritic"}, {"PathFollowCritic"}, {"PreferForwardCritic"}};
159 
160  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
161 }
162 
163 static void BM_GoalCritic(benchmark::State & state)
164 {
165  bool consider_footprint = true;
166  std::string motion_model = "Ackermann";
167  std::vector<std::string> critics = {{"GoalCritic"}};
168 
169  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
170 }
171 
172 static void BM_GoalAngleCritic(benchmark::State & state)
173 {
174  bool consider_footprint = true;
175  std::string motion_model = "Ackermann";
176  std::vector<std::string> critics = {{"GoalAngleCritic"}};
177 
178  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
179 }
180 
181 static void BM_ObstaclesCritic(benchmark::State & state)
182 {
183  bool consider_footprint = true;
184  std::string motion_model = "Ackermann";
185  std::vector<std::string> critics = {{"ObstaclesCritic"}};
186 
187  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
188 }
189 
190 static void BM_ObstaclesCriticPointFootprint(benchmark::State & state)
191 {
192  bool consider_footprint = false;
193  std::string motion_model = "Ackermann";
194  std::vector<std::string> critics = {{"ObstaclesCritic"}};
195 
196  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
197 }
198 
199 static void BM_TwilringCritic(benchmark::State & state)
200 {
201  bool consider_footprint = true;
202  std::string motion_model = "Ackermann";
203  std::vector<std::string> critics = {{"TwirlingCritic"}};
204 
205  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
206 }
207 
208 static void BM_PathFollowCritic(benchmark::State & state)
209 {
210  bool consider_footprint = true;
211  std::string motion_model = "Ackermann";
212  std::vector<std::string> critics = {{"PathFollowCritic"}};
213 
214  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
215 }
216 
217 static void BM_PathAngleCritic(benchmark::State & state)
218 {
219  bool consider_footprint = true;
220  std::string motion_model = "Ackermann";
221  std::vector<std::string> critics = {{"PathAngleCritic"}};
222 
223  prepareAndRunBenchmark(consider_footprint, motion_model, critics, state);
224 }
225 
226 BENCHMARK(BM_DiffDrivePointFootprint)->Unit(benchmark::kMillisecond);
227 BENCHMARK(BM_DiffDrive)->Unit(benchmark::kMillisecond);
228 BENCHMARK(BM_Omni)->Unit(benchmark::kMillisecond);
229 BENCHMARK(BM_Ackermann)->Unit(benchmark::kMillisecond);
230 
231 BENCHMARK(BM_GoalCritic)->Unit(benchmark::kMillisecond);
232 BENCHMARK(BM_GoalAngleCritic)->Unit(benchmark::kMillisecond);
233 BENCHMARK(BM_PathAngleCritic)->Unit(benchmark::kMillisecond);
234 BENCHMARK(BM_PathFollowCritic)->Unit(benchmark::kMillisecond);
235 BENCHMARK(BM_ObstaclesCritic)->Unit(benchmark::kMillisecond);
236 BENCHMARK(BM_ObstaclesCriticPointFootprint)->Unit(benchmark::kMillisecond);
237 BENCHMARK(BM_TwilringCritic)->Unit(benchmark::kMillisecond);
238 
239 BENCHMARK_MAIN();
Function-object for checking whether a goal has been reached.