In this tutorial we are going to write a simple program that adds its two inputs and writes them to an output.
Step 1
Open the program palette and drag and drop a Program object onto a wire sheet.
About Slots
To make our program useful we will need to add input and output points to our program. These are called slots.
Every slot has a type that allows other slots with the same type to be connected.
Step 2
Open the kitControl palette and drag and drop two NumericWritable objects onto the wire sheet.
When you add a NumericWritable object to the wire sheet it has three slots available by default Out, In10 and In16.
We are going to need two input slots that are the same type as the NumericWritable out slot and a third slot for the output.
Right-click on the NumericWritable and select Views > Slot Sheet. Notice the Type field for the slot Out is baja:StatusNumeric.
Out input slots are going to be the same type.
Right-click on the program and select Views > Program Editor. Click the Slots tab.
Right-click and select Add Slot. Give it a name of In1 and make the type baja:StatusNumeric. Select the Execute On Change flag and Summary flag.
Click OK.
Step 3
Repeat the above process and create two more slots called In2 and Out, both of type baja:StatusNumeric. The Out slot does not need the Execute On Change flag, but does need the Readonly flag.
Step 4
Select Program Editor > Save and Compile from the top menu to compile your program.
Connect the two NumericWritable objects to the In1 and In2 slots and set the values of the NumericWritable objects (right-click Actions > Set).
Step 5
We now need to write the program to add the values of the inputs. Right-click on the program and select Views > Program Editor.
Add this code to the program:
public void onExecute() throws Exception
{
BStatusNumeric in1 = getIn1();
BStatusNumeric in2 = getIn2();
BStatusNumeric out = getOut(); out.setValue( in1.getValue() + in2.getValue() );
}
Select Program Editor > Save and Compile.
Setters and Getters
You created three slots In1, In2 and Out. In order to access the value of the slot you simply put the word get in front of the name of the slot.
To set a slot value you first use get to get a handle on the slot and then call setValue on the handle.
When you compile the program these functions are automatically made for you and compiled into the program.
Variable types
In this example three variables are created, the code is: BStatusNumeric in1
.
The variable in1 is made of type BStatusNumeric.
To find the type of a slot right-click on an object (NumericWritable in this case) and select Views > Bajadoc Help. This will bring up the official type names for the slots.
Step 6
Open the wire sheet view. The program will only update its Out slot when the inputs change. Set the input NumericWritable objects (right-click Actions > Set) and see the output of your program change.
Summary
In this tutorial you have learned:
- How to add slots to a program
- How to get and set the value of slots
- How to use the
onExecute
event - How to use the Slot Sheet view to find the type of a slot
- How to use the Bajadoc to find the type name for a variable
This example only uses the baja:StatusNumeric type but you can use any type available in Coach AX.
To close the console that appears at the bottom of the screen select Window > Hide Console from the top menu.
Another example
Look at this example:
public void onExecute() throws Exception
{
BStatusNumeric in1 = getIn1();
BStatusNumeric value = new BStatusNumeric(20);
BStatusNumeric out = getOut(); out.setValue( in1.getValue() + value.getValue() );
}
Notice in this example that we have made a BStatusNumeric rather than getting it from a slot.
This program simply adds 20 to the input slot.