Aller au contenu principal

How to use a clock flow for time-based calculations

PRESENTATION

Prerequisites

Skills that you will acquire

In this tutorial, you will learn how to use the clock flow available in Virtual Bench in order for your model to measure time and act accordingly. More precisely, you will increment a clock in Virtual Bench using the model.

Duration

0h45

Designer Papyrus

Create a new model Clock, with a Project Howto_use_Clock_Flow and a service ClockService. Add a new Standard life phase and a new _See what time it is use case.

You will need a flow to the environment:

  • display_clock communicates with the Virtual Bench Hour Display element. It conveys, through the decimal value parameter of the flow, the number of seconds elapsed since 00:00:00. The element then automatically presents the corresponding hour based on this information.

You will need two flows coming from the engineer:

  • clock_flow which your model will receive at regular intervals.
  • sampling_time_flow is sent at the beginning of the scenario, encapsulating an integer value that signifies the time interval between two clock_flow flows, with a default time interval of 40 ms.

In the use case that you have created, add two user stories:

  • Initialization

We will retrieve the 'time interval' value at the beginning of the simulation

sampling_time_flow flow is received from the engineer

  • Update_clock

For this user story, it's necessary to establish a variable that will track the current state of the clock. This variable, named clock_value, will be a decimal storing the number of seconds elapsed since the beginning of the scenario, initialized to 0.0.

a new variable form indicates that the value clock_flow is of type decimal and its default value is 0.0

We will incorporate our input flow, clock_flow. Upon receiving this flow, the model recognizes that a specific duration has passed, as determined in the preceding sequence diagram, Initialized. Subsequently, we must implement an internal activity named calculate_clock_value to update the clock_value variable within our model.

clock_value += sampling_time_flow_value / 1000.0;
important

sampling_time_flow_value is an integer value in ms.

clock_value is a decimal value in s.

Upon receiving the flow, we need to increment the clock_value by the sampling_time_flow_value amount of time, applying the necessary conversion.
It is crucial to use 1000.0 instead of 1000 to ensure that the division result is interpreted as a decimal rather than an integer, preventing rounding errors and ensuring accuracy.

After completing the calculation, we can transmit the value to the Virtual Bench using the display_clock flow by passing the clock_value as a parameter.

from the engineer, a clock_flow is received, and triggers a calculate_clock_value internal activity which then triggers a display_clock to the environment

After completing these steps, generate the state machine, build the project, and export the XML.

Virtual Bench

In your project, create a new executable for your Clock model and import the previously generated XML. Create a new scenario and add the relevant websocket in the Scenario tab.

Open an HMI environment, and look for the Hour Display element. Drag and Drop the element into the scenario.

The hour display element is dragged and dropped into the scenario

Initialize the Hour Display at 00:00:00.

Read the following pages : controllers overview, controllers management, and other controller dedicated pages if you have troubles with controllers.

Create a controller from the Update Controllers section of the Hour display element :

  • Configure the controller :
    • Type of element : Model
    • Model : Clock model
    • Event : Receive flow
    • Flow : display_clock

Set the action towards the model lifeline to switch the Hour display to Only Seconds mode and set the Seconds parameter to the received value to display the clock's value.

The mapping form of the clock display mapping has the value Mode checked to Only Seconds, and the Seconds attribute receives the value parameter.

In the controller application configuration, set your clock model instance in the Used Elements section.

Finally, it's essential to configure the Model Instance settings. Go to the Model Instances tab located in the right sidebar of your scenario and go into the parameters of your model instance. In the configuration window, go to the Connectivity tab and set the Clock flow parameter to the Clock_flow flow. This will enable Virtual Bench to transmit this flow at each step.

The final parameter is the Sampling time flow. Select sampling_time_flow from the drop-down menu to enable Virtual Bench to send at the start of the scenario execution the interval value (in seconds) between consecutive visualization updates to the model.

006_model_instance_configuration

remarque

You can specify the sampling time value in your scenario by accessing your scenario parameters; by default the value is set to 0.04 seconds.

Run

Launch your model executable, and establish a connection via the Launcher. Run the scenario, and the clock should start incrementing.

The Hour Display is incremented each second on Run

Agile method

To enhance the model, we can simply get the number of seconds already setup in Edit Mode at the beginning of scenario, and keep track of the elapsed time, starting from this point. One straightforward approach is to create the following User Story:

  • Receive_a_new_clock_value
From the engineer, a new_clock_value with a seconds parameter is received, and triggers an update_new_clock_value internal activity.

The activity only involves replacing the default value with the new one:

clock_value = new_clock_value_seconds;

In Virtual Bench, after importing the new XML, the previously used Hour Display can now have a new controller in which the initial value of seconds for the Hour display element can be sent at initialization:

In the Hour display trigger controller section, create a new controller.

  • Configure the controller :
    • Type of element : Scenario
    • Event : Before scenario execution start
  • Add Hour display element lifeline
  • Add the model lifeline
  • Retrieve the Hour display external variable (trace a message between Hour display lifeline and Controller lifeline)
  • Add 1 action towards the model lifeline
    • Select the flow new_clock_value
    • Set the parameter value to the Hour display element Second variable
009_new_clock_value_controller_creation

After generating, building the new project, and updating the Seconds parameter of the Hour Display in Edit Mode, you can test your scenario. In the following example, an initial value of 55000 seconds has been set:

The Hour Display is incremented each second on Run starting from an offset value

Implementation update

To go further, you can try to use a Time Input element to dynamically change the input value while the scenario is running. This element does not allow to send a raw "Seconds" value to the model but only distinct "hourField", "minuteField", "secondField" values. You will then have to change the previously created trigger for it to take three parameters instead of one. The value that keeps track of the time will then have to be calculated from the three parameters of the flow:

clock_value = new_clock_value_seconds + new_clock_value_minutes * 60 + new_clock_value_hours * 60 * 60;

The mapping shall be updated accordingly. Be sure to select the "On Changed" event so that the model gets updated every time the user change the value while the scenario is running.