Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Student Reviews



More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Electrical

Modified on

03 Jul 2023 08:41 pm

Stateflow: A Visual Programming Language for Embedded Systems

logo

Skill-Lync

Control Logic

Control logic is a paramount part of a software program that controls the operations of the program. The control logic answers to commands from the user, and it also acts on its own to execute automated tasks that have been structured into the program. It is also known as the controller and is part of the underlying software architecture developed at the same time that the program is designed and coded.

The control logic can be modelled using a state diagram, which is a form of a hierarchical state machine. These state diagrams can also be merged with flow charts to provide a set of computational semantics for describing complex control logic. Control logic is the part of a controller that defines how a reactive system responds to events or conditional changes.

Applications for control logic include:

  • Supervising how to switch between distinct modes of operation.
  • Plan a succession of tasks for a given system.
  • Undergoing fault detection, isolation, and recovery logic.

Common tasks for modeling control logic include:

  • Modeling hierarchical state machines for large-scale systems.
  • Adding flow charts to depict complex control logic.
  • Specifying orthogonal states to illustrate systems with parallelism.

New programming methods:

A programmable Logic Controller (PLC) is an industrial process control computer that has been adapted to control the manufacturing process since all manufacturing processes are modernized and automated by adopting this technology.

PLCs from various manufacturers can be programmed in diverse ways. IEC 61131-3 is the international standard for programmable logic controllers.

The most general PLC programming methods used are:

  • Relay Ladder Logic (RLL)
  • Structured Text (ST)
  • Function Block Diagram (FBD)
  • Sequential Function Charts (SFC)
  • Instruction List (IL)

Finite State Machine

Finite State Machine (FSM) is a formulation used to describe the processes during which information or tasks move from one state to another for action, according to a set of rules. The system makes a transition from one state to another if the condition defining the change is true. A system where particular inputs cause particular changes in state can be represented using finite state machines. A state transition diagram is a graphical representation of a finite state machine. Hence, a finite state machine is a model of behavior composed of states, transitions, and actions. 

For example, we can use a state machine to represent the automatic transmission of a car. The transmission has operating states namely: park, reverse, neutral, drive, and low. When the driver shifts from one position to another, the system makes a transition from one state to another, which implies from neutral to drive in this cited example.

  • Representing a Finite State Machine

Conventionally, designers used truth tables to illustrate relationships among the inputs, outputs, and states of a finite state machine. The resulting table describes the logic necessary to control the behavior of the system undergoing study. Another approach is to design an event-driven system to model the behavior of the system by describing it in terms of transitions among states. The occurrence of events under certain conditions determines the state that is active. State-transition charts and bubble charts are graphical representations based on this technique.

The below figure depicts an FSM model for a simple door control in which the door has two states: open and close. These two states can be mutually changed into each other depending upon transition conditions resulting from corresponding events.

Stateflow

'States' and 'transitions' form the basic building blocks of a sequential logic system.

Stateflow provides a graphical language that includes state transition diagrams, flow charts, state transition tables, and truth tables. We can use Stateflow to describe how MATLAB algorithms and Simulink models react to input signals, events, and time-based conditions. A Stateflow chart can utilize MATLAB or C as the action language to implement the control logic. We can also represent stateless flow charts.

Applications:

Stateflow enables us to design and develop supervisory control, task scheduling, fault management, communication protocols, user interfaces, hybrid systems, etc.

Now, let us get familiar with some of the primary terms.

  1. States - One of the key concepts in computer programming is the idea of a state that is essentially a snapshot of the measure of various conditions in the system. A state stores information about the past, that is, a state reflects the input changes from the system start to the present moment.
  2. Action - An action is a description of the activity in a control system that is to be performed at a given moment, and has an influence on something. In some cases, one action can include several sub-activities.

Types of State Action:

State actions define what a Stateflow chart does when a state is active. The most common types of state actions are entry, during, and exit actions.

  • We can specify the type of state action by its complete keyword (entry, during, exit) or by its abbreviation (en, ex, du).
  • We can also combine state action types with commas.
  1. Transition - A transition indicates a change between states and is described by a condition that is needed to be fulfilled to enable the transition.

