跳到主要内容

How to use and/or create a Timer

PRESENTATION

Prerequisites

Skills that you will acquire

In this tutorial, you will learn two ways to model a timer:

  1. via the VB’s HMI Element “Countdown”
  2. entirely inside Designer Papyrus

At the end, we want a common behavior:

  • Start a timer for several seconds,
  • Be able to pause & resume this timer and see its state modification,
  • Be informed of the end of this timer,
  • Stop this timer at any time and see its state modification.

Duration

1h30

USE COUNTDOWN HMI ELEMENT

In Designer Papyrus

Create a new model TimerModel, with a Project TimerProject and a service TimerSystem.

In the State Machine:

  • rename "FirstState" as "Initialized" (this is the starting state, where the countdown can be set up),
  • add the state "Countdown_VB" (a specific state that may transmit the behavior of the countdown to VB).

You will need four requested flows:

  • requests_start_countdown(value: Integer),
  • requests_stop_countdown(),
  • requests_pause_countdown(),
  • requests_resume_countdown().

And four flows To ENV:

  • starts_countdown(value: Integer),
  • stops_countdown(),
  • pauses_countdown(),
  • resumes_countdown().

And one flow From ENV:

  • countdown_ended().

In Standard Use, create four UseCases and five UserStories:

  • Start a countdown
    • User starts a countdown
    User_starts_a_countdown
    • End of countdown
    End_of_countdown
  • Stop a countdown
    • User stops a countdown
    User_stops_a_countdown
  • Put countdown in pause
    • User puts a countdown in pause
    User_puts_a_countdown_in_pause
  • Resume a countdown
    • Users resumes a countdown
    User_resumes_a_countdown

In VB

Drag & drop a Countdown, put “Visibility” to hidden.

Create mappings: Four model to simu mapping instances

  • starts_countdown mapping
    • Check the triggered behaviours “Start the countdown” & select the variable “value” as “Remaining time”
    • Check “Visibility” and select visible
    mapping_start_countdown
  • stops_countdown mapping
    • Check the triggered behaviours “Stop the countdown”
    • Check “Style” and put “fill:red;”
    mapping_stop_countdown
  • pauses_countdown mapping
    • Check the triggered behaviours “Pause the countdown”
    • Check “Style” and put “fill:grey;”
    mapping_pause_countdown
  • resumes_countdown mapping
    • Check the triggered behaviours “Resume the countdown”
    • Check “Style” and put “fill:black;”
    mapping_resume_countdown

One simu to model mapping instances

  • (Countdown ended) countdown_ended mapping
    • Select the Event “Countdown ended”
    mapping_countdown_ended

In the timeline, add five “Timeline Actions” as triggers, with four simu to model mapping instances:

  • (On trigger) requests_start_countdown mapping
    • Set the “value” to 5 (seconds)
    mapping_requests_start_countdown
  • (On trigger) requests_pause_countdown mapping
  • (On trigger) requests_resume_countdown mapping
  • (On trigger) requests_stop_countdown mapping
timeline_countdown

Run

CREATE YOUR OWN TIMER INSIDE A MODEL

In Designer Papyrus

Update the State Machine:

  • add another State “Countdown_Model” (this state represents the logic and data model of the countdown),
  • add two sub-states “Started” (the countdown is actively running) & “Paused”(the countdown is temporarily halted).

Add one flow From Engineer, to get Virtual Bench’s timestep:

  • deltaT (value: Integer)
警告

The value getting from VB is in ms!

To convert this received value in seconds: add a Simulation Phase, a UseCase “Convert delta from ms to s” and a UserStory “Conversion” In the Sequence Diagram, add the Engineer Life Line, draw the Message “deltaT” from Engineer to TimerSystem (in State Initialized), then add an activity “convert()” an create a new Decimal variable named “deltaT_in_s”, copy/paste the followed code:

deltaT_in_s = deltaT_value/1000.0;
警告

Don’t forget the “.0” after “/1000”, overwise deltaT_in_s will be an Integer

You should have this Sequence Diagram at the end:

Conversion

You will need four requested flows:

  • requests_start_timer(value: Decimal)
  • requests_stop_timer()
  • requests_pause_timer()
  • requests_resume_timer()

Four feedbacks:

  • display_timer (value: Decimal)
  • paused_timer()
  • stopped_timer()

And one flow From ENV:

  • clock()

In Standard Use, create four UseCases & six UserStories:

  • Start a timer
    • User starts a timer
      • New Decimal variable named “timer”
      • Activity: register_timer_value()
        • Body: timer = requests_start_timer_value;
        User_starts_a_timer
    • Decrementation of the timer
      • GC: timer > 0
      • Activity: decrease_timer()
        • Body: timer -= deltaT_in_s;
        Decrementation_of_the_timer
    • End of timer
      • GC: timer <= 0
      • Activity: reset_timer()
        • Body: timer = 0.0;
        End_of_timer
  • Stop a timer
    • User stops a timer
      • Activity: reset_timer()
        • Body: timer = 0.0;
        User_stops_a_timer
  • Put timer in pause
    • User puts a timer in pause
    User_puts_timer_in_pause
  • Resume a timer
    • Users resumes a timer
    User_resumes_a_timer

At the end, you should have this State Machine:

SM

In VB

Drag & drop an Hour Display, put “Mode” to “Only seconds” & “Visibility” to hidden.

Create mappings: Three model to simu mapping instances

  • display_timer mapping
    • Check “Seconds” (not “Second”) & select the variable “value”
    • Set “Color” to #000000
    • Check “Visibility” and select visible
    mapping_display_timer
  • paused_timer mapping
    • Set “Color” to #949494
    mapping_paused_timer
  • stopped_timer mapping
    • Check “Seconds” (not “Second”) & set it to 0
    • Set “Color” to #ff0000
    mapping_stopped_timer

In the timeline, add 5 “Timeline Actions” as triggers, with 4 Simu to model mapping instances:

  • (On trigger) requests_start_timer mapping
    • Set the “value” to 5 (seconds)
  • (On trigger) requests_pause_timer mapping
  • (On trigger) requests_resume_timer mapping
  • (On trigger) requests_stop_timer mapping
timeline_triggers

You can finally run the simulation.