|
| 1 | +#include "simple-mpc/interpolator.hpp" |
| 2 | + |
| 3 | +namespace simple_mpc |
| 4 | +{ |
| 5 | + void Interpolator::interpolateConfiguration( |
| 6 | + const double delay, |
| 7 | + const double timestep, |
| 8 | + const std::vector<Eigen::VectorXd> & qs, |
| 9 | + Eigen::Ref<Eigen::VectorXd> q_interp) |
| 10 | + { |
| 11 | + assert(("Configuration is not of the right size", qs[0].size() == model_.nq)); |
| 12 | + |
| 13 | + // Compute the time knot corresponding to the current delay |
| 14 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 15 | + double step_progress = (delay - (double)step_nb * timestep) / timestep; |
| 16 | + |
| 17 | + // Interpolate configuration trajectory |
| 18 | + if (step_nb >= qs.size() - 1) |
| 19 | + q_interp = qs.back(); |
| 20 | + else |
| 21 | + { |
| 22 | + q_interp = pinocchio::interpolate(model_, qs[step_nb], qs[step_nb + 1], step_progress); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + void Interpolator::interpolateState( |
| 27 | + const double delay, |
| 28 | + const double timestep, |
| 29 | + const std::vector<Eigen::VectorXd> & xs, |
| 30 | + Eigen::Ref<Eigen::VectorXd> x_interp) |
| 31 | + { |
| 32 | + assert(("State is not of the right size", xs[0].size() == model_.nq + model_.nv)); |
| 33 | + |
| 34 | + // Compute the time knot corresponding to the current delay |
| 35 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 36 | + double step_progress = (delay - (double)step_nb * timestep) / timestep; |
| 37 | + |
| 38 | + // Interpolate state trajectory |
| 39 | + if (step_nb >= xs.size() - 1) |
| 40 | + x_interp = xs.back(); |
| 41 | + else |
| 42 | + { |
| 43 | + x_interp.head(model_.nq) = |
| 44 | + pinocchio::interpolate(model_, xs[step_nb].head(model_.nq), xs[step_nb + 1].head(model_.nq), step_progress); |
| 45 | + x_interp.tail(model_.nv) = |
| 46 | + xs[step_nb + 1].tail(model_.nv) * step_progress + xs[step_nb].tail(model_.nv) * (1. - step_progress); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void Interpolator::interpolateLinear( |
| 51 | + const double delay, |
| 52 | + const double timestep, |
| 53 | + const std::vector<Eigen::VectorXd> & vs, |
| 54 | + Eigen::Ref<Eigen::VectorXd> v_interp) |
| 55 | + { |
| 56 | + // Compute the time knot corresponding to the current delay |
| 57 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 58 | + double step_progress = (delay - (double)step_nb * timestep) / timestep; |
| 59 | + |
| 60 | + // Interpolate configuration trajectory |
| 61 | + if (step_nb >= vs.size() - 1) |
| 62 | + v_interp = vs.back(); |
| 63 | + else |
| 64 | + { |
| 65 | + v_interp = vs[step_nb + 1] * step_progress + vs[step_nb] * (1. - step_progress); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + void Interpolator::interpolateContacts( |
| 70 | + const double delay, const double timestep, const std::vector<std::vector<bool>> & cs, std::vector<bool> & c_interp) |
| 71 | + { |
| 72 | + // Compute the time knot corresponding to the current delay |
| 73 | + size_t step_nb = static_cast<size_t>(delay / timestep); |
| 74 | + step_nb = std::clamp(step_nb, 0UL, cs.size() - 1); |
| 75 | + |
| 76 | + // Set the output arg |
| 77 | + c_interp = cs[step_nb]; |
| 78 | + } |
| 79 | +} // namespace simple_mpc |
0 commit comments