Board index FlightGear Support Interfacing

How to connect to FG using interface UDP in C #

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

How to connect to FG using interface UDP in C #

Postby fgjw » Mon May 09, 2022 8:54 pm

Hi,
For some time I am trying to control the plane using the UDP interface in C#.

To do this, I run FlighGear with:

cd C:\Program Files\FlightGear 2020.3
SET FG_ROOT=C:\Program Files\FlightGear 2020.3\data
.\\bin\fgfs --aircraft=c172p --fdm=null --native-fdm=socket,in,30,127.0.0.1,5501,udp --native-ctrls=socket,out,30,127.0.0.1,5502,udp --fog-fastest --disable-clouds --start-date-lat=2022:05:09:09:00:00 --disable-sound --in-air --enable-freeze --airport=EPWA --runway=11 --altitude=00.0 --heading=0 --offset-distance=0 --offset-azimuth=0

In C #, I build a sample datagram and send it:


private int portHost = 5502;
private int portClient = 5501;

private string ipClient = "127.0.0.1";

byte[] buffer = new byte[409];

//version
buffer[0] = 0x00;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 24;
//padding
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
//longitude
buffer[8] = 63;
buffer[9] = 215;
buffer[10] = 101;
buffer[11] = 206;
buffer[12] = 233;
buffer[13] = 115;
buffer[14] = 170;
buffer[15] = 144;
//latitude
buffer[16] = 63;
buffer[17] = 237;
buffer[18] = 35;
buffer[19] = 84;
buffer[20] = 102;
buffer[21] = 17;
buffer[22] = 94;
buffer[23] = 112;
//altitude
buffer[24] = 64;
buffer[25] = 90;
buffer[26] = 112;
buffer[27] = 0;
buffer[28] = 0;
buffer[29] = 0;
buffer[30] = 0;
buffer[31] = 0;
//agl
buffer[32] = 0;
buffer[33] = 0;
buffer[34] = 0;
buffer[35] = 0;
//phi
buffer[36] = 0;
buffer[37] = 0;
buffer[38] = 0;
buffer[39] = 0;
//theta
buffer[40] = 0;
buffer[41] = 0;
buffer[42] = 0;
buffer[43] = 0;
//psi
buffer[44] = 0;
buffer[45] = 0;
buffer[46] = 0;
buffer[47] = 0;
//alpha
buffer[48] = 0;
buffer[49] = 0;
buffer[50] = 0;
buffer[51] = 0;
//beta
buffer[52] = 0;
buffer[53] = 0;
buffer[54] = 0;
buffer[55] = 0;

buffer[56] = 0;
buffer[57] = 0;
buffer[58] = 0;
buffer[59] = 0;

buffer[60] = 0;
buffer[61] = 0;
buffer[62] = 0;
buffer[63] = 0;

buffer[64] = 0;
buffer[65] = 0;
buffer[66] = 0;
buffer[67] = 0;

buffer[68] = 0;
buffer[69] = 0;
buffer[70] = 0;
buffer[71] = 0;

buffer[72] = 0;
buffer[73] = 0;
buffer[74] = 0;
buffer[75] = 0;
buffer[76] = 0;
buffer[77] = 0;
buffer[78] = 0;
buffer[79] = 0;

buffer[80] = 0;
buffer[81] = 0;
buffer[82] = 0;
buffer[83] = 0;
buffer[84] = 0;
buffer[85] = 0;
buffer[86] = 0;
buffer[87] = 0;
buffer[88] = 0;
buffer[89] = 0;

buffer[90] = 0;
buffer[91] = 0;
buffer[92] = 0;
buffer[93] = 0;
buffer[94] = 0;
buffer[95] = 0;
buffer[96] = 0;
buffer[97] = 0;
buffer[98] = 0;
buffer[99] = 0;


for (int n = 0; n <= 308; n++) buffer[100 + n] = 0;


UdPClient1.Send(buffer, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipClient), portClient));


The problem is that I don't see the effect. I get some data from FG.
Anyone where there may be a problem that I cannot change the location of the plane?



.
fgjw
 
Posts: 11
Joined: Mon May 09, 2022 8:10 pm

