# include <config.h>
#endif
+#ifdef HAVE_WINDOWS_H
+# include <windows.h>
+#else
+# include <netinet/in.h> // htonl() ntohl()
+#endif
+
#include <iostream>
#include <string>
-#include <plib/net.h>
#include <plib/sg.h>
#include <simgear/constants.h>
#include <simgear/io/lowlevel.hxx> // endian tests
#include <simgear/io/sg_file.hxx>
#include <simgear/io/sg_serial.hxx>
+#include <simgear/io/raw_socket.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/timing/timestamp.hxx>
#include "MIDG-II.hxx"
-SG_USING_STD(cout);
-SG_USING_STD(endl);
-SG_USING_STD(string);
+using std::cout;
+using std::endl;
+using std::string;
// Network channels
-static netSocket fdm_sock, ctrls_sock;
+static simgear::Socket fdm_sock, ctrls_sock;
// midg data
MIDGTrack track;
// point value. By doing the BIG_ENDIAN test, I can optimize the
// routine for big-endian processors so it can be as efficient as
// possible
-static void htond (double &x)
+static void htond (double &x)
{
if ( sgIsLittleEndian() ) {
int *Double_Overlay;
int Holding_Buffer;
-
+
Double_Overlay = (int *) &x;
Holding_Buffer = Double_Overlay [0];
-
+
Double_Overlay [0] = htonl (Double_Overlay [1]);
Double_Overlay [1] = htonl (Holding_Buffer);
} else {
}
// Float version
-static void htonf (float &x)
+static void htonf (float &x)
{
if ( sgIsLittleEndian() ) {
int *Float_Overlay;
int Holding_Buffer;
-
+
Float_Overlay = (int *) &x;
Holding_Buffer = Float_Overlay [0];
-
+
Float_Overlay [0] = htonl (Holding_Buffer);
} else {
return;
fdm->v_north = 0.0;
fdm->v_east = 0.0;
fdm->v_down = 0.0;
- fdm->v_wind_body_north = 0.0;
- fdm->v_wind_body_east = 0.0;
- fdm->v_wind_body_down = 0.0;
+ fdm->v_body_u = 0.0;
+ fdm->v_body_v = 0.0;
+ fdm->v_body_w = 0.0;
fdm->stall_warning = 0.0;
fdm->A_X_pilot = 0.0;
htonf(fdm->v_north);
htonf(fdm->v_east);
htonf(fdm->v_down);
- htonf(fdm->v_wind_body_north);
- htonf(fdm->v_wind_body_east);
- htonf(fdm->v_wind_body_down);
+ htonf(fdm->v_body_u);
+ htonf(fdm->v_body_v);
+ htonf(fdm->v_body_w);
htonf(fdm->A_X_pilot);
htonf(fdm->A_Y_pilot);
static void send_data( const MIDGpos pos, const MIDGatt att ) {
- int len;
int fdmsize = sizeof( FGNetFDM );
- // cout << "Running main loop" << endl;
-
FGNetFDM fgfdm;
FGNetCtrls fgctrls;
midg2fg( pos, att, &fgfdm, &fgctrls );
- len = fdm_sock.send(&fgfdm, fdmsize, 0);
+ fdm_sock.send(&fgfdm, fdmsize, 0);
}
// Setup up outgoing network connections
- netInit( &argc,argv ); // We must call this before any other net stuff
+ simgear::Socket::initSockets(); // We must call this before any other net stuff
if ( ! fdm_sock.open( false ) ) { // open a UDP socket
cout << "error opening fdm output socket" << endl;
MIDGpos pos0, pos1;
pos0 = pos1 = track.get_pospt( 0 );
-
+
MIDGatt att0, att1;
att0 = att1 = track.get_attpt( 0 );
-
+
while ( current_time < end_time ) {
// cout << "current_time = " << current_time << " end_time = "
// << end_time << endl;
// << endl;
// cout << (double)current_time << " " << pos.lat_deg << ", "
// << pos.lon_deg << " " << att.yaw_deg << endl;
+ if ( pos.lat_deg > -500 ) {
printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
current_time,
pos.lat_deg, pos.lon_deg, pos.altitude_msl,
att.yaw_rad * 180.0 / SG_PI,
att.pitch_rad * 180.0 / SG_PI,
att.roll_rad * 180.0 / SG_PI );
+ }
send_data( pos, att );
current_time_stamp.stamp();
/* Convert to ms */
- double elapsed_us = current_time_stamp - last_time_stamp;
+ double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
if ( elapsed_us < (frame_us - 2000) ) {
double requested_us = (frame_us - elapsed_us) - 2000 ;
ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
}
current_time_stamp.stamp();
- while ( current_time_stamp - last_time_stamp < frame_us ) {
+ while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
current_time_stamp.stamp();
}
}
cout << "Processed " << pos_count << " entries in "
- << (current_time_stamp - start_time) / 1000000 << " seconds."
+ << current_time_stamp - start_time << " seconds."
<< endl;
} else if ( serialdev.length() ) {
// process incoming data from the serial port
int count = 0;
- double current_time = 0.0;
MIDGpos pos;
MIDGatt att;
uint32_t att_time = 1;
// open the serial port device
- SGSerial input( serialdev, "115200" );
- if ( !input.open( SG_IO_IN ) ) {
+ SGSerialPort input( serialdev, 115200 );
+ if ( !input.is_enabled() ) {
cout << "Cannot open: " << serialdev << endl;
return false;
}
return false;
}
- while ( ! input.eof() ) {
+ while ( input.is_enabled() ) {
// cout << "looking for next message ..." << endl;
int id = track.next_message( &input, &output, &pos, &att );
+ cout << "message id = " << id << endl;
count++;
if ( id == 10 ) {
if ( att.get_msec() > att_time ) {
att_time = att.get_msec();
- current_time = att_time;
+ //current_time = att_time;
} else {
cout << "oops att back in time" << endl;
}
} else if ( id == 12 ) {
if ( pos.get_msec() > pos_time ) {
pos_time = pos.get_msec();
- current_time = pos_time;
+ //current_time = pos_time;
} else {
cout << "oops pos back in time" << endl;
}
}
- printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
- current_time,
- pos.lat_deg, pos.lon_deg, pos.altitude_msl,
- att.yaw_rad * 180.0 / SG_PI,
- att.pitch_rad * 180.0 / SG_PI,
- att.roll_rad * 180.0 / SG_PI );
+ if ( pos.lat_deg > -500 ) {
+ // printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
+ // current_time,
+ // pos.lat_deg, pos.lon_deg, pos.altitude_msl,
+ // att.yaw_rad * 180.0 / SG_PI,
+ // att.pitch_rad * 180.0 / SG_PI,
+ // att.roll_rad * 180.0 / SG_PI );
+ }
send_data( pos, att );
}