Animation in TwinCAT HMI Framework

A keyframe (or keyframe) in animation and filmmaking is a drawing or shot that defines the starting and ending points of any smooth transition. TwinCAT HMI allows us to create and configure animations that can be played using CSS or JavaScript. The Animation API is designed for use within controls.

It is very easy to use the animation framework in TwinCAT HMI.

In our fictitious home automation system, we have the yard door that we shall animate. We shall explain how do we achieve the animation.

Animation is made of numerous frames; when those frames are shown at a certain speed; we perceive the individual frames as moving images. Keyframes are the important frames that contain information on a start/endpoint of an action. A keyframe tells us about two things; first, it tells us what the action of our frame is at a certain point of time; second, it tells us what time that action occurs.

We need to do/use the following STEPS to achieve the animation for a control.

STEP 01:

=> Identify the control name (identifier), we have identified the yard door for our fictitious as “TcHmi_Controls_Beckhoff_TcHmiImage_5”

=> Create an animation object by using JavaScript

var animation = new TcHmi.Animation('TcHmi_Controls_Beckhoff_TcHmiImage_5', '');

Note that the second parameter is empty which selects an HTML element within the control to be animated. An empty string selects the root element of the control.

STEP 02: 

Now we need to add frames, there are few overloaded methods there. We need to select the right one depending on what types of animation we are expecting. The most important properties of addKeyframe methods are:

styles: A collection of CSS properties and their desired values as a dictionary.

progress point :  A value between 0 and 1 inclusive, which determines the point in time of the animation. 0 is the start and 1 is the end of the animation.

animation.addKeyframe('transform', 'translate(0px, 0px)', 0)
 .addKeyframe('transform', 'translate(30px, 30px)', 1);
animation.duration(15000);

Here we are telling the framework that moves the image for 30 pixels in diagonal in 15 seconds.

STEP 03: 

Another important point is what to do when it has done animated (in our example we are transforming the image to the desired location so it will stay there). We need to listen to the animation events. Following are the events that can be emitted by the framework

Name

Value

Description

CONFIGURE

0

The initial status of an animation. This status means that the animation has never been played or reset after it has been played.

INITIALIZED

1

An animation has this status when run() was called, but no effects actually work yet because the CSS has not yet been parsed or a delay is set.

RUNNING

2

A currently running animation.

PAUSED

3

The animation was paused.

ENDED

4

The animation has finished or was skipped with skip().

 

STEP 04:

 Another important p The final JavaScript code is shown in the following snippet. It can be called when an event has occurred. It can be, for example, a button event.


var animation = new TcHmi.Animation('TcHmi_Controls_Beckhoff_TcHmiImage_5', '')
animation.addKeyframe('transform', 'translate(0px, 0px)', 0)
    .addKeyframe('transform', 'translate(282px, 0px)', 1)
    .duration(5000);
animation.registerEventHandler('animationend', function (event) {
    console.log('Animation ended: ' + event.animationName);
    var someControl = TcHmi.Controls.get('TcHmi_Controls_Beckhoff_TcHmiImage_5');
    var t = [{
        "transformType": "Translate",
        "x": 282,
        "xUnit": "px",
        "y": 0,
        "yUnit": "px",
        "z": 0,
        "zUnit": "px",
    }
    ];
    someControl.setTransform(t);
    var formattedSymbol = '%s%PLC1.MAIN.yardGate.YardDoorOpenCommand%/s%';
    TcHmi.Symbol.writeEx(formattedSymbol, false, function (data) {
        if (data.error === TcHmi.Errors.NONE) {
            console.log("Writting YardDoorOpenCommand OK");
        } else {
            console.log("Writting YardDoorOpenCommand NOT OK");
        }
    });
});
animation.run();

Design of the sample:

 

STEP 01:

=>Create an HMI project by visual studio

=>insert the following control from the tool box

Text block: for the fictitious home automation text

Image  control:  for the upper symbol (same source  is used for the actual gate of the yard and the icon )

Text blocks:  for displaying the distance and showing the distance travelled

Actual gate: decorate the actual gate by pillar and the gate image

Open button: for opening the gate

Close button: for closing the gate (we could have used toggle button as well)

Now the HMI content is ready.

 

 

Configure the open button’s event as shown in the following image (the actual complete sample can be download from the site hemelix.com)

Open button | Properties (F4 or the wrench icon) | Show events (the bolt icon) | .onPressed Action

Configure the Close button’s event as shown in the following image (actual code can be checked by downloading the sample from hemelix.com)

Close button | Properties (F4 or the wrench icon) | Show events (the bolt icon) | .onPressed Action

We need a PLC program (for actual animation we don’t need  but we simulates with PLC to show that the data is actually coming from the PLC or OPC UA Server).

-We need to link those interested PLC variables with the HMI control (please see the first example)

-We can download the sample PLC code which comes with this sample project, please download it if you want to run the full example.

Following image shows the Yard door in open position.

Open button opens the door,

Close button close the door,

Distance : it shows the distance travelled from the home position

See also:

 https://www.hemelix.com/scada-hmi/beckhoff-hmi/beckhoff-hmi-installation/

 

Reference:

 https://infosys.beckhoff.com/english.php?content=../content/1033/te2000_tc3_hmi_engineering/36028800864323851.html&id=

https://developer.mozilla.org/en-US/docs/Web/CSS/animation