11 #include <simgear/constants.h>
12 #include <simgear/io/lowlevel.hxx> // endian tests
13 #include <simgear/math/sg_geodesy.hxx>
14 #include <simgear/timing/timestamp.hxx>
16 #include <Network/net_ctrls.hxx>
17 #include <Network/net_fdm.hxx>
19 #include "MIDG-II.hxx"
28 static netSocket fdm_sock, ctrls_sock;
34 static int fdm_port = 5505;
35 static int ctrls_port = 5506;
38 static string file = "";
40 // Master time counter
41 float sim_time = 0.0f;
42 double frame_us = 0.0f;
45 SGTimeStamp last_time_stamp;
46 SGTimeStamp current_time_stamp;
49 double alt_offset = 0.0;
51 // skip initial seconds
55 // double last_lat = 0.0, last_lon = 0.0;
56 // double kts_filter = 0.0;
61 // The function htond is defined this way due to the way some
62 // processors and OSes treat floating point values. Some will raise
63 // an exception whenever a "bad" floating point value is loaded into a
64 // floating point register. Solaris is notorious for this, but then
65 // so is LynxOS on the PowerPC. By translating the data in place,
66 // there is no need to load a FP register with the "corruped" floating
67 // point value. By doing the BIG_ENDIAN test, I can optimize the
68 // routine for big-endian processors so it can be as efficient as
70 static void htond (double &x)
72 if ( sgIsLittleEndian() ) {
76 Double_Overlay = (int *) &x;
77 Holding_Buffer = Double_Overlay [0];
79 Double_Overlay [0] = htonl (Double_Overlay [1]);
80 Double_Overlay [1] = htonl (Holding_Buffer);
87 static void htonf (float &x)
89 if ( sgIsLittleEndian() ) {
93 Float_Overlay = (int *) &x;
94 Holding_Buffer = Float_Overlay [0];
96 Float_Overlay [0] = htonl (Holding_Buffer);
103 static void midg2fg( const MIDGpos pos, const MIDGatt att,
104 FGNetFDM *fdm, FGNetCtrls *ctrls )
108 // Version sanity checking
109 fdm->version = FG_NET_FDM_VERSION;
112 fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS;
113 fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS;
114 fdm->altitude = pos.altitude_msl + alt_offset;
116 fdm->psi = att.yaw_rad; // heading
117 fdm->phi = att.roll_rad; // roll
118 fdm->theta = att.pitch_rad; // pitch;
125 // double az1, az2, dist;
126 // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
127 // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
128 // double v_ms = dist / (frame_us / 1000000);
129 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
130 // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
131 fdm->vcas = pos.speed_kts;
132 // last_lat = pos.lat_deg;
133 // last_lon = pos.lon_deg;
134 // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
136 fdm->climb_rate = 0; // fps
137 // cout << "climb rate = " << aero->hdota << endl;
141 fdm->v_wind_body_north = 0.0;
142 fdm->v_wind_body_east = 0.0;
143 fdm->v_wind_body_down = 0.0;
144 fdm->stall_warning = 0.0;
146 fdm->A_X_pilot = 0.0;
147 fdm->A_Y_pilot = 0.0;
148 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
151 fdm->num_engines = 1;
152 fdm->eng_state[0] = 2;
153 // cout << "state = " << fdm->eng_state[0] << endl;
154 double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
155 if ( rpm < 0.0 ) { rpm = 0.0; }
156 if ( rpm > 3000.0 ) { rpm = 3000.0; }
159 fdm->fuel_flow[0] = 0.0;
161 // cout << "egt = " << aero->EGT << endl;
162 fdm->oil_temp[0] = 0.0;
163 fdm->oil_px[0] = 0.0;
167 fdm->fuel_quantity[0] = 0.0;
168 fdm->fuel_quantity[1] = 0.0;
176 // the following really aren't used in this context
181 // cout << "Flap deflection = " << aero->dflap << endl;
182 fdm->left_flap = 0.0;
183 fdm->right_flap = 0.0;
185 fdm->elevator = -fdm->theta * 1.0;
186 fdm->elevator_trim_tab = 0.0;
187 fdm->left_flap = 0.0;
188 fdm->right_flap = 0.0;
189 fdm->left_aileron = fdm->phi * 1.0;
190 fdm->right_aileron = -fdm->phi * 1.0;
192 fdm->nose_wheel = 0.0;
193 fdm->speedbrake = 0.0;
196 // Convert the net buffer to network format
197 fdm->version = htonl(fdm->version);
199 htond(fdm->longitude);
200 htond(fdm->latitude);
201 htond(fdm->altitude);
210 htonf(fdm->thetadot);
213 htonf(fdm->climb_rate);
217 htonf(fdm->v_wind_body_north);
218 htonf(fdm->v_wind_body_east);
219 htonf(fdm->v_wind_body_down);
221 htonf(fdm->A_X_pilot);
222 htonf(fdm->A_Y_pilot);
223 htonf(fdm->A_Z_pilot);
225 htonf(fdm->stall_warning);
226 htonf(fdm->slip_deg);
228 for ( i = 0; i < fdm->num_engines; ++i ) {
229 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
231 htonf(fdm->fuel_flow[i]);
234 htonf(fdm->mp_osi[i]);
236 htonf(fdm->oil_temp[i]);
237 htonf(fdm->oil_px[i]);
239 fdm->num_engines = htonl(fdm->num_engines);
241 for ( i = 0; i < fdm->num_tanks; ++i ) {
242 htonf(fdm->fuel_quantity[i]);
244 fdm->num_tanks = htonl(fdm->num_tanks);
246 for ( i = 0; i < fdm->num_wheels; ++i ) {
247 fdm->wow[i] = htonl(fdm->wow[i]);
248 htonf(fdm->gear_pos[i]);
249 htonf(fdm->gear_steer[i]);
250 htonf(fdm->gear_compression[i]);
252 fdm->num_wheels = htonl(fdm->num_wheels);
254 fdm->cur_time = htonl( fdm->cur_time );
255 fdm->warp = htonl( fdm->warp );
256 htonf(fdm->visibility);
258 htonf(fdm->elevator);
259 htonf(fdm->elevator_trim_tab);
260 htonf(fdm->left_flap);
261 htonf(fdm->right_flap);
262 htonf(fdm->left_aileron);
263 htonf(fdm->right_aileron);
265 htonf(fdm->nose_wheel);
266 htonf(fdm->speedbrake);
267 htonf(fdm->spoilers);
271 static void send_data( const MIDGpos pos, const MIDGatt att ) {
273 int fdmsize = sizeof( FGNetFDM );
275 // cout << "Running main loop" << endl;
280 midg2fg( pos, att, &fgfdm, &fgctrls );
281 len = fdm_sock.send(&fgfdm, fdmsize, 0);
285 void usage( const string &argv0 ) {
286 cout << "Usage: " << argv0 << endl;
287 cout << "\t[ --help ]" << endl;
288 cout << "\t[ --file <file_name>" << endl;
289 cout << "\t[ --hertz <hertz> ]" << endl;
290 cout << "\t[ --host <hostname> ]" << endl;
291 cout << "\t[ --broadcast ]" << endl;
292 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
293 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
294 cout << "\t[ --altitude-offset <meters> ]" << endl;
295 cout << "\t[ --skip-seconds <seconds> ]" << endl;
299 int main( int argc, char **argv ) {
301 string out_host = "localhost";
302 bool do_broadcast = false;
304 // process command line arguments
305 for ( int i = 1; i < argc; ++i ) {
306 if ( strcmp( argv[i], "--help" ) == 0 ) {
309 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
312 hertz = atof( argv[i] );
317 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
325 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
333 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
335 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
338 fdm_port = atoi( argv[i] );
343 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
346 ctrls_port = atoi( argv[i] );
351 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
354 alt_offset = atof( argv[i] );
359 } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
362 skip = atof( argv[i] );
373 // Load the track data
375 cout << "No track file specified" << endl;
379 cout << "Loaded " << track.pos_size() << " position records." << endl;
380 cout << "Loaded " << track.att_size() << " attitude records." << endl;
382 // Setup up outgoing network connections
384 netInit( &argc,argv ); // We must call this before any other net stuff
386 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
387 cout << "error opening fdm output socket" << endl;
390 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
391 cout << "error opening ctrls output socket" << endl;
394 cout << "open net channels" << endl;
396 fdm_sock.setBlocking( false );
397 ctrls_sock.setBlocking( false );
398 cout << "blocking false" << endl;
400 if ( do_broadcast ) {
401 fdm_sock.setBroadcast( true );
402 ctrls_sock.setBroadcast( true );
405 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
407 cout << "error connecting to outgoing fdm port: " << out_host
408 << ":" << fdm_port << endl;
411 cout << "connected outgoing fdm socket" << endl;
413 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
415 cout << "error connecting to outgoing ctrls port: " << out_host
416 << ":" << ctrls_port << endl;
419 cout << "connected outgoing ctrls socket" << endl;
421 int size = track.pos_size();
423 double current_time = track.get_pospt(0).get_seconds();
424 cout << "Track begin time is " << current_time << endl;
425 double end_time = track.get_pospt(size-1).get_seconds();
426 cout << "Track end time is " << end_time << endl;
427 cout << "Duration = " << end_time - current_time << endl;
429 // advance skip seconds forward
430 current_time += skip;
432 frame_us = 1000000.0 / hertz;
433 if ( frame_us < 0.0 ) {
437 SGTimeStamp start_time;
443 pos0 = pos1 = track.get_pospt( 0 );
446 att0 = att1 = track.get_attpt( 0 );
448 while ( current_time < end_time ) {
449 // cout << "current_time = " << current_time << " end_time = "
450 // << end_time << endl;
452 // Advance position pointer
453 while ( current_time > pos1.get_seconds()
454 && pos_count < track.pos_size() )
458 // cout << "count = " << count << endl;
459 pos1 = track.get_pospt( pos_count );
461 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
464 // Advance attitude pointer
465 while ( current_time > att1.get_seconds()
466 && att_count < track.att_size() )
470 // cout << "count = " << count << endl;
471 att1 = track.get_attpt( att_count );
473 // cout << "pos0 = " << pos0.get_seconds()
474 // << " pos1 = " << pos1.get_seconds() << endl;
477 if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
481 (current_time - pos0.get_seconds()) /
482 (pos1.get_seconds() - pos0.get_seconds());
484 // cout << "Percent = " << percent << endl;
486 if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
490 (current_time - att0.get_seconds()) /
491 (att1.get_seconds() - att0.get_seconds());
493 // cout << "Percent = " << percent << endl;
495 MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent );
496 MIDGatt att = MIDGInterpAtt( att0, att1, att_percent );
497 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
499 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
501 // cout << (double)current_time << " " << pos.lat_deg << ", "
502 // << pos.lon_deg << " " << att.yaw_deg << endl;
503 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
505 pos.lat_deg, pos.lon_deg, pos.altitude_msl,
506 att.yaw_rad * 180.0 / SG_PI,
507 att.pitch_rad * 180.0 / SG_PI,
508 att.roll_rad * 180.0 / SG_PI );
510 send_data( pos, att );
512 // Update the elapsed time.
513 static bool first_time = true;
515 last_time_stamp.stamp();
519 current_time_stamp.stamp();
521 double elapsed_us = current_time_stamp - last_time_stamp;
522 if ( elapsed_us < (frame_us - 2000) ) {
523 double requested_us = (frame_us - elapsed_us) - 2000 ;
524 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
526 current_time_stamp.stamp();
527 while ( current_time_stamp - last_time_stamp < frame_us ) {
528 current_time_stamp.stamp();
531 current_time += (frame_us / 1000000.0);
532 last_time_stamp = current_time_stamp;
535 cout << "Processed " << pos_count << " entries in "
536 << (current_time_stamp - start_time) / 1000000 << " seconds." << endl;