Skip to content

Commit 9366a93

Browse files
SteveMacenskienricosutera
authored andcommitted
[MPPI] complete minor optimaization with floating point calculations (ros-navigation#3827)
* floating point calculations * Update optimizer_unit_tests.cpp * Update critics_tests.cpp * Update critics_tests.cpp Signed-off-by: enricosutera <enricosutera@outlook.com>
1 parent 7521cde commit 9366a93

File tree

12 files changed

+62
-59
lines changed

12 files changed

+62
-59
lines changed

nav2_mppi_controller/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ if(COMPILER_SUPPORTS_FMA)
4747
add_compile_options(-mfma)
4848
endif()
4949

50-
add_compile_options(-O3 -finline-limit=1000000 -ffp-contract=fast -ffast-math)
50+
# If building one the same hardware to be deployed on, try `-march=native`!
51+
add_compile_options(-O3 -finline-limit=10000000 -ffp-contract=fast -ffast-math -mtune=generic)
5152

5253
add_library(mppi_controller SHARED
5354
src/controller.cpp

nav2_mppi_controller/include/nav2_mppi_controller/critics/obstacles_critic.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ class ObstaclesCritic : public CriticFunction
7979
* @return double circumscribed cost, any higher than this and need to do full footprint collision checking
8080
* since some element of the robot could be in collision
8181
*/
82-
double findCircumscribedCost(std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap);
82+
float findCircumscribedCost(std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap);
8383

8484
protected:
8585
nav2_costmap_2d::FootprintCollisionChecker<nav2_costmap_2d::Costmap2D *>
8686
collision_checker_{nullptr};
8787

8888
bool consider_footprint_{true};
89-
double collision_cost_{0};
89+
float collision_cost_{0};
9090
float inflation_scale_factor_{0}, inflation_radius_{0};
9191

9292
float possibly_inscribed_cost_;

nav2_mppi_controller/include/nav2_mppi_controller/critics/path_angle_critic.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class PathAngleCritic : public CriticFunction
7272
void score(CriticData & data) override;
7373

7474
protected:
75-
double max_angle_to_furthest_{0};
75+
float max_angle_to_furthest_{0};
7676
float threshold_to_consider_{0};
7777

7878
size_t offset_from_furthest_{0};

nav2_mppi_controller/include/nav2_mppi_controller/models/constraints.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ namespace mppi::models
2424
*/
2525
struct ControlConstraints
2626
{
27-
double vx_max;
28-
double vx_min;
29-
double vy;
30-
double wz;
27+
float vx_max;
28+
float vx_min;
29+
float vy;
30+
float wz;
3131
};
3232

3333
/**
@@ -36,9 +36,9 @@ struct ControlConstraints
3636
*/
3737
struct SamplingStd
3838
{
39-
double vx;
40-
double vy;
41-
double wz;
39+
float vx;
40+
float vy;
41+
float wz;
4242
};
4343

4444
} // namespace mppi::models

nav2_mppi_controller/include/nav2_mppi_controller/tools/path_handler.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class PathHandler
151151
double max_robot_pose_search_dist_{0};
152152
double prune_distance_{0};
153153
double transform_tolerance_{0};
154-
double inversion_xy_tolerance_{0.2};
155-
double inversion_yaw_tolerance{0.4};
154+
float inversion_xy_tolerance_{0.2};
155+
float inversion_yaw_tolerance{0.4};
156156
bool enforce_path_inversion_{false};
157157
unsigned int inversion_locale_{0u};
158158
};

nav2_mppi_controller/include/nav2_mppi_controller/tools/utils.hpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ inline size_t findPathFurthestReachedPoint(const CriticData & data)
309309
const auto dists = dx * dx + dy * dy;
310310

311311
size_t max_id_by_trajectories = 0;
312-
double min_distance_by_path = std::numeric_limits<float>::max();
312+
float min_distance_by_path = std::numeric_limits<float>::max();
313313

