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; }
131 double theta = diff * 2.0;
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 fdm->elevator = -theta_filter * 5.0;
192 fdm->elevator_trim_tab = 0.0;
193 fdm->left_flap = 0.0;
194 fdm->right_flap = 0.0;
195 fdm->left_aileron = phi_filter * 1.5;
196 fdm->right_aileron = phi_filter * 1.5;
198 fdm->nose_wheel = 0.0;
199 fdm->speedbrake = 0.0;
202 // Convert the net buffer to network format
203 fdm->version = htonl(fdm->version);
205 htond(fdm->longitude);
206 htond(fdm->latitude);
207 htond(fdm->altitude);
216 htonf(fdm->thetadot);
219 htonf(fdm->climb_rate);
223 htonf(fdm->v_wind_body_north);
224 htonf(fdm->v_wind_body_east);
225 htonf(fdm->v_wind_body_down);
227 htonf(fdm->A_X_pilot);
228 htonf(fdm->A_Y_pilot);
229 htonf(fdm->A_Z_pilot);
231 htonf(fdm->stall_warning);
232 htonf(fdm->slip_deg);
234 for ( i = 0; i < fdm->num_engines; ++i ) {
235 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
237 htonf(fdm->fuel_flow[i]);
240 htonf(fdm->mp_osi[i]);
242 htonf(fdm->oil_temp[i]);
243 htonf(fdm->oil_px[i]);
245 fdm->num_engines = htonl(fdm->num_engines);
247 for ( i = 0; i < fdm->num_tanks; ++i ) {
248 htonf(fdm->fuel_quantity[i]);
250 fdm->num_tanks = htonl(fdm->num_tanks);
252 for ( i = 0; i < fdm->num_wheels; ++i ) {
253 fdm->wow[i] = htonl(fdm->wow[i]);
254 htonf(fdm->gear_pos[i]);
255 htonf(fdm->gear_steer[i]);
256 htonf(fdm->gear_compression[i]);
258 fdm->num_wheels = htonl(fdm->num_wheels);
260 fdm->cur_time = htonl( fdm->cur_time );
261 fdm->warp = htonl( fdm->warp );
262 htonf(fdm->visibility);
264 htonf(fdm->elevator);
265 htonf(fdm->elevator_trim_tab);
266 htonf(fdm->left_flap);
267 htonf(fdm->right_flap);
268 htonf(fdm->left_aileron);
269 htonf(fdm->right_aileron);
271 htonf(fdm->nose_wheel);
272 htonf(fdm->speedbrake);
273 htonf(fdm->spoilers);
277 static void send_data( const GPSPoint p ) {
279 // int ctrlsize = sizeof( FGNetCtrls );
280 int fdmsize = sizeof( FGNetFDM );
282 // cout << "Running main loop" << endl;
287 gps2fg( p, &fgfdm, &fgctrls );
288 len = fdm_sock.send(&fgfdm, fdmsize, 0);
292 void usage( const string &argv0 ) {
293 cout << "Usage: " << argv0 << endl;
294 cout << "\t[ --help ]" << endl;
295 cout << "\t[ --file <file_name>" << endl;
296 cout << "\t[ --hertz <hertz> ]" << endl;
297 cout << "\t[ --host <hostname> ]" << endl;
298 cout << "\t[ --broadcast ]" << endl;
299 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
300 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
304 int main( int argc, char **argv ) {
306 string out_host = "localhost";
307 bool do_broadcast = false;
309 // process command line arguments
310 for ( int i = 1; i < argc; ++i ) {
311 if ( strcmp( argv[i], "--help" ) == 0 ) {
314 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
317 hertz = atof( argv[i] );
322 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
330 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
338 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
340 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
343 fdm_port = atoi( argv[i] );
348 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
351 ctrls_port = atoi( argv[i] );
362 // Load the track data
364 cout << "No track file specified" << endl;
368 cout << "Loaded " << track.size() << " records." << endl;
370 // Setup up outgoing network connections
372 netInit( &argc,argv ); // We must call this before any other net stuff
374 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
375 cout << "error opening fdm output socket" << endl;
378 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
379 cout << "error opening ctrls output socket" << endl;
382 cout << "open net channels" << endl;
384 fdm_sock.setBlocking( false );
385 ctrls_sock.setBlocking( false );
386 cout << "blocking false" << endl;
388 if ( do_broadcast ) {
389 fdm_sock.setBroadcast( true );
390 ctrls_sock.setBroadcast( true );
393 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
395 cout << "error connecting to outgoing fdm port: " << out_host
396 << ":" << fdm_port << endl;
399 cout << "connected outgoing fdm socket" << endl;
401 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
403 cout << "error connecting to outgoing ctrls port: " << out_host
404 << ":" << ctrls_port << endl;
407 cout << "connected outgoing ctrls socket" << endl;
409 int size = track.size();
411 double current_time = track.get_point(0).get_time();
412 cout << "Track begin time is " << current_time << endl;
413 double end_time = track.get_point(size-1).get_time();
414 cout << "Track end time is " << end_time << endl;
415 cout << "Duration = " << end_time - current_time << endl;
417 double frame_us = 1000000.0 / hertz;
418 if ( frame_us < 0.0 ) {
422 SGTimeStamp start_time;
427 p0 = p1 = track.get_point( 0 );
429 while ( current_time < end_time ) {
430 // cout << "current_time = " << current_time << " end_time = "
431 // << end_time << endl;
433 if ( current_time > p1.get_time() ) {
436 // cout << "count = " << count << endl;
437 p1 = track.get_point( count );
439 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
443 if ( fabs(p1.get_time() - p0.get_time()) < 0.0001 ) {
447 (current_time - p0.get_time()) /
448 (p1.get_time() - p0.get_time());
450 // cout << "Percent = " << percent << endl;
452 GPSPoint p = GPSInterpolate( p0, p1, percent );
453 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg << endl;
454 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg << endl;
455 cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
459 // Update the elapsed time.
460 static bool first_time = true;
462 last_time_stamp.stamp();
466 current_time_stamp.stamp();
468 double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
469 if ( elapsed_us < (frame_us - 2000) ) {
470 double requested_us = (frame_us - elapsed_us) - 2000 ;
471 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
473 current_time_stamp.stamp();
474 while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
475 current_time_stamp.stamp();
478 current_time += (frame_us / 1000000.0);
479 last_time_stamp = current_time_stamp;
482 cout << "Processed " << count << " entries in "
483 << current_time_stamp - start_time << " seconds." << endl;