Input device: Difference between revisions

Jump to navigation Jump to search
m
<syntaxhighlight>
(→‎Joystick or yoke?: Yes and no templates)
m (<syntaxhighlight>)
Line 60: Line 60:
# Look under Help > Joystick Information and check the name behind "Joystick #0:".  
# Look under Help > Joystick Information and check the name behind "Joystick #0:".  
# Open your joystick's bindings file in a XML editor and add the following code to the file, below the already-present <code><name></code> tags.
# Open your joystick's bindings file in a XML editor and add the following code to the file, below the already-present <code><name></code> tags.
#:<pre><name>The EXACT name you found under step 2; including spaces, capitals etc.</name></pre>
# <syntaxhighlight lang="xml"><name>The EXACT name you found under step 2; including spaces, capitals etc.</name></syntaxhighlight>
# Please report this name [http://forum.flightgear.org/viewforum.php?f=24 at our forum], so it can be added to the official file (and next releases).
# Please report this name [http://forum.flightgear.org/viewforum.php?f=24 at our forum], so it can be added to the official file (and next releases).


Line 69: Line 69:
=== Multiple devices on Windows ===
=== Multiple devices on Windows ===
In case you have two USB devices (for instance a yoke plus pedals) to a Windows computer, there may be cases, where the same driver name is reported twice. In this case, you can get at least the yoke to work by assigning it number 0 (out of 0 and 1), by adding a line to <tt>[[$FG_ROOT]]/joystick.xml</tt> like:
In case you have two USB devices (for instance a yoke plus pedals) to a Windows computer, there may be cases, where the same driver name is reported twice. In this case, you can get at least the yoke to work by assigning it number 0 (out of 0 and 1), by adding a line to <tt>[[$FG_ROOT]]/joystick.xml</tt> like:
<js n="0" include="Input/Joysticks/Saitek/ST290-Pro.xml"/>
<syntaxhighlight lang="xml"><js n="0" include="Input/Joysticks/Saitek/ST290-Pro.xml"/></syntaxhighlight>
if you also have pedals (or another joystick), just add more lines, similar to:
if you also have pedals (or another joystick), just add more lines, similar to:
<js n="1" include="Input/Joysticks/Saitek/Pro-Flight-Rudder-Pedals.xml"/>
<syntaxhighlight lang="xml"><js n="1" include="Input/Joysticks/Saitek/Pro-Flight-Rudder-Pedals.xml"/></syntaxhighlight>


To make sure that the first input device is indeed the yoke, rotate the yoke ([[aileron]] control) and observe the Help > Joystick Information dialog. If the aileron value does not change, you have to make the yoke the preferred device first. For doing so, enter the Windows "Control panel", open "Game controllers" and select the "Advanced" button. Here you can select the yoke as the "Preferred" device. Afterwards you can check that assignment by restarting FlightGear. The yoke should now control the aileron.
To make sure that the first input device is indeed the yoke, rotate the yoke ([[aileron]] control) and observe the Help > Joystick Information dialog. If the aileron value does not change, you have to make the yoke the preferred device first. For doing so, enter the Windows "Control panel", open "Game controllers" and select the "Advanced" button. Here you can select the yoke as the "Preferred" device. Afterwards you can check that assignment by restarting FlightGear. The yoke should now control the aileron.
Line 80: Line 80:
Reboot your system and immediately enter on the [[command line]]  
Reboot your system and immediately enter on the [[command line]]  


dmesg | grep Joystick  
<syntaxhighlight lang="bash">
dmesg | grep Joystick  
</syntaxhighlight>


which pipes the boot message to grep which then prints every line in the boot message that contains the string “Joystick”. When you do this with a Saitek joystick attached, you will see a line similar to this one:  
which pipes the boot message to grep which then prints every line in the boot message that contains the string “Joystick”. When you do this with a Saitek joystick attached, you will see a line similar to this one:  


input0: USB HID v1.00 Joystick [SAITEK CYBORG 3D USB] on usb2:3.0  
<syntaxhighlight>
input0: USB HID v1.00 Joystick [SAITEK CYBORG 3D USB] on usb2:3.0  
</syntaxhighlight>


This line tells us that a joystick has identified itself as SAITEK CYBORG 3D USB to the operating system. It does not tell us that the joystick driver sees your joystick.  You can verify that the joystick driver sees the joystick by entering:
This line tells us that a joystick has identified itself as SAITEK CYBORG 3D USB to the operating system. It does not tell us that the joystick driver sees your joystick.  You can verify that the joystick driver sees the joystick by entering:


js_demo  
<syntaxhighlight>
js_demo  
</syntaxhighlight>


