简体   繁体   English

plc结构化文本循环延迟

[英]plc structured text loop delay

I am trying to have a loop where it will start at 100 and drop until it hits to a point where the while condition no longer holds true. 我试图建立一个循环,从100开始并下降,直到达到while条件不再成立的地步。

I started with 我开始

While Solar_Power_House_W_Solar_PER <= OneHundred AND BatChargePercent < OneHundred DO
    State_Dis_Charge := false
    FOR PLC_SetLoopChargeValue:= 100 TO 0 By -1  DO
        ConvertoReal   := INT_TO_LREAL(PLC_SetLoopChargeValue);
        Divide         := ConvertoReal DIV(100);
        PLC_SetCharge  := Divide;         
        PLC_Charge     := 1500 * PLC_SetCharge; 
        RB_Charge      := PLC_Charge;
        Visual_RBPower := 1500 * PLC_SetCharge;    (*Charge *) 
    END_FOR;

The problem I believe I have with this is that it cycles too fast so the condition never gets out of the while loop because it takes a while for the system to update so I thought of adding a delay portion: 我认为我的问题是它循环太快,因此条件永远不会退出while循环,因为系统需要花费一些时间来更新,所以我想到添加一个延迟部分:

While Solar_Power_House_W_Solar_PER <= OneHundred AND BatChargePercent < OneHundred DO
    State_Dis_Charge := false;
    wait(IN:=not wait.Q , PT:=T#50ms);
    if Wait.Q Then 
        FOR PLC_SetLoopChargeValue:= 100 TO 0 By -1  DO
            ConvertoReal   := INT_TO_LREAL(PLC_SetLoopChargeValue);
            Divide         := ConvertoReal DIV(100);
            PLC_SetCharge  := Divide;         
            PLC_Charge     := 1500 * PLC_SetCharge; 
            RB_Charge      := PLC_Charge;
            Visual_RBPower := 1500 * PLC_SetCharge;    (*Charge *) 
        END_FOR; 
    END_IF;
END_WHILE;     

How I think it should work is every 50ms 1 for loop should run. 我认为应该如何工作是每50ms 1 for loop应该运行一次。 Currently nothing happens every 50ms. 目前每50毫秒没有任何反应。

You have to consider that WHILE and FOR are executed synchronously. 您必须考虑WHILEFOR是同步执行的。 It means blocking. 这意味着阻止。 It means that interpreter do not execute next line, until previous line is finished. 这意味着解释器在上一行完成之前不会执行下一行。

  1. This means that "running to fast" cannot apply here. 这意味着“快速运行”不适用于此处。 It does not matter how fast it runs, the execution of the lines will be always in order. 不管运行多快,行的执行总是有序的。

  2. The only thing I would change and loop not from 100 to 0 but vice versa from 0 to 100, because I am not sure this backward will work fine. 我唯一要更改的不是从100到0的循环,而是从0到100的反循环,因为我不确定此向后的工作是否正常。 And then all you have to change: 然后您需要更改的所有内容:

     ConvertoReal := INT_TO_LREAL(100 - PLC_SetLoopChargeValue); 
  3. You do now show all code it is VERY HARD to judge but if FOR loom is complete it totally make no sense. 现在,您要显示所有代码,这是非常困难的判断,但是如果FOR织机完成,则完全没有意义。 You calculate some variables but you do not use them there. 您可以计算一些变量,但不要在其中使用它们。 You know that you cannot use them outside of your FOR loop, right? 您知道不能在FOR循环之外使用它们,对吗? Because outside of your FOR loop those variable will be always same value of last loop. 因为在FOR循环之外,这些变量将始终与上一个循环的值相同。

  4. In your second example your FOR loop, although it might work, you should not use timer to run the loop inside the loop. 在第二个示例中, FOR循环虽然可能有效,但不应使用计时器在循环内运行循环。 Because loops are synchronous and times async. 因为循环是同步的并且时间是异步的。

  5. As I understand you task you do not need WHILE at all. 据我了解你任务你不需要WHILE可言。 With this approach your program execution of other parts will be blocked until 100%. 通过这种方法,您的其他部分的程序执行将被阻止,直到100%。 That might take a while as I can see. 如我所见,这可能需要一段时间。 So you have to use IF . 因此,您必须使用IF

     IF Solar_Power_House_W_Solar_PER <= OneHundred AND BatChargePercent < OneHundred DO // .... END_IF; 

    The difference is significant. 差异是显着的。 With WHILE it will block your program till WHILE finish and other parts will not be executed for this long, in the same PLC cycle FOR might be executed so many times. 使用WHILE ,它将阻塞您的程序,直到WHILE完成,并且其他部分将不会执行很长时间,因为在同一PLC周期中, FOR可能执行了很多次。

    With IF if will run FOR one time per one PLC cycle and actualy doe snot change your logic. 随着IF如果将运行FOR每一个PLC循环一次,actualy母鹿鼻涕改变你的逻辑。

  6. If you would share your full code or at least parts where variables you have here are used so that the whole picture is visible, you might get a better help. 如果要共享完整的代码或至少共享使用此处具有变量的部分,以便可以看到整个图片,则可能会得到更好的帮助。 Edit your post and I'll edit my comment. 编辑您的信息,我将编辑我的评论。

With this answer im only adressing your issue with the for loop not being executed every 50ms. 有了这个答案,即时消息只能解决您的问题,因为for循环不是每50毫秒执行一次。 The other answers why the while loop cant be exited are correct unless the variables Solar_Power_House_W_Solar_PER and BatChargePercent aren't changed in a parrellel thread. 除非在并行线程中不更改变量Solar_Power_House_W_Solar_PERBatChargePercent否则无法退出while循环的其他答案是正确的。

I suggest wait is a TON function block. 我建议wait是TON功能块。 Please mind that names of FBs are case sensitive: wait.Q is possibly unequal Wait.Q . 请注意,FB的名称区分大小写: wait.Q可能不等于Wait.Q I think that is the main reason your for loop is not executed, because you check the output of another FB. 我认为这是您的for循环未执行的主要原因,因为您检查了另一个FB的输出。 Maybe check your declaration list for doubles with higher or lower cases. 也许检查您的申报单中是否包含大小写加倍的双打。

Another possibility is, that your condition for the while loop isn't met at all and you didn't notice. 另一种可能性是,您根本没有满足while循环的条件,并且您没有注意到。 In this case the for loop wouldn't be executed too of course. 在这种情况下,当然也不会执行for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM