refactor & optimal
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "dock_planner/dock_planner_config.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace dock_planner
|
||||
{
|
||||
@@ -26,6 +26,24 @@ bool readOptionalDouble(const robot::NodeHandle& nh, const std::string& key,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readOptionalString(const robot::NodeHandle& nh, const std::string& key,
|
||||
std::string& value, std::string& error)
|
||||
{
|
||||
if (!nh.hasParam(key))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string parsed;
|
||||
if (!nh.getParam(key, parsed, std::string()))
|
||||
{
|
||||
error = "parameter '" + key + "' must be a string";
|
||||
return false;
|
||||
}
|
||||
value = parsed;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readDockAnchor(const robot::NodeHandle& nh, DockAnchor& anchor, std::string& error)
|
||||
{
|
||||
if (!nh.hasParam("dock_anchor"))
|
||||
@@ -69,20 +87,18 @@ bool DockPlannerConfig::load(const robot::NodeHandle& nh, DockPlannerConfig& con
|
||||
std::string& error)
|
||||
{
|
||||
DockPlannerConfig loaded;
|
||||
if (!readOptionalDouble(nh, "approach/alignment_distance_padding",
|
||||
loaded.alignment_distance_padding, error) ||
|
||||
!readOptionalDouble(nh, "approach/lateral_distance_near",
|
||||
loaded.lateral_distance_near, error) ||
|
||||
!readOptionalDouble(nh, "approach/lateral_distance_far",
|
||||
loaded.lateral_distance_far, error) ||
|
||||
!readOptionalDouble(nh, "approach/angle_threshold_near",
|
||||
loaded.angle_threshold_near, error) ||
|
||||
!readOptionalDouble(nh, "approach/angle_threshold_far",
|
||||
loaded.angle_threshold_far, error) ||
|
||||
!readOptionalDouble(nh, "approach/direction_angle_hysteresis",
|
||||
loaded.direction_angle_hysteresis, error) ||
|
||||
!readOptionalDouble(nh, "approach/direction_distance_tolerance",
|
||||
loaded.direction_distance_tolerance, error) ||
|
||||
if (!readOptionalString(nh, "kinematics/front_axle_frame",
|
||||
loaded.front_axle_frame, error) ||
|
||||
!readOptionalString(nh, "kinematics/rear_axle_frame",
|
||||
loaded.rear_axle_frame, error) ||
|
||||
!readOptionalDouble(nh, "kinematics/max_steering_angle",
|
||||
loaded.max_steering_angle, error) ||
|
||||
!readOptionalDouble(nh, "terminal/reverse_distance_padding",
|
||||
loaded.reverse_distance_padding, error) ||
|
||||
!readOptionalDouble(nh, "maneuver/minimum_forward_distance",
|
||||
loaded.minimum_forward_distance, error) ||
|
||||
!readOptionalDouble(nh, "maneuver/maximum_forward_extension",
|
||||
loaded.maximum_forward_extension, error) ||
|
||||
!readOptionalDouble(nh, "sampling/path_point_spacing",
|
||||
loaded.path_point_spacing, error))
|
||||
{
|
||||
@@ -111,13 +127,11 @@ bool DockPlannerConfig::validate(std::string& error) const
|
||||
const char* name;
|
||||
double value;
|
||||
} values[] = {
|
||||
{"approach/alignment_distance_padding", alignment_distance_padding},
|
||||
{"approach/lateral_distance_near", lateral_distance_near},
|
||||
{"approach/lateral_distance_far", lateral_distance_far},
|
||||
{"approach/angle_threshold_near", angle_threshold_near},
|
||||
{"approach/angle_threshold_far", angle_threshold_far},
|
||||
{"approach/direction_angle_hysteresis", direction_angle_hysteresis},
|
||||
{"approach/direction_distance_tolerance", direction_distance_tolerance},
|
||||
{"kinematics/max_steering_angle", max_steering_angle},
|
||||
{"kinematics/wheel_base", wheel_base},
|
||||
{"terminal/reverse_distance_padding", reverse_distance_padding},
|
||||
{"maneuver/minimum_forward_distance", minimum_forward_distance},
|
||||
{"maneuver/maximum_forward_extension", maximum_forward_extension},
|
||||
{"sampling/path_point_spacing", path_point_spacing},
|
||||
};
|
||||
|
||||
@@ -129,31 +143,39 @@ bool DockPlannerConfig::validate(std::string& error) const
|
||||
}
|
||||
}
|
||||
|
||||
if (alignment_distance_padding < 0.0)
|
||||
if (front_axle_frame.empty() || rear_axle_frame.empty())
|
||||
{
|
||||
error = "parameter 'approach/alignment_distance_padding' must be >= 0 m";
|
||||
error = "kinematics axle frame names must not be empty";
|
||||
return false;
|
||||
}
|
||||
if (lateral_distance_near < 0.0 || lateral_distance_far <= lateral_distance_near)
|
||||
if (front_axle_frame == rear_axle_frame)
|
||||
{
|
||||
error = "approach lateral distances must satisfy 0 <= near < far";
|
||||
error = "kinematics front_axle_frame and rear_axle_frame must be different";
|
||||
return false;
|
||||
}
|
||||
if (angle_threshold_far <= 0.0 || angle_threshold_near >= kPi ||
|
||||
angle_threshold_near <= angle_threshold_far)
|
||||
if (max_steering_angle <= 0.0 || max_steering_angle >= kPi / 2.0)
|
||||
{
|
||||
error = "approach angle thresholds must satisfy 0 < far < near < pi";
|
||||
error = "parameter 'kinematics/max_steering_angle' must be in (0, pi/2) rad";
|
||||
return false;
|
||||
}
|
||||
if (direction_angle_hysteresis < 0.0 ||
|
||||
direction_angle_hysteresis >= angle_threshold_far)
|
||||
if (wheel_base < 0.0)
|
||||
{
|
||||
error = "parameter 'approach/direction_angle_hysteresis' must be >= 0 and smaller than angle_threshold_far";
|
||||
error = "resolved kinematics wheel_base must be >= 0 m";
|
||||
return false;
|
||||
}
|
||||
if (direction_distance_tolerance < 0.0)
|
||||
if (reverse_distance_padding < 0.0)
|
||||
{
|
||||
error = "parameter 'approach/direction_distance_tolerance' must be >= 0 m";
|
||||
error = "parameter 'terminal/reverse_distance_padding' must be >= 0 m";
|
||||
return false;
|
||||
}
|
||||
if (minimum_forward_distance <= 0.0)
|
||||
{
|
||||
error = "parameter 'maneuver/minimum_forward_distance' must be > 0 m";
|
||||
return false;
|
||||
}
|
||||
if (maximum_forward_extension < minimum_forward_distance)
|
||||
{
|
||||
error = "maneuver maximum_forward_extension must be >= minimum_forward_distance";
|
||||
return false;
|
||||
}
|
||||
if (path_point_spacing <= 0.0 || path_point_spacing > 1.0)
|
||||
@@ -166,13 +188,14 @@ bool DockPlannerConfig::validate(std::string& error) const
|
||||
return true;
|
||||
}
|
||||
|
||||
double DockPlannerConfig::angleThreshold(double lateral_distance) const
|
||||
double DockPlannerConfig::minimumTurningRadius() const
|
||||
{
|
||||
const double clamped = std::clamp(lateral_distance, lateral_distance_near,
|
||||
lateral_distance_far);
|
||||
const double ratio = (clamped - lateral_distance_near) /
|
||||
(lateral_distance_far - lateral_distance_near);
|
||||
return angle_threshold_near + ratio * (angle_threshold_far - angle_threshold_near);
|
||||
if (!(wheel_base > 0.0) || !(max_steering_angle > 0.0) ||
|
||||
!(max_steering_angle < kPi / 2.0))
|
||||
{
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
return wheel_base / std::tan(max_steering_angle);
|
||||
}
|
||||
|
||||
} // namespace dock_planner
|
||||
|
||||
Reference in New Issue
Block a user