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



在这里插入图片描述

在这里插入图片描述

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

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TOPSIS(技术协调评价法)是一种用于多准则决策分析的方法,可以用于红绿灯配时问题。但是,需要明确的是,TOPSIS本身并不是一段代码,而是一种决策分析方法。下面是一个使用TOPSIS方法进行红绿灯配时优化的示例代码,供您参考: ```python import numpy as np # 构建决策矩阵 X = np.array([[50, 10, 20, 30], [50, 20, 30, 10], [30, 20, 10, 50], [20, 30, 50, 10]]) # 构建权重向量 w = np.array([0.3, 0.2, 0.2, 0.3]) # 构建正向指标还是反向指标的标志向量,1表示正向指标,-1表示反向指标 flag = np.array([1, 1, -1, -1]) # 计算加权决策矩阵 Xw = X * w # 计算理想解和负理想解 Z = np.zeros(4) Zmax = np.max(Xw, axis=0) Zmin = np.min(Xw, axis=0) # 计算距离 Dmax = np.sqrt(np.sum((Xw - Zmax) ** 2, axis=1)) Dmin = np.sqrt(np.sum((Xw - Zmin) ** 2, axis=1)) # 计算综合得分 score = Dmin / (Dmin + Dmax) # 按照得分排序,得分越高,红绿灯时长越长 duration = np.argsort(score)[::-1] # 输出结果 print(duration) ``` 上述代码中,构建了一个4x4的决策矩阵X,其中包含了4个路口的车流量、行人流量、公交车数量和道路质量等指标。通过设置权重向量w和正向指标还是反向指标的标志向量flag,可以计算加权决策矩阵Xw,并计算理想解和负理想解。通过计算距离和综合得分,可以得到每个路口的红绿灯时长,最后按照得分排序输出。需要注意的是,这只是一个简单的示例代码,实际应用中需要根据具体情况进行适当调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值