Re: How to connect to FG using interface UDP in C #

Postby erik » Tue May 10, 2022 6:32 am

Maybe it's this command line option which freezes flightgear:
--enable-freeze Start in a frozen state

You will have to turn of the freeze state afterwards to get any action from FlightGear.

Erik
Current: Parachutist, Paraglider, Pterosaur, Pilatus PC-9M and variants, ERCO Ercoupe, Fokker Dr.1, Fokker 50, Fokker 100
Less active: Cessna T-37, T-38, Santa Claus. Previous: General Dynamics F-16. Worked on: Wright Flyer
erik
 
Posts: 2248
Joined: Thu Nov 01, 2007 2:41 pm

Re: How to connect to FG using interface UDP in C #

Postby fgjw » Wed Sep 07, 2022 5:32 am

I would like to continue the topic because I have achieved positive results, but at this stage I have a problem with sending more parameters to FG. Communication works but only with setting two parameters. As I add the third is the plane jumps. The following sources cause that substituting data for pitch and altitude the plane is super moving. If I change the roll it breaks. Does anyone know what could be causing the problem?

MainWindows.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading; //DispatcherTimer
using System.Diagnostics;

using System.Runtime.InteropServices; //DllImport
using System.Threading;

using System.Net.Sockets;
using System.Net;

namespace FGCom
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
bool bfindFGFS = false;

DispatcherTimer dispatcherTimerPLAY = new DispatcherTimer();

int iCount = 0;

UdpClient client = new UdpClient();

public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimerPLAY.Tick += new EventHandler(dispatcherTimerPLAY_Tick);
dispatcherTimerPLAY.Interval = new TimeSpan(0, 0, 0, 0, 100);

IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 49000);
client.Connect(ep);
}

float roll_deg = 0;

private void dispatcherTimerPLAY_Tick(object sender, EventArgs e)
{
iCount++;
lbl_count.Content = iCount.ToString();

// // Send the output to FlightGear
// //String sendString = X.altitude_ft + "," + X.latitude_deg + "," + X.longitude_deg + "," + X.roll_deg + "," + X.pitch_deg + "," + X.yaw_deg + "\n";

//String sendString = "100,37.613544,-122.357246," +roll_deg.ToString()+"0,0\n"; //; + 37.613544 + "," + -122.357246 + "," + 0.5f + "," + 0 + "," + 0 + "\n";

//String sendString = sld_alt.Value.ToString()+","+sld_roll.Value.ToString()+"\n"; //; + 37.613544 + "," + -122.357246 + "," + 0.5f + "," + 0 + "," + 0 + "\n";

String sendString = iCount.ToString()+ "," + sld_roll.Value.ToString() + ","+ sld_pitch.Value.ToString() +"\n";

Byte[] sendBytes = Encoding.ASCII.GetBytes(sendString);

client.Send(sendBytes, sendBytes.Length);
}

private void btn_start_Click(object sender, RoutedEventArgs e)
{
dispatcherTimerPLAY.Start();
}
}
}

MainWindows.xaml:
<Window x:Name="Window_FGCom" x:Class="FGCom.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FGCom"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="91*"/>
<RowDefinition Height="328*"/>
</Grid.RowDefinitions>
<Button x:Name="btn_start" Content="START" HorizontalAlignment="Left" Margin="71,88,0,0" VerticalAlignment="Top" Width="75" Click="btn_start_Click" Grid.RowSpan="2"/>
<Label x:Name="lbl_count" Content="0" HorizontalAlignment="Left" Margin="190,88,0,0" VerticalAlignment="Top" Grid.RowSpan="2"/>
<Slider x:Name="sld_roll" HorizontalAlignment="Left" Margin="79,240,0,0" VerticalAlignment="Top" Width="666" Maximum="360" Grid.Row="1"/>
<Label Content="Roll:" HorizontalAlignment="Left" Margin="20,236,0,0" VerticalAlignment="Top" Grid.Row="1"/>
<Slider x:Name="sld_alt" HorizontalAlignment="Left" Margin="79,199,0,0" VerticalAlignment="Top" Width="666" Maximum="100" Grid.Row="1"/>
<Label Content="Altitude:" HorizontalAlignment="Left" Margin="20,195,0,0" VerticalAlignment="Top" Grid.Row="1"/>
<Slider x:Name="sld_pitch" HorizontalAlignment="Left" Margin="79,274,0,0" VerticalAlignment="Top" Width="666" Maximum="360" Grid.Row="1"/>
<Label Content="Pitch:" HorizontalAlignment="Left" Margin="20,270,0,0" VerticalAlignment="Top" Grid.Row="1"/>
</Grid>
</Window>

