Hi fellow wiki editors!

To help newly registered users get more familiar with the wiki (and maybe older users too) there is now a {{Welcome to the wiki}} template. Have a look at it and feel free to add it to new users discussion pages (and perhaps your own).

I have tried to keep the template short, but meaningful. /Johan G

Difference between revisions of "Writing Joystick Code: Part 2"

From FlightGear wiki
Jump to: navigation, search
m
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
UNDER CONSTRUCTION
+
{{JoystickScripting Navigation}}
  
 
== Code snippets  ==
 
== Code snippets  ==
Line 5: Line 5:
 
Make sure you have read [[Input device]] and [[Writing Joystick Code: Part 1]] first.
 
Make sure you have read [[Input device]] and [[Writing Joystick Code: Part 1]] first.
  
These are what you use instead of ''Your code goes here'' amd ''Ctrl-Alt button released''. You just need to put them in the right place - inbetween the <script> and </script> tags.
 
  
Please note that some of them have concepts that will be explained in a later section.
+
Code snippets are what you use instead of ''Your code goes here'' and ''Ctrl-Alt button released'' as seen in Part 1.
 +
You just need to put them in the right place - in between the <script> and </script> tags.
 +
 
 +
 
 +
'''Please note that some of them have concepts that will be explained in Part 3.'''
 +
 
  
 
Each snippet has the following format:
 
Each snippet has the following format:
  Action R/NR
+
  Action R/NR S C
 
     Code for action
 
     Code for action
 
  U
 
  U
 
     Code for <mod-up>
 
     Code for <mod-up>
  
Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button, and U is the code for <mod-up>. If it is empty, a <mod-up> is not needed.
+
Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button,
 +
and U is the code for <mod-up>. If it is not there, a <mod-up> is not needed.
 +
 
 +
An S, if present, means that it is a step action. gearDown is not a step-action, the gear is either up or down. flapsDown is a step action. Each time you send a flapsDown command the flaps go down one notch. This matters not if you are using a non-repeatable button, but if you use a repeating button, as long as you are pressing the button, flapsDown commands are being sent. This means that if you are still pressing the button when the flaps reach the next notch, they will go down one more notch.
 +
 
 +
The C, if present, means that whatever is being controlled is continously variable between its limits. An example is elevator trim. It moves between full-down to full-up in very tiny steps. Very tiny.
  
Some Actions will have code for use with repeatable and non-repeatable buttons.
+
Some Actions will have code for use with repeatable and non-repeatable buttons. Sometimes it is not wise to use the wrong type for a certain action. And sometimes you must use the correct type for an action - using a non-repeating button for Zoom is a bad idea, the user will have to constantly keep triggering the button to reach the zoom level he requires. Using a repeatable button is possible for something like gearDown, as long as the use knows to just use a brief push, but it is a bad idea.
  
Notice that some commands have a +1 or a -1 in brackets to specify the direction of the action, and a 0 to say stop doing that.
+
Notice that some commands have a +1 or a -1 in brackets to specify the direction of the action, and a 0 to say stop doing that. The 0 version will of course be in the <mod-up> section.
  
 
If the action is upside-down for you, swap the + and - signs.
 
If the action is upside-down for you, swap the + and - signs.
  
Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out. If the action is too viscious for you, make the number smaller.
+
Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out. This is of course not approprite for all actions - you would not want to lower the gear just 75% of the way.
 +
If the action is too vicious for you, make the number smaller.
  
 
=== The snippets ===
 
=== The snippets ===
Line 29: Line 39:
 
  Gear down NR
 
  Gear down NR
 
   controls.gearDown(1)
 
   controls.gearDown(1)
U
 
  controls.gearDown(0)
 
 
   
 
   
 
  Gear up NR
 
  Gear up NR
  controls.gearDown(-1)
+
    controls.gearDown(-1)
U
+
  controls.gearDown(0)
+
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Flaps down one notch NR
+
  Flaps down one notch NR S
  controls.gearDown(1)
+
    controls.flapsDown(1)
U
+
  controls.gearDown(0)
+
 
   
 
   
  Flaps up one notch NR
