]> git.mxchange.org Git - flightgear.git/blob - utils/GPSsmooth/gps_main.cxx
Initial revision of code to read MicroGear serial output and parse,
[flightgear.git] / utils / GPSsmooth / gps_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 * 2.0;
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     fdm->elevator = -theta_filter * 5.0;
192     fdm->elevator_trim_tab = 0.0;
193     fdm->left_flap = 0.0;
194     fdm->right_flap = 0.0;
195     fdm->left_aileron = phi_filter * 1.5;
196     fdm->right_aileron = phi_filter * 1.5;
197     fdm->rudder = 0.0;
198     fdm->nose_wheel = 0.0;
199     fdm->speedbrake = 0.0;
200     fdm->spoilers = 0.0;
201
202     // Convert the net buffer to network format
203     fdm->version = htonl(fdm->version);
204
205     htond(fdm->longitude);
206     htond(fdm->latitude);
207     htond(fdm->altitude);
208     htonf(fdm->agl);
209     htonf(fdm->phi);
210     htonf(fdm->theta);
211     htonf(fdm->psi);
212     htonf(fdm->alpha);
213     htonf(fdm->beta);
214
215     htonf(fdm->phidot);
216     htonf(fdm->thetadot);
217     htonf(fdm->psidot);
218     htonf(fdm->vcas);
219     htonf(fdm->climb_rate);
220     htonf(fdm->v_north);
221     htonf(fdm->v_east);
222     htonf(fdm->v_down);
223     htonf(fdm->v_wind_body_north);
224     htonf(fdm->v_wind_body_east);
225     htonf(fdm->v_wind_body_down);
226
227     htonf(fdm->A_X_pilot);
228     htonf(fdm->A_Y_pilot);
229     htonf(fdm->A_Z_pilot);
230
231     htonf(fdm->stall_warning);
232     htonf(fdm->slip_deg);
233
234     for ( i = 0; i < fdm->num_engines; ++i ) {
235         fdm->eng_state[i] = htonl(fdm->eng_state[i]);
236         htonf(fdm->rpm[i]);
237         htonf(fdm->fuel_flow[i]);
238         htonf(fdm->egt[i]);
239         htonf(fdm->cht[i]);
240         htonf(fdm->mp_osi[i]);
241         htonf(fdm->tit[i]);
242         htonf(fdm->oil_temp[i]);
243         htonf(fdm->oil_px[i]);
244     }
245     fdm->num_engines = htonl(fdm->num_engines);
246
247     for ( i = 0; i < fdm->num_tanks; ++i ) {
248         htonf(fdm->fuel_quantity[i]);
249     }
250     fdm->num_tanks = htonl(fdm->num_tanks);
251
252     for ( i = 0; i < fdm->num_wheels; ++i ) {
253         fdm->wow[i] = htonl(fdm->wow[i]);
254         htonf(fdm->gear_pos[i]);
255         htonf(fdm->gear_steer[i]);
256         htonf(fdm->gear_compression[i]);
257     }
258     fdm->num_wheels = htonl(fdm->num_wheels);
259
260     fdm->cur_time = htonl( fdm->cur_time );
261     fdm->warp = htonl( fdm->warp );
262     htonf(fdm->visibility);
263
264     htonf(fdm->elevator);
265     htonf(fdm->elevator_trim_tab);
266     htonf(fdm->left_flap);
267     htonf(fdm->right_flap);
268     htonf(fdm->left_aileron);
269     htonf(fdm->right_aileron);
270     htonf(fdm->rudder);
271     htonf(fdm->nose_wheel);
272     htonf(fdm->speedbrake);
273     htonf(fdm->spoilers);
274 }
275
276
277 static void send_data( const GPSPoint p ) {
278     int len;
279     int ctrlsize = sizeof( FGNetCtrls );
280     int fdmsize = sizeof( FGNetFDM );
281
282     // cout << "Running main loop" << endl;
283
284     FGNetFDM fgfdm;
285     FGNetCtrls fgctrls;
286
287     gps2fg( p, &fgfdm, &fgctrls );
288     len = fdm_sock.send(&fgfdm, fdmsize, 0);
289 }
290
291
292 void usage( const string &argv0 ) {
293     cout << "Usage: " << argv0 << endl;
294     cout << "\t[ --help ]" << endl;
295     cout << "\t[ --file <file_name>" << endl;
296     cout << "\t[ --hertz <hertz> ]" << endl;
297     cout << "\t[ --host <hostname> ]" << endl;
298     cout << "\t[ --broadcast ]" << endl;
299     cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
300     cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
301 }
302
303
304 int main( int argc, char **argv ) {
305     double hertz = 60.0;
306     string out_host = "localhost";
307     bool do_broadcast = false;
308
309     // process command line arguments
310     for ( int i = 1; i < argc; ++i ) {
311         if ( strcmp( argv[i], "--help" ) == 0 ) {
312             usage( argv[0] );
313             exit( 0 );
314         } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
315             ++i;
316             if ( i < argc ) {
317                 hertz = atof( argv[i] );
318             } else {
319                 usage( argv[0] );
320                 exit( -1 );
321             }
322         } else if ( strcmp( argv[i], "--file" ) == 0 ) {
323             ++i;
324             if ( i < argc ) {
325                 file = argv[i];
326             } else {
327                 usage( argv[0] );
328                 exit( -1 );
329             }
330         } else if ( strcmp( argv[i], "--host" ) == 0 ) {
331             ++i;
332             if ( i < argc ) {
333                 out_host = argv[i];
334             } else {
335                 usage( argv[0] );
336                 exit( -1 );
337             }
338         } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
339           do_broadcast = true;
340         } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
341             ++i;
342             if ( i < argc ) {
343                 fdm_port = atoi( argv[i] );
344             } else {
345                 usage( argv[0] );
346                 exit( -1 );
347             }
348         } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
349             ++i;
350             if ( i < argc ) {
351                 ctrls_port = atoi( argv[i] );
352             } else {
353                 usage( argv[0] );
354                 exit( -1 );
355             }
356         } else {
357             usage( argv[0] );
358             exit( -1 );
359         }
360     }
361
362     // Load the track data
363     if ( file == "" ) {
364         cout << "No track file specified" << endl;
365         exit(-1);
366     }
367     track.load( file );
368     cout << "Loaded " << track.size() << " records." << endl;
369
370     // Setup up outgoing network connections
371
372     netInit( &argc,argv ); // We must call this before any other net stuff
373
374     if ( ! fdm_sock.open( false ) ) {  // open a UDP socket
375         cout << "error opening fdm output socket" << endl;
376         return -1;
377     }
378     if ( ! ctrls_sock.open( false ) ) {  // open a UDP socket
379         cout << "error opening ctrls output socket" << endl;
380         return -1;
381     }
382     cout << "open net channels" << endl;
383
384     fdm_sock.setBlocking( false );
385     ctrls_sock.setBlocking( false );
386     cout << "blocking false" << endl;
387
388     if ( do_broadcast ) {
389         fdm_sock.setBroadcast( true );
390         ctrls_sock.setBroadcast( true );
391     }
392
393     if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
394         perror("connect");
395         cout << "error connecting to outgoing fdm port: " << out_host
396              << ":" << fdm_port << endl;
397         return -1;
398     }
399     cout << "connected outgoing fdm socket" << endl;
400
401     if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
402         perror("connect");
403         cout << "error connecting to outgoing ctrls port: " << out_host
404              << ":" << ctrls_port << endl;
405         return -1;
406     }
407     cout << "connected outgoing ctrls socket" << endl;
408
409     int size = track.size();
410
411     double current_time = track.get_point(0).get_time();
412     cout << "Track begin time is " << current_time << endl;
413     double end_time = track.get_point(size-1).get_time();
414     cout << "Track end time is " << end_time << endl;
415     cout << "Duration = " << end_time - current_time << endl;
416
417     double frame_us = 1000000.0 / hertz;
418     if ( frame_us < 0.0 ) {
419         frame_us = 0.0;
420     }
421
422     SGTimeStamp start_time;
423     start_time.stamp();
424     int count = 0;
425
426     GPSPoint p, p0, p1;
427     p0 = p1 = track.get_point( 0 );
428     
429     while ( current_time < end_time ) {
430         // cout << "current_time = " << current_time << " end_time = "
431         //      << end_time << endl;
432
433         if ( current_time > p1.get_time() ) {
434             p0 = p1;
435             ++count;
436             // cout << "count = " << count << endl;
437             p1 = track.get_point( count );
438         }
439         // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
440         //      << endl;
441
442         double percent;
443         if ( fabs(p1.get_time() - p0.get_time()) < 0.0001 ) {
444             percent = 0.0;
445         } else {
446             percent =
447                 (current_time - p0.get_time()) /
448                 (p1.get_time() - p0.get_time());
449         }
450         // cout << "Percent = " << percent << endl;
451
452         GPSPoint p = GPSInterpolate( p0, p1, percent );
453         // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg << endl;
454         // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg << endl;
455         cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
456
457         send_data( p );
458
459         // Update the elapsed time.
460         static bool first_time = true;
461         if ( first_time ) {
462             last_time_stamp.stamp();
463             first_time = false;
464         }
465
466         current_time_stamp.stamp();
467         /* Convert to ms */
468         double elapsed_us = current_time_stamp - last_time_stamp;
469         if ( elapsed_us < (frame_us - 2000) ) {
470             double requested_us = (frame_us - elapsed_us) - 2000 ;
471             ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
472         }
473         current_time_stamp.stamp();
474         while ( current_time_stamp - last_time_stamp < frame_us ) {
475             current_time_stamp.stamp();
476         }
477
478         current_time += (frame_us / 1000000.0);
479         last_time_stamp = current_time_stamp;
480     }
481
482     cout << "Processed " << count << " entries in "
483          << current_time_stamp - start_time << " seconds." << endl;
484
485     return 0;
486 }