The goal of this example is to demonstrate how to setup a Virtual Joystick and control its features using modified values from a physical joystick. This assumes that the reader is familiar with the basics of creating Building Blocks as shown in the Detailed Tutorial.
The CH Products CH Pro Throttle USB has 19 buttons, three axes (X, Y, and Z), and a hatswitch. The values from the axes have a theoretical range of 0–255, but the actual values they produce do not cover that entire range. Also, the Z-Axis is seemingly reversed – its value decreases as the slider is pushed away from the user. In some applications, it would be more natural for the value to increase as the slider is pushed away from the user. This example will create a virtual joystick that contains the same controls as the CH Pro Throttle USB, and will be controlled with modified values from the actual device.
The first step is to create a virtual joystick which has the same controls as the CH Pro Throttle USB.
Right-click in the list of Programming Items and select Create Virtual Device ‣ Virtual Joystick.
Enable the Z-Axis and Hatswitch options, and set the number of joystick buttons to "19".
For this example, the behavior of the buttons and hatswitch can remain unchanged. Therefore, building blocks for those controls on the actual device are connected directly to building blocks for the corresponding controls on the virtual device. A few examples are shown here.
The axis values from the CH Pro Throttle USB will be modified before they are connected to the axes on the virtual joystick.
In each case, the Axis building block for the control on the CH Pro Throttle USB is connected to an Axis Calibration building block. The control is then moved through its entire range to determine the minimum and maximum values that its building block reports. Those values are then used as the Min and Max values of the Expected input range of the Axis Calibration block.
A Virtual Axis block is then connected to the Axis Calibration block to pass the modified value to the virtual joystick.
The Axis Calibration block that modifies the value from the Z-Axis is also used to reverse the behavior of the axis. This is done by exchanging the Min and Max values of the Calibrated output range. When the Min value of the output range is larger than the Max value, then the output values will increase when the input values decrease, and vice versa.
After connecting all controls on the CH Pro Throttle USB to the virtual joystick, other applications can be configured to use the virtual joystick "Calibrated Throttle" instead of the actual CH Pro Throttle USB.