一、教程概述
本文完整覆盖 Webots 模拟器全流程操作,包含跨平台安装、仿真项目创建、场景搭建、机器人控制器开发、仿真同步机制、性能优化;文末提供四大可直接复现实战案例:红外避障、PID 轨迹跟踪、A * 路径规划、ROS2-SLAM 自主导航,配套完整源码与工程资源包。
二、Webots 安装通用流程
Webots 兼容 Windows、macOS、Linux 三大操作系统,标准安装步骤统一:

配套资源包下载
网盘下载保存地址:https://pan.baidu.com/s/1LW2TcYPpZ2rsvp7mnWoVcg?pwd=5555 提取码: 5555 资源包内部文件清单:
三、仿真项目创建与场景搭建
3.1 新建空白仿真工程
3.2 场景素材库说明
Webots 内置标准化场景资源库,可直接拖拽使用:道路、交通设施、建筑物、墙体障碍物、自然地形、激光雷达 / 红外 / 电机等各类机器人传感器、执行器组件。

四、Webots 开发完整技术流程
整体开发分为四大标准阶段

五、进阶仿真功能说明
六、仿真性能优化方案
七、官方学习资源渠道
八、四大实战项目(附完整可运行源码)
实战 1 E-puck 红外自主避障
原理
机器人搭载 8 路红外距离传感器,实时读取障碍物测距数值,左右轮差速转向规避前方、左右障碍。
python
运行
from controller import Robot, DistanceSensor, Motor
TIME_STEP = 64
MAX_SPEED = 6.28
robot = Robot()
ps = []
ps_names = ["ps0", "ps1", "ps2", "ps3", "ps4", "ps5", "ps6", "ps7"]
for name in ps_names:
sensor = robot.getDevice(name)
sensor.enable(TIME_STEP)
ps.append(sensor)
left_motor = robot.getDevice("left wheel motor")
right_motor = robot.getDevice("right wheel motor")
left_motor.setPosition(float('inf'))
right_motor.setPosition(float('inf'))
left_motor.setVelocity(0.0)
right_motor.setVelocity(0.0)
while robot.step(TIME_STEP) != -1:
ps_values = [s.getValue() for s in ps]
left_obstacle = ps_values[5] > 80 or ps_values[6] > 80 or ps_values[7] > 80
right_obstacle = ps_values[0] > 80 or ps_values[1] > 80 or ps_values[2] > 80
left_speed = MAX_SPEED * 0.5
right_speed = MAX_SPEED * 0.5
if left_obstacle:
left_speed = -MAX_SPEED * 0.3
right_speed = MAX_SPEED * 0.5
elif right_obstacle:
left_speed = MAX_SPEED * 0.5
right_speed = -MAX_SPEED * 0.3
left_motor.setVelocity(left_speed)
right_motor.setVelocity(right_speed)
配套工程包:e-puck-obstacle-avoidance.zip
实战 2 PID GPS 轨迹跟踪控制
原理
采用比例 – 积分 – 微分闭环控制,根据 GPS 坐标误差修正两轮速度,消除稳态偏移、抑制运动震荡。
python
运行
from controller import Robot, Motor, GPS
TIME_STEP = 64
MAX_SPEED = 6.28
Kp = 2.0
Ki = 0.01
Kd = 0.1
robot = Robot()
gps = robot.getDevice("gps")
gps.enable(TIME_STEP)
left_motor = robot.getDevice("left wheel motor")
right_motor = robot.getDevice("right wheel motor")
left_motor.setPosition(float('inf'))
right_motor.setPosition(float('inf'))
target_x = 0.5
target_y = 0.5
integral = 0.0
last_error = 0.0
while robot.step(TIME_STEP) != -1:
x, y, _ = gps.getValues()
error = target_x – x
integral += error * TIME_STEP / 1000.0
derivative = (error – last_error) / (TIME_STEP / 1000.0)
last_error = error
output = Kp * error + Ki * integral + Kd * derivative
left_motor.setVelocity(MAX_SPEED*0.5 + output)
right_motor.setVelocity(MAX_SPEED*0.5 – output)
配套工程包:Webots-micromouse_python.zip
实战 3 A * 全局路径规划算法
原理
启发式寻路算法,代价公式 f(n)=g(n)+h(n),g 为已行走实际代价,h 为终点预估代价,搜索全局最优无碰撞路径。
python
运行
import math
class Node:
def __init__(self, x, y):
self.x = x
self.y = y
self.g = 0
self.h = 0
self.f = 0
self.parent = None
def astar(grid, start, end):
open_list = []
closed_list = []
start_node = Node(*start)
end_node = Node(*end)
open_list.append(start)
while open_list:
current = min(open_list, key=lambda n: n.f)
open_list.remove(current)
closed_list.append(current)
if current.x == end_node.x and current.y == end_node.y:
path = []
while current:
path.append((current.x, current.y))
current = current.parent
return path[::-1]
neighbors = [(-1,0),(1,0),(0,-1),(0,1)]
for dx, dy in neighbors:
nx, ny = current.x + dx, current.y + dy
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == 0:
neighbor = Node(nx, ny)
if neighbor in closed_list:
continue
neighbor.g = current.g + 1
neighbor.h = math.hypot(nx-end_node.x, ny-end_node.y)
neighbor.f = neighbor.g + neighbor.h
neighbor.parent = current
if neighbor not in open_list:
open_list.append(neighbor)
return None
配套工程包:webots-path-planning.zip
实战 4 Webots + ROS2 SLAM 建图导航
功能模块
部署命令
bash
运行
git clone https://github.com/cyberbotics/webots_ros2.git
cd webots_ros2
colcon build
source install/setup.bash
ros2 launch webots_ros2_epuck rats_life_launch.py
配套工程包:webots_ros2.zip
九、教程总结
本文完整覆盖 Webots 基础操作、仿真开发流程、性能调优方案,四大实战案例覆盖机器人感知、运动控制、路径规划、SLAM 建图四大核心方向,全部工程与源码整合网盘,可直接导入复现,适用于课程设计、毕业设计、机器人项目开发学习。


