🤖 Week 11

四足机器人步态控制(Trot Gait)实验报告

📅 实验日期

2026-05-20

🧪 一、实验目的

🖥️ 二、实验环境

🚀 三、实验步骤

1️⃣ 启动 PyBullet 仿真环境

p.connect(p.GUI)
p.setGravity(0, 0, -9.8)
p.loadURDF("plane.urdf")

2️⃣ 加载四足机器人模型

robot = p.loadURDF(
    "a1/a1.urdf",
    [0, 0, 0.35],
    p.getQuaternionFromEuler([0, 0, 0])
)
初始高度设置为 0.35m,避免机器人加载后直接翻倒。

3️⃣ 关节结构解析

for i in range(p.getNumJoints(robot)):
    info = p.getJointInfo(robot, i)
    print(i, info[1].decode(), info[2])

4️⃣ trot 步态生成

phase = 2 * np.pi * freq * t

5️⃣ 腿部运动轨迹(IK)

x = step_length * sin(phase)
z = step_height * sin(phase)
thigh = arctan2(x, height)
calf = -2 * thigh

6️⃣ 姿态稳定控制(关键改进)

roll, pitch = getBaseOrientation()

roll_correction = -0.8 * roll
pitch_correction = -0.8 * pitch
加入 roll / pitch 修正后,机器人能够自动纠正身体倾斜。

7️⃣ 统一关节控制

p.setJointMotorControlArray(
    robot,
    jointIndices,
    p.POSITION_CONTROL,
    targetPositions=angles,
    forces=[200]*len(joints),
    positionGains=[0.25]*len(joints),
    velocityGains=[1.0]*len(joints)
)

8️⃣ 仿真循环运行

while True:
    controller.step(t)
    p.stepSimulation()
    time.sleep(1./240.)

📊 四、实验结果

📉 五、实验中出现的问题及解决方法

❌ 问题1:机器人侧翻

原因:

解决方法:

❌ 问题2:步态不稳定

原因:

解决方法:

🔄 六、新旧代码对比分析(核心改进)

🧱 1️⃣ 控制结构变化

❌ 老代码

t → gait → IK → 单关节控制

✅ 新代码

t
 ↓
trot gait
 ↓
IK计算
 ↓
balance()
 ↓
统一关节控制

⚖️ 2️⃣ 为什么现在能稳定走路?

改进内容 作用
加入 roll/pitch 修正 机器人倾斜时自动纠正
降低 step_height 减少身体晃动
减小步长 降低冲击力
统一关节控制 减少关节不同步问题
相位 trot 控制 实现稳定对角步态

📌 核心原因总结

老代码只有“让腿动”,没有“让身体保持平衡”。 新代码加入了姿态反馈控制(Balance Control),系统能够检测机器人是否倾斜,并自动施加修正,因此具备了“自我纠偏能力”。

🧠 七、核心控制代码

roll, pitch = getBaseOrientation()

roll_correction = -0.8 * roll
pitch_correction = -0.8 * pitch

angles[0] += roll_c
angles[1] += pitch_c

🎯 八、实验总结

通过本次实验,成功实现了四足机器人在 PyBullet 中的基础 trot 步态控制,并在原始开环控制基础上加入了姿态稳定控制机制。