314314
for (size_t i = 0; i < dists.shape(0); i++) {
315315
size_t min_id_by_path = 0;
@@ -337,7 +337,7 @@ inline size_t findPathTrajectoryInitialPoint(const CriticData & data)
337337
const auto dy = data.path.y - data.trajectories.y(0, 0);
338338
const auto dists = dx * dx + dy * dy;
339339

340-
double min_distance_by_path = std::numeric_limits<float>::max();
340+
float min_distance_by_path = std::numeric_limits<float>::max();
341341
size_t min_id = 0;
342342
for (size_t j = 0; j < dists.shape(0); j++) {
343343
if (dists(j) < min_distance_by_path) {
@@ -420,23 +420,23 @@ inline void setPathCostsIfNotSet(
420420
* @param forward_preference If reversing direction is valid
421421
* @return Angle between two points
422422
*/
423-
inline double posePointAngle(
423+
inline float posePointAngle(
424424
const geometry_msgs::msg::Pose & pose, double point_x, double point_y, bool forward_preference)
425425
{
426-
double pose_x = pose.position.x;
427-
double pose_y = pose.position.y;
428-
double pose_yaw = tf2::getYaw(pose.orientation);
426+
float pose_x = pose.position.x;
427+
float pose_y = pose.position.y;
428+
float pose_yaw = tf2::getYaw(pose.orientation);
429429

430-
double yaw = atan2(point_y - pose_y, point_x - pose_x);
430+
float yaw = atan2f(point_y - pose_y, point_x - pose_x);
431431

432432
// If no preference for forward, return smallest angle either in heading or 180 of heading
433433
if (!forward_preference) {
434434
return std::min(
435-
abs(angles::shortest_angular_distance(yaw, pose_yaw)),
436-
abs(angles::shortest_angular_distance(yaw, angles::normalize_angle(pose_yaw + M_PI))));
435+
fabs(angles::shortest_angular_distance(yaw, pose_yaw)),
436+
fabs(angles::shortest_angular_distance(yaw, angles::normalize_angle(pose_yaw + M_PI))));
437437
}
438438

439-
return abs(angles::shortest_angular_distance(yaw, pose_yaw));
439+
return fabs(angles::shortest_angular_distance(yaw, pose_yaw));
440440
}
441441

442442
/**
@@ -447,21 +447,21 @@ inline double posePointAngle(
447447
* @param point_yaw Yaw of the point to consider along Z axis
448448
* @return Angle between two points
449449
*/
450-
inline double posePointAngle(
450+
inline float posePointAngle(
451451
const geometry_msgs::msg::Pose & pose,
452452
double point_x, double point_y, double point_yaw)
453453
{
454-
double pose_x = pose.position.x;
455-
double pose_y = pose.position.y;
456-
double pose_yaw = tf2::getYaw(pose.orientation);
454+
float pose_x = pose.position.x;
455+
float pose_y = pose.position.y;
456+
float pose_yaw = tf2::getYaw(pose.orientation);
457457

458-
double yaw = atan2(point_y - pose_y, point_x - pose_x);
458+
float yaw = atan2f(point_y - pose_y, point_x - pose_x);
459459

460-
if (abs(angles::shortest_angular_distance(yaw, point_yaw)) > M_PI_2) {
460+
if (fabs(angles::shortest_angular_distance(yaw, point_yaw)) > M_PI_2) {
461461
yaw = angles::normalize_angle(yaw + M_PI);
462462
}
463463

464-
return abs(angles::shortest_angular_distance(yaw, pose_yaw));
464+
return fabs(angles::shortest_angular_distance(yaw, pose_yaw));
465465
}
466466

467467
/**
@@ -650,17 +650,17 @@ inline unsigned int findFirstPathInversion(nav_msgs::msg::Path & path)
650650
// Iterating through the path to determine the position of the path inversion
651651
for (unsigned int idx = 1; idx < path.poses.size() - 1; ++idx) {
652652
// We have two vectors for the dot product OA and AB. Determining the vectors.
653-
double oa_x = path.poses[idx].pose.position.x -
653+
float oa_x = path.poses[idx].pose.position.x -
654654
path.poses[idx - 1].pose.position.x;
655-
double oa_y = path.poses[idx].pose.position.y -
655+
float oa_y = path.poses[idx].pose.position.y -
656656
path.poses[idx - 1].pose.position.y;
657-
double ab_x = path.poses[idx + 1].pose.position.x -
657+
float ab_x = path.poses[idx + 1].pose.position.x -
658658
path.poses[idx].pose.position.x;
659-
double ab_y = path.poses[idx + 1].pose.position.y -
659+
float ab_y = path.poses[idx + 1].pose.position.y -
660660
path.poses[idx].pose.position.y;
661661

662662
// Checking for the existance of cusp, in the path, using the dot product.
663-
double dot_product = (oa_x * ab_x) + (oa_y * ab_y);
663+
float dot_product = (oa_x * ab_x) + (oa_y * ab_y);
664664
if (dot_product < 0.0) {
665665
return idx + 1;
666666
}

nav2_mppi_controller/src/critics/obstacles_critic.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void ObstaclesCritic::initialize()
3232
collision_checker_.setCostmap(costmap_);
3333
possibly_inscribed_cost_ = findCircumscribedCost(costmap_ros_);
3434

35-
if (possibly_inscribed_cost_ < 1) {
35+
if (possibly_inscribed_cost_ < 1.0f) {
3636
RCLCPP_ERROR(
3737
logger_,
3838
"Inflation layer either not found or inflation is not set sufficiently for "
@@ -50,7 +50,7 @@ void ObstaclesCritic::initialize()
5050
"footprint" : "circular");
5151
}
5252

53-
double ObstaclesCritic::findCircumscribedCost(
53+
float ObstaclesCritic::findCircumscribedCost(
5454
std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap)
5555
{
5656
double result = -1.0;
@@ -75,15 +75,15 @@ double ObstaclesCritic::findCircumscribedCost(
7575

7676
if (!inflation_layer_found) {
7777
RCLCPP_WARN(
78-
rclcpp::get_logger("computeCircumscribedCost"),
78+
logger_,
7979
"No inflation layer found in costmap configuration. "
8080
"If this is an SE2-collision checking plugin, it cannot use costmap potential "
8181
"field to speed up collision checking by only checking the full footprint "
8282
"when robot is within possibly-inscribed radius of an obstacle. This may "
8383
"significantly slow down planning times and not avoid anything but absolute collisions!");
8484
}
8585

86-
return result;
86+
return static_cast<float>(result);
8787
}
8888

8989
float ObstaclesCritic::distanceToObstacle(const CollisionCost & cost)
@@ -137,7 +137,7 @@ void ObstaclesCritic::score(CriticData & data)
137137
}
138138

139139
// Cannot process repulsion if inflation layer does not exist
140-
if (inflation_radius_ == 0 || inflation_scale_factor_ == 0) {
140+
if (inflation_radius_ == 0.0f || inflation_scale_factor_ == 0.0f) {
141141
continue;
142142
}
143143

@@ -197,7 +197,9 @@ CollisionCost ObstaclesCritic::costAtPose(float x, float y, float theta)
197197
}
198198
cost = collision_checker_.pointCost(x_i, y_i);
199199

200-
if (consider_footprint_ && (cost >= possibly_inscribed_cost_ || possibly_inscribed_cost_ < 1)) {
200+
if (consider_footprint_ &&
201+
(cost >= possibly_inscribed_cost_ || possibly_inscribed_cost_ < 1.0f))
202+
{
201203
cost = static_cast<float>(collision_checker_.footprintCostAtPose(
202204
x, y, theta, costmap_ros_->getRobotFootprint()));
203205
collision_cost.using_footprint = true;

nav2_mppi_controller/src/critics/path_align_critic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void PathAlignCritic::score(CriticData & data)
8989
}
9090

9191
float dist_sq = 0.0f, dx = 0.0f, dy = 0.0f, dyaw = 0.0f, summed_dist = 0.0f;
92-
double min_dist_sq = std::numeric_limits<float>::max();
92+
float min_dist_sq = std::numeric_limits<float>::max();
9393
size_t min_s = 0;
9494

9595
for (size_t t = 0; t < batch_size; ++t) {

nav2_mppi_controller/src/noise_generator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ void NoiseGenerator::generateNoisedControls()
9494
auto & s = settings_;
9595

9696
xt::noalias(noises_vx_) = xt::random::randn<float>(
97-
{s.batch_size, s.time_steps}, 0.0,
97+
{s.batch_size, s.time_steps}, 0.0f,
9898
s.sampling_std.vx);
9999
xt::noalias(noises_wz_) = xt::random::randn<float>(
100-
{s.batch_size, s.time_steps}, 0.0,
100+
{s.batch_size, s.time_steps}, 0.0f,
101101
s.sampling_std.wz);
102102
if (is_holonomic_) {
103103
xt::noalias(noises_vy_) = xt::random::randn<float>(
104-
{s.batch_size, s.time_steps}, 0.0,
104+
{s.batch_size, s.time_steps}, 0.0f,
105105
s.sampling_std.vy);
106106
}
107107
}

nav2_mppi_controller/src/path_handler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ bool PathHandler::transformPose(
166166
double PathHandler::getMaxCostmapDist()
167167
{
168168
const auto & costmap = costmap_->getCostmap();
169-
return std::max(costmap->getSizeInCellsX(), costmap->getSizeInCellsY()) *
170-
costmap->getResolution() / 2.0;
169+
return static_cast<double>(std::max(costmap->getSizeInCellsX(), costmap->getSizeInCellsY())) *
170+
costmap->getResolution() * 0.50;
171171
}
172172

173173
void PathHandler::setPath(const nav_msgs::msg::Path & plan)
@@ -190,11 +190,11 @@ bool PathHandler::isWithinInversionTolerances(const geometry_msgs::msg::PoseStam
190190
{
191191
// Keep full path if we are within tolerance of the inversion pose
192192
const auto last_pose = global_plan_up_to_inversion_.poses.back();
193-
double distance = std::hypot(
193+
float distance = hypotf(
194194
robot_pose.pose.position.x - last_pose.pose.position.x,
195195
robot_pose.pose.position.y - last_pose.pose.position.y);
196196

197-
double angle_distance = angles::shortest_angular_distance(
197+
float angle_distance = angles::shortest_angular_distance(
198198
tf2::getYaw(robot_pose.pose.orientation),
199199
tf2::getYaw(last_pose.pose.orientation));
200200

0 commit comments

Comments
 (0)