Here is a small function to simulate a Timer with Lua, in order to manage one or more events (signals, contact, display panel, tooltips, etc …).

Summary

Presentation

The idea is as follows: we want a train to stop in front of a signal and after X seconds, restart and manage the signals on its route in order to respect the cantonment for the other trains behind it. Of course, we could use the flexible transit to manage the cantonment in the station but this is just an example of one of the possibilities offered by this function.

The ideal would be to have a Timer function which runs continuously AND which is also used to manage something other than a train. why not go further, as long as this function is used for other objects.

In short, let’s recap our needs for this work:

  1. We want to intervene on the waiting time for a train at the station,
  2. Trigger a counter at a given time and place,
  3. Define the stop time in advance,
  4. Our train must be able to manage the signals encountered on its route.

To run this script, you must be in automatic  mode.

Here is the detailed script line by line:

				
					    clearlog()  -- erases and clears the eep event window 
    print("EEP Version ", EEPVer)  -- Returns the EEP version used
    nb_Inc = 0;    -- Counter for the incrementation
    nb_Ecart = 200;    -- 200 milliseconds (EEPMain is called 5 times per second)
    nb_Sec = 0;    -- Seconds counter
    nb_Temps_Attente = 30; -- Train waiting time at the station: 30 sec (example)
    nb_Compteur_Attente = 0;  -- Counter for waiting on signal
    bool_Contact_Franchi = nil;    -- Boolean to know if contact n ° 1 is crossed or not


    -- MAIN FUNCTION EEPMain () called 5 times per second
    function EEPMain()    
        fnc_Calcul_Temps()   -- Calls the function fnc_Calcul_Temps ()
        return 1
    end  -- End of main function

    -- Time calculation function in seconds
    -- This function can be used as a working basis to manage several events at the same time
    function fnc_Calcul_Temps()
        nb_Inc = nb_Inc + nb_Ecart; --We add the number of increments of 200 milliseconds
       -- Modulo operator which returns the value 0 if nb_Inc = 1000 (1000 milliseconds = 1 second)
        if (nb_Inc % 1000 == 0) then
            nb_Sec = nb_Sec + 1; -- Increments one second each time
            -- If contact n ° 1 is crossed AND the waiting counter = 0
            if (bool_Contact_Franchi == true) then
                    -- We increment the waiting counter by one second
                    nb_Compteur_Attente = nb_Compteur_Attente + 1;
                    -- If the wait counter is equal to the initial wait time
                    if (nb_Compteur_Attente == nb_Temps_Attente) then
                        print("Ouverture du signal n° 1"); -- For information
                        EEPSetSignal(1, 1);    --Open signal n ° 1
                        bool_Contact_Franchi = false;	-- Contact has already been crossed
                        nb_Compteur_Attente = 0;    -- Resets the counter to 0
                    end
            end
            -- print ("Number of seconds elapsed:", nb_Sec) -- For information
            print("Nombre nb_Compteur_Attente : ", nb_Compteur_Attente) -- for information
            nb_Inc = 0;  -- One second has elapsed (new increment to calculate)
        end
    end  -- End of the function

    -- Once the vehicle contact point has been crossed (before signal n ° 1), set the boolean to true
    function fnc_Commute_Contact_1()
        print("Signal n° 1 franchi"); -- for information
        bool_Contact_Franchi = true;
    end  -- End of the function

    -- After restarting, once the point of contact n ° 2 crossed, set the boolean to false
    -- to reposition the signal to Stop for the next train
    function fnc_Commute_Contact_2()
        EEPSetSignal(1, 2);
    end  -- End of the function

				
			

Development

For our example, we need to place two contact points upstream and downstream of signal n ° 1 as shown in figure n ° 1:

Figure n° 1

Let’s already open the properties window for contact point 1:

Figure n ° 2 (contact point before signal)

The name of the function fnc_Commute_Contact_1 (without parentheses!) Is entered in the Lua Function edit box (framed in red).

When the locomotive crosses the contact point, the function is called and assigns the value true to the variable bool_Contact_Franchi.

Since the fnc_Calcul_Temps () function is called 5 times per second via the main function EEPMain (), it is easy to know where we are with the time count. If one second has elapsed (modulo% operator on line n ° 22), the variable nb_Sec is incremented by 1 second (line n ° 23).

After that, it’s just logic: the bool_Contact_Franchi variable being equal to true, the test is therefore true and the nb_Counter_Attente variable is incremented by an additional second (line n ° 27).

So … As long as the variable nb_Counter_Attente is not equal to the variable nb_Temps_Attente (line n ° 29), the test can not be carried out and the program continues to count.

Inevitably, after 30 seconds, the variable nb_Counter_Attente is equal to the variable nb_Temps_Attente (line n ° 29) so the test is therefore true and can be executed. We still have to use the LUA EEPSetSignal (1, 1) function on line 31 to open the signal on clear in order to restart the locomotive and reset the variables bool_Contact_Franchi to false and nb_Counter_Attente to 0 (Lines 32 and 33) . In fact, not resetting these variables would lead to the next pass restarting the locomotive immediately.

Important : The EEPSetSignal (n, n) function contains two parameters. The first parameter is the number of the signal displayed in the 2D view or in its properties window and the second parameter is the position (command given to the driver) of the signal. Here in our case, 1 = clear, 2 = stop. Please note that depending on the signals, you may find several possible positions. In this case, it is advisable to make sure of the exact number of the position by looking directly in the properties of the corresponding signal.

In both cases, the variable nb_Inc is reinitialized to 0 (line n ° 38) because a second has just passed.

With the signal opens, it should now be closed again in the Stop position and this is precisely the role of the fnc_Commute_Contact_2 function.

Figure n ° 3 (point of contact after the signal)

As for the first point of contact, the name of the function fnc_Commute_Contact_2 (without parentheses!) Is entered in the Lua Function edit box (framed in red).

When the locomotive passes the contact point, the function is called and will switch the signal to Stop via the LUA EEPSetSignal (1, 2) function.

For educational purposes, we have deliberately inserted a second vehicle contact point after the signal to call the function fnc_Commute_Contact_2. We could very well have been content to put a contact for signaling in relation to signal n ° 1 and to set its position to Stop. Thus, when the locomotive crosses the contact point, the signal will be switched back to Stop:

Figure n ° 4 (contact point of signal n ° 1)

This replaces the LUA EEPSetSignal (1, 2) function in the fnc_Commute_Contact_2 function.

At this stage, you can delete the vehicle contact point n ° 2 located after the signal and the function fnc_Commute_Contact_2 in the script which are no longer needed.

You have the possibility to download this project by clicking on the button below. This was done with the EEP15 version .

You can also view the demo of this script by clicking on the video below:

This article is now complete. If you have any questions or suggestions, please give us your feedback in the leave a reply input box below.

Thank you for your helpful comments. Have fun reading an other article. 

eep-world.com team

This article was translated by Pierre for the English side of the EEP-World from the article written by Domi for the French side of the EEP-World.

Leave a Reply