Creating Reactive Transitions with Change Events
Overview
In addition to call events and time events, another way to initiate behaviors in a Behavior Execution Engine simulation is through change events.
A change event evaluates a boolean expression over time
and triggers its transition when the expression changes from false
to true
.
In this section, you will use a change event to have the computer detect when the power is on
and then instruct the flight controller to arm (unlock the safety on) the UAV's motors.
This section covers the following concept:
Figure A1: A change event
Prerequisites
Prerequisite | Description |
---|---|
Behavior Execution Engine Installation | You must have installed Behavior Execution Engine. |
Tutorial Project | You must start this section with the Behavior Execution Engine simulation project from the previous section. If you did not complete the previous section, you can use the following project from the Behavior Execution Engine installation: \documentation\tutorialFiles\02\CompositeStates.mdzip |
Recommended Reading |
Before completing this section, you may want to read the following help topic:
|
Instructions
Add arming-related properties and an operation
- Open the project from the previous section.
-
In the System Definition (the BDD),
select the
FlightController
block, click the Create Element button () on the upper-right corner of the block, and select Value Property. -
Enter
isArmed : Boolean = false
as the property's name, type, and default value. - Double-click the new property, set the Visibility to public, and click . You may need to select Expert in the Properties filter in the upper right of the window in order to find the Visibility property.
-
Select the
FlightController
block again, click the Create Element button () on the upper-right corner of the block, and select Operation (or press ). -
Enter
armMotors
as the operation's name. - Double-click the new operation, set the Visibility to public, and click .
-
Select the
Computer
block, click the Create Element button () on the upper-right corner of the block, and select Part Property. -
Enter
armingWaitDuration : FixedDuration
as the property's name and type. - Double-click the new property, set the Visibility to private, and click .
Figure B1: The system definition (BDD)
Set the properties in the instance specifications
-
In the Instance Specification Diagram,
double-click the
FlightController
instance specification and select Slots from the left pane of the specification window. -
In the center pane,
select the
isArmed
slot and click . -
Ensure the value is set to
false
and click . -
In the Containment Tree,
right-click the Moxie-Base > Structure >
FixedDuration
block and select Tools > Create Instance.... -
In the 1. Select parts page,
set the
seconds
value to5
and click . - In the 2. Select a package page, select the Simulation package and click .
- In the 3. Create a diagram page, clear the Create a new diagram checkbox and click .
-
In the Containment Tree,
rename the new
fixedDuration
instance specification toarmingWaitDuration
. -
Drag the Simulation >
armingWaitDuration
instance specification from the Containment Tree into theuav.computer
instance specification on the diagram and click . -
(Optional)
Click the Link button () on the context toolbar next to the
armingWaitDuration
instance specification. Then click on theuav.computer
instance specification to add the link.
Figure B2: The instance specification diagram
Add a call event to the flight controller state machine
-
In the FlightController State Machine diagram,
select Choice from the toolbar to the left of the canvas,
click the transition from
Idle
toReadyForTakeoff
, and then click either or . -
Double-click the transition from the choice pseudostate to the
ReadyForTakeoff
state, set the Guard to the stringisArmed
, and click . -
Select the choice pseudostate,
click the Transition button () on the context toolbar,
and then click the
Idle
state. -
Double-click the new transition,
set the Guard to the string
else
, and click . -
Double-click the transition from the
Idle
state to the choice pseudostate. - In the Trigger category, set the Event Type to CallEvent.
-
Set the Operation to
armMotors()
. - In the Effect category, set the Behavior Type to Opaque Behavior.
-
Set the Body and Language to the string
isArmed = true;
, (optionally) add a line break beforeisArmed = true;
, and click .
Figure B3: The flight controller state machine
Add a change event to the computer state machine
-
In the Computer State Machine diagram,
select State from the toolbar to the left of the canvas,
click the transition from
Idle
toReadyForTakeoff
, and then click either or . -
Enter
ArmingMotors
as the new state's name. -
Double-click the transition from
Idle
toArmingMotors
. - In the Trigger category, set the Event Type to ChangeEvent.
-
Set the Change Expression to the string
powerSystem.isPoweredOn
, - In the Effect category, set the Behavior Type to Opaque Behavior.
-
Set the Body and Language to the string
flightController.armMotors();
, (optionally) add a line break beforeflightController.armMotors();
, and click .
Create a choice that checks the result
-
Select Choice from the toolbar to the left of the canvas,
click the transition from
ArmingMotors
toReadyForTakeoff
, and then click either or . -
Double-click the transition from the choice pseudostate to the
ReadyForTakeoff
state, set the Guard to the stringflightController.isArmed
, and click . -
Select the choice pseudostate,
click the Transition button () on the context toolbar,
and then click the
ArmingMotors
state. -
Double-click the new transition,
set the Guard to the string
else
, and click . -
Double-click the transition from the
ArmingMotors
state to the choice pseudostate. - In the Trigger category, set the Event Type to TimeEvent.
-
Enter
armingWaitDuration
for When, set Is Relative totrue
, and click . - Execute the simulation
and look in the state history trace to observe
that the second region of the computer's composite state remains in the
Idle
state until thepowerSystem.isPoweredOn
property changes totrue
.Figure B4: The second computer state machine region
- Save your work before continuing.
Next Section >