1.数据库
有三个网络节点,两个报文,四个信号,其中EngineState报文发送两个信号:EngineSpeed,OnOff。LightState发送FlashLight,HeadLight两个信号。OnOff信号是value table型。
双击Display节点
所有信号都是它的接收信号。
2.系统变量
3..面板设计
|
|
就这么看首先Ignition控制Display的小车子变化,Speed控制车速的显示,包括转盘上的显示和车速值的显示,如果Ignition不打开,那么车速不会显示;Head和Hazard控制车灯的显示。
Control面板中Ignition与EngineStateSwitch系统变量关联,speed与EngineSpeedEntry系统变量关联,Head与HeadLight信号相关联,Hazard与HazardLightsSwitch系统变量相关联。
Display面板中小车与信号OnOff相关联,Meter1与EngineSpeedDspMeter系统变量相关联,下面的TextBox与EngineSpeed信号相关联,车前灯与系统变量LightDisplay相关联,车前灯有八种状态,如下所示
4.CAPL编码
- 下面是Engine代码
/*@@var:*/ variables { } /*@@end*/ /*@@sysvarChange:Engine::EngineStateSwitch:*/ on sysvar sysvar::Engine::EngineStateSwitch { $EngineState::OnOff = @this; if(@this) $EngineState::EngineSpeed = @sysvar::Engine::EngineSpeedEntry; else $EngineState::EngineSpeed = 0; } /*@@end*/ /*@@sysvarChange:Engine::EngineSpeedEntry:*/ on sysvar sysvar::Engine::EngineSpeedEntry { if(@sysvar::Engine::EngineStateSwitch) { $EngineState::EngineSpeed = @this; } } /*@@end*/
当系统变量sysvar::Engine::EngineStateSwitch发生变化时(因为Ignition与sysvar::Engine::EngineStateSwitch相关联,所以在面板中打开Ignition时变量sysvar::Engine::EngineStateSwitch会发生变化)触发事件:
$EngineState::OnOff = @this;
将变量的值赋给信号OnOff,因为小车与OnOff绑定,所以小车状态会变化。
if(@this) $EngineState::EngineSpeed = @sysvar::Engine::EngineSpeedEntry; else $EngineState::EngineSpeed = 0;
如果Ignition关闭,那么变量值为0,则速度值显示为0;如果Ignition打开,则变量值会变成1,那么就会执行
$EngineState::EngineSpeed = @sysvar::Engine::EngineSpeedEntry;
将系统变量EngineSpeedEntry的值赋给信号EngineSpeed,这里Speed与变量EngineSpeedEntry相关联,所以你在control面板中拖动speed的速度条,值会变,又信号EngineSpeed与TextBox相关联,所以会显示出速度值。同时因为系统变量EngineSpeedEntry发生变化,又会触发下面的事件
on sysvar sysvar::Engine::EngineSpeedEntry { if(@sysvar::Engine::EngineStateSwitch) { $EngineState::EngineSpeed = @this; } }
这里为什么要加条件,因为当发动机没开启时,即使速度条变化,也不可能有速度值,所以要先判断系统变量EngineStateSwitch值是否为1,再把速度值赋给EngineSpeed,?这里很疑惑,因为作用和上面信号触发事件中条件语句的作用一样???????????
- 下面是Light的脚本
/*@@var:*/ variables { msTimer tFlashLightFrequency; const int gFlashLightFrequency = 500; int gHazardLightsStatus = 0; int gDebugCounterTX = 0; int gDebugCounterTXRQ = 0; int gDebugCounterRX = 0; } /*@@end*/ /*@@startStart:Start:*/ on start { setWriteDbgLevel(0); // set DbgLevel = 1 to get more information in Write-Window } /*@@end*/ /*@@msg:CAN1.easy::LightState (0x321):*/ on message LightState { if (this.dir == TX) { gDebugCounterTX++; if(gDebugCounterTX == 10) { writeDbgLevel(1,"LightState TX received by node %NODE_NAME%"); gDebugCounterTX = 0; } } if(this.dir == TXREQUEST) { gDebugCounterTXRQ++; if(gDebugCounterTXRQ == 10) { writeDbgLevel(1,"LightState TXREQUEST received by node %NODE_NAME%"); gDebugCounterTXRQ = 0; } } if (this.dir == RX) { gDebugCounterRX++; if(gDebugCounterRX == 10) { writeDbgLevel(1,"Error: LightState RX received by node %NODE_NAME%"); gDebugCounterRX = 0; } } } /*@@end*/ /*@@timer:tFlashLightFrequency:*/ on timer tFlashLightFrequency { gHazardLightsStatus = (gHazardLightsStatus == 1 ? 0 : 1); $LightState::FlashLight = gHazardLightsStatus; setTimer(this, gFlashLightFrequency); } /*@@end*/ /*@@key:'0':*/ on key '0' { setwriteDbgLevel(0); } /*@@end*/ /*@@key:'1':*/ on key '1' { setwriteDbgLevel(1); } /*@@end*/ /*@@sysvarChange:Lights::HazardLightsSwitch:*/ on sysvar sysvar::Lights::HazardLightsSwitch { if (@this) { gHazardLightsStatus = 1; setTimer(tFlashLightFrequency, gFlashLightFrequency); } else { cancelTimer(tFlashLightFrequency); gHazardLightsStatus = 0; } $LightState::FlashLight = gHazardLightsStatus; } /*@@end*/
好长 一段一段分析
on start { setWriteDbgLevel(0); // set DbgLevel = 1 to get more information in Write-Window }
这里没懂啥意思
后面的代码理解在后一篇写吧
标签:Engine,end,EngineState,sysvar,EngineSpeedEntry,easy,软件,自带,EngineSpeed From: https://www.cnblogs.com/wangyihe20000719/p/17549971.html