Skip to content

Commit 120feaa

Browse files
author
earlaud
committed
Template interpolator
1 parent 634cc39 commit 120feaa

File tree

8 files changed

+103
-163
lines changed

8 files changed

+103
-163
lines changed

.gersemirc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
definitions: [./CMakeLists.txt, ./benchmark, ./bindings, ./cmake, ./tests]
1+
definitions: [./CMakeLists.txt, ./benchmark, ./bindings, ./tests]
22
line_length: 80
33
indent: 2
44
warn_about_unknown_commands: false

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ repos:
3030
rev: 0.19.3
3131
hooks:
3232
- id: gersemi
33+
args: ["--print-config=verbose"]

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)
99+
file(GLOB mpc_HEADER CONFIGURE_DEPENDS include/${PROJECT_NAME}/*.hpp include/${PROJECT_NAME}/*.hxx)
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.hpp"
12+
#include "simple-mpc/interpolator.hxx"
1313

1414
namespace simple_mpc
1515
{

include/simple-mpc/interpolator.hpp

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

src/interpolator.cpp

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

tests/interpolator.cpp

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

22
#include <boost/test/unit_test.hpp>
33

4-
#include "simple-mpc/interpolator.hpp"
4+
#include "simple-mpc/interpolator.hxx"
55
#include "test_utils.cpp"
66

77
BOOST_AUTO_TEST_SUITE(interpolator)

0 commit comments

Comments
 (0)