11 #include <simgear/io/lowlevel.hxx> // endian tests
12 #include <simgear/timing/timestamp.hxx>
14 #include <Network/net_ctrls.hxx>
15 #include <Network/net_fdm.hxx>
26 static netSocket fdm_sock, ctrls_sock;
32 static int fdm_port = 5505;
33 static int ctrls_port = 5506;
36 static string file = "";
38 // Master time counter
39 float sim_time = 0.0f;
42 SGTimeStamp last_time_stamp;
43 SGTimeStamp current_time_stamp;
48 // The function htond is defined this way due to the way some
49 // processors and OSes treat floating point values. Some will raise
50 // an exception whenever a "bad" floating point value is loaded into a
51 // floating point register. Solaris is notorious for this, but then
52 // so is LynxOS on the PowerPC. By translating the data in place,
53 // there is no need to load a FP register with the "corruped" floating
54 // point value. By doing the BIG_ENDIAN test, I can optimize the
55 // routine for big-endian processors so it can be as efficient as
57 static void htond (double &x)
59 if ( sgIsLittleEndian() ) {
63 Double_Overlay = (int *) &x;
64 Holding_Buffer = Double_Overlay [0];
66 Double_Overlay [0] = htonl (Double_Overlay [1]);
67 Double_Overlay [1] = htonl (Holding_Buffer);
74 static void htonf (float &x)
76 if ( sgIsLittleEndian() ) {
80 Float_Overlay = (int *) &x;
81 Holding_Buffer = Float_Overlay [0];
83 Float_Overlay [0] = htonl (Holding_Buffer);
90 static void gps2fg( const GPSPoint p, FGNetFDM *fdm, FGNetCtrls *ctrls )
94 static double last_psi;
95 static double last_alt;
96 static double phi_filter = 0.0;
97 static double theta_filter = 0.0;
100 if ( phi_filter != phi_filter ) {
103 if ( theta_filter != theta_filter ) {
107 // Version sanity checking
108 fdm->version = FG_NET_FDM_VERSION;
111 fdm->longitude = p.lon_deg * SGD_DEGREES_TO_RADIANS;
112 fdm->latitude = p.lat_deg * SGD_DEGREES_TO_RADIANS;
113 fdm->altitude = p.altitude_msl;
115 fdm->psi = p.course_true; // heading
117 double diff = p.course_true - last_psi;
118 if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
119 if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
120 double phi = diff * 100.0;
121 if ( phi > 0.5*SGD_PI ) { phi = 0.5*SGD_PI; }
122 if ( phi < -0.5*SGD_PI ) { phi = -0.5*SGD_PI; }
123 phi_filter = 0.99*phi_filter + 0.01*phi;
124 fdm->phi = phi_filter;
125 last_psi = p.course_true;
126 // cout << p.course_true << endl;
128 diff = p.altitude_msl - last_alt;
129 if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
130 if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
132 if ( theta > 0.5*SGD_PI ) { theta = 0.5*SGD_PI; }
133 if ( theta < -0.5*SGD_PI ) { theta = -0.5*SGD_PI; }
134 theta_filter = 0.99*theta_filter + 0.01*theta;
135 fdm->theta = theta_filter;
136 last_alt = p.altitude_msl;
141 fdm->vcas = p.speed_kts;
142 fdm->climb_rate = 0; // fps
143 // cout << "climb rate = " << aero->hdota << endl;
147 fdm->v_wind_body_north = 0.0;
148 fdm->v_wind_body_east = 0.0;
149 fdm->v_wind_body_down = 0.0;
150 fdm->stall_warning = 0.0;
152 fdm->A_X_pilot = 0.0;
153 fdm->A_Y_pilot = 0.0;
154 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
157 fdm->num_engines = 1;
158 fdm->eng_state[0] = 2;
159 // cout << "state = " << fdm->eng_state[0] << endl;
160 double rpm = ((p.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
161 if ( rpm < 0.0 ) { rpm = 0.0; }
162 if ( rpm > 3000.0 ) { rpm = 3000.0; }
165 fdm->fuel_flow[0] = 0.0;
167 // cout << "egt = " << aero->EGT << endl;
168 fdm->oil_temp[0] = 0.0;
169 fdm->oil_px[0] = 0.0;
173 fdm->fuel_quantity[0] = 0.0;
174 fdm->fuel_quantity[1] = 0.0;
182 // the following really aren't used in this context
187 // cout << "Flap deflection = " << aero->dflap << endl;
188 fdm->left_flap = 0.0;
189 fdm->right_flap = 0.0;
191 // Convert the net buffer to network format
192 fdm->version = htonl(fdm->version);
194 htond(fdm->longitude);
195 htond(fdm->latitude);
196 htond(fdm->altitude);
205 htonf(fdm->thetadot);
208 htonf(fdm->climb_rate);
212 htonf(fdm->v_wind_body_north);
213 htonf(fdm->v_wind_body_east);
214 htonf(fdm->v_wind_body_down);
216 htonf(fdm->A_X_pilot);
217 htonf(fdm->A_Y_pilot);
218 htonf(fdm->A_Z_pilot);
220 htonf(fdm->stall_warning);
221 htonf(fdm->slip_deg);
223 for ( i = 0; i < fdm->num_engines; ++i ) {
224 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
226 htonf(fdm->fuel_flow[i]);
229 htonf(fdm->mp_osi[i]);
231 htonf(fdm->oil_temp[i]);
232 htonf(fdm->oil_px[i]);
234 fdm->num_engines = htonl(fdm->num_engines);
236 for ( i = 0; i < fdm->num_tanks; ++i ) {
237 htonf(fdm->fuel_quantity[i]);
239 fdm->num_tanks = htonl(fdm->num_tanks);
241 for ( i = 0; i < fdm->num_wheels; ++i ) {
242 fdm->wow[i] = htonl(fdm->wow[i]);
243 htonf(fdm->gear_pos[i]);
244 htonf(fdm->gear_steer[i]);
245 htonf(fdm->gear_compression[i]);
247 fdm->num_wheels = htonl(fdm->num_wheels);
249 fdm->cur_time = htonl( fdm->cur_time );
250 fdm->warp = htonl( fdm->warp );
251 htonf(fdm->visibility);
253 htonf(fdm->elevator);
254 htonf(fdm->elevator_trim_tab);
255 htonf(fdm->left_flap);
256 htonf(fdm->right_flap);
257 htonf(fdm->left_aileron);
258 htonf(fdm->right_aileron);
260 htonf(fdm->nose_wheel);
261 htonf(fdm->speedbrake);
262 htonf(fdm->spoilers);
266 static void send_data( const GPSPoint p ) {
268 int ctrlsize = sizeof( FGNetCtrls );
269 int fdmsize = sizeof( FGNetFDM );
271 // cout << "Running main loop" << endl;
276 gps2fg( p, &fgfdm, &fgctrls );
277 len = fdm_sock.send(&fgfdm, fdmsize, 0);
281 void usage( const string &argv0 ) {
282 cout << "Usage: " << argv0 << endl;
283 cout << "\t[ --help ]" << endl;
284 cout << "\t[ --file <file_name>" << endl;
285 cout << "\t[ --hertz <hertz> ]" << endl;
286 cout << "\t[ --host <hostname> ]" << endl;
287 cout << "\t[ --broadcast ]" << endl;
288 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
289 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
293 int main( int argc, char **argv ) {
295 string out_host = "localhost";
296 bool do_broadcast = false;
298 // process command line arguments
299 for ( int i = 1; i < argc; ++i ) {
300 if ( strcmp( argv[i], "--help" ) == 0 ) {
303 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
306 hertz = atof( argv[i] );
311 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
319 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
327 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
329 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
332 fdm_port = atoi( argv[i] );
337 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
340 ctrls_port = atoi( argv[i] );
351 // Load the track data
353 cout << "No track file specified" << endl;
357 cout << "Loaded " << track.size() << " records." << endl;
359 // Setup up outgoing network connections
361 netInit( &argc,argv ); // We must call this before any other net stuff
363 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
364 cout << "error opening fdm output socket" << endl;
367 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
368 cout << "error opening ctrls output socket" << endl;
371 cout << "open net channels" << endl;
373 fdm_sock.setBlocking( false );
374 ctrls_sock.setBlocking( false );
375 cout << "blocking false" << endl;
377 if ( do_broadcast ) {
378 fdm_sock.setBroadcast( true );
379 ctrls_sock.setBroadcast( true );
382 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
384 cout << "error connecting to outgoing fdm port: " << out_host
385 << ":" << fdm_port << endl;
388 cout << "connected outgoing fdm socket" << endl;
390 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
392 cout << "error connecting to outgoing ctrls port: " << out_host
393 << ":" << ctrls_port << endl;
396 cout << "connected outgoing ctrls socket" << endl;
398 int size = track.size();
400 double current_time = track.get_point(0).get_time();
401 cout << "Track begin time is " << current_time << endl;
402 double end_time = track.get_point(size-1).get_time();
403 cout << "Track end time is " << end_time << endl;
404 cout << "Duration = " << end_time - current_time << endl;
406 double frame_us = 1000000.0 / hertz;
407 if ( frame_us < 0.0 ) {
411 SGTimeStamp start_time;
416 p0 = p1 = track.get_point( 0 );
418 while ( current_time < end_time ) {
419 // cout << "current_time = " << current_time << " end_time = "
420 // << end_time << endl;
422 if ( current_time > p1.get_time() ) {
425 // cout << "count = " << count << endl;
426 p1 = track.get_point( count );
428 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
432 (current_time - p0.get_time()) /
433 (p1.get_time() - p0.get_time());
434 // cout << "Percent = " << percent << endl;
436 GPSPoint p = GPSInterpolate( p0, p1, percent );
437 cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
441 // Update the elapsed time.
442 static bool first_time = true;
444 last_time_stamp.stamp();
448 current_time_stamp.stamp();
450 double elapsed_us = current_time_stamp - last_time_stamp;
451 if ( elapsed_us < (frame_us - 2000) ) {
452 double requested_us = (frame_us - elapsed_us) - 2000 ;
453 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
455 current_time_stamp.stamp();
456 while ( current_time_stamp - last_time_stamp < frame_us ) {
457 current_time_stamp.stamp();
460 current_time += (frame_us / 1000000.0);
461 last_time_stamp = current_time_stamp;
464 cout << "Processed " << count << " entries in "
465 << current_time_stamp - start_time << " seconds." << endl;