| Line |
Branch |
Exec |
Source |
| 1 |
|
|
// Copyright (c) 2018, Joseph Mirabel |
| 2 |
|
|
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) |
| 3 |
|
|
// |
| 4 |
|
|
|
| 5 |
|
|
// Redistribution and use in source and binary forms, with or without |
| 6 |
|
|
// modification, are permitted provided that the following conditions are |
| 7 |
|
|
// met: |
| 8 |
|
|
// |
| 9 |
|
|
// 1. Redistributions of source code must retain the above copyright |
| 10 |
|
|
// notice, this list of conditions and the following disclaimer. |
| 11 |
|
|
// |
| 12 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
|
|
// notice, this list of conditions and the following disclaimer in the |
| 14 |
|
|
// documentation and/or other materials provided with the distribution. |
| 15 |
|
|
// |
| 16 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 |
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 |
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 |
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 |
|
|
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 |
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 |
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 27 |
|
|
// DAMAGE. |
| 28 |
|
|
|
| 29 |
|
|
#include <hpp/core/path-vector.hh> |
| 30 |
|
|
#include <hpp/manipulation/constraint-set.hh> |
| 31 |
|
|
#include <hpp/manipulation/graph/edge.hh> |
| 32 |
|
|
#include <hpp/manipulation/graph/graph.hh> |
| 33 |
|
|
#include <hpp/manipulation/graph/state.hh> |
| 34 |
|
|
#include <hpp/manipulation/path-optimization/enforce-transition-semantic.hh> |
| 35 |
|
|
#include <hpp/pinocchio/util.hh> |
| 36 |
|
|
|
| 37 |
|
|
namespace hpp { |
| 38 |
|
|
namespace manipulation { |
| 39 |
|
|
namespace pathOptimization { |
| 40 |
|
|
using graph::Edges_t; |
| 41 |
|
|
using graph::StatePtr_t; |
| 42 |
|
|
using hpp::core::Path; |
| 43 |
|
|
using hpp::core::PathPtr_t; |
| 44 |
|
|
using hpp::core::PathVector; |
| 45 |
|
|
using hpp::core::PathVectorPtr_t; |
| 46 |
|
|
|
| 47 |
|
✗ |
Edges_t getAllEdges(const StatePtr_t& from, const StatePtr_t& to) { |
| 48 |
|
✗ |
Edges_t edges; |
| 49 |
|
✗ |
for (graph::Neighbors_t::const_iterator it = from->neighbors().begin(); |
| 50 |
|
✗ |
it != from->neighbors().end(); ++it) { |
| 51 |
|
✗ |
if (it->second->stateTo() == to) edges.push_back(it->second); |
| 52 |
|
|
} |
| 53 |
|
✗ |
for (Edges_t::const_iterator it = from->hiddenNeighbors().begin(); |
| 54 |
|
✗ |
it != from->hiddenNeighbors().end(); ++it) { |
| 55 |
|
✗ |
if ((*it)->stateTo() == to) edges.push_back(*it); |
| 56 |
|
|
} |
| 57 |
|
✗ |
return edges; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
✗ |
PathVectorPtr_t EnforceTransitionSemantic::optimize( |
| 61 |
|
|
const PathVectorPtr_t& path) { |
| 62 |
|
|
PathVectorPtr_t input = |
| 63 |
|
✗ |
PathVector::create(path->outputSize(), path->outputDerivativeSize()); |
| 64 |
|
|
PathVectorPtr_t output = |
| 65 |
|
✗ |
PathVector::create(path->outputSize(), path->outputDerivativeSize()); |
| 66 |
|
✗ |
path->flatten(input); |
| 67 |
|
|
|
| 68 |
|
✗ |
ConstraintSetPtr_t c; |
| 69 |
|
✗ |
for (std::size_t i = 0; i < input->numberPaths(); ++i) { |
| 70 |
|
✗ |
PathPtr_t current = input->pathAtRank(i); |
| 71 |
|
✗ |
output->appendPath(current); |
| 72 |
|
✗ |
c = HPP_DYNAMIC_PTR_CAST(ConstraintSet, current->constraints()); |
| 73 |
|
✗ |
if (!c) { |
| 74 |
|
|
hppDout(info, "No manipulation::ConstraintSet"); |
| 75 |
|
✗ |
break; |
| 76 |
|
|
} |
| 77 |
|
✗ |
Configuration_t q0 = current->initial(); |
| 78 |
|
✗ |
Configuration_t q1 = current->end(); |
| 79 |
|
✗ |
StatePtr_t src = c->edge()->stateFrom(); |
| 80 |
|
✗ |
StatePtr_t dst = c->edge()->stateTo(); |
| 81 |
|
✗ |
if (src == dst) continue; |
| 82 |
|
|
|
| 83 |
|
✗ |
bool q0_in_src = src->contains(q0); |
| 84 |
|
✗ |
bool q1_in_src = src->contains(q1); |
| 85 |
|
✗ |
bool q0_in_dst = dst->contains(q0); |
| 86 |
|
✗ |
bool q1_in_dst = dst->contains(q1); |
| 87 |
|
|
|
| 88 |
|
✗ |
if (q0_in_src && q1_in_dst) // Nominal case |
| 89 |
|
✗ |
continue; |
| 90 |
|
|
hppDout(warning, "Transition " |
| 91 |
|
|
<< i |
| 92 |
|
|
<< ". " |
| 93 |
|
|
"\nsrc=" |
| 94 |
|
|
<< src->name() << "\ndst=" << dst->name() |
| 95 |
|
|
<< "\nq0_in_src=" << q0_in_src << "\nq1_in_src=" |
| 96 |
|
|
<< q1_in_src << "\nq0_in_dst=" << q0_in_dst |
| 97 |
|
|
<< "\nq1_in_dst=" << q1_in_dst << setpyformat |
| 98 |
|
|
<< "\nq0=" << one_line(q0) << "\nq1=" << one_line(q1) |
| 99 |
|
|
<< unsetpyformat << "\nTrying with state."); |
| 100 |
|
|
|
| 101 |
|
✗ |
StatePtr_t from, to; |
| 102 |
|
✗ |
if (q0_in_dst && q1_in_src) { // Reversed from nominal case |
| 103 |
|
✗ |
from = dst; |
| 104 |
|
✗ |
to = src; |
| 105 |
|
✗ |
} else if (q0_in_dst && q1_in_dst) { |
| 106 |
|
✗ |
from = dst; |
| 107 |
|
✗ |
to = dst; |
| 108 |
|
✗ |
} else if (q0_in_src && q1_in_src) { |
| 109 |
|
✗ |
from = src; |
| 110 |
|
✗ |
to = src; |
| 111 |
|
✗ |
} else if (q0_in_src) { // Keep same transition |
| 112 |
|
✗ |
continue; |
| 113 |
|
✗ |
} else if (q0_in_dst) { // Reverse current transition |
| 114 |
|
✗ |
from = dst; |
| 115 |
|
✗ |
to = src; |
| 116 |
|
✗ |
} else if (q1_in_src) { // Keep same transition |
| 117 |
|
✗ |
continue; |
| 118 |
|
✗ |
} else if (q1_in_dst) { // Reverse current transition |
| 119 |
|
✗ |
from = dst; |
| 120 |
|
✗ |
to = src; |
| 121 |
|
|
} |
| 122 |
|
✗ |
if (from && to) { |
| 123 |
|
|
// Check that a path from dst to to exists. |
| 124 |
|
✗ |
Edges_t transitions = getAllEdges(from, to); |
| 125 |
|
✗ |
if (transitions.size() >= 1) { |
| 126 |
|
✗ |
if (transitions.size() > 1) { |
| 127 |
|
|
hppDout(info, "More than one transition..."); |
| 128 |
|
|
} |
| 129 |
|
✗ |
c->edge(transitions[0]); |
| 130 |
|
✗ |
continue; |
| 131 |
|
|
} |
| 132 |
|
|
} |
| 133 |
|
|
hppDout(warning, "Enable to find a suitable transition for " |
| 134 |
|
|
<< i |
| 135 |
|
|
<< ". " |
| 136 |
|
|
"\nsrc=" |
| 137 |
|
|
<< src->name() << "\ndst=" << dst->name() |
| 138 |
|
|
<< "\nq0_in_src=" << q0_in_src << "\nq1_in_src=" |
| 139 |
|
|
<< q1_in_src << "\nq0_in_dst=" << q0_in_dst |
| 140 |
|
|
<< "\nq1_in_dst=" << q1_in_dst << setpyformat |
| 141 |
|
|
<< "\nq0=" << one_line(q0) << "\nq1=" << one_line(q1) |
| 142 |
|
|
<< unsetpyformat << "\nTrying with state."); |
| 143 |
|
|
|
| 144 |
|
✗ |
StatePtr_t state = c->edge()->state(); |
| 145 |
|
|
// Check that a path from dst to to exists. |
| 146 |
|
✗ |
Edges_t transitions = getAllEdges(state, state); |
| 147 |
|
✗ |
if (transitions.size() >= 1) { |
| 148 |
|
✗ |
if (transitions.size() > 1) { |
| 149 |
|
|
hppDout(info, "More than one transition..."); |
| 150 |
|
|
} |
| 151 |
|
✗ |
c->edge(transitions[0]); |
| 152 |
|
✗ |
continue; |
| 153 |
|
|
} else { |
| 154 |
|
|
hppDout(error, "Enable to find a suitable transition for " << *current); |
| 155 |
|
|
} |
| 156 |
|
|
} |
| 157 |
|
✗ |
return output; |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
} // namespace pathOptimization |
| 161 |
|
|
} // namespace manipulation |
| 162 |
|
|
} // namespace hpp |
| 163 |
|
|
|