Skip to content

Commit 34049ed

Browse files
committed
[tmp] only constrain before publish
1 parent d48ff16 commit 34049ed

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

‎nav2_mppi_controller/include/nav2_mppi_controller/motion_models.hpp‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,43 @@ class MotionModel
8989
0).select(state.vx.col(i - 1) + max_delta_vx,
9090
state.vx.col(i - 1) - min_delta_vx);
9191

92+
// state.cvx.col(i - 1) = state.cvx.col(i - 1)
93+
// .cwiseMax(lower_bound_vx)
94+
// .cwiseMin(upper_bound_vx);
95+
// state.vx.col(i) = state.cvx.col(i - 1);
96+
97+
// state.u(i) = state.cu(i-1)
98+
// => we start from current robot speed (state.u(0)) and then apply u_virt (state.cu(i-1))
99+
// but we also need to constrain u_virt before applying it based on static limits (max vel & curvature) to compute
100+
// u_app
101+
// then state.u(i) = u_app(i-1)
102+
// ==> x_k = F(x_k-1, u_app_k-1) = F(x_k-1, g(u_virt_k-1))
103+
104+
// TODO 2 constrains state.u based on static limits (max vel & curvature) to compute u_app
105+
106+
// u_app(0) should be limited on acceleration relative to either feedback (state.u(0)) as done now
107+
// TODO 3 or ideally based on last published command
108+
92109
state.vx.col(i) = state.cvx.col(i - 1)
93110
.cwiseMax(lower_bound_vx)
94111
.cwiseMin(upper_bound_vx);
95112

113+
// state.cwz.col(i - 1) = state.cwz.col(i - 1)
114+
// .cwiseMax(state.wz.col(i - 1) - max_delta_wz)
115+
// .cwiseMin(state.wz.col(i - 1) + max_delta_wz);
116+
// state.wz.col(i) = state.cwz.col(i - 1);
117+
96118
state.wz.col(i) = state.cwz.col(i - 1)
97119
.cwiseMax(state.wz.col(i - 1) - max_delta_wz)
98120
.cwiseMin(state.wz.col(i - 1) + max_delta_wz);
99121

100122
// constrain u_virt 0 & 1 to make sure accelerations are limited at the first step (state.vx(0) is from feedback)
101123
// also constrain u_virt_1 as this is published as command
102-
if (i <= 2)
103-
{
104-
state.cvx.col(i - 1) = state.vx.col(i);
105-
state.cwz.col(i - 1) = state.wz.col(i);
106-
}
124+
// if (i <= 2)
125+
// {
126+
// state.cvx.col(i - 1) = state.vx.col(i);
127+
// state.cwz.col(i - 1) = state.wz.col(i);
128+
// }
107129

