基于PikePLCStudio用FBD图形化编程语言开发了一个用于控制交通灯的简单示例。 本文最后的ST源代码是FBD编译器将FBD转换为ST语言后的结果。 说明:本示例主要是为了演示使用PikePLCStudio基
基于PikePLCStudio用FBD图形化编程语言开发了一个用于控制交通灯的简单示例。
本文最后的ST源代码是FBD编译器将FBD转换为ST语言后的结果。
说明:本示例主要是为了演示使用PikePLCStudio基于FBD开发PLC程序的能力,至于交通灯控制程序逻辑本身,不排除还会有其它更好的逻辑算法。另外,本人不保证FBD程序逻辑的正确性
- 例子描述如下:
十字路口有南北向和东西向四个红绿灯。
南北向和东西向的红灯亮时长为30秒。
南北向和东西向的绿灯亮时长为27秒,黄灯亮时长为3秒。
当电源控制开关接通后,南北向的先绿灯亮,东西向的红先灯亮。
当电源控制开关断开后,所有的红绿灯都关闭。
- 定义变量
1 PROGRAM TrafficLight3 2 VAR 3 power AT %IX0.0:BOOL; //电源控制开关 4 snRed AT %QX0.0:BOOL; //南北向红灯 5 snGreen AT %QX0.1:BOOL; //南北向绿灯 6 snYellow AT %QX0.2:BOOL; //南北向黄灯 7 weRed AT %QX0.3:BOOL; //东西向红灯 8 weGreen AT %QX0.4:BOOL; //东西向绿灯 9 weYellow AT %QX0.5:BOOL; //东西向黄灯 10 time0:TIME; 11 time1:TIME; 12 time2:TIME; 13 time3:TIME; 14 time4:TIME; 15 time5:TIME; 16 END_VAR 17 18 VAR 19 R_TRIG0:R_TRIG;//接通电源,上升沿触发红绿灯开始工作 20 F_TRIG0:F_TRIG;//关闭电源,下降沿触发将所有红绿灯都关闭 21 22 TON0:TON;//控制南北向路灯亮27秒的定时器 23 TON1:TON;//控制南北向黄灯亮3秒的定时器 24 TON2:TON;//控制南北向红灯亮30秒的定时器 25 TON3:TON;//控制东西向红灯亮30秒的定时器 26 TON4:TON;//控制东西向的绿灯亮27秒的定时器 27 TON5:TON;//控制东西向的黄灯亮3秒的定时器 28 END_VAR 29 30 END_PROGRAM
- 程序逻辑
- PikePLCStudio界面截图
- FBD编译器生成的ST代码
PROGRAM TrafficLight3 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; END_VAR VAR R_TRIG0:R_TRIG;//接通电源,上升沿触发红绿灯开始工作 F_TRIG0:F_TRIG;//关闭电源,下降沿触发将所有红绿灯都关闭 TON0:TON;//控制南北向路灯亮27秒的定时器 TON1:TON;//控制南北向黄灯亮3秒的定时器 TON2:TON;//控制南北向红灯亮30秒的定时器 TON3:TON;//控制东西向红灯亮30秒的定时器 TON4:TON;//控制东西向的绿灯亮27秒的定时器 TON5:TON;//控制东西向的黄灯亮3秒的定时器 OR_OUT:BOOL; AND_OUT:BOOL; END_VAR //START NETWORK_1 R_TRIG0 ( CLK:=power ); OR_OUT:=OR ( IN1:=R_TRIG0.Q, IN2:=snGreen ); snGreen:=OR_OUT; OR_OUT:=OR ( IN1:=R_TRIG0.Q, IN2:=weRed ); weRed:=OR_OUT; //END NETWORK_1 //START NETWORK_2 F_TRIG0 ( CLK:=power ); AND_OUT:=AND ( IN1:=F_TRIG0.Q, IN2:=power ); OR_OUT:=OR ( IN1:=AND_OUT, IN2:=snGreen ); snGreen:=OR_OUT; OR_OUT:=OR ( IN1:=AND_OUT, IN2:=snRed ); snRed:=OR_OUT; OR_OUT:=OR ( IN1:=AND_OUT, IN2:=snYellow ); snYellow:=OR_OUT; OR_OUT:=OR ( IN1:=AND_OUT, IN2:=weGreen ); weGreen:=OR_OUT; OR_OUT:=OR ( IN1:=AND_OUT, IN2:=weRed ); weRed:=OR_OUT; OR_OUT:=OR ( IN1:=AND_OUT, IN2:=weYellow ); weYellow:=OR_OUT; //END NETWORK_2 //START NETWORK_3 TON0 ( IN:=snGreen, PT:=t#27s, ET=>time0 ); OR_OUT:=OR ( IN1:=TON0.Q, IN2:=snYellow ); snYellow:=OR_OUT; OR_OUT:=OR ( IN1:=OR_OUT, IN2:=snRed, IN3:=NOT(IN:=power) ); snGreen:=NOT(IN:=OR_OUT); time0:=TON0.ET; //END NETWORK_3 //START NETWORK_4 TON1 ( IN:=snYellow, PT:=t#3s, ET=>time1 ); OR_OUT:=OR ( IN1:=TON1.Q, IN2:=snRed ); snRed:=OR_OUT; OR_OUT:=OR ( IN1:=OR_OUT, IN2:=snGreen, IN3:=NOT(IN:=power) ); snYellow:=OR_OUT; time1:=TON1.ET; //END NETWORK_4 //START NETWORK_5 TON2 ( IN:=snRed, PT:=t#30s, ET=>time2 ); OR_OUT:=OR ( IN1:=TON2.Q, IN2:=snGreen ); snGreen:=OR_OUT; OR_OUT:=OR ( IN1:=OR_OUT, IN2:=snYellow, IN3:=NOT(IN:=power) ); snRed:=NOT(IN:=OR_OUT); time2:=TON2.ET; //END NETWORK_5 //START NETWORK_6 TON3 ( IN:=weRed, PT:=t#30s, ET=>time3 ); OR_OUT:=OR ( IN1:=TON3.Q, IN2:=weGreen ); weGreen:=OR_OUT; OR_OUT:=OR ( IN1:=OR_OUT, IN2:=weYellow, IN3:=NOT(IN:=power) ); weRed:=NOT(IN:=OR_OUT); time3:=TON3.ET; //END NETWORK_6 //START NETWORK_7 TON4 ( IN:=weGreen, PT:=t#27s, ET=>time4 ); OR_OUT:=OR ( IN1:=TON4.Q, IN2:=weYellow ); weYellow:=OR_OUT; OR_OUT:=OR ( IN1:=OR_OUT, IN2:=weRed, IN3:=NOT(IN:=power) ); weGreen:=NOT(IN:=OR_OUT); time4:=TON4.ET; //END NETWORK_7 //START NETWORK_8 TON5 ( IN:=weYellow, PT:=t#3s, ET=>time5 ); OR_OUT:=OR ( IN1:=TON5.Q, IN2:=weRed ); weRed:=OR_OUT; OR_OUT:=OR ( IN1:=OR_OUT, IN2:=weGreen, IN3:=NOT(IN:=power) ); snYellow:=NOT(IN:=OR_OUT); time5:=TON5.ET; //END NETWORK_8 END_PROGRAM
【文章出处:香港站群多ip服务器 http://www.558idc.com/hkzq.html提供,感恩】