Writing Joystick Code: Part 3: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
No edit summary
mNo edit summary
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{WIP}}
{{JoystickScripting Navigation}}
{{JoystickScripting Navigation}}


Line 7: Line 9:


-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
To toggle the Parking brake, use this. Entire code-block shown. Non-repeatable button.
To toggle the Parking brake, use this. Entire code-block shown. Non-repeatable button. (You have to specifically set <repeatable> to true if you want repeatable. It is assumed to be false.)
(You have to specifically set <repeatable> to true if you want repeatable. It is assumed to be false.
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
   <button n="1">
   <button n="1">
Line 18: Line 19:
   </button>
   </button>
</syntaxhighlight>  
</syntaxhighlight>  
This method can be used to toggle any property that is on/off.
This method can be used to toggle any property that is on/off. Use Debug - Browse Internal Properties to find suitable candidates.  
Use Debug - Browse Internal Properties to find suitable candidates.  
Also see getProp, setProp and setAll below.
Also see getProp, setProp and setAll below.
   
   
Remember, it ''toggles'' the property, you can't use it to set the property into a specific state.
Remember, it ''toggles'' the property, you can't use it to set the property into a specific state.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
To make condition work with mixture on a lever (axis). This is pertinent to turboprops.
To make condition work with mixture on a lever (axis). This is pertinent to turboprops.
Change
Change
    controls.mixtureAxis()
<pre style="white-space: pre-wrap;
to
white-space: -moz-pre-wrap;
    controls.mixtureAxis();
white-space: -pre-wrap;
    props.setAll("controls/engines/engine", "condition", getprop("controls/engines/engine/mixture"))
white-space: -o-pre-wrap;
word-wrap: break-word">
  controls.mixtureAxis()
</pre>
to
<pre style="white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word">
  controls.mixtureAxis();
  props.setAll("controls/engines/engine", "condition", getprop("controls/engines/engine/mixture"))
</pre>
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
To make condition work with mixture on a non-repeatable button.
Make mixture richer NR
  <syntaxhighlight lang="xml">
  <![CDATA[
    if(getprop("controls/engines/engine/mixture") < 1 ) {
      setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.05);
      props.setAll("controls/engines/engine", "condition", getprop("controls/engines/engine/mixture"))
    }
  ]]>
  </syntaxhighlight>
 
Make mixture leaner NR
  <![CDATA[
    if(getprop("controls/engines/engine/mixture") > 0 ) {
        setprop("controls/engines/engine/mixture", getprop("controls/engines/engine/mixture") - 0.05)
      props.setAll("controls/engines/engine", "condition", getprop("controls/engines/engine/mixture"))
    }
  ]]>
Make mixture richer R
  <![CDATA[
    if(getprop("controls/engines/engine/mixture") < 1 ) {
      setprop("controls/engines/engine/mixture", getprop ("controls/engines/engine/mixture") + 0.001);
      props.setAll("controls/engines/engine", "condition", getprop("controls/engines/engine/mixture"))
    }
  ]]>
 
Make mixture leaner R
  <![CDATA[
    if(getprop("controls/engines/engine/mixture") > 0 ) {
        setprop("controls/engines/engine/mixture", getprop("controls/engines/engine/mixture") - 0.001)
      props.setAll("controls/engines/engine", "condition", getprop("controls/engines/engine/mixture"))
    }
  ]]>
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
This is an alternative method to adjust a property
This is an alternative method to adjust a property
   <repeatable>true</repeatable>
   <repeatable>true</repeatable>
Line 78: Line 53:
   </binding>
   </binding>
This will continuously reduce /sim/current-view/goal-pitch-offset-deg in steps of 2 while the button is held in.
This will continuously reduce /sim/current-view/goal-pitch-offset-deg in steps of 2 while the button is held in.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------


== Advanced Programming ==
== Advanced Programming ==
Line 84: Line 64:
=== if ... then ... else ===
=== if ... then ... else ===


Sometimes you only want to do something if a certain condition is true (or false.) In this case you use an if clause. The general form is
Sometimes you only want to do something if a certain condition is true (or false.) In this case you use an if clause. The general form in most programming languages is
  if (condition) then action endif


