|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// BSD 2-Clause License |
| 3 | +// |
| 4 | +// Copyright (C) 2025, INRIA |
| 5 | +// Copyright note valid unless otherwise stated in individual files. |
| 6 | +// All rights reserved. |
| 7 | +/////////////////////////////////////////////////////////////////////////////// |
| 8 | +#include "simple-mpc/interpolator.hpp" |
| 9 | + |
| 10 | +namespace simple_mpc |
| 11 | +{ |
| 12 | + |
| 13 | + Interpolator::Interpolator(const long nx, const long nv, const long nu, const long nf, const double MPC_timestep) |
| 14 | + : MPC_timestep_(MPC_timestep) |
| 15 | + { |
| 16 | + x_interpolated_.resize(nx); |
| 17 | + u_interpolated_.resize(nu); |
| 18 | + a_interpolated_.resize(nv); |
| 19 | + forces_interpolated_.resize(nf); |
| 20 | + } |
| 21 | + |
| 22 | + void Interpolator::interpolate( |
| 23 | + const double delay, |
| 24 | + std::vector<Eigen::VectorXd> xs, |
| 25 | + std::vector<Eigen::VectorXd> us, |
| 26 | + std::vector<Eigen::VectorXd> ddqs, |
| 27 | + std::vector<Eigen::VectorXd> forces) |
| 28 | + { |
| 29 | + // Compute the time knot corresponding to the current delay |
| 30 | + size_t step_nb = static_cast<size_t>(delay / MPC_timestep_); |
| 31 | + double step_progress = (delay - (double)step_nb * MPC_timestep_) / MPC_timestep_; |
| 32 | + |
| 33 | + // Interpolate state and command trajectories |
| 34 | + if (step_nb >= xs.size() - 1) |
| 35 | + { |
| 36 | + step_nb = xs.size() - 1; |
| 37 | + step_progress = 0.0; |
| 38 | + x_interpolated_ = xs[step_nb]; |
| 39 | + u_interpolated_ = us[step_nb]; |
| 40 | + a_interpolated_ = ddqs[step_nb]; |
| 41 | + forces_interpolated_ = forces[step_nb]; |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + x_interpolated_ = xs[step_nb + 1] * step_progress + xs[step_nb] * (1. - step_progress); |
| 46 | + u_interpolated_ = us[step_nb + 1] * step_progress + us[step_nb] * (1. - step_progress); |
| 47 | + a_interpolated_ = ddqs[step_nb + 1] * step_progress + ddqs[step_nb] * (1. - step_progress); |
| 48 | + forces_interpolated_ = forces[step_nb + 1] * step_progress + forces[step_nb] * (1. - step_progress); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | +} // namespace simple_mpc |
0 commit comments