-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathConfigurationSelectorLeastOverallAngularDisplacement.cpp
58 lines (46 loc) · 1.62 KB
/
ConfigurationSelectorLeastOverallAngularDisplacement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include "ConfigurationSelector.hpp"
#include <cmath> // std::abs
#include <numeric> // std::accumulate
#include <set>
#include <utility> // std::pair
using namespace roboticslab;
bool ConfigurationSelectorLeastOverallAngularDisplacement::findOptimalConfiguration(const KDL::JntArray & qGuess)
{
if (lastValid != INVALID_CONFIG)
{
// either keep last valid configuration or skip looking (out of reach)
return configs[lastValid].isValid();
}
// best for all revolute/prismatic joints
std::set<std::pair<double, int>> displacementPerConfiguration;
for (int i = 0; i < configs.size(); i++)
{
if (configs[i].isValid())
{
const auto diffs = getDiffs(qGuess, configs[i]);
double sum = std::accumulate(diffs.begin(), diffs.end(), 0.0);
displacementPerConfiguration.emplace(sum, i);
}
}
if (displacementPerConfiguration.empty())
{
// no valid configuration found
return false;
}
// std::map keys are sorted, pick std::pair with lowest key (angle sum)
auto it = displacementPerConfiguration.begin();
lastValid = it->second;
return true;
}
std::vector<double> ConfigurationSelectorLeastOverallAngularDisplacement::getDiffs(const KDL::JntArray & qGuess, const Configuration & config)
{
std::vector<double> diffs;
diffs.reserve(qGuess.rows());
for (int i = 0; i < qGuess.rows(); i++)
{
const auto & q = *config.retrievePose();
diffs.push_back(std::abs(qGuess(i) - q(i)));
}
return diffs;
}