Skip to content

Commit 063190e

Browse files
Fixed the bug of assigning ilqr optimization start state (#39)
1 parent 3508aae commit 063190e

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

ilqr_trajectory_tracker/demos/ilqr_optimizer_demo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ int main(int argc, char ** argv)
5656
newton_optimizer.setAlpha(alpha);
5757

5858
const auto start{std::chrono::steady_clock::now()};
59-
auto u_optimal = newton_optimizer.optimize(x_feasible, Q, R, dt);
59+
// TODO: Fix demo later, we should pass the actual robot pose instead of
60+
// the beginning pose of the feasible trajectory
61+
auto u_optimal = newton_optimizer.optimize(x_feasible[0], x_feasible, Q, R, dt);
6062
const auto end{std::chrono::steady_clock::now()};
6163

6264
const std::chrono::duration<double> elapsed_seconds{end - start};

ilqr_trajectory_tracker/include/ilqr_trajectory_tracker/ilqr_optimizer.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ class NewtonOptimizer : public Optimizer
5151
const MatrixXd & P);
5252
std::tuple<std::vector<typename RobotModel::StateT>,
5353
std::vector<typename RobotModel::InputT>> forwardPass(
54+
const typename RobotModel::StateT & x0,
5455
const std::vector<typename RobotModel::StateT> & x_feasible,
5556
const std::vector<typename RobotModel::InputT> & u_feasible,
5657
const std::vector<MatrixXd> & K_gains, const double dt, const double alpha);
5758

5859
std::vector<typename RobotModel::InputT> optimize(
60+
const typename RobotModel::StateT & x0,
5961
const std::vector<typename RobotModel::StateT> & x_feasible, const Matrix3d & Q,
6062
const Matrix2d & R, const double dt);
6163
double cost(
@@ -122,6 +124,7 @@ std::vector<MatrixXd> NewtonOptimizer<RobotModel>::backwardPass(
122124
template<typename RobotModel>
123125
std::tuple<std::vector<typename RobotModel::StateT>,
124126
std::vector<typename RobotModel::InputT>> NewtonOptimizer<RobotModel>::forwardPass(
127+
const typename RobotModel::StateT & x0,
125128
const std::vector<typename RobotModel::StateT> & x_feasible,
126129
const std::vector<typename RobotModel::InputT> & u_feasible,
127130
const std::vector<MatrixXd> & K_gains, const double dt, const double alpha)
@@ -133,7 +136,7 @@ std::tuple<std::vector<typename RobotModel::StateT>,
133136
std::vector<typename RobotModel::InputT> u_applied(input_size);
134137

135138
// assert trajectory_size > 0
136-
x_tracked[0] = x_feasible[0];
139+
x_tracked[0] = x0;
137140
for (typename std::vector<typename RobotModel::StateT>::difference_type i = 0,
138141
max_difference = std::distance(x_feasible.begin(), std::prev(x_feasible.end(), 1));
139142
i < max_difference; ++i)
@@ -165,6 +168,7 @@ std::tuple<MatrixXd, MatrixXd> NewtonOptimizer<RobotModel>::solveDiscreteLQRProb
165168

166169
template<typename RobotModel>
167170
std::vector<typename RobotModel::InputT> NewtonOptimizer<RobotModel>::optimize(
171+
const typename RobotModel::StateT & x0,
168172
const std::vector<typename RobotModel::StateT> & x_trajectory, const Matrix3d & Q,
169173
const Matrix2d & R, const double dt)
170174
{
@@ -183,7 +187,7 @@ std::vector<typename RobotModel::InputT> NewtonOptimizer<RobotModel>::optimize(
183187
double previous_best_trajectory_cost = best_trajectory_cost;
184188
for (size_t i = 0; i < iteration_number_; ++i) {
185189
auto K_gain_list = this->backwardPass(x_trajectory, u_optimized, Q, R, dt);
186-
auto [x_tracked, u_tracked] = this->forwardPass(
190+
auto [x_tracked, u_tracked] = this->forwardPass(x0,
187191
x_trajectory, u_optimized, K_gain_list, dt,
188192
alpha);
189193
u_optimized = u_tracked;

nav2_frenet_ilqr_controller/include/nav2_frenet_ilqr_controller/frenet_ilqr_controller.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class FrenetILQRController : public nav2_core::Controller
109109
nav2_core::GoalChecker * /*goal_checker*/) override;
110110

111111
Vector2d findOptimalInputForTrajectory(
112-
const geometry_msgs::msg::PoseStamped & robot_pose,
112+
const Vector3d & c_state_robot,
113113
const frenet_trajectory_planner::CartesianTrajectory & robot_cartesian_trajectory);
114114

115115
bool cancel() override;

nav2_frenet_ilqr_controller/src/frenet_ilqr_controller.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ nav_msgs::msg::Path FrenetILQRController::truncateGlobalPlanWithLookAheadDist(
206206
}
207207

208208
Vector2d FrenetILQRController::findOptimalInputForTrajectory(
209-
const geometry_msgs::msg::PoseStamped & /*robot_pose*/,
209+
const Vector3d & c_state_robot,
210210
const frenet_trajectory_planner::CartesianTrajectory & robot_cartesian_trajectory)
211211
{
212212

@@ -230,7 +230,7 @@ Vector2d FrenetILQRController::findOptimalInputForTrajectory(
230230
ilqr_trajectory_tracker::NewtonOptimizer<DiffDriveRobotModel> newton_optimizer;
231231
newton_optimizer.setIterationNumber(20);
232232
newton_optimizer.setAlpha(alpha);
233-
auto U_optimal = newton_optimizer.optimize(X_feasible, Q, R, dt);
233+
auto U_optimal = newton_optimizer.optimize(c_state_robot, X_feasible, Q, R, dt);
234234

235235
if (U_optimal.empty()) {
236236
throw nav2_core::NoValidControl("Iterative LQR couldn't find any solution!");
@@ -301,13 +301,16 @@ geometry_msgs::msg::TwistStamped FrenetILQRController::computeVelocityCommands(
301301
transformed_plan.header.frame_id,
302302
planned_cartesian_trajectory);
303303
frenet_plan = path_handler_->transformPath(costmap_ros_->getBaseFrameID(), frenet_plan);
304-
305304
planned_cartesian_trajectory = convertToCartesianTrajectory(frenet_plan);
306305

307306
truncated_path_pub_->publish(frenet_plan);
308307
robot_pose_pub_->publish(robot_pose);
309308

310-
auto u_opt = findOptimalInputForTrajectory(robot_pose, planned_cartesian_trajectory);
309+
path_handler_->transformPose(costmap_ros_->getBaseFrameID(), robot_pose, robot_pose);
310+
Vector3d c_state_robot;
311+
c_state_robot << robot_pose.pose.position.x, robot_pose.pose.position.y,
312+
tf2::getYaw(robot_pose.pose.orientation);
313+
auto u_opt = findOptimalInputForTrajectory(c_state_robot, planned_cartesian_trajectory);
311314

312315
// populate and return message
313316
geometry_msgs::msg::TwistStamped cmd_vel;

0 commit comments

Comments
 (0)