Basically, State’ and ‘transition’ are instructions that we write inside a state or next to a transition to define how a given Stateflow chart will behave during the simulation.

Truth Table

It illustrates logical decision-making behavior using conditions, decisions, and actions.

The Truth Table block is a truth table function that uses MATLAB as the action language. When we want to use truth table logic directly in a Simulink model, use this block. This block requires Stateflow.

When we add a Truth Table block directly to a model instead of calling truth table functions from a Stateflow chart, these advantages apply:

  • It is a more direct approach than creating a truth table within a Stateflow chart, especially if our model requires only a single truth table.
  • We can define truth table inputs and outputs with inherited types and sizes.
  • The Truth Table block works with a subset of the MATLAB language that is optimized for generating embeddable C code. This block generates content as a MATLAB code. As a result, we can take advantage of other tools to debug your Truth Table block during the simulation. If we double-click the Truth Table block, the Truth Table Editor opens to display its conditions, actions, and decisions.

With the aid of the Truth Table Editor, one can implement the following:

  • Enter and edit conditions, decisions, and actions.
  • Add or modify Stateflow data and ports by using the Ports and Data Manager.
  • Run diagnostics to detect parser errors.
  • Visualize generated data after the simulation.

Hierarchy of Stateflow Objects

One Stateflow object can contain other Stateflow objects.

  • Representing a Stateflow Chart

A Stateflow chart can contain sequential and combinatorial logic in the form of state transition diagrams, flow charts, state transition tables, and truth tables. One can include Stateflow charts as blocks in a Simulink model. The collection of these blocks in a Simulink model is the ‘Stateflow machine.’ Stateflow charts can contain states, boxes, functions, data, events, messages, transitions, junctions, and annotations. States, boxes, and functions can contain other states, boxes, functions, transitions, junctions, annotations, data, events, and messages. The level of nesting can be continued indefinitely.

Now, without further delay, let's discuss how a Stateflow chart is created in Simulink.

  • Creating a Stateflow Chart

The vital components of the Stateflow Editor are the object palette, the chart canvas, and the Symbols pane.

(a) The chart canvas is a drawing area where we will now create a chart by combining states and transitions with other graphical elements.

(b) On the left side of the canvas, the object palette displays a set of tools for adding graphical elements to our created chart.

(c) On the right side of the canvas, in the Symbols pane, we add new data to the chart and resolve any undefined or unused symbols.

Adding States and Transitions

(1) From the object palette, click the State icon and move the pointer to the chart canvas. A state with its default transition appears. To place the state, click a location on the canvas. When the text prompts, enter the state name ‘first’ and the state action, if any.

(2) Add another state. Right-click and drag the ‘first’ state. Blue graphical cues help us to align our states horizontally or vertically. The name of the new state changes to ‘last’. Click on the name of the state to change it now to ‘second.’

(3) Replicate the above step to form the state named ‘third’ and ‘fourth.’

(4) Realign the above states. Blue transition cues indicate several ways in which we can connect the states. To add transitions, click the appropriate cue.

(5) Alternatively, to draw a transition, click and drag from the edge of one state to the edge of the other state.

(6) Double-click each transition and type the appropriate transition condition. The conditions will appear inside the square brackets.

(7) Now, go to the 'Symbols Pane' option under the 'SIMULATION' tab on the tool strip and specify all the variables used in your model as either 'Input' or 'Output' so that the model gets to know the variables which you have employed.

Let us understand the above concept we came across by studying the following example.

Example 1:

Consider the below Stateflow problem. If the value of the input is lesser than 10, then the system outputs -30 and if the input is greater than 10, it outputs 20. Let’s get to know how to implement this in a state machine.

Approach:

(1) Open Simulink by going to the MATLAB toolstrip. Click on New ----> Simulink Model ----> Create Model.

(2) Search for Chart and double-click inside it to go to the chart mode.

(3) Now follow the above-discussed steps to add States and Transitions.

(4) We will validate the input value against the set of conditions. Here we have created two states named ‘Max’ and ‘Min’. Below a control logic has been implemented in the chart mode of Simulink.

The ‘en’ keyword has been used to define the entry. We used an output variable named ‘value’ equal to 20. Similarly, ‘value’ equals to -30 in the ‘Min’ state. Appropriate transition conditions have been given beside the arrows.

