#include <simgear/io/iochannel.hxx>
#include <simgear/math/sg_types.hxx>
#include <simgear/misc/props.hxx>
+#include <simgear/misc/sg_path.hxx>
#include <Main/fg_props.hxx>
#include <Main/globals.hxx>
}
+void FGATC610x::init_config() {
+#if defined( unix ) || defined( __CYGWIN__ )
+ // Next check home directory for .fgfsrc.hostname file
+ char *envp = ::getenv( "HOME" );
+ if ( envp != NULL ) {
+ SGPath atc610x_config( envp );
+ atc610x_config.append( ".fgfs-atc610x.xml" );
+ readProperties( atc610x_config.str(), globals->get_props() );
+ }
+#endif
+}
+
+
// Open and initialize ATC 610x hardware
bool FGATC610x::open() {
if ( is_enabled() ) {
return false;
}
+ // This loads the config parameters generated by "simcal"
+ init_config();
+
SG_LOG( SG_IO, SG_ALERT,
"Initializing ATC 610x hardware, please wait ..." );
xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
+ elevator_center = fgGetNode( "/input/atc610x/elevator/center", 0 );
+ elevator_min = fgGetNode( "/input/atc610x/elevator/min", 0 );
+ elevator_max = fgGetNode( "/input/atc610x/elevator/max", 0 );
+
+ ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", 0 );
+ ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", 0 );
+ ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", 0 );
+
+ rudder_center = fgGetNode( "/input/atc610x/rudder/center", 0 );
+ rudder_min = fgGetNode( "/input/atc610x/rudder/min", 0 );
+ rudder_max = fgGetNode( "/input/atc610x/rudder/max", 0 );
+
+ throttle_min = fgGetNode( "/input/atc610x/throttle/min", 0 );
+ throttle_max = fgGetNode( "/input/atc610x/throttle/max", 0 );
+
+ mixture_min = fgGetNode( "/input/atc610x/mixture/min", 0 );
+ mixture_max = fgGetNode( "/input/atc610x/mixture/max", 0 );
+
+ trim_center = fgGetNode( "/input/atc610x/trim/center", 0 );
+ trim_min = fgGetNode( "/input/atc610x/trim/min", 0 );
+ trim_max = fgGetNode( "/input/atc610x/trim/max", 0 );
+
+ nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", 0 );
+ nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", 0 );
+
+ nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", 0 );
+ nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", 0 );
+
return true;
}
#define ATC_ELEVATOR_CENTER 543
#define ATC_RUDDER_CENTER 519
+// scale a number between min and max (with center defined) to a scale
+// from -1.0 to 1.0
+static double scale( int center, int min, int max, int value ) {
+ // cout << center << " " << min << " " << max << " " << value << " ";
+ double result;
+ double range;
+
+ if ( value <= center ) {
+ range = center - min;
+ result = (value - center) / range;
+ } else {
+ range = max - center;
+ result = (value - center) / range;
+ }
+
+ if ( result < -1.0 ) result = -1.0;
+ if ( result > 1.0 ) result = 1.0;
+
+ // cout << result << endl;
+
+ return result;
+}
+
+
+// scale a number between min and max to a scale from 0.0 to 1.0
+static double scale( int min, int max, int value ) {
+ // cout << center << " " << min << " " << max << " " << value << " ";
+ double result;
+ double range;
+
+ range = max - min;
+ result = (value - min) / range;
+
+ if ( result < 0.0 ) result = 0.0;
+ if ( result > 1.0 ) result = 1.0;
+
+ // cout << result << endl;
+
+ return result;
+}
+
+
bool FGATC610x::do_analog_in() {
// Read raw data in byte form
ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
float tmp, tmp1, tmp2;
// aileron
- tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
+ tmp = scale( ailerons_center->getIntValue(), ailerons_min->getIntValue(),
+ ailerons_max->getIntValue(), analog_in_data[0] );
fgSetFloat( "/controls/aileron", tmp );
// cout << "aileron = " << analog_in_data[0] << " = " << tmp;
// elevator
- tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
- fgSetFloat( "/controls/elevator-trim", tmp );
+ tmp = -scale( elevator_center->getIntValue(), elevator_min->getIntValue(),
+ elevator_max->getIntValue(), analog_in_data[5] );
+ fgSetFloat( "/controls/elevator", tmp );
// cout << "trim = " << analog_in_data[4] << " = " << tmp;
- // trim
- tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
- fgSetFloat( "/controls/elevator", tmp );
+ // elevator trim
+ tmp = scale( trim_center->getIntValue(), trim_min->getIntValue(),
+ trim_max->getIntValue(), analog_in_data[4] );
+ fgSetFloat( "/controls/elevator-trim", tmp );
// cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
// mixture
- tmp = (float)analog_in_data[7] / 680.0f;
+ tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
+ analog_in_data[7] );
fgSetFloat( "/controls/mixture[0]", tmp );
fgSetFloat( "/controls/mixture[1]", tmp );
// throttle
- tmp = ((float)analog_in_data[8] - 141.0) / 632.0f;
+ tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
+ analog_in_data[8] );
fgSetFloat( "/controls/throttle[0]", tmp );
fgSetFloat( "/controls/throttle[1]", tmp );
#if 0
// rudder
- tmp = (float)(ATC_RUDDER_CENTER - analog_in_data[10]) / 145.0f;
+ tmp = scale( rudder_center->getIntValue(), rudder_min->getIntValue(),
+ rudder_max->getIntValue(), analog_in_data[10] );
fgSetFloat( "/controls/rudder", tmp );
#endif
unsigned char switch_data[ATC_SWITCH_BYTES];
float compass_position;
+
SGPropertyNode *mag_compass;
SGPropertyNode *dme_min, *dme_kt, *dme_nm;
SGPropertyNode *com1_freq, *com1_stby_freq;
SGPropertyNode *xpdr_fl_ann, *xpdr_alt_ann, *xpdr_gnd_ann, *xpdr_on_ann;
SGPropertyNode *xpdr_sby_ann, *xpdr_reply_ann;
+ // configuration values
+ SGPropertyNode *elevator_center, *elevator_min, *elevator_max;
+ SGPropertyNode *ailerons_center, *ailerons_min, *ailerons_max;
+ SGPropertyNode *rudder_center, *rudder_min, *rudder_max;
+ SGPropertyNode *throttle_min, *throttle_max;
+ SGPropertyNode *mixture_min, *mixture_max;
+ SGPropertyNode *trim_center, *trim_min, *trim_max;
+ SGPropertyNode *nav1vol_min, *nav1vol_max;
+ SGPropertyNode *nav2vol_min, *nav2vol_max;
+
int dme_switch;
bool do_analog_in();
~FGATC610x() { }
bool open();
+ void init_config();
bool process();