Skip to content

Commit f529f18

Browse files
author
earlaud
committed
Move back interpolator to cpp
1 parent 90ec57a commit f529f18

File tree

6 files changed

+138
-114
lines changed

6 files changed

+138
-114
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ include(dependencies.cmake)
9696

9797
# Main Library
9898
file(GLOB mpc_SOURCE CONFIGURE_DEPENDS src/*.cpp)
99-
file(GLOB mpc_HEADER CONFIGURE_DEPENDS include/${PROJECT_NAME}/*.hpp include/${PROJECT_NAME}/*.hxx)
99+
file(GLOB mpc_HEADER CONFIGURE_DEPENDS include/${PROJECT_NAME}/*.hpp)
100100

101101
add_library(${PROJECT_NAME} SHARED ${mpc_HEADER} ${mpc_SOURCE})
102102
target_include_directories(

bindings/expose-interpolate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <eigenpy/deprecation-policy.hpp>
1010
#include <eigenpy/std-vector.hpp>
1111

12-
#include "simple-mpc/interpolator.hxx"
12+
#include "simple-mpc/interpolator.hpp"
1313

1414
namespace simple_mpc
1515
{

include/simple-mpc/interpolator.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.hpp
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+
void interpolateConfiguration(
29+
const double delay,
30+
const double timestep,
31+
const std::vector<Eigen::VectorXd> & qs,
32+
Eigen::Ref<Eigen::VectorXd> q_interp);
33+
34+
void interpolateState(
35+
const double delay,
36+
const double timestep,
37+
const std::vector<Eigen::VectorXd> & xs,
38+
Eigen::Ref<Eigen::VectorXd> x_interp);
39+
40+
void interpolateLinear(
41+
const double delay,
42+
const double timestep,
43+
const std::vector<Eigen::VectorXd> & vs,
44+
Eigen::Ref<Eigen::VectorXd> v_interp);
45+
46+
void interpolateContacts(
47+
const double delay,
48+
const double timestep,
49+
const std::vector<std::vector<bool>> & cs,
50+
std::vector<bool> & c_interp);
51+
52+
// Pinocchio model
53+
Model model_;
54+
};
55+
56+
} // namespace simple_mpc

include/simple-mpc/interpolator.hxx

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/interpolator.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

tests/interpolator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

22
#include <boost/test/unit_test.hpp>
3-
#include <vector>
43

5-
#include "simple-mpc/interpolator.hxx"
4+
#include "simple-mpc/interpolator.hpp"
65
#include "test_utils.cpp"
76

87
BOOST_AUTO_TEST_SUITE(interpolator)

0 commit comments

Comments
 (0)