Debian Codegraph @

utils/GPSsmooth/gps_main.cxx

raw symbols SCIP
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 <chrono>
12 #include <iostream>
13 #include <string>
14 #include <thread>
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 "GPSsmooth.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 "corrupted" 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_body_u = 0.0;
154 fdm->v_body_v = 0.0;
155 fdm->v_body_w = 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_body_u);
230 htonf(fdm->v_body_v);
231 htonf(fdm->v_body_w);
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 ctrlsize = sizeof( FGNetCtrls );
285 int fdmsize = sizeof( FGNetFDM );
286
287 // cout << "Running main loop" << endl;
288
289 FGNetFDM fgfdm;
290 FGNetCtrls fgctrls;
291
292 gps2fg( p, &fgfdm, &fgctrls );
293 fdm_sock.send(&fgfdm, fdmsize, 0);
294 }
295
296
297 void usage( const string &argv0 ) {
298 cout << "Usage: " << argv0 << endl;
299 cout << "\t[ --help ]" << endl;
300 cout << "\t[ --file <file_name>" << endl;
301 cout << "\t[ --hertz <hertz> ]" << endl;
302 cout << "\t[ --host <hostname> ]" << endl;
303 cout << "\t[ --broadcast ]" << endl;
304 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
305 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
306 }
307
308
309 int main( int argc, char **argv ) {
310 double hertz = 60.0;
311 string out_host = "localhost";
312 bool do_broadcast = false;
313
314 // process command line arguments
315 for ( int i = 1; i < argc; ++i ) {
316 if ( strcmp( argv[i], "--help" ) == 0 ) {
317 usage( argv[0] );
318 exit( 0 );
319 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
320 ++i;
321 if ( i < argc ) {
322 hertz = atof( argv[i] );
323 } else {
324 usage( argv[0] );
325 exit( -1 );
326 }
327 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
328 ++i;
329 if ( i < argc ) {
330 file = argv[i];
331 } else {
332 usage( argv[0] );
333 exit( -1 );
334 }
335 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
336 ++i;
337 if ( i < argc ) {
338 out_host = argv[i];
339 } else {
340 usage( argv[0] );
341 exit( -1 );
342 }
343 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
344 do_broadcast = true;
345 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
346 ++i;
347 if ( i < argc ) {
348 fdm_port = atoi( argv[i] );
349 } else {
350 usage( argv[0] );
351 exit( -1 );
352 }
353 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
354 ++i;
355 if ( i < argc ) {
356 ctrls_port = atoi( argv[i] );
357 } else {
358 usage( argv[0] );
359 exit( -1 );
360 }
361 } else {
362 usage( argv[0] );
363 exit( -1 );
364 }
365 }
366
367 // Load the track data
368 if ( file == "" ) {
369 cout << "No track file specified" << endl;
370 exit(-1);
371 }
372 track.load( file );
373 cout << "Loaded " << track.size() << " records." << endl;
374
375 // Setup up outgoing network connections
376
377 simgear::Socket::initSockets(); // We must call this before any other net stuff
378
379 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
380 cout << "error opening fdm output socket" << endl;
381 return -1;
382 }
383 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
384 cout << "error opening ctrls output socket" << endl;
385 return -1;
386 }
387 cout << "open net channels" << endl;
388
389 fdm_sock.setBlocking( false );
390 ctrls_sock.setBlocking( false );
391 cout << "blocking false" << endl;
392
393 if ( do_broadcast ) {
394 fdm_sock.setBroadcast( true );
395 ctrls_sock.setBroadcast( true );
396 }
397
398 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
399 perror("connect");
400 cout << "error connecting to outgoing fdm port: " << out_host
401 << ":" << fdm_port << endl;
402 return -1;
403 }
404 cout << "connected outgoing fdm socket" << endl;
405
406 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
407 perror("connect");
408 cout << "error connecting to outgoing ctrls port: " << out_host
409 << ":" << ctrls_port << endl;
410 return -1;
411 }
412 cout << "connected outgoing ctrls socket" << endl;
413
414 int size = track.size();
415
416 double current_time = track.get_point(0).get_time();
417 cout << "Track begin time is " << current_time << endl;
418 double end_time = track.get_point(size-1).get_time();
419 cout << "Track end time is " << end_time << endl;
420 cout << "Duration = " << end_time - current_time << endl;
421
422 double frame_us = 1000000.0 / hertz;
423 if ( frame_us < 0.0 ) {
424 frame_us = 0.0;
425 }
426
427 SGTimeStamp start_time;
428 start_time.stamp();
429 int count = 0;
430
431 GPSPoint p, p0, p1;
432 p0 = p1 = track.get_point( 0 );
433
434 while ( current_time < end_time ) {
435 // cout << "current_time = " << current_time << " end_time = "
436 // << end_time << endl;
437
438 if ( current_time > p1.get_time() ) {
439 p0 = p1;
440 ++count;
441 // cout << "count = " << count << endl;
442 p1 = track.get_point( count );
443 }
444 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
445 // << endl;
446
447 double percent;
448 if ( fabs(p1.get_time() - p0.get_time()) < 0.0001 ) {
449 percent = 0.0;
450 } else {
451 percent =
452 (current_time - p0.get_time()) /
453 (p1.get_time() - p0.get_time());
454 }
455 // cout << "Percent = " << percent << endl;
456
457 GPSPoint p = GPSInterpolate( p0, p1, percent );
458 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg << endl;
459 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg << endl;
460 cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
461
462 send_data( p );
463
464 // Update the elapsed time.
465 static bool first_time = true;
466 if ( first_time ) {
467 last_time_stamp.stamp();
468 first_time = false;
469 }
470
471 current_time_stamp.stamp();
472 /* Convert to ms */
473 double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
474 if ( elapsed_us < (frame_us - 2000) ) {
475 double requested_us = (frame_us - elapsed_us) - 2000 ;
476 std::this_thread::sleep_for(std::chrono::milliseconds((int) (requested_us / 1000.0)));
477 }
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 }