11 #include <simgear/constants.h>
12 #include <simgear/io/lowlevel.hxx> // endian tests
13 #include <simgear/io/sg_serial.hxx>
14 #include <simgear/math/sg_geodesy.hxx>
15 #include <simgear/timing/timestamp.hxx>
17 #include <Network/net_ctrls.hxx>
18 #include <Network/net_fdm.hxx>
20 #include "MIDG-II.hxx"
29 static netSocket fdm_sock, ctrls_sock;
35 static int fdm_port = 5505;
36 static int ctrls_port = 5506;
39 static string file = "";
40 static string serialdev = "";
42 // Master time counter
43 float sim_time = 0.0f;
44 double frame_us = 0.0f;
47 SGTimeStamp last_time_stamp;
48 SGTimeStamp current_time_stamp;
51 double alt_offset = 0.0;
53 // skip initial seconds
57 // double last_lat = 0.0, last_lon = 0.0;
58 // double kts_filter = 0.0;
63 // The function htond is defined this way due to the way some
64 // processors and OSes treat floating point values. Some will raise
65 // an exception whenever a "bad" floating point value is loaded into a
66 // floating point register. Solaris is notorious for this, but then
67 // so is LynxOS on the PowerPC. By translating the data in place,
68 // there is no need to load a FP register with the "corruped" floating
69 // point value. By doing the BIG_ENDIAN test, I can optimize the
70 // routine for big-endian processors so it can be as efficient as
72 static void htond (double &x)
74 if ( sgIsLittleEndian() ) {
78 Double_Overlay = (int *) &x;
79 Holding_Buffer = Double_Overlay [0];
81 Double_Overlay [0] = htonl (Double_Overlay [1]);
82 Double_Overlay [1] = htonl (Holding_Buffer);
89 static void htonf (float &x)
91 if ( sgIsLittleEndian() ) {
95 Float_Overlay = (int *) &x;
96 Holding_Buffer = Float_Overlay [0];
98 Float_Overlay [0] = htonl (Holding_Buffer);
105 static void midg2fg( const MIDGpos pos, const MIDGatt att,
106 FGNetFDM *fdm, FGNetCtrls *ctrls )
110 // Version sanity checking
111 fdm->version = FG_NET_FDM_VERSION;
114 fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS;
115 fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS;
116 fdm->altitude = pos.altitude_msl + alt_offset;
118 fdm->psi = att.yaw_rad; // heading
119 fdm->phi = att.roll_rad; // roll
120 fdm->theta = att.pitch_rad; // pitch;
127 // double az1, az2, dist;
128 // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
129 // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
130 // double v_ms = dist / (frame_us / 1000000);
131 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
132 // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
133 fdm->vcas = pos.speed_kts;
134 // last_lat = pos.lat_deg;
135 // last_lon = pos.lon_deg;
136 // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
138 fdm->climb_rate = 0; // fps
139 // cout << "climb rate = " << aero->hdota << endl;
143 fdm->v_wind_body_north = 0.0;
144 fdm->v_wind_body_east = 0.0;
145 fdm->v_wind_body_down = 0.0;
146 fdm->stall_warning = 0.0;
148 fdm->A_X_pilot = 0.0;
149 fdm->A_Y_pilot = 0.0;
150 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
153 fdm->num_engines = 1;
154 fdm->eng_state[0] = 2;
155 // cout << "state = " << fdm->eng_state[0] << endl;
156 double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
157 if ( rpm < 0.0 ) { rpm = 0.0; }
158 if ( rpm > 3000.0 ) { rpm = 3000.0; }
161 fdm->fuel_flow[0] = 0.0;
163 // cout << "egt = " << aero->EGT << endl;
164 fdm->oil_temp[0] = 0.0;
165 fdm->oil_px[0] = 0.0;
169 fdm->fuel_quantity[0] = 0.0;
170 fdm->fuel_quantity[1] = 0.0;
178 // the following really aren't used in this context
183 // cout << "Flap deflection = " << aero->dflap << endl;
184 fdm->left_flap = 0.0;
185 fdm->right_flap = 0.0;
187 fdm->elevator = -fdm->theta * 1.0;
188 fdm->elevator_trim_tab = 0.0;
189 fdm->left_flap = 0.0;
190 fdm->right_flap = 0.0;
191 fdm->left_aileron = fdm->phi * 1.0;
192 fdm->right_aileron = -fdm->phi * 1.0;
194 fdm->nose_wheel = 0.0;
195 fdm->speedbrake = 0.0;
198 // Convert the net buffer to network format
199 fdm->version = htonl(fdm->version);
201 htond(fdm->longitude);
202 htond(fdm->latitude);
203 htond(fdm->altitude);
212 htonf(fdm->thetadot);
215 htonf(fdm->climb_rate);
219 htonf(fdm->v_wind_body_north);
220 htonf(fdm->v_wind_body_east);
221 htonf(fdm->v_wind_body_down);
223 htonf(fdm->A_X_pilot);
224 htonf(fdm->A_Y_pilot);
225 htonf(fdm->A_Z_pilot);
227 htonf(fdm->stall_warning);
228 htonf(fdm->slip_deg);
230 for ( i = 0; i < fdm->num_engines; ++i ) {
231 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
233 htonf(fdm->fuel_flow[i]);
236 htonf(fdm->mp_osi[i]);
238 htonf(fdm->oil_temp[i]);
239 htonf(fdm->oil_px[i]);
241 fdm->num_engines = htonl(fdm->num_engines);
243 for ( i = 0; i < fdm->num_tanks; ++i ) {
244 htonf(fdm->fuel_quantity[i]);
246 fdm->num_tanks = htonl(fdm->num_tanks);
248 for ( i = 0; i < fdm->num_wheels; ++i ) {
249 fdm->wow[i] = htonl(fdm->wow[i]);
250 htonf(fdm->gear_pos[i]);
251 htonf(fdm->gear_steer[i]);
252 htonf(fdm->gear_compression[i]);
254 fdm->num_wheels = htonl(fdm->num_wheels);
256 fdm->cur_time = htonl( fdm->cur_time );
257 fdm->warp = htonl( fdm->warp );
258 htonf(fdm->visibility);
260 htonf(fdm->elevator);
261 htonf(fdm->elevator_trim_tab);
262 htonf(fdm->left_flap);
263 htonf(fdm->right_flap);
264 htonf(fdm->left_aileron);
265 htonf(fdm->right_aileron);
267 htonf(fdm->nose_wheel);
268 htonf(fdm->speedbrake);
269 htonf(fdm->spoilers);
273 static void send_data( const MIDGpos pos, const MIDGatt att ) {
275 int fdmsize = sizeof( FGNetFDM );
277 // cout << "Running main loop" << endl;
282 midg2fg( pos, att, &fgfdm, &fgctrls );
283 len = fdm_sock.send(&fgfdm, fdmsize, 0);
287 void usage( const string &argv0 ) {
288 cout << "Usage: " << argv0 << endl;
289 cout << "\t[ --help ]" << endl;
290 cout << "\t[ --file <file_name>" << endl;
291 cout << "\t[ --hertz <hertz> ]" << endl;
292 cout << "\t[ --host <hostname> ]" << endl;
293 cout << "\t[ --broadcast ]" << endl;
294 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
295 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
296 cout << "\t[ --altitude-offset <meters> ]" << endl;
297 cout << "\t[ --skip-seconds <seconds> ]" << endl;
301 int main( int argc, char **argv ) {
303 string out_host = "localhost";
304 bool do_broadcast = false;
306 // process command line arguments
307 for ( int i = 1; i < argc; ++i ) {
308 if ( strcmp( argv[i], "--help" ) == 0 ) {
311 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
314 hertz = atof( argv[i] );
319 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
327 } else if ( strcmp( argv[i], "--serial" ) == 0 ) {
335 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
343 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
345 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
348 fdm_port = atoi( argv[i] );
353 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
356 ctrls_port = atoi( argv[i] );
361 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
364 alt_offset = atof( argv[i] );
369 } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
372 skip = atof( argv[i] );
383 // Setup up outgoing network connections
385 netInit( &argc,argv ); // We must call this before any other net stuff
387 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
388 cout << "error opening fdm output socket" << endl;
391 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
392 cout << "error opening ctrls output socket" << endl;
395 cout << "open net channels" << endl;
397 fdm_sock.setBlocking( false );
398 ctrls_sock.setBlocking( false );
399 cout << "blocking false" << endl;
401 if ( do_broadcast ) {
402 fdm_sock.setBroadcast( true );
403 ctrls_sock.setBroadcast( true );
406 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
408 cout << "error connecting to outgoing fdm port: " << out_host
409 << ":" << fdm_port << endl;
412 cout << "connected outgoing fdm socket" << endl;
414 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
416 cout << "error connecting to outgoing ctrls port: " << out_host
417 << ":" << ctrls_port << endl;
420 cout << "connected outgoing ctrls socket" << endl;
422 if ( file.length() ) {
423 // Load data from a track data
425 cout << "Loaded " << track.pos_size() << " position records." << endl;
426 cout << "Loaded " << track.att_size() << " attitude records." << endl;
428 int size = track.pos_size();
430 double current_time = track.get_pospt(0).get_seconds();
431 cout << "Track begin time is " << current_time << endl;
432 double end_time = track.get_pospt(size-1).get_seconds();
433 cout << "Track end time is " << end_time << endl;
434 cout << "Duration = " << end_time - current_time << endl;
436 // advance skip seconds forward
437 current_time += skip;
439 frame_us = 1000000.0 / hertz;
440 if ( frame_us < 0.0 ) {
444 SGTimeStamp start_time;
450 pos0 = pos1 = track.get_pospt( 0 );
453 att0 = att1 = track.get_attpt( 0 );
455 while ( current_time < end_time ) {
456 // cout << "current_time = " << current_time << " end_time = "
457 // << end_time << endl;
459 // Advance position pointer
460 while ( current_time > pos1.get_seconds()
461 && pos_count < track.pos_size() )
465 // cout << "count = " << count << endl;
466 pos1 = track.get_pospt( pos_count );
468 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
471 // Advance attitude pointer
472 while ( current_time > att1.get_seconds()
473 && att_count < track.att_size() )
477 // cout << "count = " << count << endl;
478 att1 = track.get_attpt( att_count );
480 // cout << "pos0 = " << pos0.get_seconds()
481 // << " pos1 = " << pos1.get_seconds() << endl;
484 if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
488 (current_time - pos0.get_seconds()) /
489 (pos1.get_seconds() - pos0.get_seconds());
491 // cout << "Percent = " << percent << endl;
493 if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
497 (current_time - att0.get_seconds()) /
498 (att1.get_seconds() - att0.get_seconds());
500 // cout << "Percent = " << percent << endl;
502 MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent );
503 MIDGatt att = MIDGInterpAtt( att0, att1, att_percent );
504 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
506 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
508 // cout << (double)current_time << " " << pos.lat_deg << ", "
509 // << pos.lon_deg << " " << att.yaw_deg << endl;
510 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
512 pos.lat_deg, pos.lon_deg, pos.altitude_msl,
513 att.yaw_rad * 180.0 / SG_PI,
514 att.pitch_rad * 180.0 / SG_PI,
515 att.roll_rad * 180.0 / SG_PI );
517 send_data( pos, att );
519 // Update the elapsed time.
520 static bool first_time = true;
522 last_time_stamp.stamp();
526 current_time_stamp.stamp();
528 double elapsed_us = current_time_stamp - last_time_stamp;
529 if ( elapsed_us < (frame_us - 2000) ) {
530 double requested_us = (frame_us - elapsed_us) - 2000 ;
531 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
533 current_time_stamp.stamp();
534 while ( current_time_stamp - last_time_stamp < frame_us ) {
535 current_time_stamp.stamp();
538 current_time += (frame_us / 1000000.0);
539 last_time_stamp = current_time_stamp;
542 cout << "Processed " << pos_count << " entries in "
543 << (current_time_stamp - start_time) / 1000000 << " seconds."
545 } else if ( serialdev.length() ) {
546 // process incoming data from the serial port
549 double current_time = 0.0;
554 uint32_t pos_time = 1;
555 uint32_t att_time = 1;
558 SGSerial input( serialdev, "115200" );
559 if ( !input.open( SG_IO_IN ) ) {
560 cout << "Cannot open file: " << file << endl;
564 while ( ! input.eof() ) {
565 // cout << "looking for next message ..." << endl;
566 int id = track.next_message( &input, &pos, &att );
570 if ( att.get_msec() > att_time ) {
571 att_time = att.get_msec();
572 current_time = att_time;
574 cout << "oops att back in time" << endl;
576 } else if ( id == 12 ) {
577 if ( pos.get_msec() > pos_time ) {
578 pos_time = pos.get_msec();
579 current_time = pos_time;
581 cout << "oops pos back in time" << endl;
585 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
587 pos.lat_deg, pos.lon_deg, pos.altitude_msl,
588 att.yaw_rad * 180.0 / SG_PI,
589 att.pitch_rad * 180.0 / SG_PI,
590 att.roll_rad * 180.0 / SG_PI );
592 send_data( pos, att );