108130
if (is_holo) {
109131
auto lower_bound_vy = (state.vy.col(i - 1) >

‎nav2_mppi_controller/src/optimizer.cpp‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ void Optimizer::computeControlSequenceAccel(const models::ControlSequence& contr
237237
// Check if accelerations exceed constraints
238238
float ax = (control_sequence.vx(i) - control_sequence.vx(i - 1)) / s.model_dt;
239239
if (std::abs(ax) > s.constraints.ax_max + epsilon) {
240-
std::cout << "Acceleration constraint violated at index " << i << ":\n";
240+
std::cout << "****Acceleration constraint violated at index " << i << ":\n";
241241
std::cout << "vx[i-1]: " << control_sequence.vx(i - 1) << ", vx[i]: " << control_sequence.vx(i) << ", ax: " << ax << "\n";
242242
}
243243

244244
float wz_accel = (control_sequence.wz(i) - control_sequence.wz(i - 1)) / s.model_dt;
245245
if (std::abs(wz_accel) > s.constraints.az_max + epsilon) {
246-
std::cout << "Angular acceleration constraint violated at index " << i << ":\n";
246+
std::cout << "***Angular acceleration constraint violated at index " << i << ":\n";
247247
std::cout << "wz[i-1]: " << control_sequence.wz(i - 1) << ", wz[i]: " << control_sequence.wz(i) << ", wz_accel: " << wz_accel << "\n";
248248
}
249249
}
@@ -308,7 +308,12 @@ void Optimizer::shiftControlSequence()
308308
control_sequence_.vx(size - 1) = control_sequence_.vx(size - 2);
309309
control_sequence_.wz(size - 1) = control_sequence_.wz(size - 2);
310310

311-
if (isHolonomic()) {
311+
// std::cout << "\n\t control_sequence_ After:\n\t\t" << control_sequence_.vx(Eigen::seq(0, 5)).transpose() << "\n\t\t"
312+
// << control_sequence_.wz(Eigen::seq(0, 5)).transpose() << "\n\t\t"
313+
// /*<< control_sequence_.vx(Eigen::seq(Eigen::last -5, Eigen::last)).transpose()*/<< std::endl;
314+
315+
if (isHolonomic())
316+
{
312317
utils::shiftColumnsByOnePlace(control_sequence_.vy, -1);
313318
control_sequence_.vy(size - 1) = control_sequence_.vy(size - 2);
314319
}
@@ -338,17 +343,35 @@ void Optimizer::applyControlSequenceConstraints()
338343
float max_delta_vy = s.model_dt * s.constraints.ay_max;
339344
float min_delta_vy = s.model_dt * s.constraints.ay_min;
340345
float max_delta_wz = s.model_dt * s.constraints.az_max;
346+
// --tried 1 limit ctrl_seq_(0) based on accel_limit from current robot speed (= state.vx(0,0)) (instead of in predict -> see it still accel issue)
347+
// TODO 1 only constrain the published controls (u0 & u1), but keep the unconstrained sequence for warm start
348+
// TODO 4 or ideally based on last published command
349+
350+
// at this point, control_sequence_ contains the softmax mean of state_.cu (u_virt)]
351+
352+
/*
341353
float vx_last = utils::clamp(s.constraints.vx_min, s.constraints.vx_max, control_sequence_.vx(0));
342354
float wz_last = utils::clamp(-s.constraints.wz, s.constraints.wz, control_sequence_.wz(0));
355+
343356
control_sequence_.vx(0) = vx_last;
344357
control_sequence_.wz(0) = wz_last;
345358
float vy_last = 0;
346359
if (isHolonomic()) {
347360
vy_last = utils::clamp(-s.constraints.vy, s.constraints.vy, control_sequence_.vy(0));
348361
control_sequence_.vy(0) = vy_last;
349362
}
363+
*/
364+
365+
// limit acceleration between current feedback speed and first control in the sequence
366+
float vx_last = static_cast<float>(state_.speed.linear.x);
367+
float wz_last = static_cast<float>(state_.speed.angular.z);
368+
369+
float vy_last = 0;
370+
if (isHolonomic()) {
371+
vy_last = static_cast<float>(state_.speed.linear.y);
372+
}
350373

351-
for (unsigned int i = 1; i != control_sequence_.vx.size(); i++) {
374+
for (unsigned int i = 0; i != control_sequence_.vx.size(); i++) {
352375
float & vx_curr = control_sequence_.vx(i);
353376
vx_curr = utils::clamp(s.constraints.vx_min, s.constraints.vx_max, vx_curr);
354377
if(vx_last > 0) {
@@ -359,10 +382,18 @@ void Optimizer::applyControlSequenceConstraints()
359382
vx_last = vx_curr;
360383

361384
float & wz_curr = control_sequence_.wz(i);
385+
if (i==0)
386+
{
387+
std::cout << "control_sequence_.wz(0) BEFORE: " << control_sequence_.wz(i) << std::endl;
388+
}
362389
wz_curr = utils::clamp(-s.constraints.wz, s.constraints.wz, wz_curr);
363390
wz_curr = utils::clamp(wz_last - max_delta_wz, wz_last + max_delta_wz, wz_curr);
364391
wz_last = wz_curr;
365392

393+
if (i==0)
394+
{
395+
std::cout << "control_sequence_.wz(0) AFTER: " << control_sequence_.wz(i) << std::endl;
396+
}
366397
if (isHolonomic()) {
367398
float & vy_curr = control_sequence_.vy(i);
368399
vy_curr = utils::clamp(-s.constraints.vy, s.constraints.vy, vy_curr);

0 commit comments

Comments
 (0)