fg.bat:
cd "C:\\Program Files\\FlightGear 2020.3\\bin"
fgfs.exe --fdm=null --generic=socket,out,10,127.0.0.1,49001,udp,outputprotocol ^
--generic=socket,in,10,127.0.0.1,49000,udp,inputprotocol1 ^
--aircraft=c172p ^
--enable-hud ^
--units-meters ^
--altitude=stopy ^
--airport=EPWA
pause

inpuprotocol1.xml:
<?xml version="1.0" encoding="utf-8" ?>
<PropertyList>
<generic>
<input>
<line_separator>newline</line_separator>
<var_separator>,</var_separator>

<!-- Position -->
<chunk>
<name>altitude-ft</name>
<type>float</type>
<node>/position/altitude-ft</node>
<format>%f</format>
</chunk>

<!-- Orientation -->
<chunk>
<name>roll-deg</name>
<type>float</type>
<node>/orientation/roll-deg</node>
<format>%f</format>
</chunk>

<chunk>
<name>pitch-deg</name>
<type>float</type>
<node>/orientation/pitch-deg</node>
<format>%f</format>
</chunk>

</input>
</generic>
</PropertyList>
fgjw
 
Posts: 11
Joined: Mon May 09, 2022 8:10 pm

Re: How to connect to FG using interface UDP in C #

Postby wkitty42 » Wed Sep 07, 2022 10:37 am

please put code in code tags as i've done in this quote...it really helps for readability and code alignment... also, you left out outputprotocol.xml

fgjw wrote in Wed Sep 07, 2022 5:32 am:MainWindows.xaml.cs:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading; //DispatcherTimer
using System.Diagnostics;

using System.Runtime.InteropServices; //DllImport
using System.Threading;

using System.Net.Sockets;
using System.Net;

namespace FGCom
{
    /// <summary>
    /// Logika interakcji dla klasy MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool bfindFGFS = false;

        DispatcherTimer dispatcherTimerPLAY = new DispatcherTimer();

        int iCount = 0;

        UdpClient client = new UdpClient();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dispatcherTimerPLAY.Tick += new EventHandler(dispatcherTimerPLAY_Tick);
            dispatcherTimerPLAY.Interval = new TimeSpan(0, 0, 0, 0, 100);

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 49000);
            client.Connect(ep);
        }

        float roll_deg = 0;

        private void dispatcherTimerPLAY_Tick(object sender, EventArgs e)
        {
            iCount++;
            lbl_count.Content = iCount.ToString();

            // // Send the output to FlightGear
            // //String sendString = X.altitude_ft + "," + X.latitude_deg + "," + X.longitude_deg + "," + X.roll_deg + "," + X.pitch_deg + "," + X.yaw_deg + "\n";

            //String sendString = "100,37.613544,-122.357246," +roll_deg.ToString()+"0,0\n";   //; + 37.613544 + "," + -122.357246 + "," + 0.5f + "," + 0 + "," + 0 + "\n";

            //String sendString = sld_alt.Value.ToString()+","+sld_roll.Value.ToString()+"\n";   //; + 37.613544 + "," + -122.357246 + "," + 0.5f + "," + 0 + "," + 0 + "\n";

            String sendString = iCount.ToString()+ "," + sld_roll.Value.ToString() + ","+ sld_pitch.Value.ToString() +"\n";

            Byte[] sendBytes = Encoding.ASCII.GetBytes(sendString);
                                         
            client.Send(sendBytes, sendBytes.Length);
        }

        private void btn_start_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimerPLAY.Start();
        }
    }
}