We write then as {, and endif as }.
: '''if''' '''('''condition''')''' '''then''' action '''endif'''
And each "action" is terminated by a semicolon:


<syntaxhighlight lang="php">
In Nasal, we write "then" as "{" and "endif" as "}". Each "action" (or statement) is terminated by a semicolon:
 
if (condition)
{ # the opening curly brace means THEN
  action;
} # the closing curly brace means ENDIF, i.e. end of this block


<syntaxhighlight lang="nasal">
if (condition)
{ # the opening curly brace means THEN
  action;
} # the closing curly brace means ENDIF, i.e. end of this block
</syntaxhighlight>
</syntaxhighlight>


Actions can be arbitrary Nasal expressions, including function calls and other conditional blocks.
Actions can be arbitrary Nasal expressions, including function calls and other conditional blocks.


 
==== An example ====
Lets say that if x is less than 5 we want to add 2 to it. We write:
Lets say that if x is less than 5 we want to add 2 to it. We write:
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (x < 5)  { x = x + 2;  }
if (x < 5)  { x = x + 2;  }
</syntaxhighlight>
</syntaxhighlight>


The more readable way of writing it is
The more readable way of writing it is
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
   }
}
</syntaxhighlight>
 
And we can even omit the braces if there's only one action:
<syntaxhighlight lang="nasal">
if (x < 5) # no opening brace...
   x = x + 2; # ...so the if statement ends at the first semicolon
</syntaxhighlight>
</syntaxhighlight>


If we also want to add 1 to y if the condition is true, we write
If we also want to add 1 to y if the condition is true, we write
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
    y + y + 1;
  y = y + 1;
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 131: Line 115:
For example:
For example:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (x < 5) {
if (x < 5) {
    x += 2; # means x = x + 2
  x += 2; # means x = x + 2
    y += 1; # means y = y + 1
  y += 1; # means y = y + 1
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 141: Line 125:
Now lets pretend that we still want to increase x by 2 if it less than 5, and if it is 5 or more we want to add 1 to it. We use else.
Now lets pretend that we still want to increase x by 2 if it less than 5, and if it is 5 or more we want to add 1 to it. We use else.
It looks like this
It looks like this
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
  }
} else {
  else {
  x = x + 1;
    x = x + 1;
}
  }
</syntaxhighlight>
</syntaxhighlight>


Now lets say we have the following rules:
Now lets say we have the following rules:
  If x < 5 add 2 to it
* If x < 5 add 2 to it
  If x < 10 add 1 to it
* If x < 10 add 1 to it
  If x is 10 or more add 7 to it. In this case elseif becomes useful.
* If x is 10 or more add 7 to it.


In English our code would be
In English our code would be
  If (x < 5) then (x = x + 2) else if (x < 10) then (x = x + 1).
* If (x < 5) then (x = x + 2) else if (x < 10) then (x = x + 1).
  If none of these are true then (x = x + 7). End of if clause.
* If none of these are true then (x = x + 7).
* End of if clause.


We test for x < 5 first. If it fails we test for x < 10. If x , 5 succeeds we do (x = x + 2) and then move on past the end of if clause.
We test for x < 5 first. If it fails we test for x < 10. If x , 5 succeeds we do (x = x + 2) and then move on past the end of if clause.
Line 163: Line 147:


Writing our code properly we get
Writing our code properly we get
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
 
if (x < 5) {
  if (x < 5) {
  x = x + 2;
    x = x + 2;
} elsif (x < 10) {
  }
  x = x + 1;
  elseif (x < 10) {
} else {
    x = x + 1;
  x = x + 7;
  }
}
  else {
    x = x + 7;
  }
</syntaxhighlight>
</syntaxhighlight>


