|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// BSD 3-Clause License |
| 3 | +// |
| 4 | +// Copyright (C) 2025, INRIA |
| 5 | +// Copyright note valid unless otherwise stated in individual files. |
| 6 | +// All rights reserved. |
| 7 | +/////////////////////////////////////////////////////////////////////////////// |
| 8 | +/** |
| 9 | + * @file interpolator.hxx |
| 10 | + * @brief Interpolation class for practical control of the robot |
| 11 | + */ |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +#include <pinocchio/algorithm/joint-configuration.hpp> |
| 16 | + |
| 17 | +#include "simple-mpc/fwd.hpp" |
| 18 | +#include "simple-mpc/model-utils.hpp" |
| 19 | + |
| 20 | +namespace simple_mpc |
| 21 | +{ |
| 22 | + class Interpolator |
| 23 | + { |
| 24 | + public: |
| 25 | + explicit Interpolator(const Model & model) |
| 26 | + : model_(model) {}; |
| 27 | + |
| 28 | + template<typename T> |
| 29 | + void interpolateConfiguration(const double delay, const double timestep, const std::vector<T> & qs, T & q_interp) |
| 30 | + { |
| 31 | + assert(("Configuration is not of the right size", qs[0].size() == model_.nq)); |
| 32 | + |
| 33 | + // Compute the time knot corresponding to the current delay |
| 34 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 35 | + double step_progress = (delay - (double)step_nb * timestep) / timestep; |
| 36 | + |
| 37 | + // Interpolate configuration trajectory |
| 38 | + if (step_nb >= qs.size() - 1) |
| 39 | + q_interp = qs.back(); |
| 40 | + else |
| 41 | + { |
| 42 | + q_interp = pinocchio::interpolate(model_, qs[step_nb], qs[step_nb + 1], step_progress); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + template<typename T> |
| 47 | + void interpolateState(const double delay, const double timestep, const std::vector<T> & xs, T & x_interp) |
| 48 | + { |
| 49 | + assert(("State is not of the right size", xs[0].size() == model_.nq + model_.nv)); |
| 50 | + |
| 51 | + // Compute the time knot corresponding to the current delay |
| 52 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 53 | + double step_progress = (delay - (double)step_nb * timestep) / timestep; |
| 54 | + |
| 55 | + // Interpolate state trajectory |
| 56 | + if (step_nb >= xs.size() - 1) |
| 57 | + x_interp = xs.back(); |
| 58 | + else |
| 59 | + { |
| 60 | + x_interp.head(model_.nq) = |
| 61 | + pinocchio::interpolate(model_, xs[step_nb].head(model_.nq), xs[step_nb + 1].head(model_.nq), step_progress); |
| 62 | + x_interp.tail(model_.nv) = |
| 63 | + xs[step_nb + 1].tail(model_.nv) * step_progress + xs[step_nb].tail(model_.nv) * (1. - step_progress); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + template<typename T> |
| 68 | + void interpolateLinear(const double delay, const double timestep, const std::vector<T> & vs, T & v_interp) |
| 69 | + { |
| 70 | + // Compute the time knot corresponding to the current delay |
| 71 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 72 | + double step_progress = (delay - (double)step_nb * timestep) / timestep; |
| 73 | + |
| 74 | + // Interpolate configuration trajectory |
| 75 | + if (step_nb >= vs.size() - 1) |
| 76 | + v_interp = vs.back(); |
| 77 | + else |
| 78 | + { |
| 79 | + v_interp = vs[step_nb + 1] * step_progress + vs[step_nb] * (1. - step_progress); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + template<typename T> |
| 84 | + void interpolateDiscrete(const double delay, const double timestep, const std::vector<T> & vs, T & v_interp) |
| 85 | + { |
| 86 | + // Compute the time knot corresponding to the current delay |
| 87 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 88 | + step_nb = std::clamp(step_nb, 0UL, vs.size() - 1); |
| 89 | + |
| 90 | + // Set the output arg |
| 91 | + v_interp = vs[step_nb]; |
| 92 | + } |
| 93 | + |
| 94 | + // Pinocchio model |
| 95 | + Model model_; |
| 96 | + }; |
| 97 | + |
| 98 | +} // namespace simple_mpc |
0 commit comments