【从0开始自动驾驶】用python做一个简单的自动驾驶仿真可视化界面

【从0开始自动驾驶】用python做一个简单的自动驾驶仿真可视化界面

废话几句

废话不多说,直接上源码

目录结构

init.py

python 复制代码
import json
import copy


def import_veh_cfg(veh_cfg_path):
    with open(veh_cfg_path, "r", encoding="utf-8") as load_f:
        veh_cfg_ori = json.load(load_f)
        return veh_cfg_ori


def init_veh_cfg(veh_cfg_path):
    veh_cfg_ori = import_veh_cfg(veh_cfg_path)
    # print(veh_cfg_ori)
    veh_cfg = copy.deepcopy(veh_cfg_ori)
    return veh_cfg

simulator.py

  • 使用class编写的仿真器
  • 输入为车辆x、y、yaw,进行可视化
  • 后段接入运动学等函数可随意进行扩展
python 复制代码
import numpy
import matplotlib.pyplot as plt


class simulator:
    def init_simulator(self, veh_cfg):
        plt.ion()
        self.veh_cfg = veh_cfg

    def draws(self, x, y, yaw, xmin, xmax, ymin, ymax):
        self.veh_x = x
        self.veh_y = y
        self.veh_yaw = yaw  # 角度

        plt.clf()  # 清除之前画的图
        plt.xlim(xmin, xmax)
        plt.ylim(ymin, ymax)
        plt.title("simulator")
        plt.xlabel("X/m")
        plt.ylabel("Y/m")
        ax = plt.gca()
        ax.set_aspect(1)  # 保持纵横比

        self.draw_veh()
        plt.pause(0.001)

    def draws_close(self):
        plt.ioff()

    def draw_veh(self):  # yaw以x轴为0,逆时针为正

        plt.plot(self.veh_x, self.veh_y, "o", color="r")
        self.ca_veh_points()
        plt.plot(self.veh_x_points, self.veh_y_points, color="r")

    def ca_veh_points(self):  # 计算车辆包络框的所有点
        half_veh_width = self.veh_cfg["width"] / 2
        self.veh_yaw_rad = numpy.deg2rad(self.veh_yaw)

        self.veh_x_points = [
            self.veh_x
            + (self.veh_cfg["length"] - self.veh_cfg["rear_overhang"])
            * numpy.cos(self.veh_yaw_rad)
            + half_veh_width * numpy.sin(self.veh_yaw_rad),
            self.veh_x
            + (self.veh_cfg["length"] - self.veh_cfg["rear_overhang"])
            * numpy.cos(self.veh_yaw_rad)
            - half_veh_width * numpy.sin(self.veh_yaw_rad),
            self.veh_x
            - (self.veh_cfg["rear_overhang"]) * numpy.cos(self.veh_yaw_rad)
            - half_veh_width * numpy.sin(self.veh_yaw_rad),
            self.veh_x
            - (self.veh_cfg["rear_overhang"]) * numpy.cos(self.veh_yaw_rad)
            + half_veh_width * numpy.sin(self.veh_yaw_rad),
            self.veh_x
            + (self.veh_cfg["length"] - self.veh_cfg["rear_overhang"])
            * numpy.cos(self.veh_yaw_rad)
            + half_veh_width * numpy.sin(self.veh_yaw_rad),
        ]
        self.veh_y_points = [
            self.veh_y
            + (self.veh_cfg["length"] - self.veh_cfg["rear_overhang"])
            * numpy.sin(self.veh_yaw_rad)
            - half_veh_width * numpy.cos(self.veh_yaw_rad),
            self.veh_y
            + (self.veh_cfg["length"] - self.veh_cfg["rear_overhang"])
            * numpy.sin(self.veh_yaw_rad)
            + half_veh_width * numpy.cos(self.veh_yaw_rad),
            self.veh_y
            - (self.veh_cfg["rear_overhang"]) * numpy.sin(self.veh_yaw_rad)
            + half_veh_width * numpy.cos(self.veh_yaw_rad),
            self.veh_y
            - (self.veh_cfg["rear_overhang"]) * numpy.sin(self.veh_yaw_rad)
            - half_veh_width * numpy.cos(self.veh_yaw_rad),
            self.veh_y
            + (self.veh_cfg["length"] - self.veh_cfg["rear_overhang"])
            * numpy.sin(self.veh_yaw_rad)
            - half_veh_width * numpy.cos(self.veh_yaw_rad),
        ]

simple_simulator_app.py

  • 主函数入口
python 复制代码
import time

from lib.init import *
from lib.simulator import *

if __name__ == "__main__":
    
    veh_cfg_path = r"./config/vehicle_config.json"

    veh_cfg = init_veh_cfg(veh_cfg_path)

    simulator_ = simulator()
    simulator_.init_simulator(veh_cfg)

    for i in range(0, 20):
        simulator_.draws(
            i, 15 + i * 0.3, 30 + i, 0, 30, 0, 30
        )  # veh_x, veh_y, veh_yaw, xmin, xmax, ymin, ymax
        time.sleep(0.1)

    plt.pause(1000)  # 暂停几秒看一看结果

vehicle_config.json

-车辆配置文件

yaml 复制代码
{
    "vehicle_type": "test",
    "front_wheel_base": 1.3,
    "rear_wheel_base": 1.3,
    "width": 1.9,
    "length": 4,
    "rear_overhang": 0.4,
    "max_steer_wheel_angle": 35.0,
    "steer_ratio": 17.5
}
相关推荐
訾博ZiBo4 分钟前
AI日报 - 2025年3月7日
人工智能
梓羽玩Python7 分钟前
一夜刷屏AI圈!Manus:这不是聊天机器人,是你的“AI打工仔”!
人工智能
Gene_INNOCENT8 分钟前
大型语言模型训练的三个阶段:Pre-Train、Instruction Fine-tuning、RLHF (PPO / DPO / GRPO)
人工智能·深度学习·语言模型
游戏智眼8 分钟前
中国团队发布通用型AI Agent产品Manus;GPT-4.5正式面向Plus用户推出;阿里发布并开源推理模型通义千问QwQ-32B...|游戏智眼日报
人工智能·游戏·游戏引擎·aigc
挣扎与觉醒中的技术人10 分钟前
如何优化FFmpeg拉流性能及避坑指南
人工智能·深度学习·性能优化·ffmpeg·aigc·ai编程
君秋水10 分钟前
FastAPI教程:20个核心概念从入门到 happy使用
后端·python·程序员
watersink13 分钟前
Dify框架下的基于RAG流程的政务检索平台
人工智能·深度学习·机器学习
脑极体17 分钟前
在MWC2025,读懂华为如何以行践言
大数据·人工智能·华为
DeepBI19 分钟前
AI+大数据:DeepBI重构竞品分析新思路
人工智能
KoiC21 分钟前
内网环境部署Deepseek+Dify,构建企业私有化AI应用
linux·人工智能·ubuntu·docker·大模型·ai应用·deepseek