+
  Flaps up one notch NR S
  controls.gearDown(-1)
+
    controls.flapsDown(-1)
U
+
  controls.gearDown(0)
+
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Deploy spoilers NR
+
  Deploy spoilers NR S
  controls.stepSpoilers(1)
+
    controls.stepSpoilers(1)
U
+
  controls.stepSpoilers(0)
+
 
   
 
   
  Retract spoilers NR
+
  Retract spoilers NR S
 
   controls.stepSpoilers(-1)
 
   controls.stepSpoilers(-1)
U
 
  controls.stepSpoilers(0)
 
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
 
  Cycle View NR
 
  Cycle View NR
  view.stepView(1)
+
    view.stepView(1)
U
+
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Elevator trim down R
+
  Elevator trim down R C
  controls.elevatorTrim(0.75)
+
    controls.elevatorTrim(0.75)
U
+
 
   
 
   
  Elevator trim up R
+
  Elevator trim up R C
  controls.elevatorTrim(-0.75)
+
    controls.elevatorTrim(-0.75)
U
+
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Elevator Trim down NR
+
  Elevator Trim down NR C
  interpolate("/controls/flight/elevator-trim", 1, 30 * (1 - getprop("controls/flight/elevator-trim")))
+
    interpolate("/controls/flight/elevator-trim", 1, 30 * (1 - getprop("controls/flight/elevator-trim")))
 
  U
 
  U
  interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)
+
    interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)
 
   
 
   
  Elevator trim up NR
+
  Elevator trim up NR C
  interpolate("/controls/flight/elevator-trim", -1, 30 * (1 - getprop("controls/flight/elevator-trim")))
+
    interpolate("/controls/flight/elevator-trim", -1, 30 * (1 - getprop("controls/flight/elevator-trim")))
 
  U
 
  U
  interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)
+
    interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)
 
   
 
   
These commands are all on one line.
 
 
  To make it go faster, make the 30 smaller.
 
  To make it go faster, make the 30 smaller.
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Aileron trim left R
+
  Aileron trim left R C
  controls.aileronTrim(-0.75)
+
    controls.aileronTrim(-0.75)
U
+
 
   
 
   
  Aileron trim right R
+
  Aileron trim right R C
  controls.aileronTrim(0.75)
+
    controls.aileronTrim(0.75)
U
+
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Aileron Trim left NR
+
  Aileron trim left NR C
  interpolate("/controls/flight/aileron-trim", -1, 30 * (1 - getprop("controls/flight/aileron-trim")))
+
    interpolate("/controls/flight/aileron-trim", -1, 30 * (1 - getprop("controls/flight/aileron-trim")))
 
  U
 
  U
  interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)
+
    interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)
 
+
  Aileron trim right NR
+
  Aileron trim right NR C
  interpolate("/controls/flight/aileron-trim", 1, 30 * (1 - getprop("controls/flight/aileron-trim")))
+
    interpolate("/controls/flight/aileron-trim", 1, 30 * (1 - getprop("controls/flight/aileron-trim")))
 
  U
 
  U
  interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)
+
    interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)
 
   
 
   
To make it go faster, make the 30 smaller.
+
To make it go faster, make the 30 smaller.
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator/aileron to rudder
+
  If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator-trim/aileron-trim to rudder-trim.
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
Apply all brakes NR
 
  controls.applyBrakes(1)
 
U
 
  controls.applyBrakes(0)
 
 
 
  Apply left brake NR
 
  Apply left brake NR
  controls.applyBrakes(1, -1)
+
    controls.applyBrakes(1, -1)
 
  U
 
  U
  controls.applyBrakes(0, -1)
+
    controls.applyBrakes(0, -1)
 
   
 
   
 
  Apply Right brake NR
 
  Apply Right brake NR
  controls.applyBrakes(1, 1)
+
    controls.applyBrakes(1, 1)
 
  U
 
  U
  controls.applyBrakes(0, 1)
+
    controls.applyBrakes(0, 1)
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
 
  Start Stopwatch NR
 
  Start Stopwatch NR
  globals["__dlg:stopwatch-dialog"].start()
