From e252bcf0f31c4558b810e9440da8db87787c887c Mon Sep 17 00:00:00 2001 From: ehofman Date: Tue, 27 Apr 2004 08:55:04 +0000 Subject: [PATCH] Add an example implementation for the NetFDM structure. --- examples/netfdm/README | 26 +++++++ examples/netfdm/main.cpp | 143 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 examples/netfdm/README create mode 100644 examples/netfdm/main.cpp diff --git a/examples/netfdm/README b/examples/netfdm/README new file mode 100644 index 000000000..cce746dd1 --- /dev/null +++ b/examples/netfdm/README @@ -0,0 +1,26 @@ +David Calkins writes: + +I've attached my sample code which works with FlightGear v0.9.3. Perhaps this +will be of some help to others. I'm running FG with the launcher wizard, which +uses the below command line options. The sample code I attached just rolls +back and forth ± 5 degrees so it isn't that interesting, but it works. + +C:\Program Files\FlightGear-0.9.3\bin\Win32\fgfs.exe + --fg-root=C:\Program Files\FlightGear-0.9.3\data + --fg-scenery=C:\Program Files\FlightGear-0.9.3\data\Scenery + --aircraft=c172 + --control=joystick + --disable-random-objects + --fdm=external + --vc=0 + --bpp=32 + --timeofday=noon + --native-fdm=socket,in,1,,5500,udp + +One point of interest is the cur_time field in the FGNetFDM structure. +I noticed that it didn't seem to matter what time I passed in. In looking at +the source, it appears this field is ignored completely. So, it looks like the +time of day would need to be determined by the command line parameters used to +launch FlightGear. + +Dave diff --git a/examples/netfdm/main.cpp b/examples/netfdm/main.cpp new file mode 100644 index 000000000..b905263b6 --- /dev/null +++ b/examples/netfdm/main.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +using namespace std; + +#include + +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; +} + +SOCKET sendSocket = -1; +struct sockaddr_in sendAddr; + +// IP and port where FG is listening +char * fg_ip = "127.0.0.1"; +int fg_port = 5500; + +// update period. controls how often updates are +// sent to FG. in seconds. +int update_period = 1000; + +void run(); + +int main(int argc, char ** argv) +{ + WSAData wd; + if (WSAStartup(MAKEWORD(2,0),&wd) == 0) + { + memset(&sendAddr,0,sizeof(sendAddr)); + sendAddr.sin_family = AF_INET; + sendAddr.sin_port = htons(fg_port); + sendAddr.sin_addr.S_un.S_addr = inet_addr(fg_ip); + + sendSocket = socket(AF_INET,SOCK_DGRAM,0); + if (sendSocket != INVALID_SOCKET) + { + run(); + } + else + { + cout << "socket() failed" << endl; + } + } + else + { + cout << "WSAStartup() failed" << endl; + } + + return 0; +} + +#define D2R (3.14159 / 180.0) + +void run() +{ + double latitude = 45.59823; // degs + double longitude = -120.69202; // degs + double altitude = 150.0; // meters above sea level + + float roll = 0.0; // degs + float pitch = 0.0; // degs + float yaw = 0.0; // degs + + float visibility = 5000.0; // meters + + while (true) + { + Sleep(update_period); + + FGNetFDM 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 *)&sendAddr,sizeof(sendAddr)); + + static bool flag = true; + if (flag) + { + roll += 5.0; + } + else + { + roll -= 5.0; + } + flag = !flag; + } +} + + + + + + + + + + + + + + + + + + + + + + -- 2.39.5