Skip to content

Commit dee1fb3

Browse files
committed
Take PR comments into account
1 parent 6319755 commit dee1fb3

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

bindings/expose-friction-compensation.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ namespace simple_mpc
1717
{
1818
namespace bp = boost::python;
1919

20+
/* Eigen::VectorXd computeFrictionProxy(
21+
FrictionCompensation & self,
22+
Eigen::Ref<const VectorXd> velocity,
23+
Eigen::Ref<VectorXd> torque) {
24+
self.computeFriction(velocity, torque);
25+
26+
return torque;
27+
} */
28+
2029
void exposeFrictionCompensation()
2130
{
2231
bp::class_<FrictionCompensation>(
23-
"FrictionCompensation", bp::init<const Model &, const long>(bp::args("self", "model", "actuation_size")))
32+
"FrictionCompensation", bp::init<const Model &, const bool>(bp::args("self", "model", "with_free_flyer")))
2433
.def("computeFriction", &FrictionCompensation::computeFriction)
25-
.add_property("corrected_torque", &FrictionCompensation::corrected_torque_)
2634
.add_property("dry_friction", &FrictionCompensation::dry_friction_)
2735
.add_property("viscuous_friction", &FrictionCompensation::viscuous_friction_);
2836
}

examples/go2_fulldynamics.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@
6565
kinematics_limits=True,
6666
force_cone=False,
6767
)
68-
T = 50
68+
T = 100
6969

7070
dynproblem = FullDynamicsOCP(problem_conf, model_handler)
7171
dynproblem.createProblem(model_handler.getReferenceState(), T, force_size, gravity[2], False)
7272

73-
T_ds = 10
74-
T_ss = 30
73+
T_ds = 50
74+
T_ss = 10
7575
N_simu = int(0.01 / 0.001)
7676
mpc_conf = dict(
7777
ddpIteration=1,
@@ -114,9 +114,9 @@
114114
"RR_foot": False,
115115
}
116116
contact_phases = [contact_phase_quadru] * int(T_ds / 2)
117-
contact_phases += [contact_phase_lift_FL] * T_ss
117+
contact_phases += [contact_phase_lift] * T_ss
118118
contact_phases += [contact_phase_quadru] * T_ds
119-
contact_phases += [contact_phase_lift_FR] * T_ss
119+
contact_phases += [contact_phase_lift] * T_ss
120120
contact_phases += [contact_phase_quadru] * int(T_ds / 2)
121121

