Path Trajectory Planning Example
Description
An example of using an MPC controller to track a given fixed trajectory, including:
- Straight line: starting from the robot's current position and heading, generate a trajectory of the specified length
- Square: starting from the robot's current position and heading, generate a trajectory of the specified length counterclockwise
- Circle: starting from the robot's current position and heading, generate a trajectory of the specified radius counterclockwise
- "S" curve: starting from the robot's current position and heading, generate a sinusoidal trajectory
Directory Structure
# tree -L 2 -d
.
├── CMakeLists.txt
├── docs # 文档/图片
├── include
│ └── trace_path # 头文件
├── launch
│ ├── follow_circle_traj.launch # 跟踪圆形轨迹-启动文件
│ ├── follow_s_curve_traj.launch # 跟踪S曲线轨迹-启动文件
│ └── follow_sqaure_traj.launch # 跟踪正方形轨迹-启动文件
├── package.xml
├── README.md
├── rviz
│ └── trace_path_demo.rviz
└── src
├── circle_path_generator.cpp # 圆形路径生成器
├── common # 公共方法
├── line_path_generator.cpp # 直线路径生成器
├── mpc_path_tracer.cpp # MPC 控制器
├── path_tracer.cpp # 路径跟踪器基类
├── pid_path_tracer.cpp
├── s_curve_path_generator.cpp # 正方形路径生成器
├── square_path_generator.cpp # S曲线路径生成器
└── tests # *_main.cc 入口Parameter Settings
Path Parameters
Common parameters:
- speed: speed, unit m/s
- dt: time interval, unit s
That is, a point is generated on the path every
speed * dtdistance
Straight Line
- length: length, unit m
Square
- side_length: side length, unit m
Circle
- radius: radius, unit m
S Curve
- length: half of the total length, unit m
- amplitude: amplitude
| Square Path | Circle Path | S Curve Path |
|---|---|---|
![]() | ![]() | ![]() |
Controller
PID
MPC
The solution can be constrained by setting the maximum linear velocity and maximum angular velocity via set_max_linear_velocity and set_max_angular_velocity
Build
Modify the path parameters and controller parameters in the corresponding file src/demo/trace_path/src/tests/*_main.cc, then build and run:
cd ~/kuavo-ros-opensource
catkin build trace_path # 编译Launch
Terminal 1: Launch the robot program
source devel/setup.bash # 如果使用zsh,则使用source devel/setup.zsh
# 仿真环境运行
roslaunch humanoid_controllers load_kuavo_mujoco_sim.launch # 启动控制器、mpc、wbc、仿真器
# 实物请运行
roslaunch humanoid_controllers load_kuavo_real.launch # 启动实物节点Terminal 2: Generate and track the trajectory
Launch parameters:
rviz: visualize the reference path and the real-time path (optional):rviz:=true
source devel/setup.bash
# 正方形轨迹跟踪
roslaunch trace_path follow_sqaure_traj.launch
# 圆形轨迹跟踪
roslaunch trace_path follow_circle_traj.launch
# S曲线轨迹跟踪
roslaunch trace_path follow_s_curve_traj.launchKey Topics
-
Obtain the robot's position in the world coordinate system
-
Set the current position as the starting point and generate a path trajectory in the
nav_msgs/Pathformat -
Send message commands to the
/cmd_veltopic subscribed to by the robot to control its motion -
How to adjust the robot's motion along the path trajectory via PID/MPC control
Getting Robot Position Information
Topic: /odom
Message: nav_msgs/Odometry
Control Robot Motion
Topic: /cmd_vel
Message: geometry_msgs/Twist
rostopic pub /cmd_vel geometry_msgs/Twist "linear:
x: 0.1
y: 0.0
z: 0.0
angular:
x: 0
y: 0.0
z: 0.0" -r 1000Path Representation
Message: nav_msgs/Path
show nav_msgs/Path
std_msgs/Header header
uint32 seq
time stamp
string frame_id
geometry_msgs/PoseStamped[] poses
std_msgs/Header header
uint32 seq
time stamp
string frame_id
geometry_msgs/Pose pose
geometry_msgs/Point position
float64 x
float64 y
float64 z
geometry_msgs/Quaternion orientation
float64 x
float64 y
float64 z
float64 wPublished Topics
trace_path/path: the given fixed path being tracked (square, circle, S curve...)trace_path/realtime_path: the robot's real-time motion trajectorytrace_path/mpc/path: the local path solved by the MPC controller
Result Preview
PID
Description:
- Path: straight line - square - circle - S curve

MPC
Description:
- The red path is the given fixed trajectory
- The blue path is the tracking trajectory generated by the robot's walking
- The green path is the local trajectory solved by MPC



