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 "Howto:Use Arduino with FlightGear"

From FlightGear wiki
Jump to: navigation, search
(Testing serial output: Warning → Tip – Since there are no software crashes, broken hardware or injuries)
(More cleanup)
Line 1: Line 1:
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.
+
Thanks to FlightGear's [[generic protocol]], [[:Category:Hardware|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 ==
'''[http://www.arduino.cc/ Arduino]''' is an open-source electronics prototyping platform based on flexible, easy-to-use [[:Category:Hardware|hardware]] (consisting of a board designed around an 8-bit or a 32-bit microcontroller) and software [http://arduino.cc/en/main/software Arduino IDE]).
+
'''[http://www.arduino.cc/ Arduino]''' is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.  The hardware is a microcontroller designed around an 8-bit or 32-bit microcontroller, with several digital and analog {{Abbr|I/O|Input/Output}} ports.  The software is the [http://arduino.cc/en/Main/Software Arduino {{Abbr|IDE|Integrated Development Environment}}].
  
== Controlling Flightgear with Arduino ==
+
== Example 1: Controlling internal properties ==
This example demonstrates the use of a switch and a potentiometer to control the [[Property Tree]]
+
<big>By {{usr|Vaipe}}</big>
 +
 
 +
This example demonstrates the use of a switch and a potentiometer to control the [[Property Tree]].
  
 
=== Equipment and software ===
 
=== Equipment and software ===
This example uses the following components and software:
+
The following equipment was used for this example:
* [[Changelog_3.2|FlightGear 3.2]] or higher
+
* [[Changelog_3.2|FlightGear 3.2]]
* [[FlightGear Launch Control]] (or FGRun)  
+
* [[FGRun]]
* Arduino UNO
+
* [[Cessna 172P|Cessna 172P Skyhawk]] (default aircraft)  
* Linux (Ubuntu 14.04)
+
* [http://arduino.cc/en/Main/ArduinoBoardUno Arduino Uno]
* On/off switch
+
* Linux ([http://en.wikipedia.org/wiki/List_of_Ubuntu_releases#Ubuntu_10.04_LTS_.28Lucid_Lynx.29 Ubuntu 14.04])
 +
* Simple on/off switch
 
* Potentiometer
 
* Potentiometer
* [[Cessna 172P|Cessna 172P Skyhawk]] (default aircraft)
 
  
 
=== Input protocol file ===
 
=== Input protocol file ===
Line 55: Line 57:
  
 
==== Wiring ====
 
==== Wiring ====
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.
+
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.  The diagram below illustrates the setup.
 
[[File:Arduino switch and potentiometer wiring.png|frame|none|Wiring schematic for connecting the 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 C code to Arduino IDE and send it to the Arduino Uno:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
     /*
 
     /*
Line 126: Line 128:
  
 
==== Testing serial output ====
 
==== Testing serial output ====
Use Arduino IDEs serial monitor and you should see something like this:
+
Use Arduino IDE's 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 IDE's serial monitor output]]
  
 
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.
 
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.
Line 138: Line 140:
  
 
==== 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:
+
===== Method 1: Command line =====
 
+
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:
 +
<syntaxhighlight>
 
  --generic=serial,in,30,/dev/ttyACM0,controltest
 
  --generic=serial,in,30,/dev/ttyACM0,controltest
 +
</syntaxhighlight>
  
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]]
+
===== Method 2: FGRun =====
 +
Alternatively, you can use FlightGear's graphical user interface (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 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]
 
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]

Revision as of 08:18, 10 February 2015

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.

About Arduino

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. The hardware is a microcontroller designed around an 8-bit or 32-bit microcontroller, with several digital and analog I/O ports. The software is the Arduino IDE.

Example 1: Controlling internal properties

By Vaipe

This example demonstrates the use of a switch and a potentiometer to control the Property Tree.

Equipment and software

The following equipment was used for this example:

Input protocol file

Input protocol file is used to specify how serial information is read by Flightgear. In Ubuntu protocol files are found in: /usr/share/games/flightgear/protocol directory.

Protocol file structure

Create controltest.xml file in your protocol folder and paste code from below to it.

<?xml version="1.0"?>

<PropertyList>

<generic>
    <input>   
        <line_separator>\n</line_separator>
        <var_separator>,</var_separator>
   
        <chunk>
            <name>Strobe</name>
            <node>/controls/lighting/strobe</node>
            <type>bool</type>
        </chunk>
   
        <chunk>
            <name>Throttle</name>
            <node>/controls/engines/engine/throttle</node>
            <type>float</type>
        </chunk>
 
    </input>
</generic>

</PropertyList>

See Generic protocol for a description of the various XML tags.

Wiring and coding

Wiring

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. The diagram below illustrates the setup.

Wiring schematic for connecting the potentiometer and switch to Arduino

Code

Copy this C code to Arduino IDE and send it to the Arduino Uno:

    /*
      FGFS Input Test
      Reads a digital input on pin 7, prints the result to the serial port.
      Reads a potentiometer input on A0 and print result to serial port.
      This example code is in the public domain.
    */
 
    int potPin = 0;       // potentiometer on A0
    int switchPin = 7;    // switch on pin 7
    float potValue = 0;   // float variable to store potentiometer value
 
    void setup() {
    Serial.begin(9600);          // open serial connection
    pinMode(switchPin, INPUT);   // pin 7 declared as input
    }
        
         
    void loop() {
 
    Serial.print(digitalRead(switchPin));   // read and print switch state
    Serial.print(",");                      // print ,
    potValue = analogRead(potPin);          // read potentiometer and store it to potValue
    potValue = potValue / 1024;             // divide potValue with 1024 to make it between 0 and 1
    PrintDouble(potValue, 2);               // pass potValue to PrintDouble-function, read from below what magic happens
    Serial.print("\n");                     // print new line
    delay(500);                             // delay only for making this guide easier to follow on serial monitor
 
    }
 
 
    void PrintDouble(double val, byte precision){
      // prints val with number of decimal places determine by precision
      // precision is a number from 0 to 6 indicating the desired decimial places
      // example: lcdPrintDouble( 3.1415, 2); // prints 3.14 (two decimal places)
      // From http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548
     
      if(val < 0.0){
        Serial.print('-');
        val = -val;
      }
 
      Serial.print (int(val));  //prints the int part
      if( precision > 0) {
        Serial.print("."); // print the decimal point
        unsigned long frac;
        unsigned long mult = 1;
        byte padding = precision -1;
        while(precision--)
      mult *=10;
 
        if(val >= 0)
     frac = (val - int(val)) * mult;
        else
     frac = (int(val)- val ) * mult;
        unsigned long frac1 = frac;
        while( frac1 /= 10 )
     padding--;
        while(  padding--)
     Serial.print("0");
        Serial.print(frac,DEC) ;
      }
    }

Testing serial output

Use Arduino IDE's serial monitor and you should see something like this:

Arduino IDE's serial monitor output

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.

Tip  Remember to unplug Arduino's USB cable and plug it back.

FlightGear will not be able to read serial without doing this!

You have to do this every time after you use the Arduino IDE.

Start FlightGear

Method 1: Command line

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
Method 2: FGRun
Alternatively, you can use FlightGear's graphical user interface (FGRun) to launch Flightgear. Select the correct settings from Advanced Option tab.
Starting Flightgear with FGRun, selecting input/output options

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 Flightgear, Arduino and Linux

Arduino LCD panel displaying speed, heading and altitude.

Example

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:

<?xml version="1.0"?>

<PropertyList>

<generic>
    <output>
        <binary_mode>false</binary_mode>
        <line_separator>newline</line_separator>
        <var_separator>newline</var_separator>
        <preamble></preamble>
        <postamble></postamble>

        <chunk>
            <name>Altitude</name>
            <node>/position/altitude-ft</node>
            <type>integer</type>
            <format>altitude=%i</format>
        </chunk>

        <chunk>
            <name>RPM</name>
            <node>/engines/engine/rpm</node>
            <type>integer</type>
            <format>rpm=%i</format>
        </chunk>

    </output>

    <!-- <input>
        <line_separator>newline</line_separator>
        <var_separator>tab</var_separator>
        <chunk>
        </chunk>
    </input> -->

</generic>

</PropertyList>

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: [1]

As hardware, five seven segment displays were used, multiplexed straight on the Arduino device. In production, you'd rather use some 74HC595 or other shift register chips to drive them, to unload the Arduino and have more current. A demo is uploaded to youtube, with voiceover in which the display shows the RPM of the first engine of (the single engine) DR400: [2]

Related content

External links