Board index FlightGear Support Interfacing

Interfacing flightgear with UDP/TCP socket.

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

Interfacing flightgear with UDP/TCP socket.

Postby Kalai » Sun Dec 03, 2017 12:11 pm

I am trying to build home cockpit simulator with Flightgear 2017.3.1. For connecting switches and other hardware, I planned to use
Raspberry pi which can receive hardware(switches,yoke,pedals) inputs.From those input I can send data to flightgear via udp/tcp protocol from Raspberry pi. Before I could implement this with Raspberry pi I did a sample to send and receive data to/from flightgear using udp. Receiving data from fg worked but sending data to fg did not work.

output.xml
Code: Select all
<?xml version="1.0"?>
 <PropertyList>
 <generic>
    <output>
        <line_separator>newline</line_separator>
        <var_separator>,</var_separator>
 
        <chunk>
           <node>/controls/flight/elevator</node>
           <name>elevator</name>
           <type>float</type>
           <format>%f</format>
         </chunk>

          <chunk>
           <node>/controls/flight/aileron</node>
           <name>aileron</name>
           <type>float</type>
           <format>%f</format>
         </chunk>

    </output>
  </generic>
 </PropertyList>


input.xml
Code: Select all
<?xml version="1.0"?>
 <PropertyList>
 <generic>
    <input>
        <line_separator>newline</line_separator>
        <var_separator>,</var_separator>
 
        <chunk>
           <node>/controls/flight/elevator</node>
           <name>elevator</name>
           <type>float</type>
         </chunk>

          <chunk>
           <node>/controls/flight/aileron</node>
           <name>aileron</name>
           <type>float</type>
         </chunk>

    </input>
  </generic>
 </PropertyList>


UdpSocketListen.py
Code: Select all
import socket
import sys

HOST = ''"   # ip address of my device which act as server
PORT = 9999 # Arbitrary non-privileged port

# Datagram (udp) socket
try :
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print 'Socket created'
except socket.error, msg :
    print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()


# Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#now keep talking with the client
while 1:
    # receive data from client (data, addr)
    d = s.recvfrom(1024)
    data = d[0]
    addr = d[1]

    if not data:
        break

    reply = 'OK...' + data

    #s.sendto(reply , addr)
    print 'Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.strip()

s.close()


UdpSendData.xml
Code: Select all
import socket

UDP_IP_ADDRESS = " " # ip address of device that act as sever
UDP_PORT_NO = 9900
Message = "0.100000,0.200000\n"
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
bytesToSend = str.encode(Message)
clientSock.sendto(bytesToSend, (UDP_IP_ADDRESS, UDP_PORT_NO))
print bytesToSend


In flightgear i gave,

fgfs --httpd=5400 --generic=socket,in,10,<ip>,9900,udp,myinput --generic=socket,out,10,<ip>,9999,udp,myoutput
In place of <ip>, I gave ip address of the device that act as server.
Help me with this so that I could resume my proceeding. Also If anyone is building home cockpit fg try sharing your experience too.
Kalai
 
Posts: 6
Joined: Mon Nov 27, 2017 7:29 pm

Re: Interfacing flightgear with UDP/TCP socket.

Postby Kalai » Sun Dec 10, 2017 3:50 pm

I have upgraded my work further by integrating arduino-- to--python socket-- to ---flightgear. All hardware input are recognized by the arduino and those inputs are sent to PC using serial connection. Python script in pc reads the serial data sent by arduino then send those data to flightgear via TCP socket connection. I tested a sample and it works!!!
Kalai
 
Posts: 6
Joined: Mon Nov 27, 2017 7:29 pm

Re: Interfacing flightgear with UDP/TCP socket.

Postby Hooray » Sun Dec 10, 2017 6:42 pm

this is the kind of stuff that would be best added the "hardware" portal in the wiki - so that other people can find find it and learn from it.
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: Interfacing flightgear with UDP/TCP socket.

Postby Kalai » Sun Dec 10, 2017 7:11 pm

Will post it in wiki very soon and will update the link here.
Kalai
 
Posts: 6
Joined: Mon Nov 27, 2017 7:29 pm


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 1 guest