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.