]> git.mxchange.org Git - flightgear.git/blob - utils/GPSsmooth/MIDG_main.cxx
08981305b22df24188666de159f9d9e5c4b0373a
[flightgear.git] / utils / GPSsmooth / MIDG_main.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <iostream>
6 #include <string>
7
8 #include <plib/net.h>
9 #include <plib/sg.h>
10
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>
15
16 #include <Network/net_ctrls.hxx>
17 #include <Network/net_fdm.hxx>
18
19 #include "MIDG-II.hxx"
20
21
22 SG_USING_STD(cout);
23 SG_USING_STD(endl);
24 SG_USING_STD(string);
25
26
27 // Network channels
28 static netSocket fdm_sock, ctrls_sock;
29
30 // midg data
31 MIDGTrack track;
32
33 // Default ports
34 static int fdm_port = 5505;
35 static int ctrls_port = 5506;
36
37 // Default path
38 static string file = "";
39
40 // Master time counter
41 float sim_time = 0.0f;
42 double frame_us = 0.0f;
43
44 // sim control
45 SGTimeStamp last_time_stamp;
46 SGTimeStamp current_time_stamp;
47
48 // altitude offset
49 double alt_offset = 0.0;
50
51 // skip initial seconds
52 double skip = 0.0;
53
54 // for speed estimate
55 // double last_lat = 0.0, last_lon = 0.0;
56 // double kts_filter = 0.0;
57
58 bool inited = false;
59
60
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
69 // possible
70 static void htond (double &x)   
71 {
72     if ( sgIsLittleEndian() ) {
73         int    *Double_Overlay;
74         int     Holding_Buffer;
75     
76         Double_Overlay = (int *) &x;
77         Holding_Buffer = Double_Overlay [0];
78     
79         Double_Overlay [0] = htonl (Double_Overlay [1]);
80         Double_Overlay [1] = htonl (Holding_Buffer);
81     } else {
82         return;
83     }
84 }
85
86 // Float version
87 static void htonf (float &x)    
88 {
89     if ( sgIsLittleEndian() ) {
90         int    *Float_Overlay;
91         int     Holding_Buffer;
92     
93         Float_Overlay = (int *) &x;
94         Holding_Buffer = Float_Overlay [0];
95     
96         Float_Overlay [0] = htonl (Holding_Buffer);
97     } else {
98         return;
99     }
100 }
101
102
103 static void midg2fg( const MIDGpos pos, const MIDGatt att,
104                      FGNetFDM *fdm, FGNetCtrls *ctrls )
105 {
106     unsigned int i;
107
108     // Version sanity checking
109     fdm->version = FG_NET_FDM_VERSION;
110
111     // Aero parameters
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;
115     fdm->agl = -9999.0;
116     fdm->psi = att.yaw_rad; // heading
117     fdm->phi = att.roll_rad; // roll
118     fdm->theta = att.pitch_rad; // pitch;
119
120     fdm->phidot = 0.0;
121     fdm->thetadot = 0.0;
122     fdm->psidot = 0.0;
123
124     // estimate speed
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;
135
136     fdm->climb_rate = 0; // fps
137     // cout << "climb rate = " << aero->hdota << endl;
138     fdm->v_north = 0.0;
139     fdm->v_east = 0.0;
140     fdm->v_down = 0.0;
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;
145
146     fdm->A_X_pilot = 0.0;
147     fdm->A_Y_pilot = 0.0;
148     fdm->A_Z_pilot = 0.0 /* (should be -G) */;
149
150     // Engine parameters
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; }
157     fdm->rpm[0] = rpm;
158
159     fdm->fuel_flow[0] = 0.0;
160     fdm->egt[0] = 0.0;
161     // cout << "egt = " << aero->EGT << endl;
162     fdm->oil_temp[0] = 0.0;
163     fdm->oil_px[0] = 0.0;
164
165     // Consumables
166     fdm->num_tanks = 2;
167     fdm->fuel_quantity[0] = 0.0;
168     fdm->fuel_quantity[1] = 0.0;
169
170     // Gear and flaps
171     fdm->num_wheels = 3;
172     fdm->wow[0] = 0;
173     fdm->wow[1] = 0;
174     fdm->wow[2] = 0;
175
176     // the following really aren't used in this context
177     fdm->cur_time = 0;
178     fdm->warp = 0;
179     fdm->visibility = 0;
180
181     // cout << "Flap deflection = " << aero->dflap << endl;
182     fdm->left_flap = 0.0;
183     fdm->right_flap = 0.0;
184
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;
191     fdm->rudder = 0.0;
192     fdm->nose_wheel = 0.0;
193     fdm->speedbrake = 0.0;
194     fdm->spoilers = 0.0;
195
196     // Convert the net buffer to network format
197     fdm->version = htonl(fdm->version);
198
199     htond(fdm->longitude);
200     htond(fdm->latitude);
201     htond(fdm->altitude);
202     htonf(fdm->agl);
203     htonf(fdm->phi);
204     htonf(fdm->theta);
205     htonf(fdm->psi);
206     htonf(fdm->alpha);
207     htonf(fdm->beta);
208
209     htonf(fdm->phidot);
210     htonf(fdm->thetadot);
211     htonf(fdm->psidot);
212     htonf(fdm->vcas);
213     htonf(fdm->climb_rate);
214     htonf(fdm->v_north);
215     htonf(fdm->v_east);
216     htonf(fdm->v_down);
217     htonf(fdm->v_wind_body_north);
218     htonf(fdm->v_wind_body_east);
219     htonf(fdm->v_wind_body_down);
220
221     htonf(fdm->A_X_pilot);
222     htonf(fdm->A_Y_pilot);
223     htonf(fdm->A_Z_pilot);
224
225     htonf(fdm->stall_warning);
226     htonf(fdm->slip_deg);
227
228     for ( i = 0; i < fdm->num_engines; ++i ) {
229         fdm->eng_state[i] = htonl(fdm->eng_state[i]);
230         htonf(fdm->rpm[i]);
231         htonf(fdm->fuel_flow[i]);
232         htonf(fdm->egt[i]);
233         htonf(fdm->cht[i]);
234         htonf(fdm->mp_osi[i]);
235         htonf(fdm->tit[i]);
236         htonf(fdm->oil_temp[i]);
237         htonf(fdm->oil_px[i]);
238     }
239     fdm->num_engines = htonl(fdm->num_engines);
240
241     for ( i = 0; i < fdm->num_tanks; ++i ) {
242         htonf(fdm->fuel_quantity[i]);
243     }
244     fdm->num_tanks = htonl(fdm->num_tanks);
245
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]);
251     }
252     fdm->num_wheels = htonl(fdm->num_wheels);
253
254     fdm->cur_time = htonl( fdm->cur_time );
255     fdm->warp = htonl( fdm->warp );
256     htonf(fdm->visibility);
257
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);
264     htonf(fdm->rudder);
265     htonf(fdm->nose_wheel);
266     htonf(fdm->speedbrake);
267     htonf(fdm->spoilers);
268 }
269
270
271 static void send_data( const MIDGpos pos, const MIDGatt att ) {
272     int len;
273     int fdmsize = sizeof( FGNetFDM );
274
275     // cout << "Running main loop" << endl;
276
277     FGNetFDM fgfdm;
278     FGNetCtrls fgctrls;
279
280     midg2fg( pos, att, &fgfdm, &fgctrls );
281     len = fdm_sock.send(&fgfdm, fdmsize, 0);
282 }
283
284
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;
296 }
297
298
299 int main( int argc, char **argv ) {
300     double hertz = 60.0;
301     string out_host = "localhost";
302     bool do_broadcast = false;
303
304     // process command line arguments
305     for ( int i = 1; i < argc; ++i ) {
306         if ( strcmp( argv[i], "--help" ) == 0 ) {
307             usage( argv[0] );
308             exit( 0 );
309         } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
310             ++i;
311             if ( i < argc ) {
312                 hertz = atof( argv[i] );
313             } else {
314                 usage( argv[0] );
315                 exit( -1 );
316             }
317         } else if ( strcmp( argv[i], "--file" ) == 0 ) {
318             ++i;
319             if ( i < argc ) {
320                 file = argv[i];
321             } else {
322                 usage( argv[0] );
323                 exit( -1 );
324             }
325         } else if ( strcmp( argv[i], "--host" ) == 0 ) {
326             ++i;
327             if ( i < argc ) {
328                 out_host = argv[i];
329             } else {
330                 usage( argv[0] );
331                 exit( -1 );
332             }
333         } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
334           do_broadcast = true;
335         } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
336             ++i;
337             if ( i < argc ) {
338                 fdm_port = atoi( argv[i] );
339             } else {
340                 usage( argv[0] );
341                 exit( -1 );
342             }
343         } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
344             ++i;
345             if ( i < argc ) {
346                 ctrls_port = atoi( argv[i] );
347             } else {
348                 usage( argv[0] );
349                 exit( -1 );
350             }
351         } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
352             ++i;
353             if ( i < argc ) {
354                 alt_offset = atof( argv[i] );
355             } else {
356                 usage( argv[0] );
357                 exit( -1 );
358             }
359         } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
360             ++i;
361             if ( i < argc ) {
362                 skip = atof( argv[i] );
363             } else {
364                 usage( argv[0] );
365                 exit( -1 );
366             }
367         } else {
368             usage( argv[0] );
369             exit( -1 );
370         }
371     }
372
373     // Load the track data
374     if ( file == "" ) {
375         cout << "No track file specified" << endl;
376         exit(-1);
377     }
378     track.load( file );
379     cout << "Loaded " << track.pos_size() << " position records." << endl;
380     cout << "Loaded " << track.att_size() << " attitude records." << endl;
381
382     // Setup up outgoing network connections
383
384     netInit( &argc,argv ); // We must call this before any other net stuff
385
386     if ( ! fdm_sock.open( false ) ) {  // open a UDP socket
387         cout << "error opening fdm output socket" << endl;
388         return -1;
389     }
390     if ( ! ctrls_sock.open( false ) ) {  // open a UDP socket
391         cout << "error opening ctrls output socket" << endl;
392         return -1;
393     }
394     cout << "open net channels" << endl;
395
396     fdm_sock.setBlocking( false );
397     ctrls_sock.setBlocking( false );
398     cout << "blocking false" << endl;
399
400     if ( do_broadcast ) {
401         fdm_sock.setBroadcast( true );
402         ctrls_sock.setBroadcast( true );
403     }
404
405     if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
406         perror("connect");
407         cout << "error connecting to outgoing fdm port: " << out_host
408              << ":" << fdm_port << endl;
409         return -1;
410     }
411     cout << "connected outgoing fdm socket" << endl;
412
413     if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
414         perror("connect");
415         cout << "error connecting to outgoing ctrls port: " << out_host
416              << ":" << ctrls_port << endl;
417         return -1;
418     }
419     cout << "connected outgoing ctrls socket" << endl;
420
421     int size = track.pos_size();
422
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;
428
429     // advance skip seconds forward
430     current_time += skip;
431
432     frame_us = 1000000.0 / hertz;
433     if ( frame_us < 0.0 ) {
434         frame_us = 0.0;
435     }
436
437     SGTimeStamp start_time;
438     start_time.stamp();
439     int pos_count = 0;
440     int att_count = 0;
441
442     MIDGpos pos0, pos1;
443     pos0 = pos1 = track.get_pospt( 0 );
444     
445     MIDGatt att0, att1;
446     att0 = att1 = track.get_attpt( 0 );
447     
448     while ( current_time < end_time ) {
449         // cout << "current_time = " << current_time << " end_time = "
450         //      << end_time << endl;
451
452         // Advance position pointer
453         while ( current_time > pos1.get_seconds()
454                 && pos_count < track.pos_size() )
455         {
456             pos0 = pos1;
457             ++pos_count;
458             // cout << "count = " << count << endl;
459             pos1 = track.get_pospt( pos_count );
460         }
461         // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
462         //      << endl;
463
464         // Advance attitude pointer
465         while ( current_time > att1.get_seconds()
466                 && att_count < track.att_size() )
467         {
468             att0 = att1;
469             ++att_count;
470             // cout << "count = " << count << endl;
471             att1 = track.get_attpt( att_count );
472         }
473         //  cout << "pos0 = " << pos0.get_seconds()
474         // << " pos1 = " << pos1.get_seconds() << endl;
475
476         double pos_percent;
477         if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
478             pos_percent = 0.0;
479         } else {
480             pos_percent =
481                 (current_time - pos0.get_seconds()) /
482                 (pos1.get_seconds() - pos0.get_seconds());
483         }
484         // cout << "Percent = " << percent << endl;
485         double att_percent;
486         if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
487             att_percent = 0.0;
488         } else {
489             att_percent =
490                 (current_time - att0.get_seconds()) /
491                 (att1.get_seconds() - att0.get_seconds());
492         }
493         // cout << "Percent = " << percent << endl;
494
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
498         //      << endl;
499         // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
500         //      << endl;
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",
504                 current_time,
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 );
509
510         send_data( pos, att );
511
512         // Update the elapsed time.
513         static bool first_time = true;
514         if ( first_time ) {
515             last_time_stamp.stamp();
516             first_time = false;
517         }
518
519         current_time_stamp.stamp();
520         /* Convert to ms */
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) ) ;
525         }
526         current_time_stamp.stamp();
527         while ( current_time_stamp - last_time_stamp < frame_us ) {
528             current_time_stamp.stamp();
529         }
530
531         current_time += (frame_us / 1000000.0);
532         last_time_stamp = current_time_stamp;
533     }
534
535     cout << "Processed " << pos_count << " entries in "
536          << (current_time_stamp - start_time) / 1000000 << " seconds." << endl;
537
538     return 0;
539 }