Board index FlightGear Support Interfacing

Help with Serial out to Arduino

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

Help with Serial out to Arduino

Postby felipetallabs » Wed Sep 16, 2020 9:40 pm

Hi All

I am trying to send some information regarding the stall status of a yasim aircraft from FG to Arduino, I am using a simplified xml and arduino code just for testing, I am trying to populate a simple two member array but is not working, I am running windows 10, with FG 2020.1.3
ANY idea will be most appreciated, cheers and thanks.

the xml as follows:

Code: Select all
<?xml version="1.0"?>
<output>
<line_separator>newline</line_separator>
<var_separator>,</var_separator>

<chunk>
<name>surface</name>
<node>/fdm/yasim/debug/surfaces/surface/alpha</node>    // this is node 0 from debug that has stall information about surfaces
<type>float</type>
<format>%1.3f</format>
</chunk>

<chunk>
<name>surface</name>
<node>/fdm/yasim/debug/surfaces/surface[1]/alpha</node>    // this is node 1 from debug that has the information about surfaces
<type>float</type>
<format>%1.3f</format>
</chunk>
</output>


The Arduino code:

Code: Select all
int pinArray[2]={0,1};  // this is the array I am trying to populate with the FG information
void setup() {
  // put your setup code here, to run once:
pinMode(pinArray[1],OUTPUT);   
Serial.begin(9600);
if(Serial.available()>0);{
  pinArray[2]=Serial.read();
 
}

}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print(pinArray[0]);
delay(300);
Serial.print(pinArray[1]);
delay(300);
}


and the socket:

Code: Select all
fgfs --generic=serial,out,10,COM3,test_out
Last edited by Johan G on Thu Sep 17, 2020 6:45 am, edited 1 time in total.
Reason: Added [code] tags. They can be added manually or with the [^_ ] button obove the text editing box.
felipetallabs
 
Posts: 13
Joined: Tue Jun 09, 2020 7:21 pm

Re: Help with Serial out to Arduino

Postby SE-HK » Thu Sep 17, 2020 5:59 am

Hello!

You need to specify the baud rate (9600) in fgfs as well, should be after the device, ie ....COM3,9600,test_out
If you read the logs it will probably complain about this.

Also, the output from FG is in ascii but in format %1.3f and I am not sure what it will look like.

I recommend to do this in 2 steps.
1. Save the output to a file first, ie --generic=file,out,1,data.txt, test_out and see what the output will look like.
2. Run the serial console in Arduino IDE and manually copy-paste lines from the previous file. Then you can see how your code behaves to the input.

When you are satisfied with both steps, you can connect them directly using --generic=serial...
SE-HK
 
Posts: 54
Joined: Wed Sep 02, 2009 2:16 pm
Location: Stockholm, Sweden
Version: git
OS: Ubuntu 20.04

Re: Help with Serial out to Arduino

Postby xcvb » Thu Sep 17, 2020 6:10 am

Please add code tags in your post to improve readability. "It does not work" is not a very precise definition. Did you check with a second serial adaptor? I never used serial output but I think the following points could be wrong:

- Cable
Did you use a null modem cable (TxD connected with RxD)? I remember, Arduino uses USB. Forget about this.

- Baud rate
Baud rate is not defined

- ASCII vs binary protocol
FlightGear sends ASCII data but your Arduino waits for binary data. This cannot work. Use this to switch to binary protocol little endian:

Code: Select all
         <binary_mode>true</binary_mode>
         <byte_order>host</byte_order>
Last edited by xcvb on Thu Sep 17, 2020 6:21 am, edited 1 time in total.
xcvb
 
Posts: 132
Joined: Sat Mar 14, 2015 3:08 pm
Version: Next
OS: Fedora Kinoite

Re: Help with Serial out to Arduino

Postby xcvb » Thu Sep 17, 2020 6:17 am

I forgot float vs int. At some point you need to convert the float values to int before you can write the data to the pins.
xcvb
 
Posts: 132
Joined: Sat Mar 14, 2015 3:08 pm
Version: Next
OS: Fedora Kinoite

Re: Help with Serial out to Arduino

Postby felipetallabs » Fri Sep 18, 2020 4:28 am

Hi
Thanks a lot for both of your comments, I think to begin with there must be something wrong either with the xml file or the address of the file, just to be sure as I am very new at this, my xml file should be located as follows: C:\Program Files\FlightGear 2020.1.3\data\Protocol\test.xml ???
I followed SE-HK advice and I send the data from the xml file to a text file named datum, and I used the following line: fgfs --generic=file,out,1,datum.txt,test

I located the line at the FG launcher at settings/additional settings. However, no data is being transferred to the datum.txt file, can anyone see where the
mistake(s) are?

Thanks again
felipetallabs
 
Posts: 13
Joined: Tue Jun 09, 2020 7:21 pm

Re: Help with Serial out to Arduino

Postby SE-HK » Fri Sep 18, 2020 6:18 am

I think your xml file is missing some tags <PropertyList> and <generic>.

Code: Select all
<?xml version="1.0"?>
<PropertyList>
 <generic>
  <output>
........................ your list of <chunks> here
  </output>
 </generic>
</PropertyList>


If you still do not get any output to file:
When you run FG you will get some text output to the console window (if there is any in windows version of FG?) or to a log file (in Linux the log file is in the user's directory, not sure about windows location). Please take a look in the log, if it cannot load the protocol you will get some error such as "Couldn't find protocol file for 'test'". And if there is some other problem you will see.
SE-HK
 
Posts: 54
Joined: Wed Sep 02, 2009 2:16 pm
Location: Stockholm, Sweden
Version: git
OS: Ubuntu 20.04

Re: Help with Serial out to Arduino

Postby felipetallabs » Fri Sep 18, 2020 9:10 pm

Hi SE-HK

I found the following lines in the FG debug window regarding I/O
191.81 [ALRT]: io Error opening file: path "datum.txt"
191.81 [ALRT]: io Error opening channel communication layer
191.81 [ALRT]: io I/O channel config failed

evidently there is a I/O channel config error, any guess what the problem might be?

Thanks again
felipetallabs
 
Posts: 13
Joined: Tue Jun 09, 2020 7:21 pm

Re: Help with Serial out to Arduino

Postby SE-HK » Sat Sep 19, 2020 1:19 am

Perhaps you do not have write permission to that file. If you just write "datum.txt" the location depends on from where you launch Flightgear, it will not automatically be saved to your home directory. How about trying an absolute path, such as C:\datum.txt or C:\User\me\datum.txt?
SE-HK
 
Posts: 54
Joined: Wed Sep 02, 2009 2:16 pm
Location: Stockholm, Sweden
Version: git
OS: Ubuntu 20.04


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 4 guests