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