Board index FlightGear Support Interfacing

Interface with Qt

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

Interface with Qt

Postby tsalkt » Thu Jul 27, 2017 1:08 pm

I have a desktop application developed in Qt (C++ and QML) that is receiving data from flightgear using UDP sockets. Everything is working fine, I just need to run FG sending to a particular port and specify in the generic protocol ".xml" which parameters I want to send. However, I would like to get rid of the C++ part.

The Phi web server looks really interesting, and I have seen that the property tree is also available in the browser. I was thinking if there is any way of accessing that property tree (read/write) from QML/Javascript, so I can avoid tx/rx UDP messages by myself. My constrains are:

    Read/write data from/to FG
    Read about 40 parameters
    Sampling frequency of 2-4 Hz
    Accessible from Javascript/QML

These are some solutions I have found, but I'm not sure about them:
    On the other hand, I have discovered that each field of the property tree can be retrieved in a JSON by using http://localhost:XXXX/json/orientation, where orientation is a field of the property tree. However, with this method I can't write to the property tree, can I?
tsalkt
 
Posts: 3
Joined: Thu Mar 16, 2017 8:18 am

Re: Interface with Qt

Postby joaopagotto » Tue Oct 03, 2017 5:20 pm

Hi friend, good afternoon, I also need to communicate FlightGear with Qt, did you get anything related to this? Could you give me some tips? Awaiting...
joaopagotto
 
Posts: 2
Joined: Tue Oct 03, 2017 4:50 pm

Re: Interface with Qt

Postby joaopagotto » Wed Oct 18, 2017 12:18 pm

See an IO solution communication Flightgear with Qt.
I'm using Qt 5.9.2

FgTest01.pro
Code: Select all
QT -= gui
QT += network

CONFIG += c++11 console
CONFIG -= app_bundle

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += main.cpp


main.cpp
Code: Select all
#include <QCoreApplication>
#include <QNetworkDatagram>
#include <QUdpSocket>
#include <QDataStream>
#include <QDebug>

//-----------------------------------------------------------------------------

typedef struct {
   double indicatedSpeed_Kt;
   double pitchAtt_Deg;
   int magnetos_Pos;

   int footer;
} Receiver;

//-------------------------------------

typedef struct {
   int magnetos_Pos;
} Sender;

//-----------------------------------------------------------------------------

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   //--------------------------------------------------------------------------
   // Informations:

   // Docs: http://wiki.flightgear.org/Generic_protocol

   // Copy file simcore_sender.xml to $FG_ROOT/Protocol/proto_sender.xml
   // Copy file simcore_receiver.xml to $FG_ROOT/Protocol/proto_receiver.xml

   // --generic=socket,out,20,localhost,5051,udp,simcore_sender
   // --generic=socket,in,20,,5052,udp,simcore_receiver

   //--------------------------------------------------------------------------
   // Data Read/Write

   Receiver receiver;
   Sender sender;

   char buffer[32];

   int interaction = 0;

   QUdpSocket udp;
   if ( udp.bind(QHostAddress::LocalHost, 5051) )
   {
      printf("- Connected.\n");
      forever
      {
         while ( udp.hasPendingDatagrams() )
         {
            //--------------------------------------
            // Receiver

            QNetworkDatagram datagram = udp.receiveDatagram();
            memcpy(&receiver, datagram.data().data(), sizeof(receiver));
            if ( receiver.footer == 0x12345678 )
            {
               qDebug() << interaction++
                        << "rx:"
                        << receiver.indicatedSpeed_Kt
                        << receiver.pitchAtt_Deg
                        << receiver.magnetos_Pos;

               //--------------------------------------
               // Sender

               sender.magnetos_Pos = 0;

               sprintf(buffer, "%d\t\n", sender.magnetos_Pos);
               udp.writeDatagram(buffer, strlen(buffer), QHostAddress::LocalHost, 5052);
            }
         }
      }
   }

   //--------------------------------------------------------------------------

   return a.exec();
}

//-----------------------------------------------------------------------------


proto_sender.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
   <generic>
      <output>
         <binary_mode>true</binary_mode>
         <binary_footer>magic,0x12345678</binary_footer>
       <byte_order>host</byte_order>
         <chunk>
            <node>/velocities/airspeed-kt</node>
            <type>double</type>
         </chunk>
         <chunk>
            <node>/orientation/pitch-deg</node>
            <type>double</type>
         </chunk>
         <chunk>
            <node>/controls/switches/magnetos</node>
            <type>int</type>
         </chunk>      
      </output>
   </generic>
</PropertyList>


proto_receiver.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
   <generic>
      <input>
         <line_separator>newline</line_separator>
         <var_separator>tab</var_separator>
         <chunk>
            <node>/controls/switches/magnetos</node>
            <type>int</type>
         </chunk>
      </input>
   </generic>
</PropertyList>
joaopagotto
 
Posts: 2
Joined: Tue Oct 03, 2017 4:50 pm

Re: Interface with Qt

Postby saul » Wed Nov 22, 2017 7:40 pm

Hi guys,

I dont know if it is useful, but a few years ago I wrote some code in Qt 4 that connects a Raspberry Pi to Flightgear via telnet. I made a tutorial how to do this (link below). You can both read and write to the tree.... however, I dont think it is the best way if you need to read at a very high rate. It will however work well if you want to write new values for the controls/instuments. For reading parameters very quickly, I think the udp is the best option.

https://sites.google.com/site/raspberryflightgear/

Saul
saul
 
Posts: 40
Joined: Tue Nov 26, 2013 10:57 pm

Re: Interface with Qt

Postby bugman » Thu Nov 23, 2017 12:36 am

@Saul: Have you thought of contributing all of this to the FG wiki? It can pretty much be bulk copied in as multiple wiki pages (or subpages) and would be a nice complement to all of the other hardware pages there.

Cheers,
Edward
bugman
Moderator
 
Posts: 1808
Joined: Thu Mar 19, 2015 10:01 am
Version: next


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 4 guests