I am currently using FGFS 0.9.10 and trying to interface flightgear with
external fdm data from a program in C.I tested the interfacing using a
sample program.I found that Flightgear responds to the data generated by
external fdm program, however not smoothly i.e. there is no smooth movement
of aircraft.
When I send the packets every 1 ms, the log-level entry in the command
windows shows"Sucess reading data".However the FGFS graphical screen freezes
and the aircraft doesn't move.But when I disable the internet connection
temporarily,it responds momentarily.It means that FGFS is responding to
packets.
I am using this command to invoke flightgear:
cd C:\FlightGear\bin\Win32\
FGFS.EXE --native-fdm=socket,in,30,,5500,udp --fdm=external
--log-level=debug --timeofday=noon --disable-random-objects.

If someone has any related information of sucessful interface of external
fdm with Flightgear then please help......

The sample program which I am using is:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include <math.h>
#include <string.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

const uint32_t FG_NET_FDM_VERSION = 24;
int fgfs_port=5500;
char* fgfs_ip="10.101.13.41";
#define D2R (3.14159 / 180.0)
#define FG_MAX_ENGINES 4
#define FG_MAX_WHEELS 3
#define FG_MAX_TANKS 4
int update_period=1;
int sendSocket;
void run();
struct sockaddr_in serv_addr;
typedef struct {

/* enum {
       FG_MAX_ENGINES = 4,
       FG_MAX_WHEELS = 3,
       FG_MAX_TANKS = 4
   };*/

   uint32_t version;        // increment when data values change
   uint32_t padding;        // padding

   // Positions
   double longitude;        // geodetic (radians)
   double latitude;        // geodetic (radians)
   double altitude;        // above sea level (meters)
   float agl;            // above ground level (meters)
   float phi;            // roll (radians)
   float theta;        // pitch (radians)
   float psi;            // yaw or true heading (radians)
   float alpha;                // angle of attack (radians)
   float beta;                 // side slip angle (radians)

   // Velocities
   float phidot;        // roll rate (radians/sec)
   float thetadot;        // pitch rate (radians/sec)
   float psidot;        // yaw rate (radians/sec)
   float vcas;                // calibrated airspeed
   float climb_rate;        // feet per second
   float v_north;              // north velocity in local/body frame, fps
   float v_east;               // east velocity in local/body frame, fps
   float v_down;               // down/vertical velocity in local/body
frame, fps
   float v_wind_body_north;    // north velocity in local/body frame
                               // relative to local airmass, fps
   float v_wind_body_east;     // east velocity in local/body frame
                               // relative to local airmass, fps
   float v_wind_body_down;     // down/vertical velocity in local/body
                               // frame relative to local airmass, fps

   // Accelerations
   float A_X_pilot;        // X accel in body frame ft/sec^2
   float A_Y_pilot;        // Y accel in body frame ft/sec^2
   float A_Z_pilot;        // Z accel in body frame ft/sec^2

   // Stall
   float stall_warning;        // 0.0 - 1.0 indicating the amount of stall
   float slip_deg;        // slip ball deflection

   // Pressure

   // Engine status
   uint32_t num_engines;         // Number of valid engines
   uint32_t eng_state[FG_MAX_ENGINES];// Engine state (off, cranking,
running)
   float rpm[FG_MAX_ENGINES];         // Engine RPM rev/min
   float fuel_flow[FG_MAX_ENGINES]; // Fuel flow gallons/hr
   float fuel_px[FG_MAX_ENGINES];   // Fuel pressure psi
   float egt[FG_MAX_ENGINES];         // Exhuast gas temp deg F
   float cht[FG_MAX_ENGINES];         // Cylinder head temp deg F
   float mp_osi[FG_MAX_ENGINES];    // Manifold pressure
   float tit[FG_MAX_ENGINES];         // Turbine Inlet Temperature
   float oil_temp[FG_MAX_ENGINES];  // Oil temp deg F
   float oil_px[FG_MAX_ENGINES];    // Oil pressure psi

   // Consumables
   uint32_t num_tanks;        // Max number of fuel tanks
   float fuel_quantity[FG_MAX_TANKS];

   // Gear status
   uint32_t num_wheels;
   uint32_t wow[FG_MAX_WHEELS];
   float gear_pos[FG_MAX_WHEELS];
   float gear_steer[FG_MAX_WHEELS];
   float gear_compression[FG_MAX_WHEELS];

   // Environment
   uint32_t cur_time;           // current unix time
                                // FIXME: make this uint64_t before 2038
   int32_t warp;                // offset in seconds to unix time
   float visibility;            // visibility in meters (for env. effects)

   // Control surface positions (normalized values)
   float elevator;
   float elevator_trim_tab;
   float left_flap;
   float right_flap;
   float left_aileron;
   float right_aileron;
   float rudder;
   float nose_wheel;
   float speedbrake;
   float spoilers;

}  fdm_data;