MainWindows.xaml:
Code: Select all
<Window x:Name="Window_FGCom" x:Class="FGCom.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FGCom"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="91*"/>
            <RowDefinition Height="328*"/>
        </Grid.RowDefinitions>
        <Button x:Name="btn_start" Content="START" HorizontalAlignment="Left" Margin="71,88,0,0" VerticalAlignment="Top" Width="75" Click="btn_start_Click" Grid.RowSpan="2"/>
        <Label x:Name="lbl_count" Content="0" HorizontalAlignment="Left" Margin="190,88,0,0" VerticalAlignment="Top" Grid.RowSpan="2"/>
        <Slider x:Name="sld_roll" HorizontalAlignment="Left" Margin="79,240,0,0" VerticalAlignment="Top" Width="666" Maximum="360" Grid.Row="1"/>
        <Label Content="Roll:" HorizontalAlignment="Left" Margin="20,236,0,0" VerticalAlignment="Top" Grid.Row="1"/>
        <Slider x:Name="sld_alt" HorizontalAlignment="Left" Margin="79,199,0,0" VerticalAlignment="Top" Width="666" Maximum="100" Grid.Row="1"/>
        <Label Content="Altitude:" HorizontalAlignment="Left" Margin="20,195,0,0" VerticalAlignment="Top" Grid.Row="1"/>
        <Slider x:Name="sld_pitch" HorizontalAlignment="Left" Margin="79,274,0,0" VerticalAlignment="Top" Width="666" Maximum="360" Grid.Row="1"/>
        <Label Content="Pitch:" HorizontalAlignment="Left" Margin="20,270,0,0" VerticalAlignment="Top" Grid.Row="1"/>
    </Grid>
</Window>


fg.bat:
Code: Select all
cd "C:\\Program Files\\FlightGear 2020.3\\bin"
fgfs.exe --fdm=null --generic=socket,out,10,127.0.0.1,49001,udp,outputprotocol ^
 --generic=socket,in,10,127.0.0.1,49000,udp,inputprotocol1 ^
 --aircraft=c172p ^
 --enable-hud ^
 --units-meters ^
 --altitude=stopy ^
 --airport=EPWA
pause


inpuprotocol1.xml:
Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<PropertyList>
  <generic>
    <input>
      <line_separator>newline</line_separator>
      <var_separator>,</var_separator>

      <!-- Position -->
      <chunk>
        <name>altitude-ft</name>
        <type>float</type>
        <node>/position/altitude-ft</node>
        <format>%f</format>
      </chunk>

      <!-- Orientation -->
      <chunk>
        <name>roll-deg</name>
        <type>float</type>
        <node>/orientation/roll-deg</node>
        <format>%f</format>
      </chunk>

      <chunk>
        <name>pitch-deg</name>
        <type>float</type>
        <node>/orientation/pitch-deg</node>
        <format>%f</format>
      </chunk>
 
    </input>
  </generic>
</PropertyList>
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 9162
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 22.04

Re: How to connect to FG using interface UDP in C #

Postby fgjw » Sun Sep 11, 2022 1:11 pm

wkitty42 thank you for your advice. Below I give source: outputprotocol.xml
Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<PropertyList>
  <generic>
    <output>
      <line_separator>newline</line_separator>
      <var_separator>,</var_separator>

      <chunk>
        <name>throttle</name>
        <type>float</type>
        <node>/controls/engines/engine/throttle</node>
        <format>%f</format>
      </chunk>

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

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

      <chunk>
        <name>rudder</name>
        <type>float</type>
        <node>/controls/flight/rudder</node>
        <format>%f</format>
      </chunk>
    </output>
  </generic>
</PropertyList>


wkitty42 do you know how to solve my above problem?
fgjw
 
Posts: 11
Joined: Mon May 09, 2022 8:10 pm

Re: How to connect to FG using interface UDP in C #

Postby wkitty42 » Sun Sep 11, 2022 4:14 pm

fgjw wrote in Sun Sep 11, 2022 1:11 pm:wkitty42 thank you for your advice.

you are welcome ;)

fgjw wrote in Sun Sep 11, 2022 1:11 pm:wkitty42 do you know how to solve my above problem?

i do not, sorry...
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 9162
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 22.04


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 4 guests