io.cpp
Go to the documentation of this file.
1#include "io.h"
2
3#include <fstream>
4#include <sstream>
5#include <stdexcept>
6
8
10{
11
13 loadSmoothingParams(const std::string& filePath)
14 {
15 SmoothingParams params;
16
17 const auto fullFilePath = armarx::PackagePath("armarx_navigation", filePath).toSystemPath();
18 std::ifstream file(fullFilePath);
19 if (!file.is_open())
20 {
21 throw std::runtime_error("Could not open config file: " + fullFilePath.string());
22 }
23
24 std::string line;
25 while (std::getline(file, line))
26 {
27 // Strip inline comments
28 auto commentPos = line.find('#');
29 if (commentPos != std::string::npos)
30 line = line.substr(0, commentPos);
31
32 // Skip blank lines (all whitespace)
33 bool allSpace = true;
34 for (char c : line)
35 {
36 if (!std::isspace(static_cast<unsigned char>(c)))
37 {
38 allSpace = false;
39 break;
40 }
41 }
42 if (allSpace)
43 continue;
44
45 std::istringstream iss(line);
46 std::string key;
47 if (!std::getline(iss, key, '='))
48 continue;
49
50 // Trim trailing whitespace from key
51 {
52 size_t end = key.find_last_not_of(" \t\r\n");
53 if (end != std::string::npos)
54 key = key.substr(0, end + 1);
55 else
56 key.clear();
57 }
58 if (key.empty())
59 continue;
60
61 std::string value;
62 if (!std::getline(iss, value))
63 continue;
64
65 // Trim leading/trailing whitespace from value
66 {
67 size_t start = value.find_first_not_of(" \t\r\n");
68 if (start != std::string::npos)
69 {
70 size_t end = value.find_last_not_of(" \t\r\n");
71 value = value.substr(start, end - start + 1);
72 }
73 else
74 {
75 value.clear();
76 }
77 }
78
79 try
80 {
81 // Pre-processing
82 if (key == "clearance")
83 params.clearance = std::stof(value);
84 else if (key == "hillClimbStepSize")
85 params.hill_climb_step_size = std::stof(value);
86 else if (key == "hillClimbMaxIterations")
87 params.hill_climb_max_iterations = std::stoi(value);
88 // Obstacle avoidance
89 else if (key == "weightObstacle")
90 params.w_obs = std::stof(value);
91 else if (key == "obsMaxDistance")
92 params.obs_max_distance = std::stof(value);
93 else if (key == "obsBarrierEpsMm")
94 params.obs_barrier_eps_mm = std::stof(value);
95 else if (key == "useObstacle")
96 params.use_obs = (value == "true");
97 else if (key == "useFuzzyOrientation")
98 params.use_fuzzy_orientation = (value == "true");
99 else if (key == "fuzzyOrientationWindowBins")
100 params.fuzzy_orientation_window_bins = std::stoi(value);
101 else if (key == "fuzzyOrientationAlpha")
102 params.fuzzy_orientation_alpha = std::stof(value);
103 else if (key == "weightThetaTrack")
104 params.w_theta_track = std::stof(value);
105 else if (key == "useObstacleFreeze")
106 params.use_obstacle_freeze = (value == "true");
107 else if (key == "obstacleFreezeThreshold")
108 params.obstacle_freeze_threshold = std::stof(value);
109 // Pose smoothness
110 else if (key == "weightBoundary")
111 params.w_boundary = std::stof(value);
112 else if (key == "weightPoseSmooth")
113 params.w_pose_smooth = std::stof(value);
114 else if (key == "weightOrientationSmooth")
115 params.w_orientation_smooth = std::stof(value);
116 else if (key == "weightPoseJerk")
117 params.w_pose_jerk = std::stof(value);
118 else if (key == "useJerk")
119 params.use_jerk = (value == "true");
120 else if (key == "weightRobotSmooth")
121 params.w_robot_smooth = std::stof(value);
122 else if (key == "useRobotSmooth")
123 params.use_robot_smooth = (value == "true");
124 // Spacing
125 else if (key == "weightSpacing")
126 params.w_spacing = std::stof(value);
127 else if (key == "useSoftSpacing")
128 params.use_soft_spacing = (value == "true");
129 else if (key == "spacingRelativeDeviationThreshold")
130 params.spacing_relative_deviation_threshold = std::stof(value);
131 else if (key == "weightSpacingLinear")
132 params.w_spacing_linear = std::stof(value);
133 else if (key == "weightSpacingQuadratic")
134 params.w_spacing_quadratic = std::stof(value);
135 // Velocity profile
136 else if (key == "startVelocity")
137 params.start_velocity = std::stof(value);
138 else if (key == "weightAccelDecelLimit")
139 params.w_accel_decel_limit = std::stof(value);
140 else if (key == "maxAccelPerSegment")
141 params.max_accel_per_segment = std::stof(value);
142 else if (key == "maxDecelPerSegment")
143 params.max_decel_per_segment = std::stof(value);
144 else if (key == "weightNominalVelocity")
145 params.w_nominal_velocity = std::stof(value);
146 else if (key == "weightVelocityProximity")
147 params.w_velocity_proximity = std::stof(value);
148 else if (key == "slowDownDistance")
149 params.slow_down_distance = std::stof(value);
150 else if (key == "minVelocityNearObstacle")
151 params.min_velocity_near_obstacle = std::stof(value);
152 // Tracking
153 else if (key == "weightTracking")
154 params.w_track = std::stof(value);
155 else if (key == "useTracking")
156 params.use_tracking = (value == "true");
157 else if (key == "useTrackingClearanceAdaptive")
158 params.use_tracking_clearance_adaptive = (value == "true");
159 else if (key == "trackingFadeStartClearance")
160 params.tracking_fade_start_clearance = std::stof(value);
161 else if (key == "trackingFadeEndClearance")
162 params.tracking_fade_end_clearance = std::stof(value);
163 // Robot-pose proximity
164 else if (key == "useRobotPoseProximity")
165 params.use_robot_pose_proximity = (value == "true");
166 else if (key == "robotPoseProximityThresholdMm")
167 params.robot_pose_proximity_threshold_mm = std::stof(value);
168 else if (key == "weightRobotPoseProximity")
169 params.w_robot_pose_proximity = std::stof(value);
170 else if (key == "robotPoseProximityEpsMm")
171 params.robot_pose_proximity_eps_mm = std::stof(value);
172 // Ceres solver
173 else if (key == "maxIterations")
174 params.max_iterations = std::stoi(value);
175 else if (key == "numThreads")
176 params.num_threads = std::stoi(value);
177 else if (key == "initialTrustRegionRadius")
178 params.initial_trust_region_radius = std::stof(value);
179 else if (key == "maxTrustRegionRadius")
180 params.max_trust_region_radius = std::stof(value);
181 else if (key == "minTrustRegionRadius")
182 params.min_trust_region_radius = std::stof(value);
183 // Planner behavior
184 else if (key == "alwaysReturnSmoothed")
185 params.always_return_smoothed = (value == "true");
186 }
187 catch (const std::invalid_argument& e)
188 {
189 throw std::runtime_error("Invalid value for key " + key + ": " + value);
190 }
191 }
192
193 return params;
194 }
195
196} // namespace armarx::navigation::algorithms::orientation_aware::smoothing::io
constexpr T c
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
SmoothingParams loadSmoothingParams(const std::string &filePath)
Definition io.cpp:13