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;
52 // double last_lat = 0.0, last_lon = 0.0;
53 // double kts_filter = 0.0;
58 // The function htond is defined this way due to the way some
59 // processors and OSes treat floating point values. Some will raise
60 // an exception whenever a "bad" floating point value is loaded into a
61 // floating point register. Solaris is notorious for this, but then
62 // so is LynxOS on the PowerPC. By translating the data in place,
63 // there is no need to load a FP register with the "corruped" floating
64 // point value. By doing the BIG_ENDIAN test, I can optimize the
65 // routine for big-endian processors so it can be as efficient as
67 static void htond (double &x)
69 if ( sgIsLittleEndian() ) {
73 Double_Overlay = (int *) &x;
74 Holding_Buffer = Double_Overlay [0];
76 Double_Overlay [0] = htonl (Double_Overlay [1]);
77 Double_Overlay [1] = htonl (Holding_Buffer);
84 static void htonf (float &x)
86 if ( sgIsLittleEndian() ) {
90 Float_Overlay = (int *) &x;
91 Holding_Buffer = Float_Overlay [0];
93 Float_Overlay [0] = htonl (Holding_Buffer);
100 static void midg2fg( const MIDGpos pos, const MIDGatt att,
101 FGNetFDM *fdm, FGNetCtrls *ctrls )
105 // Version sanity checking
106 fdm->version = FG_NET_FDM_VERSION;
109 fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS;
110 fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS;
111 fdm->altitude = pos.altitude_msl + alt_offset;
113 fdm->psi = att.yaw_rad; // heading
114 fdm->phi = att.roll_rad; // roll
115 fdm->theta = att.pitch_rad; // pitch;
122 // double az1, az2, dist;
123 // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
124 // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
125 // double v_ms = dist / (frame_us / 1000000);
126 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
127 // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
128 fdm->vcas = pos.speed_kts;
129 // last_lat = pos.lat_deg;
130 // last_lon = pos.lon_deg;
131 // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
133 fdm->climb_rate = 0; // fps
134 // cout << "climb rate = " << aero->hdota << endl;
138 fdm->v_wind_body_north = 0.0;
139 fdm->v_wind_body_east = 0.0;
140 fdm->v_wind_body_down = 0.0;
141 fdm->stall_warning = 0.0;
143 fdm->A_X_pilot = 0.0;
144 fdm->A_Y_pilot = 0.0;
145 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
148 fdm->num_engines = 1;
149 fdm->eng_state[0] = 2;
150 // cout << "state = " << fdm->eng_state[0] << endl;
151 double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
152 if ( rpm < 0.0 ) { rpm = 0.0; }
153 if ( rpm > 3000.0 ) { rpm = 3000.0; }
156 fdm->fuel_flow[0] = 0.0;
158 // cout << "egt = " << aero->EGT << endl;
159 fdm->oil_temp[0] = 0.0;
160 fdm->oil_px[0] = 0.0;
164 fdm->fuel_quantity[0] = 0.0;
165 fdm->fuel_quantity[1] = 0.0;
173 // the following really aren't used in this context
178 // cout << "Flap deflection = " << aero->dflap << endl;
179 fdm->left_flap = 0.0;
180 fdm->right_flap = 0.0;
182 fdm->elevator = -fdm->theta * 1.0;
183 fdm->elevator_trim_tab = 0.0;
184 fdm->left_flap = 0.0;
185 fdm->right_flap = 0.0;
186 fdm->left_aileron = fdm->phi * 1.0;
187 fdm->right_aileron = -fdm->phi * 1.0;
189 fdm->nose_wheel = 0.0;
190 fdm->speedbrake = 0.0;
193 // Convert the net buffer to network format
194 fdm->version = htonl(fdm->version);
196 htond(fdm->longitude);
197 htond(fdm->latitude);
198 htond(fdm->altitude);
207 htonf(fdm->thetadot);
210 htonf(fdm->climb_rate);
214 htonf(fdm->v_wind_body_north);
215 htonf(fdm->v_wind_body_east);
216 htonf(fdm->v_wind_body_down);
218 htonf(fdm->A_X_pilot);
219 htonf(fdm->A_Y_pilot);
220 htonf(fdm->A_Z_pilot);
222 htonf(fdm->stall_warning);
223 htonf(fdm->slip_deg);
225 for ( i = 0; i < fdm->num_engines; ++i ) {
226 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
228 htonf(fdm->fuel_flow[i]);
231 htonf(fdm->mp_osi[i]);
233 htonf(fdm->oil_temp[i]);
234 htonf(fdm->oil_px[i]);
236 fdm->num_engines = htonl(fdm->num_engines);
238 for ( i = 0; i < fdm->num_tanks; ++i ) {
239 htonf(fdm->fuel_quantity[i]);
241 fdm->num_tanks = htonl(fdm->num_tanks);
243 for ( i = 0; i < fdm->num_wheels; ++i ) {
244 fdm->wow[i] = htonl(fdm->wow[i]);
245 htonf(fdm->gear_pos[i]);
246 htonf(fdm->gear_steer[i]);
247 htonf(fdm->gear_compression[i]);
249 fdm->num_wheels = htonl(fdm->num_wheels);
251 fdm->cur_time = htonl( fdm->cur_time );
252 fdm->warp = htonl( fdm->warp );
253 htonf(fdm->visibility);
255 htonf(fdm->elevator);
256 htonf(fdm->elevator_trim_tab);
257 htonf(fdm->left_flap);
258 htonf(fdm->right_flap);
259 htonf(fdm->left_aileron);
260 htonf(fdm->right_aileron);
262 htonf(fdm->nose_wheel);
263 htonf(fdm->speedbrake);
264 htonf(fdm->spoilers);
268 static void send_data( const MIDGpos pos, const MIDGatt att ) {
270 int ctrlsize = sizeof( FGNetCtrls );
271 int fdmsize = sizeof( FGNetFDM );
273 // cout << "Running main loop" << endl;
278 midg2fg( pos, att, &fgfdm, &fgctrls );
279 len = fdm_sock.send(&fgfdm, fdmsize, 0);
283 void usage( const string &argv0 ) {
284 cout << "Usage: " << argv0 << endl;
285 cout << "\t[ --help ]" << endl;
286 cout << "\t[ --file <file_name>" << endl;
287 cout << "\t[ --hertz <hertz> ]" << endl;
288 cout << "\t[ --host <hostname> ]" << endl;
289 cout << "\t[ --broadcast ]" << endl;
290 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
291 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
292 cout << "\t[ --altitude-offset <meters> ]" << endl;
296 int main( int argc, char **argv ) {
298 string out_host = "localhost";
299 bool do_broadcast = false;
301 // process command line arguments
302 for ( int i = 1; i < argc; ++i ) {
303 if ( strcmp( argv[i], "--help" ) == 0 ) {
306 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
309 hertz = atof( argv[i] );
314 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
322 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
330 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
332 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
335 fdm_port = atoi( argv[i] );
340 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
343 ctrls_port = atoi( argv[i] );
348 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
351 alt_offset = atof( argv[i] );
362 // Load the track data
364 cout << "No track file specified" << endl;
368 cout << "Loaded " << track.possize() << " position records." << endl;
369 cout << "Loaded " << track.attsize() << " attitude records." << endl;
371 // Setup up outgoing network connections
373 netInit( &argc,argv ); // We must call this before any other net stuff
375 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
376 cout << "error opening fdm output socket" << endl;
379 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
380 cout << "error opening ctrls output socket" << endl;
383 cout << "open net channels" << endl;
385 fdm_sock.setBlocking( false );
386 ctrls_sock.setBlocking( false );
387 cout << "blocking false" << endl;
389 if ( do_broadcast ) {
390 fdm_sock.setBroadcast( true );
391 ctrls_sock.setBroadcast( true );
394 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
396 cout << "error connecting to outgoing fdm port: " << out_host
397 << ":" << fdm_port << endl;
400 cout << "connected outgoing fdm socket" << endl;
402 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
404 cout << "error connecting to outgoing ctrls port: " << out_host
405 << ":" << ctrls_port << endl;
408 cout << "connected outgoing ctrls socket" << endl;
410 int size = track.possize();
412 double current_time = track.get_pospt(0).get_seconds();
413 cout << "Track begin time is " << current_time << endl;
414 double end_time = track.get_pospt(size-1).get_seconds();
415 cout << "Track end time is " << end_time << endl;
416 cout << "Duration = " << end_time - current_time << endl;
418 frame_us = 1000000.0 / hertz;
419 if ( frame_us < 0.0 ) {
423 SGTimeStamp start_time;
429 pos0 = pos1 = track.get_pospt( 0 );
432 att0 = att1 = track.get_attpt( 0 );
434 while ( current_time < end_time ) {
435 // cout << "current_time = " << current_time << " end_time = "
436 // << end_time << endl;
438 // Advance position pointer
439 if ( current_time > pos1.get_seconds() ) {
442 // cout << "count = " << count << endl;
443 pos1 = track.get_pospt( pos_count );
445 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
448 // Advance attitude pointer
449 if ( current_time > att1.get_seconds() ) {
452 // cout << "count = " << count << endl;
453 att1 = track.get_attpt( att_count );
455 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
459 if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
463 (current_time - pos0.get_seconds()) /
464 (pos1.get_seconds() - pos0.get_seconds());
466 // cout << "Percent = " << percent << endl;
468 if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
472 (current_time - att0.get_seconds()) /
473 (att1.get_seconds() - att0.get_seconds());
475 // cout << "Percent = " << percent << endl;
477 MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent );
478 MIDGatt att = MIDGInterpAtt( att0, att1, att_percent );
479 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
481 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
483 // cout << (double)current_time << " " << pos.lat_deg << ", "
484 // << pos.lon_deg << " " << att.yaw_deg << endl;
485 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
487 pos.lat_deg, pos.lon_deg, pos.altitude_msl,
488 att.yaw_rad * 180.0 / SG_PI,
489 att.pitch_rad * 180.0 / SG_PI,
490 att.roll_rad * 180.0 / SG_PI );
492 send_data( pos, att );
494 // Update the elapsed time.
495 static bool first_time = true;
497 last_time_stamp.stamp();
501 current_time_stamp.stamp();
503 double elapsed_us = current_time_stamp - last_time_stamp;
504 if ( elapsed_us < (frame_us - 2000) ) {
505 double requested_us = (frame_us - elapsed_us) - 2000 ;
506 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
508 current_time_stamp.stamp();
509 while ( current_time_stamp - last_time_stamp < frame_us ) {
510 current_time_stamp.stamp();
513 current_time += (frame_us / 1000000.0);
514 last_time_stamp = current_time_stamp;
517 cout << "Processed " << pos_count << " entries in "
518 << (current_time_stamp - start_time) / 1000000 << " seconds." << endl;