+
    globals["__dlg:stopwatch-dialog"].start()
+
 
   
 
   
 
  Stop Stopwatch NR
 
  Stop Stopwatch NR
  globals["__dlg:stopwatch-dialog"].stop()
+
    globals["__dlg:stopwatch-dialog"].stop()
+
 
   
 
   
 
  Reset Stopwatch NR
 
  Reset Stopwatch NR
  globals["__dlg:stopwatch-dialog"].reset()
+
    globals["__dlg:stopwatch-dialog"].reset()
+
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  Make mixture richer NR
+
  Make mixture richer R C
  <![CDATA[
+
    controls.adjMixture(0.05)
    if(getprop("controls/engines/engine/mixture") < 1 ) {
+
      setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
+
Make mixture leaner R C
    }
+
    controls.adjMixture(-0.05)
  ]]>
+
-----------------------------------------------------------------------------
 +
Start all engines R
 +
    controls.startEngine(1)
 
  U
 
  U
 +
    controls.startEngine(0)
 
   
 
   
  Make mixture leaner NR
+
  Start specific engine R
  <![CDATA[
+
    controls.startEngine(1, x)
    if(getprop("controls/engines/engine/mixture") > 0 ) {
+
        setprop("controls/engines/engine/mixture", getprop("controls/engines/engine/mixture") - 0.05)
+
    }
+
  ]]>
+
 
  U
 
  U
 +
    controls.startEngine(0, x)
 +
 +
x is the engine number. First engine is 0.
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  start engine
+
  Wingsweep
 +
 +
How this is implemented in each aircraft depends on the design of the aircraft. If it has predefined wingsweep settings it will step through these settings.
 +
If not, it will step 20% of the way each time called.
 +
 +
Wingsweep forward NR
 +
    controls.wingSweep(1)
 +
 
 +
Wingsweep backwards NR
 +
    controls.wingSweep(-1)
 +
 
 +
It is possible (but desirable?) to use any value from 1 to 5 in the brackets. This will make the wings sweep more steps at a time.
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  perIndexAxisHandler
+
  Weapons
 +
   
 +
  Fire selected weapon NR
 +
  controls.trigger(1)
 +
  U
 +
  controls.trigger(0)
 +
 +
  Select previous weapon NR
 +
    controls.weaponSelect(-1)
 +
 +
  Select next weapon NR
 +
    controls.weaponSelect(-1)
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  wingsweep
+
  Toggle all lights on/off NR
 +
    controls.toggleLights()
 +
 +
Switch specific lights on NR
 +
  setProp(("controls/lighting/xxx"), true)
 +
 +
Switch specific lights off NR
 +
  setProp(("controls/lighting/xxx"), false)
 +
 +
Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light,
 +
taxi-lights, cabin-lights, map-lights.
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  weapons
+
  Instrument lights level brighter R
 +
    <![CDATA[
 +
      if(getprop("controls/lighting/instruments-norm") < 1 ) {
 +
          setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.05)
 +
      }
 +
    ]]>
 +
 +
Instrument lights level dimmer R
 +
    <![CDATA[
 +
      if(getprop("controls/lighting/instruments-norm") > 0 ) {
 +
          setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.05)
 +
      }
 +
    ]]>
 +
 +
Instrument lights level up NR
 +
    <![CDATA[
 +
      if(getprop("controls/lighting/instruments-norm") < 1 ) {
 +
          setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.2)
 +
      }
 +
    ]]>
 +
 +
Instrument lights level down NR
 +
    <![CDATA[
 +
      if(getprop("controls/lighting/instruments-norm") > 0 ) {
 +
          setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.2)
 +
      }
 +
    ]]>
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
  ptt
+
  Step magnetos up NR
-----------------------------------------------------------------------------
+
    controls.stepMagnetos(1)
  lights; individual and all
+
   
-----------------------------------------------------------------------------
+
  Step magnetos down NR
  step magnetos
+
    controls.stepMagnetos(-1)
 
-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------
 +
 
   
 
   
If you find a line is too long for your page width, then you can press Enter and go to a new line. FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.
+
While editing an xml file, if you find a line is too long for your page width, then you can press Enter and go to a new line.
  setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
