@@ -206,7 +206,7 @@ nav_msgs::msg::Path FrenetILQRController::truncateGlobalPlanWithLookAheadDist(
206206}
207207
208208Vector2d FrenetILQRController::findOptimalInputForTrajectory (
209- const geometry_msgs::msg::PoseStamped & /* robot_pose */ ,
209+ const Vector3d & x0 ,
210210 const frenet_trajectory_planner::CartesianTrajectory & robot_cartesian_trajectory)
211211{
212212
@@ -219,7 +219,8 @@ Vector2d FrenetILQRController::findOptimalInputForTrajectory(
219219 using ilqr_trajectory_tracker::AckermannRobotModelInput;
220220
221221 std::vector<AckermannRobotModelState> X_feasible;
222- for (auto cartesian_state : robot_cartesian_trajectory) {
222+ for (size_t i = 1 ; i < robot_cartesian_trajectory.size (); ++i) {
223+ auto cartesian_state = robot_cartesian_trajectory[i];
223224 AckermannRobotModelState x;
224225 x << cartesian_state[0 ],
225226 cartesian_state[3 ],
@@ -229,26 +230,62 @@ Vector2d FrenetILQRController::findOptimalInputForTrajectory(
229230
230231 Matrix3d Q = Matrix3d::Identity () * 1 ;
231232 Matrix2d R = Matrix2d::Identity () * 0.2 ;
232- double alpha = 1 ;
233- double dt = 0.05 ;
234- ilqr_trajectory_tracker::NewtonOptimizer<AckermannRobotModel> newton_optimizer (0.5 );
235233
236- AckermannRobotModelInput input_limits_min;
237- input_limits_min << -0.5 , -2.5 ;
234+ if (params_->robot_type == " ackermann" ) {
235+ using ilqr_trajectory_tracker::AckermannRobotModel;
236+ ilqr_trajectory_tracker::NewtonOptimizer<AckermannRobotModel> newton_optimizer (params_->wheelbase );
238237
239- AckermannRobotModelInput input_limits_max;
240- input_limits_max << 0.5 , 2.5 ;
241- newton_optimizer.setInputConstraints (input_limits_min, input_limits_max);
238+ Vector2d input_limits_min;
239+ input_limits_min << -0.5 , -0.523599 ;
242240
243- newton_optimizer. setIterationNumber ( 20 ) ;
244- newton_optimizer. setAlpha (alpha) ;
245- auto U_optimal = newton_optimizer.optimize (X_feasible, Q, R, dt );
241+ Vector2d input_limits_max ;
242+ input_limits_max << 0.5 , 0.523599 ;
243+ newton_optimizer.setInputConstraints (input_limits_min, input_limits_max );
246244
247- if (U_optimal.empty ()) {
248- throw nav2_core::NoValidControl (" Iterative LQR couldn't find any solution!" );
249- }
245+ newton_optimizer.setIterationNumber (20 );
246+ auto U_optimal = newton_optimizer.optimize (x0, X_feasible, Q, R, params_->time_discretization );
247+
248+ RCLCPP_INFO (logger_, " --------------------" );
249+ for (auto cstate : X_feasible) {
250+ RCLCPP_INFO (logger_, " [%f, %f, %f]" , cstate[0 ], cstate[1 ], cstate[2 ]);
251+ }
252+ RCLCPP_INFO (logger_, " ********************" );
253+
254+ if (U_optimal.empty ()) {
255+ throw nav2_core::NoValidControl (" Iterative LQR couldn't find any solution!" );
256+ }
257+
258+ Vector2d u_ackermann;
259+ u_ackermann << U_optimal[0 ][0 ], (U_optimal[0 ][0 ] / params_->wheelbase ) * std::tan (U_optimal[0 ][1 ]);
260+ RCLCPP_INFO (logger_, " --------------------" );
261+ for (auto u : U_optimal) {
262+ RCLCPP_INFO (logger_, " [%f, %f]" , u[0 ], u[1 ]);
263+ }
264+ RCLCPP_INFO (logger_, " ********************" );
265+
266+ return u_ackermann;
267+ } else if (params_->robot_type == " diffdrive" ) {
268+ using ilqr_trajectory_tracker::DiffDriveRobotModel;
269+ ilqr_trajectory_tracker::NewtonOptimizer<DiffDriveRobotModel> newton_optimizer;
250270
251- return U_optimal[0 ];
271+ Vector2d input_limits_min;
272+ input_limits_min << -0.5 , -2.5 ;
273+
274+ Vector2d input_limits_max;
275+ input_limits_max << 0.5 , 2.5 ;
276+ newton_optimizer.setInputConstraints (input_limits_min, input_limits_max);
277+
278+ newton_optimizer.setIterationNumber (20 );
279+ auto U_optimal = newton_optimizer.optimize (x0, X_feasible, Q, R, params_->time_discretization );
280+
281+ if (U_optimal.empty ()) {
282+ throw nav2_core::NoValidControl (" Iterative LQR couldn't find any solution!" );
283+ }
284+
285+ return U_optimal[0 ];
286+ } else {
287+ throw nav2_core::NoValidControl (" Unknown robot type! Possible robot types : ackermann, diffdrive" );
288+ }
252289}
253290
254291geometry_msgs::msg::TwistStamped FrenetILQRController::computeVelocityCommands (
@@ -289,19 +326,19 @@ geometry_msgs::msg::TwistStamped FrenetILQRController::computeVelocityCommands(
289326 frenet_trajectory_planner::CartesianState robot_cartesian_state =
290327 frenet_trajectory_planner::CartesianState::Zero ();
291328 double robot_yaw = tf2::getYaw (robot_pose.pose .orientation );
292- double linear_speed = speed.linear .x ;
329+ double linear_speed = std::abs ( speed.linear .x ) ;
293330
294331 // if (std::abs(linear_speed) <= 0.5) {
295332 // params_->frenet_trajectory_planner_config.time_interval = 10;
296333 // }
297- double max_curvature = 2.5 ;
298- linear_speed = 1 / max_curvature;
334+ // double max_curvature = 2.5;
335+ // linear_speed = 1 / max_curvature;
299336 robot_cartesian_state[0 ] = robot_pose.pose .position .x ;
300337 robot_cartesian_state[1 ] = linear_speed * std::cos (robot_yaw);
301- robot_cartesian_state[2 ] = -linear_speed * std::sin (robot_yaw);
338+ // robot_cartesian_state[2] = -linear_speed * std::sin(robot_yaw);
302339 robot_cartesian_state[3 ] = robot_pose.pose .position .y ;
303340 robot_cartesian_state[4 ] = linear_speed * std::sin (robot_yaw);
304- robot_cartesian_state[5 ] = linear_speed * std::cos (robot_yaw);
341+ // robot_cartesian_state[5] = linear_speed * std::cos(robot_yaw);
305342 robot_cartesian_state[6 ] = robot_yaw;
306343
307344 frenet_trajectory_planner_.setFrenetTrajectoryPlannerConfig (
@@ -319,13 +356,16 @@ geometry_msgs::msg::TwistStamped FrenetILQRController::computeVelocityCommands(
319356 transformed_plan.header .frame_id ,
320357 planned_cartesian_trajectory);
321358 frenet_plan = path_handler_->transformPath (costmap_ros_->getBaseFrameID (), frenet_plan);
322-
323359 planned_cartesian_trajectory = convertToCartesianTrajectory (frenet_plan);
324360
325361 truncated_path_pub_->publish (frenet_plan);
326362 robot_pose_pub_->publish (robot_pose);
327363
328- auto u_opt = findOptimalInputForTrajectory (robot_pose, planned_cartesian_trajectory);
364+ path_handler_->transformPose (costmap_ros_->getBaseFrameID (), robot_pose, robot_pose);
365+ Vector3d c_state_robot;
366+ c_state_robot << robot_pose.pose .position .x , robot_pose.pose .position .y ,
367+ tf2::getYaw (robot_pose.pose .orientation );
368+ auto u_opt = findOptimalInputForTrajectory (c_state_robot, planned_cartesian_trajectory);
329369
330370 // populate and return message
331371 geometry_msgs::msg::TwistStamped cmd_vel;
0 commit comments