Howto:Add thrust reversal

From FlightGear wiki
Jump to navigation Jump to search

JSBSim

-set.xml

Add the following XML code in your -set.xml file, outside of the <sim> tag. If your aircraft has more than two engines, add more <engine> tags.

 <engines>
  <engine n="0">
   <reverser-pos-norm type="double">0</reverser-pos-norm>
  </engine>
  <engine n="1">
   <reverser-pos-norm type="double">0</reverser-pos-norm>
  </engine>
 </engines>

This is the XML code for the key binding, which should also be placed outside the <sim> tag.

 <input>
  <keyboard>
   <key n="127">
    <name>Delete</name>
    <desc>Toggle Reversers</desc>
    <binding>
     <command>nasal</command>
     <script>reversethrust.togglereverser()</script>
    </binding>
   </key>
  </keyboard>
 </input>

Finally, here is the reference to our Nasal script. This block should also be placed outside <sim>.

 <nasal>
  <reversethrust>
   <file>Aircraft/.../Nasal/reverse-thrust.nas</file>
  </reversethrust>
 </nasal>

reverse-thrust.nas

Create a new file into your data/Aircraft/.../Nasal/ directory named reverse-thrust.nas. The piece of code shown below is designed for a twin-engine plane. If your aircraft has more engines, look at the Boeing 747-400 to see how to code the script.

togglereverser = func {
  r1 = props.globals.getNode("/fdm/jsbsim/propulsion/engine[0]");
  r2 = props.globals.getNode("/fdm/jsbsim/propulsion/engine[1]");
  r3 = props.globals.getNode("/controls/engines/engine[0]");
  r4 = props.globals.getNode("/controls/engines/engine[1]");
  r5 = props.globals.getNode("/sim/input/selected");
  rv1 = props.globals.getNode("/engines/engine[0]/reverser-pos-norm");
  rv2 = props.globals.getNode("/engines/engine[1]/reverser-pos-norm");

  val = rv1.getValue();
  if (val == 0 or val == nil) {
    interpolate(rv1.getPath(), 1.0, 1.4); 
    interpolate(rv2.getPath(), 1.0, 1.4);  
    r1.getChild("reverser-angle-rad").setValue(math.pi);
    r2.getChild("reverser-angle-rad").setValue(math.pi);
    r3.getChild("reverser").setBoolValue(1);
    r4.getChild("reverser").setBoolValue(1);
    r5.getChild("engine[0]").setBoolValue(1);
    r5.getChild("engine[1]").setBoolValue(1);
  } else {
    if (val == 1.0){
      interpolate(rv1.getPath(), 0.0, 1.4); 
      interpolate(rv2.getPath(), 0.0, 1.4);  
      r1.getChild("reverser-angle-rad").setValue(0);
      r2.getChild("reverser-angle-rad").setValue(0);
      r3.getChild("reverser").setBoolValue(0);
      r4.getChild("reverser").setBoolValue(0);
      r5.getChild("engine[0]").setBoolValue(1);
      r5.getChild("engine[1]").setBoolValue(1); 
    }  
  }
}

-model.xml

So far, we only did the "invisible" FDM part. To make it look really cool, we need to animate the engines. You could add an animation group and do all engines at once, or do each engine individually as shown here.

<animation>
 <type>translate</type>
 <object-name>eng.1.reverser</object-name>
 <property>/engines/engine[0]/reverser-pos-norm</property>
 <interpolation>
   <entry>
     <ind>0</ind>
     <dep>0.0</dep>
   </entry>
   <entry>
     <ind>1</ind>
     <dep>0.6</dep>
   </entry>
 </interpolation>
 <axis>
   <x>1</x>
   <y>0</y>
   <z>0</z>
 </axis>
</animation>

YASim

-set.xml

Add the following codes in the -set.xml file, below the </sim> tag. You've to add such piece for every engine, containing thrust reversal.

<input>
 <keyboard>
  <key n="127">
   <name>Delete</name>
   <desc>Toggle Reversers</desc>
   <binding>
    <command>property-toggle</command>
    <property>controls/engines/engine[0]/reverser</property>
   </binding>
   <binding>
    <command>property-toggle</command>
    <property>controls/engines/engine[1]/reverser</property>
   </binding>
  </key>
 </keyboard>
</input>

-yasim.xml

Every engine with reversal should contain the following code. The transition-time defines the time that it takes to turn the engines to reversed position.

<jet>
 <control-input axis="/controls/engines/engine[0]/reverser" control="REVERSE_THRUST"/>
 <control-output control="REVERSE_THRUST" prop="/surface-positions/reverser-norm"/>
 <control-speed control="REVERSE_THRUST" transition-time="1.5"/>
</jet>

<jet>
 <control-input axis="/controls/engines/engine[1]/reverser" control="REVERSE_THRUST"/>
 <control-output control="REVERSE_THRUST" prop="/surface-positions/reverser-norm[1]"/>
 <control-speed control="REVERSE_THRUST" transition-time="1.5"/>
</jet>

-model.xml

So far, we only did the "invisible" FDM part. To make it look really cool, we need to animate the engines.

<animation>
 <type>translate</type>
 <object-name>eng.1.reverser</object-name>
 <object-name>eng.2.reverser</object-name>
 <property>/surface-positions/reverser-norm</property>
 <interpolation>
   <entry>
     <ind>0</ind>
     <dep>0.0</dep>
   </entry>
   <entry>
     <ind>1</ind>
     <dep>0.6</dep>
   </entry>
 </interpolation>
 <axis>
   <x>1</x>
   <y>0</y>
   <z>0</z>
 </axis>
</animation>