Board index FlightGear Support Interfacing

Arduino <--> FlightGear via Serial Trouble  Topic is solved

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

Arduino <--> FlightGear via Serial Trouble

Postby mistersoar » Wed May 05, 2021 6:58 am

Hello,

I've been scratching my head for a while trying to figure out how to get my Arduino + Ethernet Shield to talk to FlightGear. Using the information from this forum and others, I've written up an Arduino IDE code as shown below. It is simply taking in a potentiometer reading (analog input) and I'm converting it to be [-1 1] and then just trying to send the data.

Code: Select all
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>


// DECLARE VARIABLES
const int stickxio = A1; //Define stick aileron (x) input
float stickx = 0;        //Start aileron (x) central
String lat_stick;

// SETUP THE ETHERNET CONNECTION (MAC ADDRESS & IP ADDRESS)
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x84, 0xA1};
IPAddress PC_IP(192, 168, 1, 191);
IPAddress UNO_IP(192, 168, 1, 190);
//IPAddress ip(192, 168, 1, 191);

unsigned int UDP_Port = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  //Serial.begin(9600); 
  // start the Ethernet and UDP:
  Ethernet.begin(mac,UNO_IP);
  // begin the UDP
  Udp.begin(UDP_Port);
}

void loop() {

  // GATHER THE VOLTAGES AND CONVERT THEM TO [-1 1] signals
  stickx = (analogRead(stickxio)/512.0)-0.998; //Calibration span and offset
  //Serial.print(stickx);
  lat_stick = String(stickx,2);
  //Serial.println(lat_stick);

  //sendUDP(stickx);
  Udp.beginPacket(PC_IP, UDP_Port);
  Udp.print(lat_stick);
  Udp.endPacket();
  delay(10);
 
}


By uncommenting the lines of Serial.print, I've finally got the String value called "lat_stick" to display the actual value shown by stickx. However, I still cannot get this to come through in the Debugger in FlightGear. I'm using the following XML file for communication:

Code: Select all
--generic=socket,in,100,192.168.1.191,8888,udp,hardware ^


I'm receiving the data like so (that being said, I've tried float, integer, and string to no avail):

Code: Select all
<?xml version="1.0"?>

<PropertyList>

<generic>
   <input>
      <line_separator>\n</line_separator>
      <var_separator>,</var_separator>

      <chunk>
         <name>arduino</name>
         <type>string</type>
         <node>/instrumentation/altimeter/arduino</node>
      </chunk>
      
   </input>   
</generic>

</PropertyList>


If there is anyone out there that has experience with this, I would greatly appreciate any advice. I am new to the forum so if I've uploaded anything wrong please let me know and I'll change it or add whatever information is needed.

Thank you.
Last edited by mistersoar on Fri May 07, 2021 11:40 am, edited 1 time in total.
mistersoar
 
Posts: 5
Joined: Wed May 05, 2021 6:47 am

Re: Arduino > FlightGear via UDP Trouble

Postby tom_nl » Wed May 05, 2021 11:23 am

I've done a lot of head scratching with arduinos and flightgear - the first time you try it it can be confusing.

Couple of questions:
1) is the xml file called hardware.xml? because that's what the launch command is looking for relative to where you run the command. In the
Code: Select all
--generic=socket,in,100,192.168.1.191,8888,udp,hardware
line, change 'hardware' to the full absolute path to 'hardware.xml' i.e. /path/to/hardware.xml to make sure FG finds the file.

2) What property are you trying to write to/change in the property tree? Bear in mind that the property '/instrumentation/altimeter/arduino' isn't actually bound to anything (unless you have some custom nasal code that reads it and does something) so even if it changes, it won't do anything! However it will still get updated - Check the property tree when FG is running to see if turning the pot changes that property. You can then re-bind '/instrumentation/altimeter/arduino' to something you want to change (assuming it's something like altimeter pressure).

One other thought - Arduino's don't like working with floating point numbers - they have to handle all this in software which takes a lot of cycles and can introduce rounding errors. It's best to stick with integers. There's a few ways to handle this.

1) In the arduino code. instead of mapping the value -1 to 1, map it to -1000 to 1000 instead, then do the rescaling -1 to 1 on the FG computer by multiplying by 0.001. This is very easy by adding a <factor> to hardware.xml to rescale it:

Code: Select all
      <chunk>
         <name>arduino</name>
         <type>float</type>
         <node>/instrumentation/altimeter/arduino</node>
        <factor>0.001</factor>
      </chunk>


Also, if it's a floating point number, specify it as 'float' in hardware.xml.

