Board index FlightGear Support Interfacing

Arduino flightgear interface  Topic is solved

Connecting two computers, using generic protocol, connecting with Matlab?

Arduino flightgear interface

Postby wil » Sun Mar 12, 2017 11:45 pm

I am trying to create a project with arduino that will control the throttle, mixture, gear, and parking brake of flight gear. So far I have the following code for the arduino that reads values from sensors and outputs them over serial. Because from reading other forums posts that said there were problems when interfacing arduino and flight gear directly on serial I wrote a simple python script, shown below, that reads the data from the arduino over serial. Other people have suggested using telnet to send the data from python to flight gear. How would I go about achieving that. At the bottom of my post I have added the code I put in an xml file in flight gears protocol folder that I based off of other arduino/flight gear tutorials that I found online. However none of those actually worked for me so I am not sure if it is written correctly.

Code: Select all
int potPin = 0;
int pot2Pin = 1;
int BUTTON = 7;
int BUTTON2 = 6;
int reading3 = 0;
int reading4 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON, INPUT);
  pinMode(BUTTON2, INPUT);
}
 float x = 1;
 float x2 = 1;
 int b1 = 0;
void loop() {
  float reading = analogRead(potPin);
  float reading2 = analogRead(pot2Pin);
  reading3 = digitalRead(BUTTON);
  reading4 = digitalRead(BUTTON2);
  float x2 = reading2/1023;
  float x = reading/1023;
  Serial.print(x);
  Serial.print(",");
  Serial.print(x2);
  Serial.print(",");
  Serial.print(reading3);
  Serial.print(",");
  Serial.println(reading4);
}


Code: Select all
import serial
ser = serial.Serial('COM21', 9600)
while True:
    x=ser.readline().strip().decode()
    print(x)


Code: Select all
<?xml version="1.0"?>
<PropertyList>
    <generic>
        <input>
            <line_separator>newline</line_separator>
         <var_separator>,</var_separator>
            <chunk>
                <name>throttle</name>
            <type>double</type>
                <format>%d</format>
                <node>/controls/engines/engine/throttle</node>
            </chunk>
         <chunk>
            <name>mixture</name>
            <type>double</type>
                <format>%d</format>
                <node>/controls/engines/engine/mixture</node>
         </chunk>
         <chunk>
            <name>park</name>
            <type>double</type>
                <format>%d</format>
                <node>/controls/gear/brake-parking</node>
         </chunk>
         <chunk>
            <name>gear</name>
            <type>bool</type>
                <format>%d</format>
                <node>/controls/gear/gear-down</node>
         </chunk>
        </input>
    </generic>
</PropertyList>
wil
 
Posts: 22
Joined: Sat Dec 08, 2012 2:51 pm

Re: Arduino flightgear interface

Postby wil » Mon Mar 13, 2017 9:16 pm

Ok so I did a little looking around and I found out that I can use nasal to set these properties. The code I tried is below. Is there a good way to send nasal values for variables from python? I am trying to google an answer but I am having trouble finding information on the subject.

Code: Select all
setprop("/controls/engines/engine/throttle", 1);
setprop("/controls/engines/engine/mixture", 1);
setprop("/controls/gear/brake-parking",1);
setprop("/controls/gear/gear-down", "false");
wil
 
Posts: 22
Joined: Sat Dec 08, 2012 2:51 pm

Re: Arduino flightgear interface

Postby jam007 » Mon Mar 13, 2017 9:44 pm

wil wrote in Sun Mar 12, 2017 11:45 pm:Other people have suggested using telnet to send the data from python to flight gear. How would I go about achieving that.

Have you seen this Wiki page and this code?
jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Arduino flightgear interface

Postby wil » Mon Mar 13, 2017 10:57 pm

Thanks for the help. I had not seen that code however it is not working for me on line 100 there is a syntax error with the comma highlighted. Replacing the comma and space of lines 100 and lines 102 remove the error but it is replaced with saying cannot import name split. To fix that I commented out that line because according to google you do not need to import string into python and that allowed the code to run. So I tried running the commands mentioned in the comments of that code.
Code: Select all
fg = FlightGear('127.0.0.1', 23)
fg['/controls/gear/brake-parking'] = 1

