matlab simulink交通信号机,适用于prescan VTD等仿真软件的后台控制红绿灯

模拟真实环境下,丁字路口的交通信号机的控制办法。使用matlab-function输出连接到仿真软件的红绿灯三个灯色的控制口,置1相应的灯色亮。

function [east_phaseid,east_red_starttime,east_red_endtime,east_green_starttime,east_green_endtime,east_yellow_starttime,east_yellow_endtime,north_phaseid,north_red_starttime,north_red_endtime,north_green_starttime,north_green_endtime,north_yellow_starttime,north_yellow_endtime,east_green,east_yellow,east_red,north_red,north_yellow,north_green] = fcn(t)
    light_run_time = mod(fix(t),15);%丁字路口周期15s
    %% 东西直行灯,路权9s,east_phaseid=1
    east_phaseid=1;%东西相位phaseid
    east_red=0;east_yellow=0;east_green=0;
    %v2x spat信息初始化:
    east_red_starttime=0;
    east_red_endtime = 0;
    east_green_starttime=0;
    east_green_endtime = 0;
    east_yellow_starttime=0;
    east_yellow_endtime = 0;
    if(light_run_time<6)
        east_green=0;east_yellow=0;east_red=1;%灯色控制
        %v2x spat信息:
        east_red_starttime=0;
        east_red_endtime = 6-light_run_time;
        east_green_starttime=6-light_run_time;
        east_green_endtime = 12-light_run_time;%6s绿灯
        east_yellow_starttime=12-light_run_time;
        east_yellow_endtime = 15-light_run_time;%3s黄灯
    end
    if(light_run_time>=6&&light_run_time<12)%6s绿灯
        east_red=0;east_yellow=0;east_green=1;%灯色控制
        %v2x spat信息:
        east_red_starttime=15-light_run_time;
        east_red_endtime = 21-light_run_time;
        east_green_starttime = 0;
        east_green_endtime = 12-light_run_time;%6s绿灯
        east_yellow_starttime=12-light_run_time;
        east_yellow_endtime = 15-light_run_time;%3s黄灯
    end
    if(light_run_time>=12)%3s黄灯
        east_red=0;east_yellow=1;east_green=0;%灯色控制
        %v2x spat信息:
        east_red_starttime = 15-light_run_time;
        east_red_endtime = 21-light_run_time;
        east_green_starttime = 21-light_run_time;
        east_green_endtime = 27-light_run_time;%6s绿灯
        east_yellow_starttime=0;
        east_yellow_endtime = 15-light_run_time;%3s黄灯
    end

    %% 南北红绿灯,路权6s
    north_red=0;north_yellow=0;north_green=0;
    north_phaseid=2;%南北相位phaseid
    %v2x spat信息初始化:    
    north_red_starttime = 0;
    north_red_endtime = 0;
    north_green_starttime = 0;
    north_green_endtime = 0;
    north_yellow_starttime=0;
    north_yellow_endtime = 0;
    if(light_run_time<3)%3s绿灯
        north_red=0;north_yellow=0;north_green=1;
        %v2x spat信息:    
        north_red_starttime = 6-light_run_time;
        north_red_endtime = 15-light_run_time;%9s红灯
        north_green_starttime = 0;
        north_green_endtime = 3-light_run_time;%3s绿灯
        north_yellow_starttime=3-light_run_time;
        north_yellow_endtime = 6-light_run_time;%3s黄灯    
    end
    if(light_run_time>=3&&light_run_time<6)%3s黄灯
        north_red=0;north_yellow=1;north_green=0;
        %v2x spat信息:
        north_red_starttime = 6-light_run_time;
        north_red_endtime = 15-light_run_time;%9s红灯
        north_green_starttime = 15-light_run_time;
        north_green_endtime = 18-light_run_time;%3s绿灯
        north_yellow_starttime=0;
        north_yellow_endtime = 6-light_run_time;%3s黄灯
    end
    if(light_run_time>=6)%9s红灯
        north_red=1;north_yellow=0;north_green=0;
        %v2x spat信息:
        north_red_starttime = 0;
        north_red_endtime = 15-light_run_time;%9s红灯
        north_green_starttime = 15-light_run_time;
        north_green_endtime = 18-light_run_time;%3s绿灯
        north_yellow_starttime=18-light_run_time;
        north_yellow_endtime = 21-light_run_time;%3s黄灯
    end
end



matlab simulink交通信号机,适用于prescan VTD等仿真软件的后台控制红绿灯_第1张图片

matlab simulink交通信号机,适用于prescan VTD等仿真软件的后台控制红绿灯_第2张图片

在simulink中使用time模块驱动matlabfuntion,然后信号灯的运行时间作为变量,控制信号灯的亮灭。这段代码是固定时间时长的,如果想要可以随时改时长,可以将常量改成变量。这里权当提供个思路,如果想要完整的信号机的控制程序可以关注和联系我。
后期还会更新stateflow的控制办法,慢慢上传些源代码,供大家下载。
m文件下载

你可能感兴趣的:(V2X开发,MATLAB,c++,c语言,自动驾驶,matlab,simulink)