However, I found this also introduced rounding errors. Analogue reading in Arduinos goes from 0-1024, so I handled this in a different way which I think is more elegant plus should be less computationally demanding. I set a limit on the pot values so they are constrained to read between 11 to 1011, then subtracted 511 to result in the pot reading -500 to 500 - i.e. a line in the arduino code like this (elevtrimraw is the raw pot value):

Code: Select all
elevtrimout = ((constrain(elevtrimraw, 11, 1011)) - 511);


Then in the hardware.xml. a factor of 0.002 ( <factor>0.001</factor>) in the XML) can be applied to map this -1 to 1.

A thought - do you absolutely have to use UDP? I've had all sorts of 'fun' with ethernet on Arduinos, so for my panel I use serial communications which I find much more straightforward. As you already have the properties sending successfully by serial, try binding them to FG using that - change the 'udp' line to serial instead:
Code: Select all
--generic=serial,in,<refresh_rate_in_hz>,<com_port>,<baudrate>,/path/to/hardware.xml
Then once that's working. go on to getting into working with ethernet.

Hope this helps,

Tom
tom_nl
 
Posts: 84
Joined: Tue Aug 04, 2020 11:41 am
Location: Netherlands
OS: OS X Big Sur

Re: Arduino > FlightGear via UDP Trouble

Postby mistersoar » Thu May 06, 2021 1:02 am

Tom,

Thank you for your response. Let me address a few of your questions:

1. Yes, the XML file is named 'hawdware.xml'. I have updated the protocol file and it's posted lower in my response.

I've taken your advice of using an integer instead of float as well as your advice to send the whole numbers and factor afterward--thank you for that advice as I had no idea floats slowed things down.

2. As of now, I'm not trying to write to any specific property as I just wanted to check in FG to make sure that data was successfully even being sent (something I have not yet had). I have an XY-axis joystick hooked up and plan to send the axes to map to the aileron and elevator, respectively, once I've successfully got data sent. I made this property tree myself so there would be no interference by my keyboard writing over my Arduino data. My idea was exactly as you commented (check first, change after).