If your joystick is not recognized by the driver, you may have to manually load the joystick device module with the command:
If your joystick is not recognized by the driver, you may have to manually load the joystick device module with the command:


modprobe joydev
<syntaxhighlight>
modprobe joydev
</syntaxhighlight>


==== Windows ====
==== Windows ====
Line 100: Line 108:
FlightGear ships with a utility called js_demo. It will report the number of joysticks attached to a system, their respective "names" and their capabilities. Under Linux, you can run js_demo from the folder /FlightGear/bin as follows:  
FlightGear ships with a utility called js_demo. It will report the number of joysticks attached to a system, their respective "names" and their capabilities. Under Linux, you can run js_demo from the folder /FlightGear/bin as follows:  


$ cd /usr/local/FlightGear/bin  
<syntaxhighlight lang="bash">
$ js_demo  
$ cd /usr/local/FlightGear/bin  
$ js_demo
</syntaxhighlight>


Under Windows, open a command shell (<tt>Start > All Programs > Accessories > Command Prompt</tt>), go to the FlightGear binary folder and start the program as follows (given FlightGear is installed under <tt>C:/Program Files/Flightgear</tt>)  
Under Windows, open a command shell (<tt>Start > All Programs > Accessories > Command Prompt</tt>), go to the FlightGear binary folder and start the program as follows (given FlightGear is installed under <tt>C:/Program Files/Flightgear</tt>)  


C:
<syntaxhighlight lang="bash">
cd /Program Files/FlightGear/bin/win32  
C:
js_demo.exe  
cd /Program Files/FlightGear/bin/win32  
js_demo.exe  
</syntaxhighlight>


