Using VisSim State Charts Visual Solutions, Inc. 487 Groton Road, Westford MA 01886 USA (800) VISSIM-1 www.vissim.com
Jan 18, 2016
Using VisSim State Charts
Visual Solutions, Inc.487 Groton Road, Westford MA 01886 USA
(800) VISSIM-1www.vissim.com
Agenda• Creating State Charts• Hierarchy• Transitions• Debugging/Logging• Embedded Considerations
What is a State Chart?• A graphical representation of a finite-state machine
– A system with a fixed number of named states. Transitions between states occur when certain conditions are met.
• VisSim State Charts are OMG UML 2.1 conformant• Create state charts anywhere in a VisSim diagram• Exchange data between the state chart and the
continuous portion of a diagram– Use VisSim variables or Triggers in chart, as well as
defined input and output pins
Example State Chart
Basic state chart elements• States – set of named system configurations• Transitions - control movement between states• Pseudo states – Alter transition operation• Behaviors- code associated with transitions
and state events• Triggers – named events that can cause
transitions or code to be executed
Creating a State Chart
• Insert a State Chart block into a VisSim diagram.• Right click drills down and enters State Chart world
– Notice normal VisSim blocks are disabled and State Chart elements are enabled
• Insert an Initial State and two states
Creating Transitions• 1. Position the cursor over the edge of a source state. The
cursor shape changes to a pencil– If the cursor is a crosshair with arrow heads, moving the mouse moves
the state rather than draws the arc.• 2. Depress the mouse and drag into the target state body.
– If the state is a composite state, you may connect to a state within the composite state.
• 3. Release the mouse button.
Moving Transitions
• Click the mouse over a transition you want to bend or move.– The transition line turns purple, indicating that you can edit it.
• To bend the transition, left click on it and drag the mouse.– Labels will move with the arc
• To move where the transition arc connects to a state edge, position the cursor over the end of the arc you want to move. – When the cursor changes to a 4-arrow crosshair, depress and drag the mouse
to the desired location.
Controlling Transitions
• You can use triggers and C expressions to control if and when transitions are taken
• Double click on Transition to edit• Transition syntax: trigger(s) [guard] / C-code
– Guard is C expression– Triggers and guard are optional, but ‘/’ is not– All triggers and guard must be true to execute
transition and C-code
Triggers• Triggers are True or False• Declare in Continuous space via Trigger Block• or Data Browser (type = Trigger)• Assign value in continuous space
– Like VisSim variables only one definition point• or State Chart Action via SendT(<trigname>)• Reference in State Chart Transitions and
Actions.
Deferred Triggers
• If you defer a Trigger in a state, the state will remember any True setting and hold it for one tick after the state is exited. – This makes it available for an action in the next
state transitioned to for 1 tick.– defer a trigger by adding to Deferred Triggers list
in State Activity Manager
Transition Evaluation Order• Multiple output transitions have an evaluation order
indicated by number near start of arc• To change, right click transition, select “Reorder
Transitions”– Click transitions in order of desired execution
• Transitions with no trigger are always evaluated after transitions with trigger (regardless of order number)
• At most one transition fires per Chart Region during one simulation or codegen timer tick.
Defining state chart variables
• To exchange data between the VisSim model and the state chart, use variables.– State chart variables are declared in the State Chart Block Properties– Activate View > Connector Labels to see the pin names
• To define input and output variables:– Add Connector command (or toolbar button)– Or ctrl+right click the state chart block and click Data Browser. Data
Browser gives you ability to choose name, data type and storage for each variable
Data Browser• Click (new item icon) to create new variable• Type
– char .. double = data type as in C– Trigger = Boolean (True or False)– State ID = currently active state
• Scope– Local = only seen within State Chart– Input/output = input/output pins– Constant = constant value– External = Seen outside State Chart
Data Browser (cont.)• Pin – assigned to inputs and outputs• Default value – initial value before 1st
assignment• Value – current value• Comment – whatever you like• To edit a variable, double click the property
Configuring States• Right click in State title bar to open properties
– Options tab gives name and comment fields and shows some activity code (not editable)
• Activity Manager Tab lets you associate C-code to be called on certain events (actions)– Entry – on State entry– Exit – on State exit– Do – while in State– On trigger – when in State and trigger is True
Allowable C-code in Activity• ANSI C expressions, statements and
functions are OK.– Can put initial C file in Sim Properties>
Preferences > startup script file• Standard library math functions (pow,
abs, exp, min, max etc)• No preprocessor commands supported
(#define, #if, #else)
Creating Actions
• Click on the action or trigger you wish to activate C-code then click “Add Action”– Code is inserted in “Edit Behavior” and may be
edited further.• Action code syntax
– {action name}/{C-code} i.e. Entry / startup = 1;
Controlling State Chart Execution• Initial State - indicates starting point for State
Chart, Submachines and Composite States• Terminate - indicates final state end point.
– Exits from Submachine or Composite region• Choice – expression selects destination state• Fork – Notational convenience for entering
multiple regions in Composite State• Join– Notational convenience for leaving
multiple regions in Composite State
Transition Choice
if (A) { /* exit State One*/ B;
if (C) {D; /*Enter State Two*/ }
else if (E){ F; /*Enter State Three*/ }
}
• It is a FATAL ERROR to enter a choice but not take exit leg to a state.– Only place State Charts can halt besides
Terminate
To avoid Choice FATAL ERROR
• Always have one exit leg with no predicate– Check Integrity will check for this.
• Don’t use Choice, use Join with predicates. That way, current state will never be left unless possible path exists.
Interacting with State Charts• To send data from the continuous portion to a
State Chart you can use:– State Chart input/output pins– VisSim variables– Triggers
• To read the current state in the continuous portion– Use an output pin set to State ID– Use a state id block.
Controlling VisSim execution• Use state id block “is active” mode• Test “State Id” output pin of state against state
id block in “State Id” mode.– State ID values are unique but can change. Don’t
depend on actual value. Values change between simulation and code generation and as states are added and removed.
• Can use merge to select data or enabled Compound block to control execution
Hierarchical States
• You can encapsulate a state chart within a state chart by using a Submachine state.– Enter a Submachine by double click on title bar
• You can use Initial State or Terminate to enter and exit, or
• Use Entry and Exit points for different initial and final states.– Entry and exits are placed inside the Submachine
and will automatically appear on Submachine face
Parallel State Execution Regions• Use Composite State for multiple active states
– Right click in Composite body, select insert vertical/horizontal region to create new region
– To delete region, drag dashed line off screen.• Use Initial State to indicate first state in region• Use Terminate State to end region. When all regions
are done, can leave Composite State• Can transition directly from state in region to state
outside Composite State– Other regions active states are ended immediately
History
• If you transition directly out of a Composite Region without termination of other regions, and a history element is present, the history element will remember currently active states.
• If you renter Composite Region via History, prior active state(s) will be entered.– See examples ShallowHistory001&002
Deep History vs Shallow
• Shallow history only remembers active state(s) on current layer of Composite or Submachine.
• Deep history remembers all active states in all nested Composite and Submachines.
Debugging State Charts• You can set break points or log action for any
action in any state or transition– Right click to open properties, click on action, and
check “Attach Breakpoint” (red ball appears) or “Log Message” (red exclamation appears)
• When sim is run and action occurs, sim will stop at time step for breakpoint. Data Browser contains current data values.
VisSim Error Reporting
• The menu item “Check Integrity” will scan and report on ill-formed charts.
– Click error, then use arrow and hand to jump to state in error
– “Check Integrity”is also run when you click “Go”– States with errors are highlighted.
Breakpoints
• Menu item State Charts > Breakpoints…• Shows list of Breakpoints and Log points• Can remove one or more BPs• Can jump to next occurance of BP
Logging Actions
• To log actions, right click state or transition to open properties, click on action, and “Log Message” (red exclamation appears on state, red ball on transition)
• Place identifing text in the edit box to the right of “Log Message” to be entered into the event log
• To see the events in the log, leave the state chart and place an standard VisSim Block > Signal Consumer > Event Display block into the workspace.• When the simulation is run, time stamped events will
appear in the event log.
Logging C-code execution
• To enable C-code logging, Ctrl-Right Click on the enclosing State Chart and enable "Log executed C instructions“, then press Ok
• Run the diagram• Ctrl-Right Click on the chart again and click “Open”
Embedded Considerations• Compiled State Chart should work the same as
simulated, so simulate as much as you can• Compiled States IDs may be different numerical values
than simulated. – State Lookup… menu item has “embedded” option– sim ID->embedded ID convert block in works
• For low freq state change, use State ID from Data Browser as output from top level compound.
• For high freq state change use Monitor Buffer Write
Embedded Operations to Avoid
• Terminate – halts operation• Choice without Else
Q&A