Lastly, I first attempted to use serial connection but had no luck as my FlightGear would get frozen on launch every time I did serial connection. If you'd be willing to, I would greatly appreciate any insight into my method of serial connection as maybe I have some simple mistake that's causing it not to function correctly. Here is my test method for sending serial data to FlightGear (just one axis is being sent right now as you'll see in the Arduino IDE code):

XML FILE:

Code: Select all
<?xml version="1.0"?>

<PropertyList>

<generic>
   <input>
      <line_separator>\n</line_separator>
      <var_separator>,</var_separator>

      <chunk>
         <name>arduino</name>
         <type>integer</type>
         <node>/instrumentation/altimeter/arduino</node>
         <factor>0.002</factor>
      </chunk>
      
   </input>   
</generic>

</PropertyList>


My new batch file is:

Code: Select all
--generic=serial,in,50,com5,9600,hardware ^


I noticed that changing the last part from 'hardware' to anything else would cause it to not recognize it. The hardware.xml file is located in the FG Protocol subfolder (C:\Program Files\FlightGear 2018.3.2\data\Protocol\hardware.xml) and it seems to pick it up fine.

Finally, my Arduino IDE code is:

Code: Select all
// OBTAIN THE CONTROLS
const int stickxio = A1; //Define stick aileron (x) input
const int stickyio = A0; //Define stick elevator (y) input

int stickx = 0;        //Start aileron (x) central
int sticky = 0;        //Start elevator (y) central

// INITIALIZE THE SERIAL CONNECTION
void setup() {
  Serial.begin(9600);    //Open up serial communication to PC
}

void loop() {
  // GATHER THE VOLTAGES
  stickx  = (constrain(analogRead(stickxio),11,1011)-511); //Calibration span and offset
  //sticky = (constrain(analogRead(stickyio),11,1011)-511); //Calibration span and offset

  //Serial.print('A');
  Serial.print(stickx);
  //Serial.print(sticky);

  Serial.print('\n');
  delay(20);
}


Running this configuration above, FlightGear gets frozen on "finalizing position" and will never load in. Hopefully I can figure out what my mistake(s) is(are) through a bit of back and forth. I use Windows 10 OS as well (not sure if that matters).

Thank you again for your response.
mistersoar
 
Posts: 5
Joined: Wed May 05, 2021 6:47 am

Re: Arduino > FlightGear via UDP Trouble

Postby ludomotico » Thu May 06, 2021 7:41 am

The frozen part, most probably, is because you have the Arduino IDE connected to the serial port at the same time than FlightGear tries to open the port. In Windows, only one application can access to a serial port at the same time.

If this is your case, try to close the Arduino IDE before starting FlightGear. Probably, closing the "serial port monitor" of the Arduino IDE is enough.
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10

Re: Arduino > FlightGear via UDP Trouble

Postby tom_nl » Thu May 06, 2021 9:36 am

Very happy to help. Sorry - I can't help with Windows 10 much as i'm on a Mac. All the code looks fine from a quick scan (and very similar to how i'm doing things). ludomotico's suggestion sounds likely - you need to make sure that nothing else is blocking the serial port. As suggested, closing the serial monitor in the Arduino IDE should be enough. There's also the usual things - check the baud rate is set the same, and set it as high as you can. I personally use 38400 as this is the fastest that mac FG supports.

Once you get the serial working. you can get smart and only send serial data to update the property tree when the value read by the Arduino changes. I use this on my panel to reduce traffic and the load on the arduino (I believe Serial.prints() are pretty demanding computationally, especially if you are needing to do lots of other time critical things on the Arduino too). More in this thread https://forum.flightgear.org/viewtopic.php?f=36&t=39103

Tom
tom_nl
 
Posts: 84
Joined: Tue Aug 04, 2020 11:41 am
Location: Netherlands
OS: OS X Big Sur

Re: Arduino > FlightGear via UDP Trouble

Postby ludomotico » Thu May 06, 2021 9:58 am

Serial communications are not really demanding, but they must honor de baud rate. If you want to send "hello word" at 9600 bauds (10 characters, 80 bits), it will take about 0,1 seconds because that's the speed you configured. The Arduino is doing nothing most of that time, it is just waiting for the right time to send a bit.

That's why Serial communications and time critical things don't mix very well in a system like the Arduino, where there a single execution thread and serial I/O is blocking. Sending data only when you really need seems like a sensible idea.

Anyway, checking if something changed makes the Arduino code much more complex. I'd recommend fixing this only if exact timing is critical in your system. You know: code first, optimize later and only if you really need.

In the linked thread, for example, the state of 90 buttons was sent using serial communications. I guess that involves the order of thousands of bits and a very significant part of a second is consumed just inside Serial.print() In this case, yes, you totally have to reduce the number of button states you send every second. If your Arduino manages and sends only a couple of inputs from the user, then I guess sending the state every time is fine.
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10

Re: Arduino > FlightGear via UDP Trouble

Postby Hooray » Thu May 06, 2021 11:49 am

You might want to take a loo at this: https://wiki.flightgear.org/Data_Distri ... es_Support
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Arduino > FlightGear via UDP Trouble

Postby mistersoar » Fri May 07, 2021 11:39 am

I just want to start by saying thank you to everyone who posted comments. Upon digging through the old threads, I found another person with the same problem and I installed "Terminal" and followed the steps and it (somehow) worked! My end goal is to communicate in the following way:

Arduinos --serial--> master Arduino --serial--> FlightGear --UDP--> Simulink
then, going back
Simulink --UDP--> FlightGear --serial--> Arduino

I have successfully got the Arduino --> Simulink working through my method done above (here's an example of me using pots to control things and it shows in simulink: https://gyazo.com/4bfe594ef8c5e346be38a66e1fa53771). However, my next task that I need to tackle is how to send data BACK to my Arduino via serial while it's also sending data to FlightGear. If anyone knows how to receive data from FG --> Arduino via serial I would appreciate any advice.
mistersoar
 
Posts: 5
Joined: Wed May 05, 2021 6:47 am

Re: Arduino <--> FlightGear via Serial Trouble

Postby Isaak » Fri May 07, 2021 12:59 pm

Sadly there is still an issue in FlightGear on windows regarding bidirectional Serial communication: it seems FlightGear can only handle Serial in or out on the same port. Therefore I have connected two Arduinos to FlightGear: one for input and one for output. Not ideal and I hope to find a solution for this. I think someone found the cause of the issue, but I don't know if it has been fixed yet.

I also use the Terminal application if FlightGear refuses to start with serial enabled. I have the experience that FlightGear will fail to load most of the time when you change some settings in the launcher. If you hit 'fly' directly then FlightGear will load most of the time, even without the "Terminal-route".
Want to support medical research with your pc? Start Folding at Home and join team FlightGear!
Isaak
 
Posts: 768
Joined: Sat Jun 04, 2011 3:52 pm
Location: Hamme, Belgium
Pronouns: he, him
Callsign: OO-ISA
Version: next
OS: Windows 10

Re: Arduino <--> FlightGear via Serial Trouble

Postby ludomotico » Fri May 07, 2021 2:00 pm

The necessity of using that "terminal" application does not make sense. I'm not saying it is wrong, but it shows there is some windows-only error in FlightGear's code. I've got to say that I've never use the "terminal" application, but I did experiment the freeze if the Arduino IDE was running while FlightGear initializes. I consider that normal and expected: only one application can access the serial port in Windows, you cannot open the same serial port twice.

(unless you are doing some tricks at the kernel level, as that "terminal" or any other monitor such as wireshark probably does)

Regarding bidirectional communications, i.e. "bi", using a serial port for input and output... they are just not supported currently. I believe they can be emulated in Linux using two connections, one for "in" and other for "out". This is not going to work in Windows because, you know, you cannot open a serial port twice. The information and the fix for "bi" communications in Windows and Linux is here, but I don't think it was ever integrated into the code: viewtopic.php?f=36&t=37796

There is a bug report regarding this issue opened since 2013, but it seems it failed to get enough attention: https://sourceforge.net/p/flightgear/codetickets/1191/
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10

Re: Arduino <--> FlightGear via Serial Trouble

Postby Isaak » Fri May 07, 2021 2:29 pm

I have reopened the bug report and assigned it to James. It was assigned the 'done' status with the tag 'expired', so I think this bug was never really fixed. Thanks for digging this up!
Want to support medical research with your pc? Start Folding at Home and join team FlightGear!
Isaak
 
Posts: 768
Joined: Sat Jun 04, 2011 3:52 pm
Location: Hamme, Belgium
Pronouns: he, him
Callsign: OO-ISA
Version: next
OS: Windows 10

Re: Arduino <--> FlightGear via Serial Trouble

Postby tom_nl » Fri May 07, 2021 4:46 pm

My experience is that 'in' and 'out' serial from the same serial port works fine on a Mac, not that this will help you of course.

I've been through similar on my panel - this also has two Arduinos - a Mega driving all the buttons that only sends via serial (on USB), and a nano driving a TFT and touchscreen that both sends and receives on a second (USB) serial port. At one stage I did have them talking to each other because one needed to know the state of the other - I initially tried serial, but had problems that I couldn't resolve so I then used I2C for inter-Arduino communication instead - this seemed to work well.

The other option could be to use a Arduino that has more than one serial port - for example a Mega. You can then specify one port ask a 'out' and another as an 'in'. One serial on the mega being the USB port and the others being on the headers. To connect the other serial ports via USB you'll need a USB to TTL serial converter such as an FTDI232 USB converter.

Tom
tom_nl
 
Posts: 84
Joined: Tue Aug 04, 2020 11:41 am
Location: Netherlands
OS: OS X Big Sur

Re: Arduino <--> FlightGear via Serial Trouble  

Postby mistersoar » Sat May 08, 2021 5:30 am

So I'd be perfectly fine with the following:

Arduino --serial--> FG --UDP--> Simulink
BACK
Simulink --UDP--> Arduino2 |OR| Simulink --UDP--> FG --UDP--> Arduino2

Does anyone have any experience sending data from FG/Simulink via UDP to an Arduino? I've got half of the route needed for data complete but I haven't been able to figure out the bottom. I'm fine with not using serial as I own an ethernet shield and would prefer UDP if possible I just have no idea how to set it up.

In Simulink, I would be sending back 6 voltage signals which will then be sent out to an Arduino Uno w/ Ethernet shield, and then--using TX/RX between the Arduino Uno and 3 Due's--send this data to the 3 Due's which then sends out the DAC voltage to 2 motors each. However, I have no idea how to send data via UDP to an Arduino and actually successfully read that data.

I tried connecting 2 Arduino Uno's on \com3 and \com5 with one sending and the other receiving and I had no luck getting data back. I tried using a code on another thread but I just want to know how to receive data and convert it to integers to then send via TX/RX serial to another arduino. I know how to send int values via TX/RX but just have no idea how to read serial/UDP data onto my Arduino successfully.
mistersoar
 
Posts: 5
Joined: Wed May 05, 2021 6:47 am

Re: Arduino > FlightGear via UDP Trouble

Postby mistersoar » Sat May 08, 2021 5:41 am

ludomotico wrote in Thu May 06, 2021 7:41 am:The frozen part, most probably, is because you have the Arduino IDE connected to the serial port at the same time than FlightGear tries to open the port. In Windows, only one application can access to a serial port at the same time.

If this is your case, try to close the Arduino IDE before starting FlightGear. Probably, closing the "serial port monitor" of the Arduino IDE is enough.


This wasn't the issue. Whether I kept IDE open or closed it does the same thing until I open Terminal, connect, and close it. It's a Windows thing I guess but it's very annoying :cry:
mistersoar
 
Posts: 5
Joined: Wed May 05, 2021 6:47 am


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 2 guests