<!--If js_demo.exe is not included in your version, download it [http://fgfs.beggabaur.de/forum/js_demo.exe here].-->
<!--If js_demo.exe is not included in your version, download it [http://fgfs.beggabaur.de/forum/js_demo.exe here].-->
Line 113: Line 125:
On our system, the first few lines of output are (stop the program with C if it is quickly scrolling past your window!) as follows:  
On our system, the first few lines of output are (stop the program with C if it is quickly scrolling past your window!) as follows:  


Joystick test program.  
<syntaxhighlight>
Joystick 0: “CH PRODUCTS CH FLIGHT SIM YOKE USB ”  
Joystick test program.  
Joystick 1: “CH PRODUCTS CH PRO PEDALS USB”  
Joystick 0: “CH PRODUCTS CH FLIGHT SIM YOKE USB ”  
Joystick 2 not detected  
Joystick 1: “CH PRODUCTS CH PRO PEDALS USB”  
Joystick 3 not detected  
Joystick 2 not detected  
Joystick 4 not detected  
Joystick 3 not detected  
Joystick 5 not detected  
Joystick 4 not detected  
Joystick 6 not detected  
Joystick 5 not detected  
Joystick 7 not detected  
Joystick 6 not detected  
+——————–JS.0———————-+——————–JS.1———————-+  
Joystick 7 not detected  
| Btns Ax:0 Ax:1 Ax:2 Ax:3 Ax:4 Ax:5 Ax:6 | Btns Ax:0 Ax:1 Ax:2 |  
+——————–JS.0———————-+——————–JS.1———————-+  
+———————————————-+———————————————-+  
| Btns Ax:0 Ax:1 Ax:2 Ax:3 Ax:4 Ax:5 Ax:6 | Btns Ax:0 Ax:1 Ax:2 |  
| 0000 +0.0 +0.0 +1.0 -1.0 -1.0 +0.0 +0.0 . | 0000 -1.0 -1.0 -1.0 . . . . . |  
+———————————————-+———————————————-+  
 
| 0000 +0.0 +0.0 +1.0 -1.0 -1.0 +0.0 +0.0 . | 0000 -1.0 -1.0 -1.0 . . . . . |  
</syntaxhighlight>


First note that js demo reports which number is assigned to each joystick recognized by the driver. Also, note that the “name” each joystick reports is also included between quotes. We will need the names for each bindings file when we begin writing the binding xml files for each joystick.
First note that js demo reports which number is assigned to each joystick recognized by the driver. Also, note that the “name” each joystick reports is also included between quotes. We will need the names for each bindings file when we begin writing the binding xml files for each joystick.
Line 150: Line 163:
For Linux users, there is another option for identifying the “name” and the numbers assigned to each axis and button. Most Linux distributions include a very handy program, “jstest”. With a CH Product Yoke plugged into the system, the following output lines are displayed by jstest:  
For Linux users, there is another option for identifying the “name” and the numbers assigned to each axis and button. Most Linux distributions include a very handy program, “jstest”. With a CH Product Yoke plugged into the system, the following output lines are displayed by jstest:  


jstest /dev/js3  
<syntaxhighlight lang="bash">
Joystick (CH PRODUCTS CH FLIGHT SIM YOKE USB ) has 7 axes and 12 buttons. Driver version is 2.1.0  
jstest /dev/js3  
Testing…(interrupt to exit)  
Joystick (CH PRODUCTS CH FLIGHT SIM YOKE USB ) has 7 axes and 12 buttons. Driver version is 2.1.0  
Axes: 0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 Buttons: 0:off 1:off 2:off 3:on 4:off 5:off 6:off 7:off 8:off 9:off 10:off 11:off  
Testing…(interrupt to exit)  
Axes: 0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 Buttons: 0:off 1:off 2:off 3:on 4:off 5:off 6:off 7:off 8:off 9:off 10:off 11:off  
</syntaxhighlight>


Note the “name” between parentheses. This is the name the system associates with your joystick.  
Note the “name” between parentheses. This is the name the system associates with your joystick.  
Line 199: Line 214:
Before we begin to edit, we need to choose a name for our bindings xml file, create the folder for the QS joysticks, and copy the original xml file into this directory with this name.  
Before we begin to edit, we need to choose a name for our bindings xml file, create the folder for the QS joysticks, and copy the original xml file into this directory with this name.  


$ cd /usr/local/FlightGear/Input/Joysticks  
<syntaxhighlight lang="bash">
$ mkdir QS  
$ cd /usr/local/FlightGear/Input/Joysticks  
$ cd QS  
$ mkdir QS  
$ cp /usr/local/FlightGear/Input/Joysticks/Saitek/  
$ cd QS  
Cyborg-Gold-3d-USB.xml QuickStick.xml  
$ cp /usr/local/FlightGear/Input/Joysticks/Saitek/  
Cyborg-Gold-3d-USB.xml QuickStick.xml  
</syntaxhighlight>


Here, we obviously have supposed a Linux/UNIX system with FlightGear being installed under '''/usr/local/FlightGear'''. For a similar procedure under Windows with FlightGear being installed under C:/Program Files/FlightGear, open a command shell and type  
Here, we obviously have supposed a Linux/UNIX system with FlightGear being installed under '''/usr/local/FlightGear'''. For a similar procedure under Windows with FlightGear being installed under C:/Program Files/FlightGear, open a command shell and type  


C:  
<syntaxhighlight lang="bash">
cd /Program Files/FlightGear/Input/Joysticks  
C:  
mkdir QS  
cd /Program Files/FlightGear/Input/Joysticks  
cd QS  
mkdir QS  
copy /FlightGear/Input/Joysticks/Saitek/  
cd QS  
Cyborg-Gold-3d-USB.xml QuickStick.xml  
copy /FlightGear/Input/Joysticks/Saitek/  
Cyborg-Gold-3d-USB.xml QuickStick.xml  
</syntaxhighlight>


Next, open QuickStick.xml with your favorite editor. Before we forget to change the joystick name, search for the line containing <name>. You should find the line  
Next, open QuickStick.xml with your favorite editor. Before we forget to change the joystick name, search for the line containing <name>. You should find the line  


<name>SAITEK CYBORG 3D USB</name>  
<syntaxhighlight lang="xml"><name>SAITEK CYBORG 3D USB</name></syntaxhighlight>


and change it to  
and change it to  


<name>QUICK STICK 3D USB</name>
<syntaxhighlight lang="xml"><name>QUICK STICK 3D USB</name></syntaxhighlight>


This line illustrates a key feature of xml statements. They begin with a <tag> and end with a </tag>.  
This line illustrates a key feature of xml statements. They begin with a <tag> and end with a </tag>.  
Line 226: Line 245:
You can now compare your table to the comment table at the top of your file copy. Note that the comments tell us that the Saitek elevator was assigned to axis 1. Search for the string  
You can now compare your table to the comment table at the top of your file copy. Note that the comments tell us that the Saitek elevator was assigned to axis 1. Search for the string  


<axis n="1">  
<syntaxhighlight lang="xml"><axis n="1"></syntaxhighlight>


and change this to  
and change this to  


<axis n="0">
<syntaxhighlight lang="xml"><axis n="0"></syntaxhighlight>


Next, note that the Saitek rudder was assigned to axis 2. Search for the string  
Next, note that the Saitek rudder was assigned to axis 2. Search for the string  


<axis n="2">  
<syntaxhighlight lang="xml"><axis n="2"></syntaxhighlight>


and change this to  
and change this to  


<axis n="1">  
<syntaxhighlight lang="xml"><axis n="1"></syntaxhighlight>


Continue comparing your table with the comment table for the Saitek and changing the axis numbers and button numbers accordingly. Since QUICKSTICK USB and the Saitek have the same number of axes but different number of buttons, you must delete the buttons left over. Just remember to double check that you have a closing tag for each opening tag or you will get an error using the file.  
Continue comparing your table with the comment table for the Saitek and changing the axis numbers and button numbers accordingly. Since QUICKSTICK USB and the Saitek have the same number of axes but different number of buttons, you must delete the buttons left over. Just remember to double check that you have a closing tag for each opening tag or you will get an error using the file.  
Line 253: Line 272:
For configuring your joystick using this approach, open a command shell (command prompt under windows, to be found under Start|All programs|Accessories). Change to the directory <tt>[[$FG_ROOT]]/bin</tt> via e.g. (modify to your path)  
For configuring your joystick using this approach, open a command shell (command prompt under windows, to be found under Start|All programs|Accessories). Change to the directory <tt>[[$FG_ROOT]]/bin</tt> via e.g. (modify to your path)  


cd C:/Program Files/FlightGear/bin
<syntaxhighlight lang="bash">cd C:/Program Files/FlightGear/bin</syntaxhighlight>


and invoke the tool fgjs via  
and invoke the tool fgjs via  


./fgjs --fg-root=[[$FG_ROOT]]
<syntaxhighlight lang="bash">./fgjs --fg-root=[[$FG_ROOT]]</syntaxhighlight>


on a UNIX/Linux machine, or via  
on a UNIX/Linux machine, or via  


fgjs --fg-root=[[$FG_ROOT]]
<syntaxhighlight lang="bash">fgjs --fg-root=[[$FG_ROOT]]</syntaxhighlight>


on a Windows machine. The program will tell you which joysticks, if any, were detected. Now follow the commands given on screen, i.eṁove the axis and press the buttons as required. Be careful, a minor touch already “counts” as a movement. Check the reports on screen. If you feel something went wrong, just re-start the program.  
on a Windows machine. The program will tell you which joysticks, if any, were detected. Now follow the commands given on screen, i.eṁove the axis and press the buttons as required. Be careful, a minor touch already “counts” as a movement. Check the reports on screen. If you feel something went wrong, just re-start the program.  
Line 273: Line 292:
The the axis/button assignment of fgjs should, at least, get the axis assignments right, its output may need some tweaking. There may be axes moving the opposite way they should, the dead zones may be too small etc. For instance, I had to change  
The the axis/button assignment of fgjs should, at least, get the axis assignments right, its output may need some tweaking. There may be axes moving the opposite way they should, the dead zones may be too small etc. For instance, I had to change  


–prop:/input/joysticks/js[1]/axis[1]/binding/factor=-1.0  
<syntaxhighlight>
--prop:/input/joysticks/js[1]/axis[1]/binding/factor=-1.0
</syntaxhighlight>


into  
into  


–prop:/input/joysticks/js[1]/axis[1]/binding/factor=1.0  
<syntaxhighlight>
--prop:/input/joysticks/js[1]/axis[1]/binding/factor=1.0
</syntaxhighlight>


(USB CH Flightsim Yoke under Windows XP). Thus, here is a short introduction into the assignments of joystick properties.  
(USB CH Flightsim Yoke under Windows XP). Thus, here is a short introduction into the assignments of joystick properties.  
Line 283: Line 306:
Basically, all axes settings are specified via lines having the following structure:  
Basically, all axes settings are specified via lines having the following structure:  


--prop:/input/joysticks/js[n]/axis[m]/binding/command=property-scale  
<syntaxhighlight>
--prop:/input/joysticks/js[n]/axis[m]/binding/property=/controls/steering option
--prop:/input/joysticks/js[n]/axis[m]/binding/command=property-scale  
--prop:/input/joysticks/js[n]/axis[m]/binding/dead-band=db
--prop:/input/joysticks/js[n]/axis[m]/binding/property=/controls/steering option
--prop:/input/joysticks/js[n]/axis[m]/binding/offset=os
--prop:/input/joysticks/js[n]/axis[m]/binding/dead-band=db
--prop:/input/joysticks/js[n]/axis[m]/binding/factor=fa
--prop:/input/joysticks/js[n]/axis[m]/binding/offset=os
--prop:/input/joysticks/js[n]/axis[m]/binding/factor=fa
</syntaxhighlight>


where  
where  
Line 335: Line 360:
Some particularly useful ideas for programming scripts within joystick XML files:
Some particularly useful ideas for programming scripts within joystick XML files:
* getprop and setprop can be used for getting & setting properties from the internal properties tree:
* getprop and setprop can be used for getting & setting properties from the internal properties tree:
var brake = !getprop("/controls/gear/brake-parking");
<syntaxhighlight lang="nasal">
setprop("/controls/gear/brake-parking", brake);
var brake = !getprop("/controls/gear/brake-parking");
setprop("/controls/gear/brake-parking", brake);
</syntaxhighlight>
* You can also make your own values on the property tree:
* You can also make your own values on the property tree:
setprop("/input/joysticks/js[0]/myjoystick-modifier", 1);
<syntaxhighlight lang="nasal">
var mod = getprop("/input/joysticks/js[0]/myjoystick-modifier");
setprop("/input/joysticks/js[0]/myjoystick-modifier", 1);
var mod = getprop("/input/joysticks/js[0]/myjoystick-modifier");
</syntaxhighlight>
* You can print to terminal using the print function. This is very useful for debugging.
* You can print to terminal using the print function. This is very useful for debugging.
print("Just", " a ", "test");
<syntaxhighlight lang="nasal">
print("Just", " a ", "test");
</syntaxhighlight>
* You can display info in FlightGear via a popup. This is useful for giving the user feedback about changes that may not be obvious via the panel. It can also be useful for debugging. Example:
* You can display info in FlightGear via a popup. This is useful for giving the user feedback about changes that may not be obvious via the panel. It can also be useful for debugging. Example:


gui.popupTip("Parking Brake ON");
<syntaxhighlight lang="nasal">
gui.popupTip("Parking Brake ON");
</syntaxhighlight>


Arguments for gui.popupTip must be strings, so if you want to display other types of variables they should be formatted with something like sprintf:
Arguments for gui.popupTip must be strings, so if you want to display other types of variables they should be formatted with something like sprintf:


gui.popupTip(sprintf("Elevator trim: %d", 100 * getprop("/controls/flight/elevator-trim")));
<syntaxhighlight lang="nasal">
gui.popupTip(sprintf("Elevator trim: %d", 100 * getprop("/controls/flight/elevator-trim")));
</syntaxhighlight>


Or
Or


thv = getprop("/controls/engines/engine[0]/mixture");
<syntaxhighlight lang="nasal">
gui.popupTip("Thrust vector " ~ int(thv * 120 - 20));
thv = getprop("/controls/engines/engine[0]/mixture");
gui.popupTip("Thrust vector " ~ int(thv * 120 - 20));
</syntaxhighlight>


* You can just start using variables, ie,  
* You can just start using variables, ie,
x = 10;
<syntaxhighlight lang="nasal">
x = 10;
</syntaxhighlight>


But [http://wiki.flightgear.org/index.php/Nasal_scripting_language#Variables for various reasons] it is generally better to declare variables with the "var" statement:
But [http://wiki.flightgear.org/index.php/Nasal_scripting_language#Variables for various reasons] it is generally better to declare variables with the "var" statement:
var x = 10;
<syntaxhighlight lang="nasal">
var x = 10;
</syntaxhighlight>


Note that "var" creates variables that are local in scope, which may cause problems if you are intending to use a variable globally among all different bindings in your joystick XML file.
Note that "var" creates variables that are local in scope, but since all bindings for a joystick share the same scope, it will be seen across each script in the joystick file.


* You can include a section of script that runs on startup to initialize variables, create functions, etc. Example:
* You can include a section of script that runs on startup to initialize variables, create functions, etc. Example:
<PropertyList>
<syntaxhighlight lang="xml">
  <name type="string">My joystick name</name>
<PropertyList>
  <name type="string">My joystick name #2</name>
  <name type="string">My joystick name</name>
  <nasal>
  <name type="string">My joystick name #2</name>
    <script>
  <nasal>
        #initialize variables
    <script>
        f1 = f2 = 0;
      #initialize variables
        left_brake = right_brake = 0;
      f1 = f2 = 0;
        # create a function to be used with all buttons
      left_brake = right_brake = 0;
        getmod = func { getprop("/input/joysticks/js[0]/t-flight-hotas-x-modifier" ) }
      # create a function to be used with all buttons
    </script>
      getmod = func { getprop("/input/joysticks/js[0]/t-flight-hotas-x-modifier" ) }
  </nasal>
    </script>
  </nasal>
</syntaxhighlight>


* Sample code for firing weapons with the joystick is found on the [[Gun Effects]] page.
* Sample code for firing weapons with the joystick is found on the [[Gun Effects]] page.
395

edits

Navigation menu