
ACC自适应巡航控制软件使用Carsim2019.0Matlab/Simulink2021a适用场景采用模块化建模方法搭建ACC自适应巡航控制系统适用于弯道和直线行驶场景。包含模块左右轮毂电机模块Radar感知模块PID控制模块等。每个模块的详细情况都可在simulink当中实时查看。输入设定距离、自车与前车状态。传感器雷达检测距离。控制器PID 控制器根据距离误差计算制动力或目标扭矩。执行器/车辆模型根据力矩和制动力更新车辆速度和位置。Python 仿真代码示例这段代码模拟了图中所示的闭环控制逻辑。import numpy as npimport matplotlib.pyplot as pltclass RadarSensor:“”“对应图中的 ‘Radar Sensor’ 模块”“”def detect(self, ego_pos, lead_pos):# 计算相对距离distance lead_pos - ego_posreturn distanceclass PIDController:“”“对应图中的 ‘PID(s)’ 模块”“”def init(self, kp, ki, kd):self.kp kpself.ki kiself.kd kdself.prev_error 0self.integral 0def update(self, setpoint, measured_value, dt): error setpoint - measured_value self.integral error * dt derivative (error - self.prev_error) / dt output self.kp * error self.ki * self.integral self.kd * derivative self.prev_error error return outputclass VehicleDynamics:“”“对应图中的 ‘veh_sf’ (车辆模型) 和 ‘Motor_Left/Right’”“”def init(self):self.pos 0.0self.vel 0.0self.mass 1500.0 # 车辆质量 kgdef update(self, torque, brake_pressure, dt): # 简化的动力学方程: F ma # 驱动力与扭矩成正比阻力与刹车压力成正比 force_drive torque * 2.0 # 简化传动系数 force_brake brake_pressure * 1000.0 net_force force_drive - force_brake acc net_force / self.mass self.vel acc * dt self.vel max(0, self.vel) # 速度不能为负 self.pos self.vel * dt return self.vel, self.pos— 主仿真循环 —初始化模块radar RadarSensor()pid_dist PIDController(kp1.0, ki0.1, kd0.05) # 距离控制PIDpid_vel PIDController(kp0.5, ki0.01, kd0.0) # 速度控制PID (图中右侧部分)ego_car VehicleDynamics()lead_car_pos 100.0 # 假设前车在100米处setup_distance 50.0 # 设定跟车距离time_steps np.arange(0, 20, 0.1) # 仿真20秒log_distance []log_velocity []for t in time_steps:# 1. 传感器检测 (Radar Sensor)current_distance radar.detect(ego_car.pos, lead_car_pos)# 2. 距离控制回路 (左上部分) # 计算距离误差输出目标速度或制动力 # 这里简化逻辑如果距离太近PID输出刹车压力 brake_pressure pid_dist.update(setup_distance, current_distance, 0.1) brake_pressure max(0, brake_pressure) # 刹车压力非负 # 3. 速度/扭矩控制回路 (右侧部分) # 假设有一个目标速度或者直接由距离误差映射到目标扭矩 target_torque 500 # 假设恒定的驱动扭矩需求 # 图中显示左右电机分别控制这里简化为总扭矩 final_torque target_torque - brake_pressure * 10 # 刹车会抵消扭矩 # 4. 车辆更新 (Vehicle Model) current_vel, current_pos ego_car.update(final_torque, brake_pressure, 0.1) # 记录数据 log_distance.append(current_distance) log_velocity.append(current_vel)— 绘图 —plt.figure(figsize(10, 6))plt.subplot(2, 1, 1)plt.plot(time_steps, log_distance, label‘Distance to Lead Car’)plt.axhline(setup_distance, color‘r’, linestyle‘–’, label‘Setup Distance’)plt.ylabel(‘Distance (m)’)plt.legend()plt.grid(True)plt.title(‘Simulation of the Control System in the Image’)plt.subplot(2, 1, 2)plt.plot(time_steps, log_velocity, label‘Ego Vehicle Velocity’, color‘orange’)plt.ylabel(‘Velocity (m/s)’)plt.xlabel(‘Time (s)’)plt.legend()plt.grid(True)plt.tight_layout()plt.show()代码对应关系说明Radar Sensor (黄色模块)代码中的 RadarSensor 类计算 Detected Distance。PID(s) (左上角)代码中的 pid_dist用于比较 Setup Distance 和实际距离产生控制信号。Brake Master Cylinder Pressure代码中模拟为 brake_pressure 变量由 PID 输出控制。veh_sf (红色小车)代码中的 VehicleDynamics 类接收 Torque 和 Brake Pressure输出 Velocity 和位置。Motor_Left / Motor_Right在代码的 update 函数中体现为扭矩输入。Velocity (右上角示波器)代码最后绘图部分的 log_velocity。代码逻辑分析图中的工作流主要分为三步Simulated Test Specifications车辆模型参数设置模块选择数学模型如 B-Class Sports Car和测试规程Procedure。Run Control with Simulink运行控制模块选择要运行的 Simulink 模型如 ACC_Car1并点击运行。Analyze Results仿真结果与后处理设置视频输出和绘图变量如车速、制动距离。MATLAB 自动化脚本示例%% CarSim 与 Simulink 联合仿真自动化配置脚本% 对应图中 “CarSim Run Control” 界面的功能实现clear; clc; close all;% 1. 车辆模型参数设置模块 (对应图中左侧面板)% ---------------------------------------------------------% 定义 CarSim 数据库路径和模型名称% 图中选择的是: Math Model: B-Class, Sports Carcarsim_dataset ‘B-Class, Sports Car’;% 图中选择的是: Procedure: ACC_Car1carsim_procedure ‘ACC_Car1’;% 设置仿真时间sim_stop_time ‘30.0’; % 30秒% 2. 运行控制模块 (对应图中中间面板)% ---------------------------------------------------------% 配置 Simulink 模型名称% 图中显示的是: Models: ACC_Car1simulink_model ‘ACC_Car1’;% 打开 Simulink 模型if ~exist([simulink_model ‘.slx’], ‘file’)% 这里仅为演示逻辑 disp([正在加载模型: simulink_model]);end% 配置 CarSim S-Function 参数% 在 Simulink 中CarSim 通常以一个 S-Function 模块存在% 我们需要通过 set_param 设置该模块的参数% 假设 Simulink 模型中 CarSim 模块的路径为: [模型名]/VS Solvervs_solver_path [simulink_model ‘/VS Solver’];% 设置参数 (模拟图中界面的选择)% 设置数据集set_param(vs_solver_path, ‘Database’, ‘Your_CarSim_Database_Path’);set_param(vs_solver_path, ‘Dataset’, carsim_dataset);% 设置运行规程set_param(vs_solver_path, ‘Procedure’, carsim_procedure);% 3. 仿真结果与后处理 (对应图中右侧面板)% ---------------------------------------------------------% 设置输出变量 (对应图中 “Output Variables”)% 例如输出车辆速度、制动距离等set_param(simulink_model, ‘LoadExternalInput’, ‘off’);set_param(simulink_model, ‘SaveOutput’, ‘on’);set_param(simulink_model, ‘OutputSaveName’, ‘sim_output’);% 运行仿真disp(‘开始运行联合仿真…’);sim(simulink_model, str2double(sim_stop_time));disp(‘仿真结束。’);% 后处理绘制结果 (对应图中 “Analyze Results”)% 假设输出结构体为 sim_outputfigure;% 绘制车速 (例如变量 B_V_X)% subplot(2,1,1);% plot(sim_output.tout, sim_output.yout.get(‘B_V_X’).Values);% title(‘车辆纵向速度’); ylabel(‘m/s’); grid on;% 绘制制动距离 (例如变量 B_X)% subplot(2,1,2);% plot(sim_output.tout, sim_output.yout.get(‘B_X’).Values);% title(‘车辆位移’); ylabel(‘m’); xlabel(‘Time (s)’); grid on;disp(‘结果绘图完成。’);代码关键点说明carsim_dataset 和 carsim_procedure对应图中 “Simulated Test Specifications” 区域的下拉菜单选择。set_param这是 MATLAB 的核心函数用于在后台修改 Simulink 模块的参数完全替代了人工在 GUI 界面上点击下拉框的操作。sim(…)对应图中 “Run Now” 按钮的功能启动仿真求解器。后处理代码最后的绘图部分对应图中 “Analyze Results” 的功能用于提取仿真数据如车速、距离并进行可视化。