The boolean flags never changes in finite state flow chart of state machine implementation.

7 views (last 30 days)
Hello everyone,
I am working on implementing a state machine to coordinate the actions of two vehicles, SWAMP1 and SWAMP2. Here’s the intended behavior:
  1. Initial State (Stop)
  • SWAMP1 starts at rest and sends an event (SWAMP1Ready) to indicate its readiness to SWAMP2.
  • SWAMP2 receives this event, responds with its own readiness event (SWAMP2Ready), and sets its flag to true (SWAMP2Flag = true).
  • Once SWAMP1 receives the event from SWAMP2, it also sets its flag to true (SWAMP1Flag = true).
2. Transition to Move
  • The system transitions from Stop to Move when both flags are true (SWAMP1Flag && SWAMP2Flag) and the distance to the edge exceeds a threshold (distanceToEdge > threshold).
3. Transition to Stop
  • Once the vehicles reach the edge threshold, they transition back to Stop.
4. Transition to Turn
  • If both flags remain true, the system transitions from Stop to Turn.
  • The vehicles continue turning until the turning angle exceeds 90 degrees (turningAngle >= 90).
5. Restart the Process
  • After turning is completed, the system transitions back to Stop, and the cycle repeats.
Here is how I implemet it:
I’ve built a Stateflow chart with two parallel (AND) states, SWAMP1 and SWAMP2, each having Stop, Move, and Turn substates. I use local events (SWAMP1Ready, SWAMP2Ready) to do a handshake so that both SWAMPs set flags (SWAMP1Flag, SWAMP2Flag) to true before transitioning from Stop to Move. I’ve verified:
  • The top‐level decomposition is set to Parallel (AND).
  • My input signals (like distanceToEdge1) start out above the threshold (0.1).
  • I’m sending SWAMP1Ready and SWAMP2Ready exactly as recommended (“on SWAMP1Ready: …”, etc.).
  • SWAMP1Flag and SWAMP2Flag remain stuck at false, and both SWAMPs stay in Stop.
Despite all this, the states never leave Stop and the flags never become true. What could be causing my local events not to be recognized in the parallel state, and how can I fix this so the chart transitions correctly from Stop to Move?
Relevant details:
Classic Stateflow chart, local events, booleans cleared on entry to Stop, then set to true in the on event handler. Distance signals come from Step blocks set to values > 0.1.
Here is swampaction.m file to define the outputs and a pcture of SWAMP1 substate. You can find the simulation block in the attachment.
I would truly appreciate any insights.
Best regards
classdef swampaction < Simulink.IntEnumType
enumeration
Stop(0),
Move(1),
Turn(2)
end
methods (Static)
function defaultValue = getDefaultValue()
defaultValue = swampaction.Stop;
end
function dScope = getDataScope()
dScope = 'Auto';
end
function desc = getDescription()
desc = 'Action commands for SWAMP: Stop, Move, Turn.';
end
end
end

Accepted Answer

Altaïr
Altaïr on 20 Feb 2025
Edited: Altaïr on 20 Feb 2025
Based on the described logic, SWAMP2Flag becomes true only if SWAMP1Flag is true, and vice versa. However, both flags initially start as false, and there's no current mechanism for either flag to become true. For instance, a self-loop could be implemented to set SWAMP1Flag to true after 1 second if its current value is false, as shown below:
Additionally, the send statement in the SWAMP2 state seems to be misplaced, leading to rapid switching of the SWAMP2Action between Stop and Turn. Note that the start time of the ramp input was adjusted to 17 seconds.
Moving the send statement to the end of the exit action, as demonstrated below, resolves this issue.
For more information on controlling charts with message activity, the following command can be used to access the relevant documentation:
web(fullfile(docroot, 'stateflow/ug/message-operations.html'))
  2 Comments
mehrzad
mehrzad on 21 Feb 2025
Thanks alot @Ashok that solves the problem.
Just One question: I still don't get it why placing send(SWAMP2Ready) before setting the flage (SWAMP2Flag) to true makes the state flactuate between stop and turn?
Altaïr
Altaïr on 21 Feb 2025
The issue seems to arise when several conditions are simultaneously satisfied:
  • Both turning angles are less than 90 degrees.
  • SWAMP1Ready and SWAMP2Ready are true.
  • The last executed state in SWAMP1 is Turn.
  • The "on SWAMP1Ready:" action in the Stop state inside SWAMP2 is the currently executing action.
The sequence of events that follow lead to continuous toggling of SWAMP2Ready between true and false. To diagnose the problem, consider setting breakpoints along various transitions, as well as in the Stop and Turn states of SWAMP1 and SWAMP2. Monitoring the changes in different symbols as the breakpoints are triggered can provide insights into the issue.

Sign in to comment.

More Answers (0)

Categories

Find more on Syntax for States and Transitions in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!