We created two variables, namely ‘input’ and ‘output’ in the Symbols Pane option under the Simulation tab.

Configuration of the Repeating Sequence block:

Here, we have employed definite output values for the different instant of time values. Therefore, this will subsequently generate a repeating sequence of numbers as per the user input. We have added a Scope block so as to visualize the results of our implemented system.

The complete Simulink block has been depicted below where we have inculcated the repeating sequence block as an input to the problem to gain a continuous result with respect to time.

Results:

After clicking the ‘Run’ in the Simulink toolbar, the below results are obtained from the scope block.

The red signals depict the values being given as an input to our problem and the blue signals illustrate the output obtained. We can clearly analyze that the plotted data is verified as per our pre-specified conditions.

Hope you got a brief cognizance regarding the implementation and the execution of a Stateflow problem in the MATLAB Simulink software.


Author

author

Navin Baskar


Author

blogdetails

Skill-Lync

Subscribe to Our Free Newsletter

img

Continue Reading

Related Blogs

Electric PowerTrain for Two Wheelers

The average vehicle body gives any vehicle its structural integrity, while the electric motor is concerned with the generation of torque or force. The electric motor can be a permanent magnet synchronous motor, brushless dc motor etc.

Electrical

25 Aug 2022


How To Tune PID Controllers for Converter Feedback

In industrial control applications, a PID controller is a device that regulates temperature, flow, pressure, speed, and other process variables. PID (proportional integral derivative) controllers, which use a control loop feedback mechanism to control process variables, are the most accurate and trustworthy controllers.

Electrical

27 Aug 2022


Pulse Charging of Lithium Ion Battery

A lithium-ion (Li-ion) battery is a complicated battery technology that uses lithium ions as a key component of its electrochemistry. During a discharge cycle, lithium atoms within the anode are ionized and separated from their electrons.

Electrical

29 Aug 2022


A Guide on the Dashboard Library Blockset

Dash in your car is your source of information. It basically functions as a control panel that sits in front of the driver and shows numerous controls and instrumentation required for your car to function.

Electrical

01 Sep 2022


All About Series Hybrid Vehicles

A hybrid vehicle has two power sources for motion within the same vehicle. Basically the hybrid vehicle is split into two types supported the source for propulsion - combustion engine type and Hydrogen power cell.

Electrical

07 Sep 2022



Author

blogdetails

Skill-Lync

Subscribe to Our Free Newsletter

img

Continue Reading

Related Blogs

Electric PowerTrain for Two Wheelers

The average vehicle body gives any vehicle its structural integrity, while the electric motor is concerned with the generation of torque or force. The electric motor can be a permanent magnet synchronous motor, brushless dc motor etc.

Electrical

25 Aug 2022


How To Tune PID Controllers for Converter Feedback

In industrial control applications, a PID controller is a device that regulates temperature, flow, pressure, speed, and other process variables. PID (proportional integral derivative) controllers, which use a control loop feedback mechanism to control process variables, are the most accurate and trustworthy controllers.

Electrical

27 Aug 2022


Pulse Charging of Lithium Ion Battery

A lithium-ion (Li-ion) battery is a complicated battery technology that uses lithium ions as a key component of its electrochemistry. During a discharge cycle, lithium atoms within the anode are ionized and separated from their electrons.

Electrical

29 Aug 2022


A Guide on the Dashboard Library Blockset

Dash in your car is your source of information. It basically functions as a control panel that sits in front of the driver and shows numerous controls and instrumentation required for your car to function.

Electrical

01 Sep 2022


All About Series Hybrid Vehicles

A hybrid vehicle has two power sources for motion within the same vehicle. Basically the hybrid vehicle is split into two types supported the source for propulsion - combustion engine type and Hydrogen power cell.

Electrical

07 Sep 2022


Book a Free Demo, now!

Related Courses

https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/pg-certification-embedded-systems_1627925126.jpg
PG Program in Embedded Systems for EV Applications
4.8
198 Hours of content
Embedded Domain
Know more
https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/embedded-c-essentials_1645101913.jpg
5
35 Hours of content
Embedded Domain
Showing 1 of 2 courses