Creating Reactive Transitions with Change Events

Overview

In addition to call events and time events, another way to initiate behaviors in a Moxie 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
Moxie Installation You must have installed Moxie.
Tutorial Project You must start this section with the Moxie simulation project from the previous section. If you did not complete the previous section, you can use the following project from the Moxie 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

  1. Open the project from the previous section.
  2. 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.
  3. Enter isArmed : Boolean = false as the property's name, type, and default value.
  4. Double-click the new property, set the Visibility to public, and click Close. You may need to select Expert in the Properties filter in the upper right of the window in order to find the Visibility property.
  5. Select the FlightController block again, click the Create Element button () on the upper-right corner of the block, and select Operation (or press CtrlAltO).
  6. Enter armMotors as the operation's name.
  7. Double-click the new operation, set the Visibility to public, and click Close.
  8. Select the Computer block, click the Create Element button () on the upper-right corner of the block, and select Part Property.
  9. Enter armingWaitDuration : FixedDuration as the property's name and type.
  10. Double-click the new property, set the Visibility to private, and click Close.

Figure B1: The system definition (BDD)

Set the properties in the instance specifications

  1. In the Instance Specification Diagram, double-click the FlightController instance specification and select Slots from the left pane of the specification window.
  2. In the center pane, select the isArmed slot and click Create Value.
  3. Ensure the value is set to false and click Close.
  4. In the Containment Tree, right-click the Moxie-Base > Structure > FixedDuration block and select Tools > Create Instance....
  5. In the 1. Select parts page, set the seconds value to 5 and click Next >.
  6. In the 2. Select a package page, select the Simulation package and click Next >.
  7. In the 3. Create a diagram page, clear the Create a new diagram checkbox and click Finish.
  8. In the Containment Tree, rename the new fixedDuration instance specification to armingWaitDuration.
  9. Drag the Simulation > armingWaitDuration instance specification from the Containment Tree into the uav.computer instance specification on the diagram and click OK.
  10. (Optional) Click the Link button () on the context toolbar next to the armingWaitDuration instance specification. Then click on the uav.computer instance specification to add the link.

Figure B2: The instance specification diagram

Add a call event to the flight controller state machine

  1. In the FlightController State Machine diagram, select Choice from the toolbar to the left of the canvas, click the transition from Idle to ReadyForTakeoff, and then click either Before Transition or After Transition.
  2. Double-click the transition from the choice pseudostate to the ReadyForTakeoff state, set the Guard to the string isArmed, and click Close.
  3. Select the choice pseudostate, click the Transition button () on the context toolbar, and then click the Idle state.
  4. Double-click the new transition, set the Guard to the string else, and click Close.
  5. Double-click the transition from the Idle state to the choice pseudostate.
  6. In the Trigger category, set the Event Type to CallEvent.
  7. Set the Operation to armMotors().
  8. In the Effect category, set the Behavior Type to Opaque Behavior.
  9. Set the Body and Language to the string isArmed = true;, (optionally) add a line break before isArmed = true;, and click Close.

Figure B3: The flight controller state machine

Add a change event to the computer state machine

  1. In the Computer State Machine diagram, select State from the toolbar to the left of the canvas, click the transition from Idle to ReadyForTakeoff, and then click either Before Transition or After Transition.
  2. Enter ArmingMotors as the new state's name.
  3. Double-click the transition from Idle to ArmingMotors.
  4. In the Trigger category, set the Event Type to ChangeEvent.
  5. Set the Change Expression to the string powerSystem.isPoweredOn,
  6. In the Effect category, set the Behavior Type to Opaque Behavior.
  7. Set the Body and Language to the string flightController.armMotors();, (optionally) add a line break before flightController.armMotors();, and click Close.

Create a choice that checks the result

  1. Select Choice from the toolbar to the left of the canvas, click the transition from ArmingMotors to ReadyForTakeoff, and then click either Before Transition or After Transition.
  2. Double-click the transition from the choice pseudostate to the ReadyForTakeoff state, set the Guard to the string flightController.isArmed, and click Close.
  3. Select the choice pseudostate, click the Transition button () on the context toolbar, and then click the ArmingMotors state.
  4. Double-click the new transition, set the Guard to the string else, and click Close.
  5. Double-click the transition from the ArmingMotors state to the choice pseudostate.
  6. In the Trigger category, set the Event Type to TimeEvent.
  7. Enter armingWaitDuration for When, set Is Relative to true, and click Close.
  8. Run () the simulation and observe that the second region of the computer's composite state remains in the Idle state until the powerSystem.isPoweredOn property changes to true.

    Figure B4: The second computer state machine region

  9. Save your work before continuing.

Next Section >