]> git.mxchange.org Git - flightgear.git/blob - utils/GPSsmooth/gps_main.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / utils / GPSsmooth / gps_main.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #ifdef HAVE_WINDOWS_H
6 #  include <windows.h>
7 #else
8 #  include <netinet/in.h>       // htonl() ntohl()
9 #endif
10
11 #include <iostream>
12 #include <string>
13
14 #include <plib/sg.h>
15
16 #include <simgear/io/lowlevel.hxx> // endian tests
17 #include <simgear/io/raw_socket.hxx>
18 #include <simgear/timing/timestamp.hxx>
19
20 #include <Network/net_ctrls.hxx>
21 #include <Network/net_fdm.hxx>
22
23 #include "gps.hxx"
24
25
26 using std::cout;
27 using std::endl;
28 using std::string;
29
30
31 // Network channels
32 static simgear::Socket fdm_sock, ctrls_sock;
33
34 // gps data
35 GPSTrack track;
36
37 // Default ports
38 static int fdm_port = 5505;
39 static int ctrls_port = 5506;
40
41 // Default path
42 static string file = "";
43
44 // Master time counter
45 float sim_time = 0.0f;
46
47 // sim control
48 SGTimeStamp last_time_stamp;
49 SGTimeStamp current_time_stamp;
50
51 bool inited = false;
52
53
54 // The function htond is defined this way due to the way some
55 // processors and OSes treat floating point values.  Some will raise
56 // an exception whenever a "bad" floating point value is loaded into a
57 // floating point register.  Solaris is notorious for this, but then
58 // so is LynxOS on the PowerPC.  By translating the data in place,
59 // there is no need to load a FP register with the "corruped" floating
60 // point value.  By doing the BIG_ENDIAN test, I can optimize the
61 // routine for big-endian processors so it can be as efficient as
62 // possible
63 static void htond (double &x)   
64 {
65     if ( sgIsLittleEndian() ) {
66         int    *Double_Overlay;
67         int     Holding_Buffer;
68     
69         Double_Overlay = (int *) &x;
70         Holding_Buffer = Double_Overlay [0];
71     
72         Double_Overlay [0] = htonl (Double_Overlay [1]);
73         Double_Overlay [1] = htonl (Holding_Buffer);
74     } else {
75         return;
76     }
77 }
78
79 // Float version
80 static void htonf (float &x)    
81 {
82     if ( sgIsLittleEndian() ) {
83         int    *Float_Overlay;
84         int     Holding_Buffer;
85     
86         Float_Overlay = (int *) &x;
87         Holding_Buffer = Float_Overlay [0];
88     
89         Float_Overlay [0] = htonl (Holding_Buffer);
90     } else {
91         return;
92     }
93 }
94
95
96 static void gps2fg( const GPSPoint p, FGNetFDM *fdm, FGNetCtrls *ctrls )
97 {
98     unsigned int i;
99
100     static double last_psi;
101     static double last_alt;
102     static double phi_filter = 0.0;
103     static double theta_filter = 0.0;
104
105     // Nan-be-gone
106     if ( phi_filter != phi_filter ) {
107         phi_filter = 0.0;
108     }
109     if ( theta_filter != theta_filter ) {
110         theta_filter = 0.0;
111     }
112
113     // Version sanity checking
114     fdm->version = FG_NET_FDM_VERSION;
115
116     // Aero parameters
117     fdm->longitude = p.lon_deg * SGD_DEGREES_TO_RADIANS;
118     fdm->latitude = p.lat_deg * SGD_DEGREES_TO_RADIANS;
119     fdm->altitude = p.altitude_msl;
120     fdm->agl = -9999.0;
121     fdm->psi = p.course_true; // heading
122
123     double diff = p.course_true - last_psi;
124     if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
125     if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
126     double phi = diff * 100.0;
127     if ( phi > 0.5*SGD_PI ) { phi = 0.5*SGD_PI; }
128     if ( phi < -0.5*SGD_PI ) { phi = -0.5*SGD_PI; }
129     phi_filter = 0.99*phi_filter + 0.01*phi;
130     fdm->phi = phi_filter;
131     last_psi = p.course_true;
132     // cout << p.course_true << endl;
133
134     diff = p.altitude_msl - last_alt;
135     if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
136     if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
137     double theta = diff * 2.0;
138     if ( theta > 0.5*SGD_PI ) { theta = 0.5*SGD_PI; }
139     if ( theta < -0.5*SGD_PI ) { theta = -0.5*SGD_PI; }
140     theta_filter = 0.99*theta_filter + 0.01*theta;
141     fdm->theta = theta_filter;
142     last_alt = p.altitude_msl;
143
144     fdm->phidot = 0.0;
145     fdm->thetadot = 0.0;
146     fdm->psidot = 0.0;
147     fdm->vcas = p.speed_kts;
148     fdm->climb_rate = 0; // fps
149     // cout << "climb rate = " << aero->hdota << endl;
150     fdm->v_north = 0.0;
151     fdm->v_east = 0.0;
152     fdm->v_down = 0.0;
153     fdm->v_wind_body_north = 0.0;
154     fdm->v_wind_body_east = 0.0;
155     fdm->v_wind_body_down = 0.0;
156     fdm->stall_warning = 0.0;
157
158     fdm->A_X_pilot = 0.0;
159     fdm->A_Y_pilot = 0.0;
160     fdm->A_Z_pilot = 0.0 /* (should be -G) */;
161
162     // Engine parameters
163     fdm->num_engines = 1;
164     fdm->eng_state[0] = 2;
165     // cout << "state = " << fdm->eng_state[0] << endl;
166     double rpm = ((p.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
167     if ( rpm < 0.0 ) { rpm = 0.0; }
168     if ( rpm > 3000.0 ) { rpm = 3000.0; }
169     fdm->rpm[0] = rpm;
170
171     fdm->fuel_flow[0] = 0.0;
172     fdm->egt[0] = 0.0;
173     // cout << "egt = " << aero->EGT << endl;
174     fdm->oil_temp[0] = 0.0;
175     fdm->oil_px[0] = 0.0;
176
177     // Consumables
178     fdm->num_tanks = 2;
179     fdm->fuel_quantity[0] = 0.0;
180     fdm->fuel_quantity[1] = 0.0;
181
182     // Gear and flaps
183     fdm->num_wheels = 3;
184     fdm->wow[0] = 0;
185     fdm->wow[1] = 0;
186     fdm->wow[2] = 0;
187
188     // the following really aren't used in this context
189     fdm->cur_time = 0;
190     fdm->warp = 0;
191     fdm->visibility = 0;
192
193     // cout << "Flap deflection = " << aero->dflap << endl;
194     fdm->left_flap = 0.0;
195     fdm->right_flap = 0.0;
196
197     fdm->elevator = -theta_filter * 5.0;
198     fdm->elevator_trim_tab = 0.0;
199     fdm->left_flap = 0.0;
200     fdm->right_flap = 0.0;
201     fdm->left_aileron = phi_filter * 1.5;
202     fdm->right_aileron = phi_filter * 1.5;
203     fdm->rudder = 0.0;
204     fdm->nose_wheel = 0.0;
205     fdm->speedbrake = 0.0;
206     fdm->spoilers = 0.0;
207
208     // Convert the net buffer to network format
209     fdm->version = htonl(fdm->version);
210
211     htond(fdm->longitude);
212     htond(fdm->latitude);
213     htond(fdm->altitude);
214     htonf(fdm->agl);
215     htonf(fdm->phi);
216     htonf(fdm->theta);
217     htonf(fdm->psi);
218     htonf(fdm->alpha);
219     htonf(fdm->beta);
220
221     htonf(fdm->phidot);
222     htonf(fdm->thetadot);
223     htonf(fdm->psidot);
224     htonf(fdm->vcas);
225     htonf(fdm->climb_rate);
226     htonf(fdm->v_north);
227     htonf(fdm->v_east);
228     htonf(fdm->v_down);
229     htonf(fdm->v_wind_body_north);
230     htonf(fdm->v_wind_body_east);
231     htonf(fdm->v_wind_body_down);
232
233     htonf(fdm->A_X_pilot);
234     htonf(fdm->A_Y_pilot);
235     htonf(fdm->A_Z_pilot);
236
237     htonf(fdm->stall_warning);
238     htonf(fdm->slip_deg);
239
240     for ( i = 0; i < fdm->num_engines; ++i ) {
241         fdm->eng_state[i] = htonl(fdm->eng_state[i]);
242         htonf(fdm->rpm[i]);
243         htonf(fdm->fuel_flow[i]);
244         htonf(fdm->egt[i]);
245         htonf(fdm->cht[i]);
246         htonf(fdm->mp_osi[i]);
247         htonf(fdm->tit[i]);
248         htonf(fdm->oil_temp[i]);
249         htonf(fdm->oil_px[i]);
250     }
251     fdm->num_engines = htonl(fdm->num_engines);
252
253     for ( i = 0; i < fdm->num_tanks; ++i ) {
254         htonf(fdm->fuel_quantity[i]);
255     }
256     fdm->num_tanks = htonl(fdm->num_tanks);
257
258     for ( i = 0; i < fdm->num_wheels; ++i ) {
259         fdm->wow[i] = htonl(fdm->wow[i]);
260         htonf(fdm->gear_pos[i]);
261         htonf(fdm->gear_steer[i]);
262         htonf(fdm->gear_compression[i]);
263     }
264     fdm->num_wheels = htonl(fdm->num_wheels);
265
266     fdm->cur_time = htonl( fdm->cur_time );
267     fdm->warp = htonl( fdm->warp );
268     htonf(fdm->visibility);
269
270     htonf(fdm->elevator);
271     htonf(fdm->elevator_trim_tab);
272     htonf(fdm->left_flap);
273     htonf(fdm->right_flap);
274     htonf(fdm->left_aileron);
275     htonf(fdm->right_aileron);
276     htonf(fdm->rudder);
277     htonf(fdm->nose_wheel);
278     htonf(fdm->speedbrake);
279     htonf(fdm->spoilers);
280 }
281
282
283 static void send_data( const GPSPoint p ) {
284     int len;
285 //    int ctrlsize = sizeof( FGNetCtrls );
286     int fdmsize = sizeof( FGNetFDM );
287
288     // cout << "Running main loop" << endl;
289
290     FGNetFDM fgfdm;
291     FGNetCtrls fgctrls;
292
293     gps2fg( p, &fgfdm, &fgctrls );
294     len = fdm_sock.send(&fgfdm, fdmsize, 0);
295 }
296
297
298 void usage( const string &argv0 ) {
299     cout << "Usage: " << argv0 << endl;
300     cout << "\t[ --help ]" << endl;
301     cout << "\t[ --file <file_name>" << endl;
302     cout << "\t[ --hertz <hertz> ]" << endl;
303     cout << "\t[ --host <hostname> ]" << endl;
304     cout << "\t[ --broadcast ]" << endl;
305     cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
306     cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
307 }
308
309
310 int main( int argc, char **argv ) {
311     double hertz = 60.0;
312     string out_host = "localhost";
313     bool do_broadcast = false;
314
315     // process command line arguments
316     for ( int i = 1; i < argc; ++i ) {
317         if ( strcmp( argv[i], "--help" ) == 0 ) {
318             usage( argv[0] );
319             exit( 0 );
320         } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
321             ++i;
322             if ( i < argc ) {
323                 hertz = atof( argv[i] );
324             } else {
325                 usage( argv[0] );
326                 exit( -1 );
327             }
328         } else if ( strcmp( argv[i], "--file" ) == 0 ) {
329             ++i;
330             if ( i < argc ) {
331                 file = argv[i];
332             } else {
333                 usage( argv[0] );
334                 exit( -1 );
335             }
336         } else if ( strcmp( argv[i], "--host" ) == 0 ) {
337             ++i;
338             if ( i < argc ) {
339                 out_host = argv[i];
340             } else {
341                 usage( argv[0] );
342                 exit( -1 );
343             }
344         } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
345           do_broadcast = true;
346         } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
347             ++i;
348             if ( i < argc ) {
349                 fdm_port = atoi( argv[i] );
350             } else {
351                 usage( argv[0] );
352                 exit( -1 );
353             }
354         } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
355             ++i;
356             if ( i < argc ) {
357                 ctrls_port = atoi( argv[i] );
358             } else {
359                 usage( argv[0] );
360                 exit( -1 );
361             }
362         } else {
363             usage( argv[0] );
364             exit( -1 );
365         }
366     }
367
368     // Load the track data
369     if ( file == "" ) {
370         cout << "No track file specified" << endl;
371         exit(-1);
372     }
373     track.load( file );
374     cout << "Loaded " << track.size() << " records." << endl;
375
376     // Setup up outgoing network connections
377
378     simgear::Socket::initSockets(); // We must call this before any other net stuff
379
380     if ( ! fdm_sock.open( false ) ) {  // open a UDP socket
381         cout << "error opening fdm output socket" << endl;
382         return -1;
383     }
384     if ( ! ctrls_sock.open( false ) ) {  // open a UDP socket
385         cout << "error opening ctrls output socket" << endl;
386         return -1;
387     }
388     cout << "open net channels" << endl;
389
390     fdm_sock.setBlocking( false );
391     ctrls_sock.setBlocking( false );
392     cout << "blocking false" << endl;
393
394     if ( do_broadcast ) {
395         fdm_sock.setBroadcast( true );
396         ctrls_sock.setBroadcast( true );
397     }
398
399     if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
400         perror("connect");
401         cout << "error connecting to outgoing fdm port: " << out_host
402              << ":" << fdm_port << endl;
403         return -1;
404     }
405     cout << "connected outgoing fdm socket" << endl;
406
407     if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
408         perror("connect");
409         cout << "error connecting to outgoing ctrls port: " << out_host
410              << ":" << ctrls_port << endl;
411         return -1;
412     }
413     cout << "connected outgoing ctrls socket" << endl;
414
415     int size = track.size();
416
417     double current_time = track.get_point(0).get_time();
418     cout << "Track begin time is " << current_time << endl;
419     double end_time = track.get_point(size-1).get_time();
420     cout << "Track end time is " << end_time << endl;
421     cout << "Duration = " << end_time - current_time << endl;
422
423     double frame_us = 1000000.0 / hertz;
424     if ( frame_us < 0.0 ) {
425         frame_us = 0.0;
426     }
427
428     SGTimeStamp start_time;
429     start_time.stamp();
430     int count = 0;
431
432     GPSPoint p, p0, p1;
433     p0 = p1 = track.get_point( 0 );
434     
435     while ( current_time < end_time ) {
436         // cout << "current_time = " << current_time << " end_time = "
437         //      << end_time << endl;
438
439         if ( current_time > p1.get_time() ) {
440             p0 = p1;
441             ++count;
442             // cout << "count = " << count << endl;
443             p1 = track.get_point( count );
444         }
445         // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
446         //      << endl;
447
448         double percent;
449         if ( fabs(p1.get_time() - p0.get_time()) < 0.0001 ) {
450             percent = 0.0;
451         } else {
452             percent =
453                 (current_time - p0.get_time()) /
454                 (p1.get_time() - p0.get_time());
455         }
456         // cout << "Percent = " << percent << endl;
457
458         GPSPoint p = GPSInterpolate( p0, p1, percent );
459         // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg << endl;
460         // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg << endl;
461         cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
462
463         send_data( p );
464
465         // Update the elapsed time.
466         static bool first_time = true;
467         if ( first_time ) {
468             last_time_stamp.stamp();
469             first_time = false;
470         }
471
472         current_time_stamp.stamp();
473         /* Convert to ms */
474         double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
475         if ( elapsed_us < (frame_us - 2000) ) {
476             double requested_us = (frame_us - elapsed_us) - 2000 ;
477             ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
478         }
479         current_time_stamp.stamp();
480         while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
481             current_time_stamp.stamp();
482         }
483
484         current_time += (frame_us / 1000000.0);
485         last_time_stamp = current_time_stamp;
486     }
487
488     cout << "Processed " << count << " entries in "
489          << current_time_stamp - start_time << " seconds." << endl;
490
491     return 0;
492 }