国产PLC软件PikePLC——基于梯形图开发交通灯控制示例

基于PikePLCStudio用Ladder图形化编程语言开发了一个用于控制交通灯的简单示例。

本文最后的ST源代码是Ladder编译器将梯形图转换为ST语言后的结果。

说明:本示例主要是为了演示使用PikePLCStudio基于梯形图开发PLC程序的能力,至于交通灯控制程序逻辑本身,不排除还会有其它更好的逻辑算法。另外,本人不保证程序逻辑的正确性

  • 例子描述如下
十字路口有南北向和东西向四个红绿灯。
南北向和东西向的红灯亮时长为30秒。
南北向和东西向的绿灯亮时长为27秒,黄灯亮时长为3秒。
当电源控制开关接通后,南北向的先绿灯亮,东西向的红先灯亮。
当电源控制开关断开后,所有的红绿灯都关闭。
  • 定义变量
PROGRAM TrafficLight

 VAR
    power AT %IX0.0:BOOL;//红绿灯控制开关
    snRed AT %QX0.0:BOOL;//南北向红灯
    snGreen AT %QX0.1:BOOL;//南北向绿灯
    snYellow AT %QX0.2:BOOL;//南北向黄灯
    weRed AT %QX0.3:BOOL;//东西向红灯
    weGreen AT %QX0.4:BOOL;//东西向绿灯
    weYellow AT %QX0.5:BOOL;//东西向黄灯
    time0:TIME;
    time1:TIME;
    time2:TIME;
    time3:TIME;
    time4:TIME;
    time5:TIME;
    initialized:BOOL:=TRUE;//标记是否第一次初始化
 END_VAR

 VAR     
     TON0:TON;
    TON1:TON;
    TON2:TON;
    TON3:TON;
    TON4:TON;
    TON5:TON;
 END_VAR
END_PROGRAM
  • 程序逻辑

  • PikePLCStudio界面截图

  • Ladder编译器生成的ST代码
PROGRAM TrafficLight
 VAR
    power AT %IX10.1:BOOL;//红绿灯控制开关
    snRed AT %QX0.0:BOOL;//南北向红灯
    snGreen AT %QX0.1:BOOL;//南北向绿灯
    snYellow AT %QX0.2:BOOL;//南北向黄灯
    weRed AT %QX0.3:BOOL;//东西向红灯
    weGreen AT %QX0.4:BOOL;//东西向绿灯
    weYellow AT %QX0.5:BOOL;//东西向黄灯
    time0:TIME;
    time1:TIME;
    time2:TIME;
    time3:TIME;
    time4:TIME;
    time5:TIME;
    initialized:BOOL:=TRUE;//标记是否第一次初始化
     TON0:TON;
    TON1:TON;
    TON2:TON;
    TON3:TON;
    TON4:TON;
    TON5:TON;
 END_VAR

IF initialized AND power THEN
    snGreen:=TRUE;
END_IF;
IF initialized AND power THEN
    snYellow:=FALSE;
END_IF;
IF initialized AND power THEN
    snRed:=FALSE;
END_IF;
IF initialized AND power THEN
    weRed:=TRUE;
END_IF;
IF initialized AND power THEN
    weGreen:=FALSE;
END_IF;
IF initialized AND power THEN
    weYellow:=FALSE;
END_IF;
IF initialized AND power THEN
    initialized:=FALSE;
END_IF;
IF NOT(IN:=power) THEN
    snGreen:=FALSE;
END_IF;
IF NOT(IN:=power) THEN
    snRed:=FALSE;
END_IF;
IF NOT(IN:=power) THEN
    snYellow:=FALSE;
END_IF;
IF NOT(IN:=power) THEN
    weRed:=FALSE;
END_IF;
IF NOT(IN:=power) THEN
    weGreen:=FALSE;
END_IF;
IF NOT(IN:=power) THEN
    weYellow:=FALSE;
END_IF;
initialized:=(NOT(IN:=power));
TON0
(
    IN:=snGreen AND power,
    PT:=t#27s,
        ET=>time0
);
IF TON0.Q THEN
    snGreen:=FALSE;
END_IF;
IF TON0.Q THEN
    snYellow:=TRUE;
END_IF;
TON1
(
    IN:=snYellow AND power,
    PT:=t#3s,
        ET=>time1
);
IF TON1.Q THEN
    snYellow:=FALSE;
END_IF;
IF TON1.Q THEN
    snRed:=TRUE;
END_IF;
TON2
(
    IN:=snRed AND power,
    PT:=t#30s,
        ET=>time2
);
IF TON2.Q THEN
    snGreen:=TRUE;
END_IF;
IF TON2.Q THEN
    snRed:=FALSE;
END_IF;
TON3
(
    IN:=weGreen AND power,
    PT:=t#27s,
        ET=>time3
);
IF TON3.Q THEN
    weGreen:=FALSE;
END_IF;
IF TON3.Q THEN
    weYellow:=TRUE;
END_IF;
TON4
(
    IN:=weYellow AND power,
    PT:=t#3s,
        ET=>time4
);
IF TON4.Q THEN
    weYellow:=FALSE;
END_IF;
IF TON4.Q THEN
    weRed:=TRUE;
END_IF;
TON5
(
    IN:=weRed AND power,
    PT:=t#30s,
        ET=>time5
);
IF TON5.Q THEN
    weRed:=FALSE;
END_IF;
IF TON5.Q THEN
    weGreen:=TRUE;
END_IF;

END_PROGRAM

 

posted @ 2022-06-02 22:50  PLC_Master  阅读(1389)  评论(0编辑  收藏  举报