int net_init(void)
{
   int     s;
   struct     sockaddr_in cli_addr;

   s = socket(AF_INET, SOCK_DGRAM, 0);

   bzero((char *)&cli_addr, sizeof(cli_addr));
   cli_addr.sin_family = AF_INET;
   cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
   cli_addr.sin_port = htons(INADDR_ANY);

   bind(s, (struct sockaddr *)&cli_addr, sizeof(cli_addr));

   return s;
}

double htond (double x)
{
   int * p = (int*)&x;
   int tmp = p[0];
   p[0] = htonl(p[1]);
   p[1] = htonl(tmp);

   return x;
}

float htonf (float x)
{
   int * p = (int *)&x;
   *p = htonl(*p);
   return x;
}


int main(void)
{
 int s;
 s=net_init();
 sendSocket=s;
// printf("Socket no=%d",s);
//  struct sockaddr_in serv_addr;
 bzero((char *)&serv_addr, sizeof(serv_addr));
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = inet_addr(fgfs_ip);
 serv_addr.sin_port = htons(fgfs_port);
 if (s!= -1)
 { run();}
 else
 printf("\nSocket creation failed");
return 0;
}

void run()
{
   double latitude = 0.59823; // degs
   double longitude = 0.69202; // degs
   double altitude = 300.0; // meters above sea level
   double lat,lon;
   float roll = 0.0; // degs
   float pitch = 0.0; // degs
   float yaw = 0.0; // degs

   float visibility = 5000.0; // meters
   static int flag = 1;
   fdm_data fdm;
   while (1)
   {
      sleep(update_period);
      // fdm_data fdm;
       memset(&fdm,0,sizeof(fdm));
       fdm.version = htonl(FG_NET_FDM_VERSION);
      // fdm.latitude = htond(latitude * D2R);
      // fdm.longitude = htond(longitude * D2R);
       fdm.altitude = htond(altitude);
       fdm.phi = htonf(roll * D2R);
       fdm.theta = htonf(pitch * D2R);
       fdm.psi = htonf(yaw * D2R);

       fdm.num_engines = htonl(1);

       fdm.num_tanks = htonl(1);
       fdm.fuel_quantity[0] = htonf(100.0);

       fdm.num_wheels = htonl(3);

       fdm.cur_time = htonl(time(0));
       fdm.warp = htonl(1);

       fdm.visibility = htonf(visibility);

      // sendto(sendSocket,(char *)&fdm,sizeof(fdm),0,(struct sockaddr
*)&serv_addr,sizeof(serv_addr));

       //static bool flag = true;
      /* if (flag)
       {
           roll += 5.0;
       }
       else
       {
           roll -= 5.0;
       }
       flag = !flag;*/
       latitude=latitude+0.0000001;
       longitude=longitude+0.0000001;
       if(latitude<90)
       fdm.latitude=htond(latitude*D2R);

       if(longitude<180)
       fdm.longitude=htond(longitude*D2R);

       if (latitude>=90)
       {
        lat=180-latitude;
        fdm.latitude=htond(lat*D2R);
   }
   if (longitude>=180)
       {
        lon=360-longitude;
        fdm.longitude=htond(lon*D2R);
       }
      if(latitude>=180)
      { latitude=0;
        fdm.latitude=htond(0.00);
      }
      if(longitude>=360)
      {
       longitude=0;
       fdm.longitude=htond(0.00);
     }
sendto(sendSocket,(char *)&fdm,sizeof(fdm),0,(struct sockaddr
*)&serv_addr,sizeof(serv_addr));
   }
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to