Howto:Use Arduino with FlightGear: Difference between revisions

Some more
(Partial clean-up/copy-edit)
(Some more)
Line 1: Line 1:
Thanks to [[FlightGear]]'s [[generic protocol]], hardware can easily interface with FlightGear.  This hardware can be used to improve the immersion and/or realism of the simulation.  Arduino is no exception.
Thanks to [[FlightGear|FlightGear's]] [[generic protocol]], hardware can easily interface with FlightGear.  This hardware can be used to improve the immersion and/or realism of the simulation.  Arduino is no exception.


== About Arduino ==
== About Arduino ==
Line 19: Line 19:
=== Input protocol file ===
=== Input protocol file ===
Input protocol file is used to specify how serial information is read by Flightgear. In Ubuntu protocol files are found in:
Input protocol file is used to specify how serial information is read by Flightgear. In Ubuntu protocol files are found in:
<tt>''/usr/share/games/flightgear/protocol''</tt> directory.
<code>''/usr/share/games/flightgear/protocol''</code> directory.


==== Protocol file structure ====
==== Protocol file structure ====
Create <tt>''controltest.xml''</tt> file in your protocol folder and paste code from below to it.
Create <code>''controltest.xml''</code> file in your protocol folder and paste code from below to it.
<syntaxhighlight land="xml">
<syntaxhighlight land="xml">
<?xml version="1.0"?>
<?xml version="1.0"?>
Line 52: Line 52:
See [[Generic protocol]] for a description of the various XML tags.
See [[Generic protocol]] for a description of the various XML tags.


===Wiring and coding Arduino===
=== Wiring and coding ===


====Wiring====
==== Wiring ====
Potentiometer is connected to Arduinos ground and +5 volts. Potentiometers middle connector is connected to A0 analoq input. Switch is connected to ground with 10 kOhms pull-down resistor and +5 and digital pin 7.
A potentiometer is connected to Arduinos ground and +5 volts. The potentiometer's middle connector is connected to A0 analoq input. Switch is connected to ground with 10 kOhms pull-down resistor and +5 and digital pin 7.
[[File:Arduino switch and potentiometer wiring.png|frame|none|Wiring schematic for connecting potentiometer and switch to Arduino]]
[[File:Arduino switch and potentiometer wiring.png|frame|none|Wiring schematic for connecting the potentiometer and switch to Arduino]]


====Code====
==== Code ====
Copy this code to Arduino IDE and send it to Arduino Uno:
Copy this code to Arduino IDE and send it to Arduino Uno:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
Line 125: Line 125:
</syntaxhighlight>
</syntaxhighlight>


====Test serial output====
==== Testing serial output ====
 
Use Arduino IDEs serial monitor and you should see something like this:
Use Arduino IDEs serial monitor and you should see something like this:
[[File:Flightgear arduino serial monitor.png|frame|none|Arduino IDEs serial monitor output]]
[[File:Flightgear arduino serial monitor.png|frame|none|Arduino IDEs serial monitor output]]


First number is switch data, so it's either 0 (switch off) or 1 (switch on). After "," mark is our throttle data. First it's 0.00 meaning idle throttle and then potentiometer is gradually turned until it reaches 0.99.
The first number is switch data, so it's either 0 (switch off) or 1 (switch on). After the "," mark is our throttle data. First it's 0.00, which meaning idle throttle and then potentiometer is gradually turned until it reaches 0.99.


{{Warning|Remember to '''unplug Arduino's USB cable and plug it back''' in because Flightgear won't be able to read serial without doing this! You have to do this every time after you use Arduino IDE.}}
{{Warning|Remember to '''unplug Arduino's USB cable and plug it back''' in because Flightgear won't be able to read serial without doing this! You have to do this every time after you use Arduino IDE.}}


====Start Flightgear====
==== Start FlightGear ====
 
Flightgear needs to be started with a correct command line option for it to be able to read serial connection. This example uses following option:
Flightgear needs to be started with a correct command line option for it to be able to read serial connection. This example uses following option:


  --generic=serial,in,30,/dev/ttyACM0,controltest
  --generic=serial,in,30,/dev/ttyACM0,controltest


If you like, you can use graphical user interface, Flightgear Launch Control (aka FGRun), to launch Flightgear. Select correct settings from Advanced Option tab. [[File:Starting Flightgear with input options enabled.jpg|thumb|none|Starting Flightgear with FGLaunch Control, selecting input/output options]]
If you like, you can use graphical user interface, [[FlightGear Launch Control]] (aka FGRun), to launch Flightgear. Select the correct settings from Advanced Option tab. [[File:Starting Flightgear with input options enabled.jpg|thumb|none|Starting Flightgear with FGRun, selecting input/output options]]


If you don't now your correct port, you can check it with a following command in terminal: dmesg | tail. It should give you a message something like: "ttyACM0: USB ACM device" or "ttyACM1: USB ACM device". That's your port. Finally save setting by clicking 'OK' and click 'Run' to start flightgear. For more detailed guide, see [https://sites.google.com/site/flightgeararduinoandlinux/home Flightgear, Arduino and Linux]
If you don't know your correct port, you can check it with a following command in terminal: dmesg | tail. It should give you a message something like: "ttyACM0: USB ACM device" or "ttyACM1: USB ACM device". That's your port. Finally save setting by clicking 'OK' and click 'Run' to start flightgear. For a more detailed guide, see [https://sites.google.com/site/flightgeararduinoandlinux/home Flightgear, Arduino and Linux]


[[File:Arduinofgfs.jpg|thumb|270px|Arduino LCD panel displaying speed, heading and altitude.]]
[[File:Arduinofgfs.jpg|thumb|270px|Arduino LCD panel displaying speed, heading and altitude.]]
== Display/Generic protocol Example by rubdos ==
== Example ==
 
{{usr|Rubdos}} (Ruben De Smet) has built an example using the [[generic protocol]] and an Arduino Mega 2560.
Rubdos (Ruben De Smet) has built an example using the [[Generic Protocol]] and an Arduino Mega 2560.
The code used to control the Arduino with generic protocol was:
The code used to control the Arduino with generic protocol was:
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<?xml version="1.0"?>
<?xml version="1.0"?>
<PropertyList>
 
    <generic>
<PropertyList>
        <output>
 
            <binary_mode>false</binary_mode>
<generic>
            <line_separator>newline</line_separator>
    <output>
            <var_separator>newline</var_separator>
        <binary_mode>false</binary_mode>
            <preamble></preamble>
        <line_separator>newline</line_separator>
            <postamble></postamble>
        <var_separator>newline</var_separator>
            <chunk>
        <preamble></preamble>
                <name>Altitude</name>
        <postamble></postamble>
                <node>/position/altitude-ft</node>
 
                <type>integer</type>
        <chunk>
                <format>altitude=%i</format>
            <name>Altitude</name>
            </chunk>
            <node>/position/altitude-ft</node>
            <chunk>
            <type>integer</type>
                <name>RPM</name>
            <format>altitude=%i</format>
                <node>/engines/engine/rpm</node>
        </chunk>
                <type>integer</type>
 
                <format>rpm=%i</format>
        <chunk>
            </chunk>
            <name>RPM</name>
        </output>
            <node>/engines/engine/rpm</node>
<!--       <input>
            <type>integer</type>
            <line_separator>newline</line_separator>
            <format>rpm=%i</format>
            <var_separator>tab</var_separator>
        </chunk>
            <chunk>
 
            </chunk>
    </output>
        </input>
 
        -->
    <!-- <input>
    </generic>
        <line_separator>newline</line_separator>
</PropertyList>
        <var_separator>tab</var_separator>
        <chunk>
        </chunk>
    </input> -->
 
</generic>
 
</PropertyList>
</syntaxhighlight>
</syntaxhighlight>
It is a simple plaintext protocol, which can easily be parsed by an Arduino. The code used on the Arduino is available on github as a gist: [https://gist.github.com/rubdos/5422870]
It is a simple plaintext protocol, which can easily be parsed by an Arduino. The code used on the Arduino is available on github as a gist: [https://gist.github.com/rubdos/5422870]