# Tutorial 6: Steering a Locomotive with Omniverse Action Graph


This tutorial shows how to use Omniverse Action Graph to 
control a locomotive and a switch. On how to use Action Graph
it is assumed that you are familiar with the basics of
Omniverse and Action Graph. If not, please refer to the
following links:

- [Introduction to OmniGraph](https://docs.omniverse.nvidia.com/extensions/latest/ext_omnigraph/tutorials/gentle_intro.html)
- [Action Graph Quickstart](https://docs.omniverse.nvidia.com/extensions/latest/ext_omnigraph/tutorials/quickstart.html)

You can find the project for this tutorial in the data folder under
the name 'data/ActionGraph/ActionGraph_DKW.usda'.


## Step 1 - Load a Project.

From the data folder of the railOmniverse extension, load the
'TestDKW.usda' project. This project contains a locomotive and a
tracksystem with a double slip switch. 

- Save this project under a new name and from the menu select Window->Visual Scripting->Action Graph.
- In the Action Graph window, click on the 'New Action Graph' panel and create a new Action Graph.


## Step 2 - Control the Target Velocity.

- From the 'Stage' panel, find the Root/TRAXX_147/WheelFrameNorth/WheelFrame node and drag and drop it 
to the Action Graph window. 

<a href="./Images/ActionGraph1.png">
<img src="./Images/ActionGraph1.png" title="Action Graph" 
alt="Creating an Action Graph node for a WheelFrame." width="500px">
</a>

- From the context menu that appears select 'Write Attribute'.
- Select the created 'WRITE' node and from its properties panel, select the 'Attribute Name' from the 
dropdown list to be 'physics:targetVelocity'.
- Repeat the above steps for the 'Root/TRAXX_147/WheelFrameSouth/WheelFrame' node.

We want to adjust the target velocity of the locomotive's wheelframe incrementally, every time the
'V' key is pressed. To lower the velocity, we will use the 'V+Shift' key. For that we introduce a
seperate variable, 'TargetVelocity', that we will increment or decrement based on the key press.

- From the 'Action Graph' panel, select 'Variables' and hit the '+Add' button to create a new variable.
- Name the variable 'TargetVelocity' and set its type to 'Float'.
- Drag and drop the 'TargetVelocity' variable to the Action Graph window and select 'Read Variable'.
- Create two 'Constant Float' node and set their values to 5 and -5 respectively.
- Create two 'Add' nodes and connect the 'TargetVelocity' node to the first input of each 'Add' node.
- Connect the 'Constant Float' nodes to the second input of the 'Add' nodes.
- Drag and drop the 'TargetVelocity' node to the Action Graph window and select 'Write Variable'.
- Do this again, to create a second 'Write Variable' node for the 'TargetVelocity' variable.
- Connect the 'Add' nodes to the 'Write Variable' nodes.
- Connect the 'Pressed' output of the 'V' key to the first 'Write Variable' node and the 'Pressed' output of the 'V+Shift' key to the second 'Write Variable' node.

The graph now should look like this:

<a href="./Images/ActionGraph2.png">
<img src="./Images/ActionGraph2.png" title="Action Graph for TargetVelocity" 
alt="The Action Graph for the target velocity variable." width="500px">
</a>

- To actually write the target velocity to the locomotive's wheelframes, we use the 'On Variable Change' node, to trigger the 'Write Attribute' nodes.
- The 'Value' input we get from our 'TargetVelocity' variable, but we need to convert it to string, using the 'To String' node.
- Also append a string constant of 'km/h' to the string and then connect it to the 'Value' input of the 'Write Attribute' nodes.

The final graph should look like this:

<a href="./Images/ActionGraph3.png">
<img src="./Images/ActionGraph3.png" title="Full Action Graph for TargetVelocity" 
alt="The Action Graph for the target velocity." width="500px">
</a>

- Start the simulation and hit the 'V' key. The locomotive should start moving.

Be aware that the wheelframes need some thrust and brake values [0,1] to be preset. These are 
the fraction of the maximum thrust and brake force that the wheelframes can apply. In the sample
project there are Action Graphs provided to set these values by keyboard.


## Step 3 - Control the Switch.

The switch in the system can be found under: TrackSystem0/ConnectorCollection/Switch1.
It has two states, 'go' and 'branch'. To connect a state wit a key press, only two nodes are needed.

- Create a new Action Graph.
- Drag and drop the 'Switch1' node to the Action Graph window and select 'Write Attribute'.
- Set the 'Attribute Name' to 'state'.
- For 'Value' select 'go' in the property panel of the 'Write Attribute' node.
- Connect this with an 'On Keyboard Input' node and set the 'Key' to 'S'.
- Repeat the above steps for the 'branch' state and the 'A' key.

The final graph should look like this:

<a href="./Images/ActionGraph4.png">
<img src="./Images/ActionGraph4.png" title="Full Action Graph for the Double Slip Switch" 
alt="The full Action Graph for the Double Slip Switch." width="500px">
</a>