机器人定位
核心知识点讲解
机器人定位的基本概念
机器人定位是指机器人确定自身在环境中位置和姿态的过程。它是机器人导航、操作和交互的基础,对于自主移动机器人尤为重要。定位问题可以分为以下几类:
全局定位:机器人从完全未知的初始位置开始,确定自己在全局坐标系中的位置。
位姿跟踪:机器人在已知初始位置的情况下,持续跟踪自身位置和姿态的变化。
绑架问题:机器人在运行过程中被移动到未知位置,需要重新确定自身位置。
定位系统的组成部分
一个完整的机器人定位系统通常包括以下几个主要组成部分:
传感器:获取环境信息和机器人状态信息,如里程计、GPS、激光雷达、摄像头等。
感知算法:处理传感器数据,提取有用信息,如特征提取、数据关联等。
状态估计器:根据感知信息和运动模型,估计机器人的位置和姿态,如卡尔曼滤波器、粒子滤波器等。
地图:表示环境的空间信息,如栅格地图、特征地图等。
定位方法分类
根据定位所需的环境信息,机器人定位方法可以分为以下几类:
基于外部参考的定位:使用外部参考系统(如GPS)确定机器人位置。
基于环境模型的定位:使用预先构建的环境地图进行定位,如地图匹配定位。
同时定位与地图构建(SLAM):在未知环境中,同时进行定位和地图构建。
基于信标的定位:使用环境中已知位置的信标进行定位,如RFID、二维码等。
常用传感器
机器人定位中常用的传感器包括:
里程计(Odometry):通过测量机器人轮子的旋转来估计位置变化,包括编码器和惯性测量单元(IMU)。
GPS(全球定位系统):通过接收卫星信号来确定全球位置,适用于室外环境。
激光雷达(LiDAR):通过发射激光并测量反射时间来获取环境的距离信息,适用于室内和室外环境。
摄像头(Camera):通过拍摄图像来获取环境信息,包括单目相机、双目相机和RGB-D相机。
超声波传感器:通过发射超声波并测量反射时间来获取近距离的距离信息,适用于室内环境。
红外传感器:通过发射红外光并测量反射强度来获取近距离的距离信息,适用于室内环境。
定位算法
常用的机器人定位算法包括:
卡尔曼滤波器(Kalman Filter):适用于线性系统的状态估计,包括扩展卡尔曼滤波器(EKF)和无迹卡尔曼滤波器(UKF)。
粒子滤波器(Particle Filter):适用于非线性、非高斯系统的状态估计,也称为蒙特卡洛定位(MCL)。
扫描匹配算法:通过匹配激光雷达扫描数据与地图来估计位置,如ICP(迭代最近点)算法。
视觉SLAM算法:通过处理相机图像来同时进行定位和地图构建,如ORB-SLAM、DSO等。
激光SLAM算法:通过处理激光雷达数据来同时进行定位和地图构建,如GMapping、Hector SLAM等。
定位精度评估
评估机器人定位系统性能的指标包括:
定位误差:估计位置与真实位置之间的差异,通常用均方根误差(RMSE)表示。
定位鲁棒性:在各种环境条件下保持定位精度的能力。
定位更新率:定位系统能够提供位置估计的频率。
计算复杂度:定位算法的计算资源需求。
初始化时间:定位系统从启动到获得有效位置估计所需的时间。
实用案例分析
基于里程计的定位
问题描述
使用编码器和IMU数据实现机器人的位姿跟踪。
解决方案
import numpy as np
class OdometryLocalization:
def __init__(self):
"""
初始化里程计定位系统
"""
# 初始位姿 [x, y, theta]
self.pose = np.array([0.0, 0.0, 0.0])
def update_with_encoder(self, left_wheel_delta, right_wheel_delta, wheel_radius, wheel_base):
"""
使用编码器数据更新位姿
left_wheel_delta: 左轮旋转增量(弧度)
right_wheel_delta: 右轮旋转增量(弧度)
wheel_radius: 车轮半径(米)
wheel_base: 轮距(米)
"""
# 计算左右轮移动距离
left_distance = wheel_radius * left_wheel_delta
right_distance = wheel_radius * right_wheel_delta
# 计算机器人移动距离和转向角
distance = (left_distance + right_distance) / 2.0
delta_theta = (right_distance - left_distance) / wheel_base
# 更新位姿
self.pose[0] += distance * np.cos(self.pose[2] + delta_theta / 2.0)
self.pose[1] += distance * np.sin(self.pose[2] + delta_theta / 2.0)
self.pose[2] += delta_theta
# 保持角度在 [-pi, pi] 范围内
self.pose[2] = np.arctan2(np.sin(self.pose[2]), np.cos(self.pose[2]))
def update_with_imu(self, accelerometer, gyroscope, dt):
"""
使用IMU数据更新位姿
accelerometer: 加速度计数据 [ax, ay, az](m/s^2)
gyroscope: 陀螺仪数据 [wx, wy, wz](rad/s)
dt: 时间步长(秒)
"""
# 从陀螺仪获取角速度
wx, wy, wz = gyroscope
# 更新姿态(简化模型,只考虑z轴旋转)
self.pose[2] += wz * dt
# 保持角度在 [-pi, pi] 范围内
self.pose[2] = np.arctan2(np.sin(self.pose[2]), np.cos(self.pose[2]))
# 从加速度计获取线加速度(简化模型,只考虑x轴加速度)
ax = accelerometer[0]
# 更新位置(简化模型,只考虑x轴移动)
self.pose[0] += 0.5 * ax * dt**2 * np.cos(self.pose[2])
self.pose[1] += 0.5 * ax * dt**2 * np.sin(self.pose[2])
def get_pose(self):
"""
获取当前位姿
返回: [x, y, theta]
"""
return self.pose.copy()
# 示例:使用里程计定位
def main():
# 初始化定位系统
localization = OdometryLocalization()
# 模拟机器人运动
wheel_radius = 0.1 # 车轮半径(米)
wheel_base = 0.5 # 轮距(米)
dt = 0.1 # 时间步长(秒)
# 模拟直线运动
print("模拟直线运动...")
for i in range(10):
# 左轮和右轮以相同速度旋转
left_wheel_delta = 0.1 # 弧度
right_wheel_delta = 0.1 # 弧度
# 更新位姿
localization.update_with_encoder(left_wheel_delta, right_wheel_delta, wheel_radius, wheel_base)
# 打印当前位姿
pose = localization.get_pose()
print(f"时间步 {i+1}: 位置=({pose[0]:.2f}, {pose[1]:.2f}), 姿态={np.degrees(pose[2]):.2f}度")
# 模拟转弯运动
print("\n模拟转弯运动...")
for i in range(10):
# 右轮比左轮旋转更快
left_wheel_delta = 0.05 # 弧度
right_wheel_delta = 0.15 # 弧度
# 更新位姿
localization.update_with_encoder(left_wheel_delta, right_wheel_delta, wheel_radius, wheel_base)
# 打印当前位姿
pose = localization.get_pose()
print(f"时间步 {i+1}: 位置=({pose[0]:.2f}, {pose[1]:.2f}), 姿态={np.degrees(pose[2]):.2f}度")
if __name__ == "__main__":
main()基于GPS的定位
问题描述
使用GPS模块获取机器人在室外环境中的全局位置。
解决方案
import numpy as np
import time
class GPSLocalization:
def __init__(self):
"""
初始化GPS定位系统
"""
# 初始位置 [纬度, 经度, 高度]
self.position = np.array([0.0, 0.0, 0.0])
# 定位精度(米)
self.accuracy = 0.0
# 时间戳
self.timestamp = 0
def update(self, gps_data):
"""
使用GPS数据更新位置
gps_data: GPS模块返回的数据,格式为 [纬度, 经度, 高度, 精度, 时间戳]
"""
latitude, longitude, altitude, accuracy, timestamp = gps_data
# 更新位置
self.position = np.array([latitude, longitude, altitude])
# 更新精度
self.accuracy = accuracy
# 更新时间戳
self.timestamp = timestamp
def get_position(self):
"""
获取当前位置
返回: [纬度, 经度, 高度]
"""
return self.position.copy()
def get_accuracy(self):
"""
获取当前定位精度
返回: 精度(米)
"""
return self.accuracy
def get_timestamp(self):
"""
获取当前时间戳
返回: 时间戳
"""
return self.timestamp
def gps_to_local(self, origin_gps):
"""
将GPS坐标转换为局部坐标系
origin_gps: 局部坐标系原点的GPS坐标 [纬度, 经度]
返回: 局部坐标系坐标 [x, y],单位为米
"""
# 地球半径(米)
R = 6371000
# 将角度转换为弧度
lat1, lon1 = np.radians(origin_gps)
lat2, lon2 = np.radians(self.position[:2])
# 计算经纬度差
dlat = lat2 - lat1
dlon = lon2 - lon1
# 计算局部坐标
x = R * dlon * np.cos(lat1)
y = R * dlat
return np.array([x, y])
# 模拟GPS模块
def simulate_gps():
"""
模拟GPS模块返回的数据
返回: [纬度, 经度, 高度, 精度, 时间戳]
"""
# 模拟一个移动轨迹
t = time.time() % 100 # 时间变量
# 基础位置(北京天安门)
base_latitude = 39.9042
base_longitude = 116.4074
# 模拟移动
delta_latitude = 0.0001 * np.sin(t * 0.1)
delta_longitude = 0.0001 * np.cos(t * 0.1)
# 添加一些噪声
noise_latitude = 0.00001 * np.random.randn()
noise_longitude = 0.00001 * np.random.randn()
# 计算当前位置
latitude = base_latitude + delta_latitude + noise_latitude
longitude = base_longitude + delta_longitude + noise_longitude
altitude = 50.0 # 固定高度
# 模拟精度
accuracy = 3.0 + 0.5 * np.random.randn()
accuracy = max(1.0, accuracy) # 确保精度为正
# 时间戳
timestamp = time.time()
return [latitude, longitude, altitude, accuracy, timestamp]
# 示例:使用GPS定位
def main():
# 初始化GPS定位系统
gps_localization = GPSLocalization()
# 局部坐标系原点(初始位置)
origin_gps = [39.9042, 116.4074]
# 模拟GPS数据更新
print("模拟GPS定位...")
for i in range(20):
# 获取模拟的GPS数据
gps_data = simulate_gps()
# 更新定位
gps_localization.update(gps_data)
# 获取当前位置
position = gps_localization.get_position()
# 获取定位精度
accuracy = gps_localization.get_accuracy()
# 转换为局部坐标系
local_position = gps_localization.gps_to_local(origin_gps)
# 打印结果
print(f"时间步 {i+1}:")
print(f" GPS位置: 纬度={position[0]:.6f}, 经度={position[1]:.6f}, 高度={position[2]:.2f}米")
print(f" 定位精度: {accuracy:.2f}米")
print(f" 局部坐标: x={local_position[0]:.2f}米, y={local_position[1]:.2f}米")
print()
# 等待一段时间
time.sleep(0.5)
if __name__ == "__main__":
main()基于激光雷达的SLAM定位
问题描述
使用激光雷达实现同时定位与地图构建(SLAM),适用于室内环境。
解决方案
import numpy as np
import matplotlib.pyplot as plt
class LaserSLAM:
def __init__(self, map_resolution=0.05, map_size=(200, 200)):
"""
初始化激光SLAM系统
map_resolution: 地图分辨率(米/栅格)
map_size: 地图大小(栅格数),格式为 (宽度, 高度)
"""
self.map_resolution = map_resolution
self.map_size = map_size
# 初始化占据栅格地图
self.map = np.zeros(map_size)
# 初始位姿 [x, y, theta](米,米,弧度)
self.pose = np.array([map_size[0] * map_resolution / 2,
map_size[1] * map_resolution / 2, 0.0])
# 激光雷达参数
self.laser_max_range = 5.0 # 最大测量范围(米)
self.laser_fov = 270.0 # 视野角度(度)
self.laser_num_beams = 180 # 激光束数量
# 激光雷达角度增量
self.laser_angle_increment = np.radians(self.laser_fov) / (self.laser_num_beams - 1)
def update(self, laser_scan, odometry_delta):
"""
更新SLAM系统
laser_scan: 激光雷达扫描数据,格式为 [距离1, 距离2, ..., 距离N]
odometry_delta: 里程计预测的位姿变化,格式为 [dx, dy, dtheta]
"""
# 1. 预测:使用里程计更新位姿
self.pose += odometry_delta
# 保持角度在 [-pi, pi] 范围内
self.pose[2] = np.arctan2(np.sin(self.pose[2]), np.cos(self.pose[2]))
# 2. 更新:使用激光雷达数据更新地图和位姿
self.update_map(laser_scan)
def update_map(self, laser_scan):
"""
使用激光雷达数据更新地图
laser_scan: 激光雷达扫描数据
"""
# 获取机器人在地图中的位置(栅格坐标)
robot_grid = self.meters_to_grid(self.pose[:2])
# 处理每个激光束
for i in range(self.laser_num_beams):
# 计算激光束的角度
angle = self.pose[2] - np.radians(self.laser_fov) / 2 + i * self.laser_angle_increment
# 获取激光测量距离
distance = laser_scan[i]
# 限制距离在有效范围内
distance = min(distance, self.laser_max_range)
# 计算激光击中点的位置(米)
hit_x = self.pose[0] + distance * np.cos(angle)
hit_y = self.pose[1] + distance * np.sin(angle)
# 转换为栅格坐标
hit_grid = self.meters_to_grid(np.array([hit_x, hit_y]))
# 绘制激光束(更新地图)
self.draw_laser_beam(robot_grid, hit_grid)
def draw_laser_beam(self, start_grid, end_grid):
"""
在地图上绘制激光束
start_grid: 激光雷达位置(栅格坐标)
end_grid: 激光击中点位置(栅格坐标)
"""
# 使用Bresenham算法绘制线段
x0, y0 = start_grid
x1, y1 = end_grid
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
err = dx - dy
x, y = x0, y0
while True:
# 检查是否在地图范围内
if 0 <= x < self.map_size[0] and 0 <= y < self.map_size[1]:
# 激光束经过的点标记为可通行(值减小)
self.map[int(y), int(x)] -= 0.1
self.map[int(y), int(x)] = max(-1.0, self.map[int(y), int(x)])
# 如果到达终点,退出循环
if x == x1 and y == y1:
break
# 计算误差
e2 = 2 * err
if e2 > -dy:
err -= dy
x += sx
if e2 < dx:
err += dx
y += sy
# 激光击中点标记为障碍物(值增加)
if 0 <= x1 < self.map_size[0] and 0 <= y1 < self.map_size[1]:
self.map[int(y1), int(x1)] += 0.5
self.map[int(y1), int(x1)] = min(1.0, self.map[int(y1), int(x1)])
def meters_to_grid(self, position):
"""
将米坐标转换为栅格坐标
position: 米坐标 [x, y]
返回: 栅格坐标 [x, y]
"""
return position / self.map_resolution
def grid_to_meters(self, grid):
"""
将栅格坐标转换为米坐标
grid: 栅格坐标 [x, y]
返回: 米坐标 [x, y]
"""
return grid * self.map_resolution
def get_pose(self):
"""
获取当前位姿
返回: [x, y, theta]
"""
return self.pose.copy()
def get_map(self):
"""
获取当前地图
返回: 占据栅格地图
"""
return self.map.copy()
def visualize(self):
"""
可视化SLAM结果
"""
plt.figure(figsize=(10, 10))
# 绘制地图
plt.imshow(self.map, cmap='gray', origin='lower')
# 绘制机器人位置
robot_grid = self.meters_to_grid(self.pose[:2])
plt.plot(robot_grid[0], robot_grid[1], 'ro', markersize=10)
# 绘制机器人方向
direction_x = robot_grid[0] + 10 * np.cos(self.pose[2])
direction_y = robot_grid[1] + 10 * np.sin(self.pose[2])
plt.plot([robot_grid[0], direction_x], [robot_grid[1], direction_y], 'r-', linewidth=2)
# 设置标题和坐标轴
plt.title('SLAM 地图和机器人位置')
plt.xlabel('X (栅格)')
plt.ylabel('Y (栅格)')
plt.show()
# 模拟激光雷达扫描
def simulate_laser_scan(pose, map, map_resolution):
"""
模拟激光雷达扫描数据
pose: 机器人位姿 [x, y, theta]
map: 占据栅格地图
map_resolution: 地图分辨率
返回: 激光雷达扫描数据
"""
# 激光雷达参数
laser_max_range = 5.0
laser_fov = 270.0
laser_num_beams = 180
laser_angle_increment = np.radians(laser_fov) / (laser_num_beams - 1)
# 初始化扫描数据
laser_scan = []
# 机器人栅格坐标
robot_grid = pose[:2] / map_resolution
# 模拟每个激光束
for i in range(laser_num_beams):
# 计算激光束的角度
angle = pose[2] - np.radians(laser_fov) / 2 + i * laser_angle_increment
# 模拟激光束发射
distance = 0.0
hit = False
# 逐步增加距离,直到击中障碍物或达到最大范围
while distance < laser_max_range and not hit:
distance += 0.05 # 步长
# 计算当前距离的点
x = pose[0] + distance * np.cos(angle)
y = pose[1] + distance * np.sin(angle)
# 转换为栅格坐标
grid_x = int(x / map_resolution)
grid_y = int(y / map_resolution)
# 检查是否在地图范围内
if 0 <= grid_x < map.shape[1] and 0 <= grid_y < map.shape[0]:
# 检查是否击中障碍物
if map[grid_y, grid_x] > 0.5:
hit = True
else:
# 超出地图范围
break
# 如果击中障碍物,记录距离;否则记录最大范围
if hit:
laser_scan.append(distance)
else:
laser_scan.append(laser_max_range)
return laser_scan
# 示例:使用激光SLAM
def main():
# 初始化SLAM系统
slam = LaserSLAM()
# 模拟环境地图(一个简单的房间)
map_size = slam.map.shape
map_resolution = slam.map_resolution
# 创建一个方形房间
room_size = 10.0 # 房间大小(米)
room_grid_size = int(room_size / map_resolution)
room_center_x = map_size[0] // 2
room_center_y = map_size[1] // 2
room_half_size = room_grid_size // 2
# 在地图上标记墙壁
for i in range(map_size[0]):
for j in range(map_size[1]):
if (abs(i - room_center_y) == room_half_size or
abs(j - room_center_x) == room_half_size):
slam.map[i, j] = 1.0
# 模拟机器人运动和SLAM过程
print("模拟激光SLAM...")
for i in range(50):
# 模拟机器人移动(圆形轨迹)
dt = 0.1
radius = 3.0
omega = 0.1
v = radius * omega
# 计算位姿变化
dx = v * dt * np.cos(slam.pose[2])
dy = v * dt * np.sin(slam.pose[2])
dtheta = omega * dt
odometry_delta = np.array([dx, dy, dtheta])
# 模拟激光雷达扫描
laser_scan = simulate_laser_scan(slam.pose, slam.map, map_resolution)
# 更新SLAM系统
slam.update(laser_scan, odometry_delta)
# 可视化结果
slam.visualize()
if __name__ == "__main__":
main()基于视觉的定位
问题描述
使用单目相机实现机器人在室内环境中的定位。
解决方案
import numpy as np
import cv2
import matplotlib.pyplot as plt
class VisualLocalization:
def __init__(self):
"""
初始化视觉定位系统
"""
# 相机参数
self.camera_matrix = np.array([[500, 0, 320],
[0, 500, 240],
[0, 0, 1]]) # 内参矩阵
self.dist_coeffs = np.zeros(5) # 畸变系数
# 特征提取器
self.feature_detector = cv2.ORB_create(nfeatures=1000)
# 特征匹配器
self.feature_matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# 地图点云
self.map_points = []
# 地图特征描述符
self.map_descriptors = []
# 初始位姿 [x, y, z, roll, pitch, yaw](米,弧度)
self.pose = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
def initialize_map(self, first_frame):
"""
使用第一帧初始化地图
first_frame: 第一帧图像
"""
# 转换为灰度图像
gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)
# 检测特征点
keypoints, descriptors = self.feature_detector.detectAndCompute(gray, None)
# 假设第一帧的位姿为原点
self.pose = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
# 初始化地图(使用深度估计,这里简化为固定深度)
fixed_depth = 1.0 # 假设所有特征点的深度为1米
# 将特征点转换为3D地图点
for kp in keypoints:
# 获取像素坐标
u, v = kp.pt
# 使用相机内参将像素坐标转换为归一化坐标
normalized_point = np.array([(u - self.camera_matrix[0, 2]) / self.camera_matrix[0, 0],
(v - self.camera_matrix[1, 2]) / self.camera_matrix[1, 1],
1.0])
# 计算3D点(假设固定深度)
map_point = normalized_point * fixed_depth
self.map_points.append(map_point)
# 存储特征描述符
self.map_descriptors = descriptors
return keypoints, descriptors
def update(self, current_frame):
"""
更新视觉定位系统
current_frame: 当前帧图像
返回: 是否成功更新
"""
# 转换为灰度图像
gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
# 检测特征点
keypoints, descriptors = self.feature_detector.detectAndCompute(gray, None)
# 如果没有特征点,返回失败
if len(keypoints) == 0:
return False
# 如果地图为空,初始化地图
if len(self.map_points) == 0:
self.initialize_map(current_frame)
return True
# 匹配特征点
matches = self.feature_matcher.match(descriptors, self.map_descriptors)
# 如果匹配点太少,返回失败
if len(matches) < 10:
return False
# 提取匹配点
src_pts = np.float32([keypoints[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([self.map_points[m.trainIdx][:2] for m in matches]).reshape(-1, 1, 2)
# 使用PnP算法估计相机位姿
success, rvec, tvec = cv2.solvePnP(np.array(self.map_points),
src_pts.reshape(-1, 2),
self.camera_matrix,
self.dist_coeffs)
if success:
# 将旋转向量转换为旋转矩阵
R, _ = cv2.Rodrigues(rvec)
# 从旋转矩阵中提取欧拉角(滚转,俯仰,偏航)
roll = np.arctan2(R[2, 1], R[2, 2])
pitch = np.arctan2(-R[2, 0], np.sqrt(R[2, 1]**2 + R[2, 2]**2))
yaw = np.arctan2(R[1, 0], R[0, 0])
# 更新位姿
self.pose = np.array([tvec[0, 0], tvec[1, 0], tvec[2, 0], roll, pitch, yaw])
return True
else:
return False
def get_pose(self):
"""
获取当前位姿
返回: [x, y, z, roll, pitch, yaw]
"""
return self.pose.copy()
def visualize(self, frame, keypoints=None, matches=None):
"""
可视化视觉定位结果
frame: 当前帧图像
keypoints: 检测到的特征点
matches: 特征匹配结果
"""
# 绘制特征点
if keypoints is not None:
frame = cv2.drawKeypoints(frame, keypoints, None, color=(0, 255, 0), flags=0)
# 绘制匹配结果
if matches is not None:
# 这里简化处理,实际应用中需要绘制匹配线
pass
# 显示位姿信息
pose = self.get_pose()
cv2.putText(frame, f"Position: ({pose[0]:.2f}, {pose[1]:.2f}, {pose[2]:.2f})",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, f"Orientation: ({np.degrees(pose[3]):.2f}, {np.degrees(pose[4]):.2f}, {np.degrees(pose[5]):.2f})",
(10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# 显示图像
cv2.imshow('Visual Localization', frame)
cv2.waitKey(1)
# 模拟相机帧
def simulate_camera(pose, map_points, camera_matrix):
"""
模拟相机捕获的帧
pose: 相机位姿 [x, y, z, roll, pitch, yaw]
map_points: 地图点云
camera_matrix: 相机内参矩阵
返回: 模拟的相机帧
"""
# 创建一个空白图像
frame = np.zeros((480, 640, 3), dtype=np.uint8)
# 从位姿创建旋转矩阵和平移向量
roll, pitch, yaw = pose[3], pose[4], pose[5]
# 计算旋转矩阵
R_x = np.array([[1, 0, 0],
[0, np.cos(roll), -np.sin(roll)],
[0, np.sin(roll), np.cos(roll)]])
R_y = np.array([[np.cos(pitch), 0, np.sin(pitch)],
[0, 1, 0],
[-np.sin(pitch), 0, np.cos(pitch)]])
R_z = np.array([[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[0, 0, 1]])
R = R_z @ R_y @ R_x
t = pose[:3].reshape(3, 1)
# 投影地图点到相机平面
for point in map_points:
# 转换为齐次坐标
point_homogeneous = np.array([point[0], point[1], point[2], 1]).reshape(4, 1)
# 构建相机变换矩阵
T = np.hstack((R, t))
T = np.vstack((T, [0, 0, 0, 1]))
# 计算点在相机坐标系中的位置
point_camera = T @ point_homogeneous
# 检查点是否在相机前方
if point_camera[2] > 0:
# 投影到图像平面
point_image = camera_matrix @ point_camera[:3]
point_image /= point_image[2]
# 检查点是否在图像范围内
if 0 <= point_image[0] < 640 and 0 <= point_image[1] < 480:
# 在图像上绘制点
cv2.circle(frame, (int(point_image[0]), int(point_image[1])), 2, (0, 255, 0), -1)
return frame
# 示例:使用视觉定位
def main():
# 初始化视觉定位系统
visual_localization = VisualLocalization()
# 创建一个简单的地图(立方体)
map_points = []
cube_size = 1.0
# 立方体的8个顶点
cube_vertices = [
[-cube_size/2, -cube_size/2, 1.0],
[cube_size/2, -cube_size/2, 1.0],
[cube_size/2, cube_size/2, 1.0],
[-cube_size/2, cube_size/2, 1.0],
[-cube_size/2, -cube_size/2, 2.0],
[cube_size/2, -cube_size/2, 2.0],
[cube_size/2, cube_size/2, 2.0],
[-cube_size/2, cube_size/2, 2.0]
]
# 添加立方体的边
edges = [
[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]
]
# 沿着边均匀采样点
for edge in edges:
start = np.array(cube_vertices[edge[0]])
end = np.array(cube_vertices[edge[1]])
for i in range(20):
t = i / 19.0
point = start + t * (end - start)
map_points.append(point)
# 存储地图点
visual_localization.map_points = map_points
# 初始化特征描述符(简化处理)
visual_localization.map_descriptors = np.zeros((len(map_points), 32), dtype=np.uint8)
# 模拟相机运动和视觉定位过程
print("模拟视觉定位...")
for i in range(100):
# 模拟相机沿圆形轨迹移动
t = i / 10.0
radius = 1.0
x = radius * np.cos(t)
y = radius * np.sin(t)
z = 0.0
yaw = t + np.pi/2 # 相机朝向圆心
# 更新相机位姿
pose = np.array([x, y, z, 0.0, 0.0, yaw])
# 模拟相机帧
frame = simulate_camera(pose, map_points, visual_localization.camera_matrix)
# 更新视觉定位系统
visual_localization.update(frame)
# 可视化结果
visual_localization.visualize(frame)
# 关闭所有窗口
cv2.destroyAllWindows()
if __name__ == "__main__":
main()多传感器融合定位
问题描述
融合里程计、GPS和激光雷达数据,实现更准确的机器人定位。
解决方案
import numpy as np
from scipy.linalg import inv
class SensorFusionLocalization:
def __init__(self):
"""
初始化多传感器融合定位系统
"""
# 状态向量 [x, y, theta, v, omega](米,米,弧度,米/秒,弧度/秒)
self.state = np.array([0.0, 0.0, 0.0, 0.0, 0.0])
# 状态 covariance 矩阵
self.covariance = np.diag([1.0, 1.0, 0.1, 0.1, 0.01])
# 时间步长
self.dt = 0.1
# 传感器噪声 covariance 矩阵
self.odom_noise = np.diag([0.01, 0.01, 0.001]) # 里程计噪声
self.gps_noise = np.diag([0.1, 0.1]) # GPS噪声
self.laser_noise = np.diag([0.05, 0.05, 0.01]) # 激光雷达噪声
def predict(self, odometry_data):
"""
使用里程计数据预测状态
odometry_data: 里程计数据 [vx, vy, omega](米/秒,米/秒,弧度/秒)
"""
# 提取状态变量
x, y, theta, v, omega = self.state
# 提取里程计数据
vx, vy, omega_odom = odometry_data
# 更新速度和角速度
v = np.sqrt(vx**2 + vy**2)
omega = omega_odom
# 预测新状态
if abs(omega) < 0.001:
# 直线运动
x_new = x + v * np.cos(theta) * self.dt
y_new = y + v * np.sin(theta) * self.dt
theta_new = theta
else:
# 圆弧运动
x_new = x + (v / omega) * (np.sin(theta + omega * self.dt) - np.sin(theta))
y_new = y + (v / omega) * (np.cos(theta) - np.cos(theta + omega * self.dt))
theta_new = theta + omega * self.dt
# 构建状态转移矩阵
F = np.eye(5)
F[0, 2] = -v * np.sin(theta) * self.dt
F[0, 3] = np.cos(theta) * self.dt
F[1, 2] = v * np.cos(theta) * self.dt
F[1, 3] = np.sin(theta) * self.dt
F[2, 4] = self.dt
# 构建过程噪声矩阵
L = np.zeros((5, 3))
L[3, 0] = 1.0
L[3, 1] = 1.0
L[4, 2] = 1.0
# 预测 covariance 矩阵
Q = L @ self.odom_noise @ L.T
self.covariance = F @ self.covariance @ F.T + Q
# 更新状态
self.state = np.array([x_new, y_new, theta_new, v, omega])
# 保持角度在 [-pi, pi] 范围内
self.state[2] = np.arctan2(np.sin(self.state[2]), np.cos(self.state[2]))
def update_gps(self, gps_data):
"""
使用GPS数据更新状态
gps_data: GPS数据 [x, y](米,米)
"""
# 提取状态变量
x, y, theta, v, omega = self.state
# 观测模型
H = np.zeros((2, 5))
H[0, 0] = 1.0
H[1, 1] = 1.0
# 预测观测
z_pred = H @ self.state
# 观测残差
y_residual = gps_data - z_pred
# 计算 Kalman 增益
S = H @ self.covariance @ H.T + self.gps_noise
K = self.covariance @ H.T @ inv(S)
# 更新状态
self.state = self.state + K @ y_residual
# 保持角度在 [-pi, pi] 范围内
self.state[2] = np.arctan2(np.sin(self.state[2]), np.cos(self.state[2]))
# 更新 covariance 矩阵
I = np.eye(5)
self.covariance = (I - K @ H) @ self.covariance
def update_laser(self, laser_data):
"""
使用激光雷达数据更新状态
laser_data: 激光雷达数据 [x, y, theta](米,米,弧度)
"""
# 提取状态变量
x, y, theta, v, omega = self.state
# 观测模型
H = np.zeros((3, 5))
H[0, 0] = 1.0
H[1, 1] = 1.0
H[2, 2] = 1.0
# 预测观测
z_pred = H @ self.state
# 观测残差
y_residual = laser_data - z_pred
# 保持角度残差在 [-pi, pi] 范围内
y_residual[2] = np.arctan2(np.sin(y_residual[2]), np.cos(y_residual[2]))
# 计算 Kalman 增益
S = H @ self.covariance @ H.T + self.laser_noise
K = self.covariance @ H.T @ inv(S)
# 更新状态
self.state = self.state + K @ y_residual
# 保持角度在 [-pi, pi] 范围内
self.state[2] = np.arctan2(np.sin(self.state[2]), np.cos(self.state[2]))
# 更新 covariance 矩阵
I = np.eye(5)
self.covariance = (I - K @ H) @ self.covariance
def get_pose(self):
"""
获取当前位姿
返回: [x, y, theta]
"""
return self.state[:3].copy()
def get_velocity(self):
"""
获取当前速度
返回: [v, omega]
"""
return self.state[3:].copy()
def get_covariance(self):
"""
获取当前 covariance 矩阵
返回: covariance 矩阵
"""
return self.covariance.copy()
# 模拟传感器数据
def simulate_sensors(ground_truth_pose, ground_truth_velocity):
"""
模拟传感器数据
ground_truth_pose: 真实位姿 [x, y, theta]
ground_truth_velocity: 真实速度 [v, omega]
返回: 里程计数据, GPS数据, 激光雷达数据
"""
# 模拟里程计数据(添加噪声)
vx = ground_truth_velocity[0] * np.cos(ground_truth_pose[2])
vy = ground_truth_velocity[0] * np.sin(ground_truth_pose[2])
omega = ground_truth_velocity[1]
odom_data = np.array([vx, vy, omega]) + np.random.multivariate_normal([0, 0, 0], np.diag([0.01, 0.01, 0.001]))
# 模拟GPS数据(添加噪声)
gps_data = ground_truth_pose[:2] + np.random.multivariate_normal([0, 0], np.diag([0.1, 0.1]))
# 模拟激光雷达数据(添加噪声)
laser_data = ground_truth_pose + np.random.multivariate_normal([0, 0, 0], np.diag([0.05, 0.05, 0.01]))
# 保持角度在 [-pi, pi] 范围内
laser_data[2] = np.arctan2(np.sin(laser_data[2]), np.cos(laser_data[2]))
return odom_data, gps_data, laser_data
# 示例:使用多传感器融合定位
def main():
# 初始化融合定位系统
fusion_localization = SensorFusionLocalization()
# 真实轨迹(圆形)
radius = 5.0
omega = 0.1
v = radius * omega
dt = fusion_localization.dt
# 存储轨迹
ground_truth_trajectory = []
estimated_trajectory = []
gps_trajectory = []
laser_trajectory = []
# 模拟传感器融合定位过程
print("模拟多传感器融合定位...")
for i in range(200):
# 计算真实位姿
t = i * dt
x = radius * np.cos(omega * t)
y = radius * np.sin(omega * t)
theta = omega * t + np.pi/2 # 相机朝向圆心
ground_truth_pose = np.array([x, y, theta])
ground_truth_velocity = np.array([v, omega])
# 存储真实轨迹
ground_truth_trajectory.append(ground_truth_pose.copy())
# 模拟传感器数据
odom_data, gps_data, laser_data = simulate_sensors(ground_truth_pose, ground_truth_velocity)
# 预测:使用里程计数据
fusion_localization.predict(odom_data)
# 更新:使用GPS数据(每5个时间步更新一次)
if i % 5 == 0:
fusion_localization.update_gps(gps_data)
gps_trajectory.append(gps_data.copy())
# 更新:使用激光雷达数据(每10个时间步更新一次)
if i % 10 == 0:
fusion_localization.update_laser(laser_data)
laser_trajectory.append(laser_data.copy())
# 存储估计轨迹
estimated_trajectory.append(fusion_localization.get_pose().copy())
# 可视化结果
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
# 绘制真实轨迹
ground_truth_x = [p[0] for p in ground_truth_trajectory]
ground_truth_y = [p[1] for p in ground_truth_trajectory]
plt.plot(ground_truth_x, ground_truth_y, 'k-', linewidth=2, label='真实轨迹')
# 绘制估计轨迹
estimated_x = [p[0] for p in estimated_trajectory]
estimated_y = [p[1] for p in estimated_trajectory]
plt.plot(estimated_x, estimated_y, 'r-', linewidth=2, label='估计轨迹')
# 绘制GPS轨迹
if gps_trajectory:
gps_x = [p[0] for p in gps_trajectory]
gps_y = [p[1] for p in gps_trajectory]
plt.plot(gps_x, gps_y, 'g.', markersize=10, label='GPS数据')
# 绘制激光雷达轨迹
if laser_trajectory:
laser_x = [p[0] for p in laser_trajectory]
laser_y = [p[1] for p in laser_trajectory]
plt.plot(laser_x, laser_y, 'b.', markersize=10, label='激光雷达数据')
plt.xlabel('X (米)')
plt.ylabel('Y (米)')
plt.legend()
plt.title('多传感器融合定位')
plt.axis('equal')
plt.grid(True)
plt.show()
if __name__ == "__main__":
main()总结
机器人定位是机器人自主导航的核心技术之一,它使机器人能够确定自身在环境中的位置和姿态。通过本章节的学习,我们了解了:
机器人定位的基本概念:包括全局定位、位姿跟踪和绑架问题。
定位系统的组成部分:包括传感器、感知算法、状态估计器和地图。
定位方法分类:基于外部参考的定位、基于环境模型的定位、同时定位与地图构建(SLAM)和基于信标的定位。
常用传感器:里程计、GPS、激光雷达、摄像头、超声波传感器和红外传感器。
定位算法:卡尔曼滤波器、粒子滤波器、扫描匹配算法、视觉SLAM算法和激光SLAM算法。
定位精度评估:定位误差、定位鲁棒性、定位更新率、计算复杂度和初始化时间。
通过实战示例,我们学习了如何实现基于里程计的定位、基于GPS的定位、基于激光雷达的SLAM定位和多传感器融合定位。这些知识和技能对于开发自主移动机器人具有重要意义。
在实际应用中,机器人定位系统需要根据具体的环境和任务需求选择合适的传感器和算法。随着传感器技术和人工智能的不断发展,机器人定位技术也在不断进步,为自主移动机器人的广泛应用提供了有力支持。