+
FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.
 +
  setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)
 
becomes
 
becomes
  setprop("controls/engines/engine/mixture",
+
  setprop("controls/engines/engine/mixture",
    getprop ("controls/engines/engine/mixture") + 0.05)
+
      getprop ("controls/engines/engine/mixture") + 0.05)
Notice the choice of place to press Enter, and the indent in the second line. Make it ''easy'' for people to read.
+
Notice where I put the newline and how I indented the second line. Both are designed to make it easy for people to read,
 +
as part of being a programmer is making your code legible for others.
  
  
  
Any complaints/suggestions/questions/kudos can be posted [http://flightgear.org/forums/viewtopic.php?f=24&t=17892 here].
+
-----------------------------------------------------------------------------
 +
 
 +
 
 +
Any complaints/suggestions/questions/kudos can be posted [http://forum.flightgear.org/viewtopic.php?f=24&t=17892 here].
  
 +
== Related content ==
 +
* [[Joystick xml library]]
  
[[Category : Hardware]]
+
[[Category:Hardware]]

Latest revision as of 05:10, 5 December 2015


Code snippets

Make sure you have read Input device and Writing Joystick Code: Part 1 first.


Code snippets are what you use instead of Your code goes here and Ctrl-Alt button released as seen in Part 1. You just need to put them in the right place - in between the <script> and </script> tags.


Please note that some of them have concepts that will be explained in Part 3.


Each snippet has the following format:

Action R/NR S C
   Code for action
U
   Code for <mod-up>

Where Action is what you want to do, R means use with a repeatable button, NR means use with a non-repeatable button, and U is the code for <mod-up>. If it is not there, a <mod-up> is not needed.

An S, if present, means that it is a step action. gearDown is not a step-action, the gear is either up or down. flapsDown is a step action. Each time you send a flapsDown command the flaps go down one notch. This matters not if you are using a non-repeatable button, but if you use a repeating button, as long as you are pressing the button, flapsDown commands are being sent. This means that if you are still pressing the button when the flaps reach the next notch, they will go down one more notch.

The C, if present, means that whatever is being controlled is continously variable between its limits. An example is elevator trim. It moves between full-down to full-up in very tiny steps. Very tiny.

Some Actions will have code for use with repeatable and non-repeatable buttons. Sometimes it is not wise to use the wrong type for a certain action. And sometimes you must use the correct type for an action - using a non-repeating button for Zoom is a bad idea, the user will have to constantly keep triggering the button to reach the zoom level he requires. Using a repeatable button is possible for something like gearDown, as long as the use knows to just use a brief push, but it is a bad idea.

Notice that some commands have a +1 or a -1 in brackets to specify the direction of the action, and a 0 to say stop doing that. The 0 version will of course be in the <mod-up> section.

If the action is upside-down for you, swap the + and - signs.

Also, sometimes the 1 is replaced by a number, which specifies by how much the action should be carried out. This is of course not approprite for all actions - you would not want to lower the gear just 75% of the way. If the action is too vicious for you, make the number smaller.

The snippets


Gear down NR
  controls.gearDown(1)

Gear up NR
   controls.gearDown(-1)

Flaps down one notch NR S
   controls.flapsDown(1)

Flaps up one notch NR S
   controls.flapsDown(-1)

Deploy spoilers NR S
   controls.stepSpoilers(1)

Retract spoilers NR S
  controls.stepSpoilers(-1)

Cycle View NR
   view.stepView(1)

Elevator trim down R C
   controls.elevatorTrim(0.75)

Elevator trim up R C
   controls.elevatorTrim(-0.75)

Elevator Trim down NR C
   interpolate("/controls/flight/elevator-trim", 1, 30 * (1 - getprop("controls/flight/elevator-trim")))
U
   interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)

Elevator trim up NR C
   interpolate("/controls/flight/elevator-trim", -1, 30 * (1 - getprop("controls/flight/elevator-trim")))
U
   interpolate("/controls/flight/elevator-trim", getprop("controls/flight/elevator-trim"), 0)

To make it go faster, make the 30 smaller.

Aileron trim left R C
   controls.aileronTrim(-0.75)

Aileron trim right R C
   controls.aileronTrim(0.75)

Aileron trim left NR C
   interpolate("/controls/flight/aileron-trim", -1, 30 * (1 - getprop("controls/flight/aileron-trim")))
U
   interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)