You can use more than one elseif, but it gets messy. If you have a number of tests then it is better to test for each case individually.
You can use more than one elsif, but it gets messy. If you have a number of tests then it might be better to test for each case individually.
Easier to understand and much less chance of an error.
Easier to understand each condition separately and much less chance of an error.
Our above example would become
Our above example would become
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
  }
}
  if (x >= 5) and (x < 10) {
if (x >= 5) and (x < 10) {
    x = x + 1;
  x = x + 1;
  }
}
  if (x >= 10) {
if (x >= 10) {
    x = x + 7;
  x = x + 7;
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 195: Line 176:
But the advantage is that we are forced to write out each condition exactly as it should be tested. Easier to understand and easier to maintain.
But the advantage is that we are forced to write out each condition exactly as it should be tested. Easier to understand and easier to maintain.


==== Another example ====


Lets say that you want something to happen only if the gear is up. But the only property you can read is Gear-Down. Then you write
Lets say that you want something to happen only if the gear is up. But the only property you can read is GearDown. Then you write
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  if (!Gear-Down) {
if (!GearDown) {
    action;
  action;
  }
}
</syntaxhighlight>
</syntaxhighlight>


The ! means not. So it translates as: If the gear is not down perform action.
The ! means not. So it translates as: If the gear is not down perform action.
Beacuse we use < and > we will need to enclose it all with CDATA, in order not to mess up the XML syntax.


See the discussion of variables below for an example of the use of if.
See the discussion of variables below for an example of the use of if.
Line 259: Line 237:
   ]]>
   ]]>


You need CDATa because of the less-than sign.
You need CDATA because of the less-than sign.


You can use this for throttle, propeller, mixture and condition.
You can use this for throttle, propeller, mixture and condition.


BTW Here the props in props.setAll is short for property, not propellers.
BTW Here the props in props.setAll is short for property, not propellers.




Line 363: Line 340:


'''Method 1'''
'''Method 1'''
  <data>
<nowiki>  <data>
     <mode type="int">1</mode>  # Initialise mode to 1
     <mode type="int">1</mode>  # Initialise mode to 1
   </data>
   </data>
Line 369: Line 346:
   <nasal>
   <nasal>
     <script>
     <script>
       <nowiki><![CDATA[
       <![CDATA[
  var self = cmdarg().getParent();
  var self = cmdarg().getParent();
  var data = self.getNode("data");
  var data = self.getNode("data");
  var mode = data.getNode("mode");
  var mode = data.getNode("mode");
        get_mode = func { mode.getValue() }
          get_mode = func { mode.getValue(); }
       ]]></script>
       ]]>
    </script>
   </nasal>
   </nasal>
   
   
  var m  # Used whenever mode is accessed</nowiki>
    </nowiki>


This goes right at the top of your xml file, just after </name>.
This goes right at the top of your xml file, just after </name>.


The code for the button (non-repeatable)that changes mode looks like this
The code for the button (non-repeatable)that changes mode looks like this
   m = get_mode();
   var m = get_mode();
   m = m + 1;
   m = m + 1;
   if (m == 4) {
   if (m == 4) {
Line 390: Line 368:


When a button is pressed, the code looks like this
When a button is pressed, the code looks like this
   m = get_mode();
   var m = get_mode();
   if (m == 1) {
   if (m == 1) {
     ground-action
     ground-action
Line 447: Line 425:
be used somewhere in the system files. In the xml file for the Saitek Yoke, where I use a variable to control the assignments
be used somewhere in the system files. In the xml file for the Saitek Yoke, where I use a variable to control the assignments
of the levers on the quadrant, I call the variable ''SaitekMultiMode''. Unlikely to be used by the FG programmers, or anybody else.
of the levers on the quadrant, I call the variable ''SaitekMultiMode''. Unlikely to be used by the FG programmers, or anybody else.


=== perIndexAxisHandler ===
=== perIndexAxisHandler ===
Line 474: Line 450:




See [http://flightgear.org/forums/viewtopic.php?f=24&t=17851 this post] for an example of using this.
See [http://forum.flightgear.org/viewtopic.php?f=24&t=17851 this post] for an example of using this.




Line 491: Line 467:




Go back to [[Writing Joystick Code: Part 2]]
Any complaints/suggestions/questions/kudos can be posted [http://forum.flightgear.org/viewtopic.php?f=24&t=17892 here].
 
Go forward to [[Writing Joystick Code: Part 4]]
 
 
-----------------------------------------------------------------------------
 
 
Any complaints/suggestions/questions/kudos can be posted [http://flightgear.org/forums/viewtopic.php?f=24&t=17892 here].




6

edits

Navigation menu