io.cpp
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "io.h"
18
19#include <fstream>
20#include <sstream>
21#include <stdexcept>
22
24
26{
27
29 loadSmoothingParams(const std::string& filePath)
30 {
31 SmoothingParams params;
32
33 const auto fullFilePath = armarx::PackagePath("armarx_navigation", filePath).toSystemPath();
34 std::ifstream file(fullFilePath);
35 if (!file.is_open())
36 {
37 throw std::runtime_error("Could not open config file: " + fullFilePath.string());
38 }
39
40 std::string line;
41 while (std::getline(file, line))
42 {
43 auto commentPos = line.find('#');
44 if (commentPos != std::string::npos)
45 line = line.substr(0, commentPos);
46
47 bool allSpace = true;
48 for (char c : line)
49 {
50 if (!std::isspace(static_cast<unsigned char>(c)))
51 {
52 allSpace = false;
53 break;
54 }
55 }
56 if (allSpace)
57 continue;
58
59 std::istringstream iss(line);
60 std::string key;
61 if (!std::getline(iss, key, '='))
62 continue;
63
64 {
65 size_t end = key.find_last_not_of(" \t\r\n");
66 if (end != std::string::npos)
67 key = key.substr(0, end + 1);
68 else
69 key.clear();
70 }
71 if (key.empty())
72 continue;
73
74 std::string value;
75 if (!std::getline(iss, value))
76 continue;
77
78 {
79 size_t start = value.find_first_not_of(" \t\r\n");
80 if (start != std::string::npos)
81 {
82 size_t end = value.find_last_not_of(" \t\r\n");
83 value = value.substr(start, end - start + 1);
84 }
85 else
86 {
87 value.clear();
88 }
89 }
90
91 try
92 {
93 if (key == "clearance")
94 params.clearance = std::stof(value);
95 else if (key == "minClearance")
96 params.min_clearance = std::stof(value);
97 else if (key == "obsMaxDistance")
98 params.obs_max_distance = std::stof(value);
99 else if (key == "allowDegradedInput")
100 params.allow_degraded_input = (value == "true" or value == "1");
101 else if (key == "useTrackingClearanceAdaptive")
102 params.use_tracking_clearance_adaptive = (value == "true" or value == "1");
103 else if (key == "trackingFadeStartClearance")
104 params.tracking_fade_start_clearance = std::stof(value);
105 else if (key == "trackingFadeEndClearance")
106 params.tracking_fade_end_clearance = std::stof(value);
107 else if (key == "useObstacleFreeze")
108 params.use_obstacle_freeze = (value == "true" or value == "1");
109 else if (key == "obstacleFreezeThreshold")
110 params.obstacle_freeze_threshold = std::stof(value);
111 else if (key == "segmentObstacleSamples")
112 params.segment_obstacle_samples = std::stoi(value);
113 else if (key == "segmentSampleSpacing")
114 params.segment_sample_spacing = std::stof(value);
115 else if (key == "maxSegmentObstacleSamples")
116 params.max_segment_obstacle_samples = std::stoi(value);
117 else if (key == "pass1WeightObstacle")
118 params.pass1_w_obs = std::stof(value);
119 else if (key == "pass1WeightPoseSmooth")
120 params.pass1_w_pose_smooth = std::stof(value);
121 else if (key == "pass1WeightSpacing")
122 params.pass1_w_spacing = std::stof(value);
123 else if (key == "pass1WeightTracking")
124 params.pass1_w_tracking = std::stof(value);
125 else if (key == "pass1WeightSafeTracking")
126 params.pass1_w_safe_tracking = std::stof(value);
127 else if (key == "pass2WeightObstacle")
128 params.pass2_w_obs = std::stof(value);
129 else if (key == "pass2WeightPoseSmooth")
130 params.pass2_w_pose_smooth = std::stof(value);
131 else if (key == "pass2WeightSpacing")
132 params.pass2_w_spacing = std::stof(value);
133 else if (key == "pass2WeightTracking")
134 params.pass2_w_tracking = std::stof(value);
135 else if (key == "pass2WeightSafeTracking")
136 params.pass2_w_safe_tracking = std::stof(value);
137 else if (key == "pass3WeightObstacle")
138 params.pass3_w_obs = std::stof(value);
139 else if (key == "pass3WeightPoseSmooth")
140 params.pass3_w_pose_smooth = std::stof(value);
141 else if (key == "pass3WeightSpacing")
142 params.pass3_w_spacing = std::stof(value);
143 else if (key == "pass3WeightTracking")
144 params.pass3_w_tracking = std::stof(value);
145 else if (key == "pass3WeightSafeTracking")
146 params.pass3_w_safe_tracking = std::stof(value);
147 else if (key == "repairWeightObstacle")
148 params.repair_w_obs = std::stof(value);
149 else if (key == "repairWeightPoseSmooth")
150 params.repair_w_pose_smooth = std::stof(value);
151 else if (key == "repairWeightSpacing")
152 params.repair_w_spacing = std::stof(value);
153 else if (key == "repairWeightTracking")
154 params.repair_w_tracking = std::stof(value);
155 else if (key == "repairWeightSafeTracking")
156 params.repair_w_safe_tracking = std::stof(value);
157 else if (key == "repairMaxIterations")
158 params.repair_max_iterations = std::stoi(value);
159 else if (key == "trackingDeadzone")
160 params.tracking_deadzone = std::stof(value);
161 else if (key == "trackingMaxDeviation")
162 params.tracking_max_deviation = std::stof(value);
163 else if (key == "weightTrackingInBounds")
164 params.w_tracking_in_bounds = std::stof(value);
165 else if (key == "weightTrackingHard")
166 params.w_tracking_hard = std::stof(value);
167 else if (key == "maxIterations")
168 params.max_iterations = std::stoi(value);
169 else if (key == "numThreads")
170 params.num_threads = std::stoi(value);
171 else if (key == "initialTrustRegionRadius")
172 params.initial_trust_region_radius = std::stof(value);
173 else if (key == "maxTrustRegionRadius")
174 params.max_trust_region_radius = std::stof(value);
175 else if (key == "minTrustRegionRadius")
176 params.min_trust_region_radius = std::stof(value);
177 }
178 catch (const std::invalid_argument&)
179 {
180 throw std::runtime_error("Invalid value for key " + key + ": " + value);
181 }
182 }
183
184 return params;
185 }
186
187} // namespace armarx::navigation::algorithms::spfa::smoothing::io
constexpr T c
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
SmoothingParams loadSmoothingParams(const std::string &filePath)
Definition io.cpp:29
double min_clearance
Distance to obstacles [mm] below which a path is actually refused.
Definition io.h:53
double tracking_fade_end_clearance
Clearance [mm] beyond which tracking is off entirely.
Definition io.h:102
bool use_obstacle_freeze
Hold interior waypoints below obstacle_freeze_threshold fixed during the solve.
Definition io.h:110
double clearance
Distance to obstacles [mm] the optimizer aims for.
Definition io.h:32
double tracking_fade_start_clearance
Clearance [mm] at which the leash starts to relax.
Definition io.h:99
bool use_tracking_clearance_adaptive
Scale the tracking weight by the clearance at each waypoint's reference position.
Definition io.h:96
double segment_sample_spacing
Spacing [mm] of the obstacle samples along a segment.
Definition io.h:69
bool allow_degraded_input
Judge a path against what it was given, when the input is already inside the limit.
Definition io.h:86
double obstacle_freeze_threshold
Clearance [mm] at or below which an interior waypoint is pinned.
Definition io.h:122
int max_segment_obstacle_samples
Upper bound, so a long segment cannot make the problem huge.
Definition io.h:128
int segment_obstacle_samples
Lower bound on the samples per segment, so a short segment is still evaluated.
Definition io.h:125