Modelling of traffic light control logic¶
The purpose of this example is to demonstrate the possibilities of the finite automata library and to familiarise you with its basic elements. its basic elements. We will consider the implementation of a simple traffic light control logic using the Chart block.
Principle of operation¶
A traffic light has three colours and switches between them all the time. The moment when one of the colours is lit is called the state of the system. Accordingly, our model will have three states: Red, Yellow and Green. Red will be the default state, i.e. every cycle will start from it.
In this example, all transitions of the system from one state to another will be conditioned by the expiration of the specified time. The Green state becomes active after the time specified in the function after()
has elapsed.
We will set time intervals as local variables: redtime, yellowtime and greentime. Also set the output variable light to output the results.
To track the system operation in each state we will assign some value to the output variable light. For example, in the state when the traffic light is red we will assign light value 1. In the state when the yellow and green lights are on, the values are 2 and 3, respectively.
Each of the states contains the entry operator, which says that the specified action is performed once when the state becomes active.
Conclusion results¶
Let's run the model and look at the result of the execution.
# Подключение вспомогательной функции запуска модели.
function run_model( name_model, path_to_folder )
Path = path_to_folder * "/" * name_model * ".engee"
if name_model in [m.name for m in engee.get_all_models()] # Проверка условия загрузки модели в ядро
model = engee.open( name_model ) # Открыть модель
model_output = engee.run( model, verbose=true ); # Запустить модель
else
model = engee.load( Path, force=true ) # Загрузить модель
model_output = engee.run( model, verbose=true ); # Запустить модель
engee.close( name_model, force=true ); # Закрыть модель
end
return model_output
end
run_model("traffic_lights",@__DIR__) # Запуск модели.
# Считывание из simout залогированных сигналов
light = simout["traffic_lights/light"];
light = collect(light);
using Plots
plot(light.time, light.value, label = "light")
On the resulting graph we can see how the traffic light signal changed. For the first 15 seconds the red signal was on, then for 20 seconds the green signal was on and for 5 seconds the yellow signal was on, and then the cycle started all over again.
Conclusion¶
We have considered a variant of a simple implementation of the traffic light control logic using the Chart block of the finite automata library. You can learn more about the library at the link: https://engee.com/helpcenter/stable/articles/statemachines-first.html.