122122
""" contact_phases = [contact_phase_quadru] * int(T_ds / 2)
@@ -193,7 +193,7 @@
193193
L_measured = []
194194

195195
v = np.zeros(6)
196-
v[0] = 0.2
196+
v[0] = 0.
197197
mpc.velocity_base = v
198198
for t in range(500):
199199
print("Time " + str(t))
@@ -276,10 +276,11 @@
276276
mpc.getDataHandler().getData().M,
277277
)
278278

279-
fcompensation.computeFriction(x_interp[nq + 6:], qp.solved_torque)
280-
device.execute(fcompensation.corrected_torque)
279+
qp_torque = qp.solved_torque.copy()
280+
corrected_torque = fcompensation.computeFriction(x_interp[nq + 6:], qp_torque)
281+
device.execute(corrected_torque)
281282

282-
u_multibody.append(copy.deepcopy(fcompensation.corrected_torque))
283+
u_multibody.append(copy.deepcopy(corrected_torque))
283284
x_multibody.append(x_measured)
284285

285286
force_FL = np.array(force_FL)

include/simple-mpc/friction-compensation.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,24 @@ namespace simple_mpc
3030
/**
3131
* @brief Construct a new Friction Compensation object
3232
*
33-
* @param actuation_size Dimension of torque
33+
* @param model Pinocchio model containing friction coefficients
34+
@param with_free_flyer Bool indicating if model has free flyer joint
3435
*/
35-
FrictionCompensation(const Model & model, const long actuation_size);
36+
FrictionCompensation(const Model & model, const bool with_free_flyer = true);
3637

3738
/**
38-
* @brief Compute the torque correction due to friction and store it internally.
39+
* @brief Add friction correction to joint torque
3940
*
4041
* @param[in] velocity Joint velocity
4142
* @param[in] torque Joint torque
4243
*/
43-
void computeFriction(const Eigen::VectorXd & velocity, const Eigen::VectorXd & torque);
44+
void computeFriction(Eigen::Ref<const VectorXd> velocity, Eigen::Ref<VectorXd> torque);
4445

4546
// Sign function for internal computation
4647
static double signFunction(double x);
4748

48-
// Internal torque with friction compensation
49-
Eigen::VectorXd corrected_torque_;
49+
// Actuation size
50+
int nu_;
5051

5152
// Friction coefficients
5253
Eigen::VectorXd dry_friction_;

src/friction-compensation.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,28 @@
33
namespace simple_mpc
44
{
55

6-
FrictionCompensation::FrictionCompensation(const Model & model, const long actuation_size)
6+
FrictionCompensation::FrictionCompensation(const Model & model, const bool with_free_flyer)
77
{
8-
corrected_torque_.resize(actuation_size);
9-
dry_friction_ = model.friction;
10-
viscuous_friction_ = model.damping;
11-
}
12-
13-
void FrictionCompensation::computeFriction(const Eigen::VectorXd & velocity, const Eigen::VectorXd & torque)
14-
{
15-
if (velocity.size() != corrected_torque_.size())
8+
if (with_free_flyer)
169
{
17-
throw std::runtime_error("Velocity has wrong size");
10+
// Ignore universe and root joints
11+
nu_ = model.njoints - 2;
1812
}
19-
if (torque.size() != corrected_torque_.size())
13+
else
2014
{
21-
throw std::runtime_error("Torque has wrong size");
15+
// Ignore root joint
16+
nu_ = model.njoints - 1;
2217
}
23-
corrected_torque_ = torque + viscuous_friction_.cwiseProduct(velocity)
24-
+ dry_friction_.cwiseProduct(velocity.unaryExpr(std::function(signFunction)));
18+
dry_friction_ = model.friction.head(nu_);
19+
viscuous_friction_ = model.damping.head(nu_);
20+
}
21+
22+
void FrictionCompensation::computeFriction(Eigen::Ref<const VectorXd> velocity, Eigen::Ref<VectorXd> torque)
23+
{
24+
assert(("Velocity size must be equal to actuation size", velocity.size() == nu_));
25+
assert(("Torque size must be equal to actuation size", torque.size() == nu_));
26+
torque += viscuous_friction_.cwiseProduct(velocity)
27+
+ dry_friction_.cwiseProduct(velocity.unaryExpr(std::function(signFunction)));
2528
}
2629

2730
double FrictionCompensation::signFunction(double x)

tests/friction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ BOOST_AUTO_TEST_CASE(dry_viscuous_friction)
1919
model.friction = Eigen::VectorXd::Constant(nu, .5);
2020
model.damping = Eigen::VectorXd::Constant(nu, .05);
2121

22-
FrictionCompensation friction = FrictionCompensation(model, nu);
22+
FrictionCompensation friction = FrictionCompensation(model);
2323

2424
BOOST_CHECK_EQUAL(model.friction, friction.dry_friction_);
2525
BOOST_CHECK_EQUAL(model.damping, friction.viscuous_friction_);
2626

2727
Eigen::VectorXd velocity = Eigen::VectorXd::Random(nu);
2828
Eigen::VectorXd torque = Eigen::VectorXd::Random(nu);
2929

30-
friction.computeFriction(velocity, torque);
31-
3230
Eigen::VectorXd ctorque(nu);
3331
for (long i = 0; i < nu; i++)
3432
{
3533
double sgn = (velocity[i] > 0) - (velocity[i] < 0);
3634
ctorque[i] = torque[i] + model.friction[i] * sgn + model.damping[i] * velocity[i];
3735
}
3836

39-
BOOST_CHECK(friction.corrected_torque_.isApprox(ctorque));
37+
friction.computeFriction(velocity, torque);
38+
39+
BOOST_CHECK(torque.isApprox(ctorque));
4040
}
4141

4242
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)