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>
17 #include "MIDG-II.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;
46 double alt_offset = 0.0;
51 // The function htond is defined this way due to the way some
52 // processors and OSes treat floating point values. Some will raise
53 // an exception whenever a "bad" floating point value is loaded into a
54 // floating point register. Solaris is notorious for this, but then
55 // so is LynxOS on the PowerPC. By translating the data in place,
56 // there is no need to load a FP register with the "corruped" floating
57 // point value. By doing the BIG_ENDIAN test, I can optimize the
58 // routine for big-endian processors so it can be as efficient as
60 static void htond (double &x)
62 if ( sgIsLittleEndian() ) {
66 Double_Overlay = (int *) &x;
67 Holding_Buffer = Double_Overlay [0];
69 Double_Overlay [0] = htonl (Double_Overlay [1]);
70 Double_Overlay [1] = htonl (Holding_Buffer);
77 static void htonf (float &x)
79 if ( sgIsLittleEndian() ) {
83 Float_Overlay = (int *) &x;
84 Holding_Buffer = Float_Overlay [0];
86 Float_Overlay [0] = htonl (Holding_Buffer);
93 static void midg2fg( const MIDGpos pos, const MIDGatt att,
94 FGNetFDM *fdm, FGNetCtrls *ctrls )
98 // Version sanity checking
99 fdm->version = FG_NET_FDM_VERSION;
102 fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS;
103 fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS;
104 fdm->altitude = pos.altitude_msl + alt_offset;
106 fdm->psi = att.yaw_rad; // heading
107 fdm->phi = att.roll_rad; // roll
108 fdm->theta = att.pitch_rad; // pitch;
113 fdm->vcas = pos.speed_kts;
114 fdm->climb_rate = 0; // fps
115 // cout << "climb rate = " << aero->hdota << endl;
119 fdm->v_wind_body_north = 0.0;
120 fdm->v_wind_body_east = 0.0;
121 fdm->v_wind_body_down = 0.0;
122 fdm->stall_warning = 0.0;
124 fdm->A_X_pilot = 0.0;
125 fdm->A_Y_pilot = 0.0;
126 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
129 fdm->num_engines = 1;
130 fdm->eng_state[0] = 2;
131 // cout << "state = " << fdm->eng_state[0] << endl;
132 double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
133 if ( rpm < 0.0 ) { rpm = 0.0; }
134 if ( rpm > 3000.0 ) { rpm = 3000.0; }
137 fdm->fuel_flow[0] = 0.0;
139 // cout << "egt = " << aero->EGT << endl;
140 fdm->oil_temp[0] = 0.0;
141 fdm->oil_px[0] = 0.0;
145 fdm->fuel_quantity[0] = 0.0;
146 fdm->fuel_quantity[1] = 0.0;
154 // the following really aren't used in this context
159 // cout << "Flap deflection = " << aero->dflap << endl;
160 fdm->left_flap = 0.0;
161 fdm->right_flap = 0.0;
163 fdm->elevator = -fdm->theta * 1.0;
164 fdm->elevator_trim_tab = 0.0;
165 fdm->left_flap = 0.0;
166 fdm->right_flap = 0.0;
167 fdm->left_aileron = fdm->phi * 1.0;
168 fdm->right_aileron = -fdm->phi * 1.0;
170 fdm->nose_wheel = 0.0;
171 fdm->speedbrake = 0.0;
174 // Convert the net buffer to network format
175 fdm->version = htonl(fdm->version);
177 htond(fdm->longitude);
178 htond(fdm->latitude);
179 htond(fdm->altitude);
188 htonf(fdm->thetadot);
191 htonf(fdm->climb_rate);
195 htonf(fdm->v_wind_body_north);
196 htonf(fdm->v_wind_body_east);
197 htonf(fdm->v_wind_body_down);
199 htonf(fdm->A_X_pilot);
200 htonf(fdm->A_Y_pilot);
201 htonf(fdm->A_Z_pilot);
203 htonf(fdm->stall_warning);
204 htonf(fdm->slip_deg);
206 for ( i = 0; i < fdm->num_engines; ++i ) {
207 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
209 htonf(fdm->fuel_flow[i]);
212 htonf(fdm->mp_osi[i]);
214 htonf(fdm->oil_temp[i]);
215 htonf(fdm->oil_px[i]);
217 fdm->num_engines = htonl(fdm->num_engines);
219 for ( i = 0; i < fdm->num_tanks; ++i ) {
220 htonf(fdm->fuel_quantity[i]);
222 fdm->num_tanks = htonl(fdm->num_tanks);
224 for ( i = 0; i < fdm->num_wheels; ++i ) {
225 fdm->wow[i] = htonl(fdm->wow[i]);
226 htonf(fdm->gear_pos[i]);
227 htonf(fdm->gear_steer[i]);
228 htonf(fdm->gear_compression[i]);
230 fdm->num_wheels = htonl(fdm->num_wheels);
232 fdm->cur_time = htonl( fdm->cur_time );
233 fdm->warp = htonl( fdm->warp );
234 htonf(fdm->visibility);
236 htonf(fdm->elevator);
237 htonf(fdm->elevator_trim_tab);
238 htonf(fdm->left_flap);
239 htonf(fdm->right_flap);
240 htonf(fdm->left_aileron);
241 htonf(fdm->right_aileron);
243 htonf(fdm->nose_wheel);
244 htonf(fdm->speedbrake);
245 htonf(fdm->spoilers);
249 static void send_data( const MIDGpos pos, const MIDGatt att ) {
251 int ctrlsize = sizeof( FGNetCtrls );
252 int fdmsize = sizeof( FGNetFDM );
254 // cout << "Running main loop" << endl;
259 midg2fg( pos, att, &fgfdm, &fgctrls );
260 len = fdm_sock.send(&fgfdm, fdmsize, 0);
264 void usage( const string &argv0 ) {
265 cout << "Usage: " << argv0 << endl;
266 cout << "\t[ --help ]" << endl;
267 cout << "\t[ --file <file_name>" << endl;
268 cout << "\t[ --hertz <hertz> ]" << endl;
269 cout << "\t[ --host <hostname> ]" << endl;
270 cout << "\t[ --broadcast ]" << endl;
271 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
272 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
273 cout << "\t[ --altitude-offset <meters> ]" << endl;
277 int main( int argc, char **argv ) {
279 string out_host = "localhost";
280 bool do_broadcast = false;
282 // process command line arguments
283 for ( int i = 1; i < argc; ++i ) {
284 if ( strcmp( argv[i], "--help" ) == 0 ) {
287 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
290 hertz = atof( argv[i] );
295 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
303 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
311 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
313 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
316 fdm_port = atoi( argv[i] );
321 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
324 ctrls_port = atoi( argv[i] );
329 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
332 alt_offset = atof( argv[i] );
343 // Load the track data
345 cout << "No track file specified" << endl;
349 cout << "Loaded " << track.possize() << " position records." << endl;
350 cout << "Loaded " << track.attsize() << " attitude records." << endl;
352 // Setup up outgoing network connections
354 netInit( &argc,argv ); // We must call this before any other net stuff
356 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
357 cout << "error opening fdm output socket" << endl;
360 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
361 cout << "error opening ctrls output socket" << endl;
364 cout << "open net channels" << endl;
366 fdm_sock.setBlocking( false );
367 ctrls_sock.setBlocking( false );
368 cout << "blocking false" << endl;
370 if ( do_broadcast ) {
371 fdm_sock.setBroadcast( true );
372 ctrls_sock.setBroadcast( true );
375 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
377 cout << "error connecting to outgoing fdm port: " << out_host
378 << ":" << fdm_port << endl;
381 cout << "connected outgoing fdm socket" << endl;
383 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
385 cout << "error connecting to outgoing ctrls port: " << out_host
386 << ":" << ctrls_port << endl;
389 cout << "connected outgoing ctrls socket" << endl;
391 int size = track.possize();
393 double current_time = track.get_pospt(0).get_seconds();
394 cout << "Track begin time is " << current_time << endl;
395 double end_time = track.get_pospt(size-1).get_seconds();
396 cout << "Track end time is " << end_time << endl;
397 cout << "Duration = " << end_time - current_time << endl;
399 double frame_us = 1000000.0 / hertz;
400 if ( frame_us < 0.0 ) {
404 SGTimeStamp start_time;
410 pos0 = pos1 = track.get_pospt( 0 );
413 att0 = att1 = track.get_attpt( 0 );
415 while ( current_time < end_time ) {
416 // cout << "current_time = " << current_time << " end_time = "
417 // << end_time << endl;
419 // Advance position pointer
420 if ( current_time > pos1.get_seconds() ) {
423 // cout << "count = " << count << endl;
424 pos1 = track.get_pospt( pos_count );
426 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
429 // Advance attitude pointer
430 if ( current_time > att1.get_seconds() ) {
433 // cout << "count = " << count << endl;
434 att1 = track.get_attpt( att_count );
436 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
440 if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
444 (current_time - pos0.get_seconds()) /
445 (pos1.get_seconds() - pos0.get_seconds());
447 // cout << "Percent = " << percent << endl;
449 if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
453 (current_time - att0.get_seconds()) /
454 (att1.get_seconds() - att0.get_seconds());
456 // cout << "Percent = " << percent << endl;
458 MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent );
459 MIDGatt att = MIDGInterpAtt( att0, att1, att_percent );
460 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
462 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
464 // cout << (double)current_time << " " << pos.lat_deg << ", "
465 // << pos.lon_deg << " " << att.yaw_deg << endl;
466 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
468 pos.lat_deg, pos.lon_deg, pos.altitude_msl,
469 att.yaw_rad * 180.0 / SG_PI,
470 att.pitch_rad * 180.0 / SG_PI,
471 att.roll_rad * 180.0 / SG_PI );
473 send_data( pos, att );
475 // Update the elapsed time.
476 static bool first_time = true;
478 last_time_stamp.stamp();
482 current_time_stamp.stamp();
484 double elapsed_us = current_time_stamp - last_time_stamp;
485 if ( elapsed_us < (frame_us - 2000) ) {
486 double requested_us = (frame_us - elapsed_us) - 2000 ;
487 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
489 current_time_stamp.stamp();
490 while ( current_time_stamp - last_time_stamp < frame_us ) {
491 current_time_stamp.stamp();
494 current_time += (frame_us / 1000000.0);
495 last_time_stamp = current_time_stamp;
498 cout << "Processed " << pos_count << " entries in "
499 << (current_time_stamp - start_time) / 1000000 << " seconds." << endl;