Aileron trim right NR C
   interpolate("/controls/flight/aileron-trim", 1, 30 * (1 - getprop("controls/flight/aileron-trim")))
U
   interpolate("/controls/flight/aileron-trim", getprop("controls/flight/aileron-trim"), 0)

To make it go faster, make the 30 smaller.

If you want rudder-trim, use either the repeatable or non-repeatable above, changing elevator-trim/aileron-trim to rudder-trim.

Apply left brake NR
   controls.applyBrakes(1, -1)
U
   controls.applyBrakes(0, -1)

Apply Right brake NR
   controls.applyBrakes(1, 1)
U
   controls.applyBrakes(0, 1)

Start Stopwatch NR
   globals["__dlg:stopwatch-dialog"].start()

Stop Stopwatch NR
   globals["__dlg:stopwatch-dialog"].stop()

Reset Stopwatch NR
   globals["__dlg:stopwatch-dialog"].reset()

Make mixture richer R C
   controls.adjMixture(0.05)

Make mixture leaner R C
   controls.adjMixture(-0.05)

Start all engines R
   controls.startEngine(1)
U
   controls.startEngine(0)

Start specific engine R
   controls.startEngine(1, x)
U
   controls.startEngine(0, x)

x is the engine number. First engine is 0.

Wingsweep

How this is implemented in each aircraft depends on the design of the aircraft. If it has predefined wingsweep settings it will step through these settings.
If not, it will step 20% of the way each time called.

Wingsweep forward NR
   controls.wingSweep(1)
 
Wingsweep backwards NR
   controls.wingSweep(-1)
 
It is possible (but desirable?) to use any value from 1 to 5 in the brackets. This will make the wings sweep more steps at a time.

 Weapons

 Fire selected weapon NR
 controls.trigger(1)
 U
 controls.trigger(0)

 Select previous weapon NR
   controls.weaponSelect(-1)

 Select next weapon NR
   controls.weaponSelect(-1)

Toggle all lights on/off NR
   controls.toggleLights()

Switch specific lights on NR
  setProp(("controls/lighting/xxx"), true)

Switch specific lights off NR
  setProp(("controls/lighting/xxx"), false)

Where xxx = beacon, landing-lights, landing-light, landing-light[1] logo-lights, nav-lights, strobe, taxi-light,
taxi-lights, cabin-lights, map-lights. 

Instrument lights level brighter R
   <![CDATA[ 
      if(getprop("controls/lighting/instruments-norm") < 1 ) {
         setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.05)
      }
   ]]>

Instrument lights level dimmer R
   <![CDATA[ 
      if(getprop("controls/lighting/instruments-norm") > 0 ) {
         setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.05)
      }
   ]]>

Instrument lights level up NR
   <![CDATA[ 
      if(getprop("controls/lighting/instruments-norm") < 1 ) {
         setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") + 0.2)
      }
   ]]>

Instrument lights level down NR
   <![CDATA[ 
      if(getprop("controls/lighting/instruments-norm") > 0 ) {
         setprop("controls/lighting/instruments-norm", getprop ("controls/lighting/instruments-norm") - 0.2)
      }
   ]]>

Step magnetos up NR
   controls.stepMagnetos(1)

Step magnetos down NR
   controls.stepMagnetos(-1)


While editing an xml file, if you find a line is too long for your page width, then you can press Enter and go to a new line. FG will be happy - it needs a tag or a ; at the end of a line to tell it that the current line of code is finished.

  setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05)

becomes

  setprop("controls/engines/engine/mixture",
      getprop ("controls/engines/engine/mixture") + 0.05)

Notice where I put the newline and how I indented the second line. Both are designed to make it easy for people to read, as part of being a programmer is making your code legible for others.




Any complaints/suggestions/questions/kudos can be posted here.

Related content