Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

How do i setup a setup task?

Volvo742
2016-06-09
2023-08-17
  • Volvo742 - 2016-06-09

    Hello!

    I have a task who call a program in my codesys. But i want other program run after the man setup program is finished?

    I sett the main setup program at prior #1 and after that program has finished, other program can now loop.

    How do i can figure that?

     
  • Anonymous - 2016-06-12

    Originally created by: scott_cunningham

    I do this with only one task. My main program (PLC_PRG) calls both my setup program and main program. There are several ways to do this, but here is one way:

    SetupCode();
    If NOT SetupCode.Done THEN
       RETURN;
    END_IF
    MainCode();
    MoreCode();
    

    Your SetupCode program must be smart enough to set a Done output and stay there.

    There are other ways... Some people use a VAR_INPUT on every POU to Enable running the code instead of using a IF/THEN/RETURN like above... Some people use a state machine (CASE statement) with state 0 = setup, state 1 = run, etc

     
  • Volvo742 - 2016-06-12

    This will be like programming with arduino ?

     
  • Anonymous - 2016-06-22

    Originally created by: scott_cunningham

    Your question hints at a problem I had when I started with IEC61131-3 programming. If the following is already clear to you, I apologize in advance!

    IEC61131-3 is PLC style code. That means the program loops over and over. It is not like a PC program where the first line is the beginning and the last line is the end and it quits. PLC code has a hidden forever loop outside of the main PLC_PRG program. This means if you make a large FOR/NEXT or WHILE/WEND loop that takes too long, you will get a crash - basically a watch dog error. It also means you don't want 20 tasks running at once with different priorities, because all of the tasks will always be called.

    I would suggest only starting with one task (PLC_PRG) and use the following in your PLC_PRG program:

    CASE Step OF
       0: //setup
          Setup();
          IF Setup.Done THEN
             Step := 1;
          END-IF
       1: //main
          DoMainWork();
          
    END_CASE
    

    Then your Setup program can step through what it needs to do over however many PLC scans (1 or 1000 calls). When Done is set true, then the state machine moves to the main program and Setup is not called anymore.

     
  • travlytle - 2016-06-24

    I do something like the attached file:

    IMG: 2016

     
  • Volvo742 - 2016-06-26

    travlytle hat geschrieben:
    I do something like the attached file:

    Only one problem.
    A PLC cannot wait/delay.

     
  • Anonymous - 2016-06-27

    Originally created by: scott_cunningham

    You must handle a wait/delay in a PLC way:

    Delay();
    IF Delay.Q then
    Setup := TRUE;
    END_IF

    Where Delay is a TON timer.

    Think of slicing your program - it must run in small steps until it is finished. For example, a FOR/NEXT loop of 10000 will probably fail. Instead, one iteration for PLC scan until all 10000 are completed.

    This is how a "machine" can keep running all of the time without "hanging" while some delay runs. This is what I mean about not programming in a PC way.

    If you are still having issues, then specifically lay out what you are trying to do and someone will give you some code.

     

Log in to post a comment.