However I got the following error in python
Code: Select all
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    fg['/controls/gear/brake-parking'] = 1
  File "C:/Users/bentc/AppData/Local/Programs/Python/Python36/telnet.py", line 136, in __setitem__
    self.telnet.set( key, value )
  File "C:/Users/bentc/AppData/Local/Programs/Python/Python36/telnet.py", line 56, in set
    self._putcmd('set %s %s' % (var,value))
  File "C:/Users/bentc/AppData/Local/Programs/Python/Python36/telnet.py", line 68, in _putcmd
    Telnet.write(self, cmd)
  File "C:\Users\bentc\AppData\Local\Programs\Python\Python36\lib\telnetlib.py", line 287, in write
    if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes

I am not sure what the cause of that error is. Does it have something to do with the changes I made to the script or am I doing something wrong? Thanks again for your help.
wil
 
Posts: 22
Joined: Sat Dec 08, 2012 2:51 pm

Re: Arduino flightgear interface

Postby jam007 » Tue Mar 14, 2017 7:23 am

The code is written in an older version of Python (2.?) and I assume you use Python3. Changing the comma to "as" seems to be the thing to do.
See more here

The split import problem might also be due to changes in the language.
jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Arduino flightgear interface

Postby ludomotico » Tue Mar 14, 2017 9:40 am

check also this thread: https://forum.flightgear.org/viewtopic.php?f=24&t=31536

As jam007 said, the code linked previously was written for Python2. Python3 and Python2 code have some subtle differences and converting code from v2 to v3 could be a nightmare, specially if you are not familiar with the language.

One of the differences is the management of strings and byte arrays. They are the same in Python2 and completely different in Python3. In Python3, you must convert byte arrays to strings explicitly. This is the error the code your are testing shows.

I don't know which python environment you are running. In Linux, it is possible to force the use of a Python2 interpreter instead of Python3. Check if this is possible in your environment.
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10

Re: Arduino flightgear interface  

Postby jam007 » Tue Mar 14, 2017 10:19 am

Made a test script in Python3 that connects to FG, sets a property and reads it. Might be something to start from:
Code: Select all
import telnetlib

HOST="localhost"
PORT=5401
eol="\r\n".encode('ascii')                               #EOL characters

tn=telnetlib.Telnet(HOST, PORT)
tn.write(eol)                                            #Gets telnet prompt
tn.write("set /tmp/test 1".encode('ascii')+eol)          #Sets a propery
tn.read_until(eol)                                       #Reads and discards reply
tn.write("get /ai/models/count".encode('ascii')+eol)     #Gets a property
#Reads gotten reply strips EOL and splits into list
got=tn.read_until(eol).decode('ascii').strip().split()
print(" ".join(got[1:4]))                               #Prints property = value
tn.write("quit".encode('ascii')+eol)                    #Shuts down FG telnet conection






All the encode('ascii') are because Python3 uses unicode as string standard and telnet ascii.

wil wrote:reading other forums posts that said there were problems when interfacing arduino and flight gear directly on serial

Could you be more specific? Bidirectional serial does not work but are there problems using serial for only inputs as you plan? Avoiding the middle man (snake) and two different communication protocols would be better if possible IMO.

* Edited as I found two bugs in the code.
Last edited by jam007 on Wed Mar 15, 2017 11:29 am, edited 1 time in total.
jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Arduino flightgear interface

Postby wil » Tue Mar 14, 2017 3:05 pm

I am running Windows and was able to install python 2 alongside 3 which allowed the linked script to run. However jam007 the test script that you just linked makes is a whole lot simpler and I can actually understand how it works with my basic understanding of python. So I think I will use that to write the interface between flightgear and the arduino.

The problem I was having when trying to use arduino directly to flightgear over serial for an input only was that flightgear would hang when starting up at finalizing position. I was only trying to use serial for inputs. But I think I will stick with the python middleman for now because I think it gives me a little more flexibility in what I can do. I also think that once I get the inputs working that I might also want to have flightgear output to the arduio.
wil
 
Posts: 22
Joined: Sat Dec 08, 2012 2:51 pm

Re: Arduino flightgear interface

Postby jam007 » Wed Mar 15, 2017 11:39 am

wil wrote in Tue Mar 14, 2017 3:05 pm:The problem I was having when trying to use arduino directly to flightgear over serial for an input only was that flightgear would hang when starting up at finalizing position.
Ok. I have made a few tests with serial from Arduino and it has worked but good to know that it might be unstable. (Have a few cockpit thought my self.)

Edited the code above as I found two mistakes. FG replies with a copy of the set command so that has to be read and discarded. Also "quit" is the shut down command mentioned in the wiki.
jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 5 guests