1 // ATC-Inputs.hxx -- Translate ATC hardware inputs to FGFS properties
3 // Written by Curtis Olson, started November 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
30 #if defined( unix ) || defined( __CYGWIN__ )
31 # include <sys/types.h>
32 # include <sys/stat.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/misc/sg_path.hxx>
44 #include <Main/fg_props.hxx>
46 #include "ATC-Inputs.hxx"
52 // Constructor: The _board parameter specifies which board to
53 // reference. Possible values are 0 or 1. The _config_file parameter
54 // specifies the location of the input config file (xml)
55 FGATCInput::FGATCInput( const int _board, const SGPath &_config_file ) :
57 ignore_flight_controls(NULL),
58 ignore_pedal_controls(NULL),
64 config = _config_file;
69 static void ATCReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
70 #if defined( unix ) || defined( __CYGWIN__ )
72 lseek( fd, 0, SEEK_SET );
74 int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
75 if ( result != ATC_ANAL_IN_BYTES ) {
76 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
83 // Read status of radio switches and knobs
84 static void ATCReadRadios( int fd, unsigned char *switch_data ) {
85 #if defined( unix ) || defined( __CYGWIN__ )
87 lseek( fd, 0, SEEK_SET );
89 int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
90 if ( result != ATC_RADIO_SWITCH_BYTES ) {
91 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
99 static void ATCReadSwitches( int fd, unsigned char *switch_bytes ) {
100 #if defined( unix ) || defined( __CYGWIN__ )
102 lseek( fd, 0, SEEK_SET );
104 int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
105 if ( result != ATC_SWITCH_BYTES ) {
106 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
113 void FGATCInput::init_config() {
114 #if defined( unix ) || defined( __CYGWIN__ )
115 if ( config.str()[0] != '/' ) {
116 // not an absolute path, prepend the standard location
118 char *envp = ::getenv( "HOME" );
119 if ( envp != NULL ) {
121 tmp.append( ".atcflightsim" );
122 tmp.append( config.str() );
126 readProperties( config.str(), globals->get_props() );
131 // Open and initialize the ATC hardware
132 bool FGATCInput::open() {
134 SG_LOG( SG_IO, SG_ALERT, "This board is already open for input! "
139 // This loads the config parameters generated by "simcal"
142 SG_LOG( SG_IO, SG_ALERT,
143 "Initializing ATC input hardware, please wait ..." );
145 snprintf( analog_in_file, 256,
146 "/proc/atcflightsim/board%d/analog_in", board );
147 snprintf( radios_file, 256,
148 "/proc/atcflightsim/board%d/radios", board );
149 snprintf( switches_file, 256,
150 "/proc/atcflightsim/board%d/switches", board );
152 #if defined( unix ) || defined( __CYGWIN__ )
154 /////////////////////////////////////////////////////////////////////
155 // Open the /proc files
156 /////////////////////////////////////////////////////////////////////
158 analog_in_fd = ::open( analog_in_file, O_RDONLY );
159 if ( analog_in_fd == -1 ) {
160 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
162 snprintf( msg, 256, "Error opening %s", analog_in_file );
167 radios_fd = ::open( radios_file, O_RDWR );
168 if ( radios_fd == -1 ) {
169 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
171 snprintf( msg, 256, "Error opening %s", radios_file );
176 switches_fd = ::open( switches_file, O_RDONLY );
177 if ( switches_fd == -1 ) {
178 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
180 snprintf( msg, 256, "Error opening %s", switches_file );
187 /////////////////////////////////////////////////////////////////////
188 // Finished initing hardware
189 /////////////////////////////////////////////////////////////////////
191 SG_LOG( SG_IO, SG_ALERT,
192 "Done initializing ATC input hardware." );
196 /////////////////////////////////////////////////////////////////////
197 // Connect up to property values
198 /////////////////////////////////////////////////////////////////////
200 ignore_flight_controls
201 = fgGetNode( "/input/atcsim/ignore-flight-controls", true );
202 ignore_pedal_controls
203 = fgGetNode( "/input/atcsim/ignore-pedal-controls", true );
207 snprintf( base_name, 256, "/input/atc-board[%d]/analog-in", board );
208 analog_in_node = fgGetNode( base_name );
210 snprintf( base_name, 256, "/input/atc-board[%d]/radio-switches", board );
211 radio_in_node = fgGetNode( base_name );
213 snprintf( base_name, 256, "/input/atc-board[%d]/switches", board );
214 switches_node = fgGetNode( base_name );
220 /////////////////////////////////////////////////////////////////////
221 // Read analog inputs
222 /////////////////////////////////////////////////////////////////////
224 // scale a number between min and max (with center defined) to a scale
225 // from -1.0 to 1.0. The deadband value is symmetric, so specifying
226 // '1' will give you a deadband of +/-1
227 static double scale( int center, int deadband, int min, int max, int value ) {
228 // cout << center << " " << min << " " << max << " " << value << " ";
232 if ( value <= (center - deadband) ) {
233 range = (center - deadband) - min;
234 result = (value - (center - deadband)) / range;
235 } else if ( value >= (center + deadband) ) {
236 range = max - (center + deadband);
237 result = (value - (center + deadband)) / range;
242 if ( result < -1.0 ) result = -1.0;
243 if ( result > 1.0 ) result = 1.0;
245 // cout << result << endl;
251 // scale a number between min and max to a scale from 0.0 to 1.0
252 static double scale( int min, int max, int value ) {
253 // cout << center << " " << min << " " << max << " " << value << " ";
258 result = (value - min) / range;
260 if ( result < 0.0 ) result = 0.0;
261 if ( result > 1.0 ) result = 1.0;
263 // cout << result << endl;
269 static int tony_magic( int raw, int obs[3] ) {
275 if ( obs[2] >= 68 && obs[2] < 480 ) {
277 } else if ( obs[2] >= 480 ) {
282 } else if ( obs[1] < 68 ) {
285 } else if ( obs[2] < 30 ) {
286 if ( obs[1] >= 68 && obs[1] < 480 ) {
290 } else if ( obs[1] >= 480 ) {
292 if ( obs[0] < obs[1] ) {
300 } else if ( obs[1] > 980 ) {
301 if ( obs[2] <= 956 && obs[2] > 480 ) {
303 } else if ( obs[2] <= 480 ) {
308 } else if ( obs[1] > 956 ) {
311 } else if ( obs[2] > 980 ) {
312 if ( obs[1] <= 956 && obs[1] > 480 ) {
316 } else if ( obs[1] <= 480 ) {
318 if ( obs[0] > obs[1] ) {
327 if ( obs[1] < 480 && obs[2] > 480 ) {
328 // crossed gap going up
329 if ( obs[0] < obs[1] ) {
330 // caught a bogus intermediate value coming out of the gap
333 } else if ( obs[1] > 480 && obs[2] < 480 ) {
334 // crossed gap going down
335 if ( obs[0] > obs[1] ) {
336 // caught a bogus intermediate value coming out of the gap
339 } else if ( obs[0] > 480 && obs[1] < 480 && obs[2] < 480 ) {
340 // crossed the gap going down
341 if ( obs[1] > obs[2] ) {
342 // caught a bogus intermediate value coming out of the gap
345 } else if ( obs[0] < 480 && obs[1] > 480 && obs[2] > 480 ) {
346 // crossed the gap going up
347 if ( obs[1] < obs[2] ) {
348 // caught a bogus intermediate value coming out of the gap
352 result = obs[1] - obs[2];
353 if ( abs(result) > 400 ) {
361 // cout << " result = " << result << endl;
362 if ( result < -500 ) { result += 1024; }
363 if ( result > 500 ) { result -= 1024; }
369 static double instr_pot_filter( double ave, double val ) {
370 if ( fabs(ave - val) < 400 || fabs(val) < fabs(ave) ) {
371 return 0.5 * ave + 0.5 * val;
378 bool FGATCInput::do_analog_in() {
379 // Read raw data in byte form
380 ATCReadAnalogInputs( analog_in_fd, analog_in_bytes );
382 // Convert to integer values
383 for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
384 unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
385 unsigned char lo = analog_in_bytes[2 * channel + 1];
386 analog_in_data[channel] = hi * 256 + lo;
388 // printf("%02x %02x ", hi, lo );
389 // printf("%04d ", value );
392 // Process analog inputs
393 if ( analog_in_node != NULL ) {
394 for ( int i = 0; i < analog_in_node->nChildren(); ++i ) {
395 // read the next config entry from the property tree
397 SGPropertyNode *child = analog_in_node->getChild(i);
398 string cname = child->getName();
399 int index = child->getIndex();
403 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
409 if ( cname == "channel" ) {
410 SGPropertyNode *prop;
411 prop = child->getChild( "name" );
412 if ( prop != NULL ) {
413 name = prop->getStringValue();
415 prop = child->getChild( "type", 0 );
416 if ( prop != NULL ) {
417 type = prop->getStringValue();
419 prop = child->getChild( "type", 1 );
420 if ( prop != NULL ) {
421 subtype = prop->getStringValue();
424 while ( (prop = child->getChild("prop", j)) != NULL ) {
426 = fgGetNode( prop->getStringValue(), true );
427 output_nodes.push_back( tmp );
430 prop = child->getChild( "center" );
431 if ( prop != NULL ) {
432 center = prop->getIntValue();
434 prop = child->getChild( "min" );
435 if ( prop != NULL ) {
436 min = prop->getIntValue();
438 prop = child->getChild( "max" );
439 if ( prop != NULL ) {
440 max = prop->getIntValue();
442 prop = child->getChild( "deadband" );
443 if ( prop != NULL ) {
444 deadband = prop->getIntValue();
446 prop = child->getChild( "factor" );
447 if ( prop != NULL ) {
448 factor = prop->getFloatValue();
451 // Fetch the raw value
453 int raw_value = analog_in_data[index];
455 // Update the target properties
457 if ( type == "flight"
458 && !ignore_flight_controls->getBoolValue() )
460 if ( subtype != "pedals" ||
461 ( subtype == "pedals"
462 && !ignore_pedal_controls->getBoolValue() ) )
464 // "Cook" the raw value
465 float scaled_value = 0.0f;
467 scaled_value = scale( center, deadband,
468 min, max, raw_value );
470 scaled_value = scale( min, max, raw_value );
472 scaled_value *= factor;
474 // update the property tree values
475 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
476 output_nodes[j]->setDoubleValue( scaled_value );
479 } else if ( type == "avionics-simple" ) {
480 // "Cook" the raw value
481 float scaled_value = 0.0f;
483 scaled_value = scale( center, deadband,
484 min, max, raw_value );
486 scaled_value = scale( min, max, raw_value );
488 scaled_value *= factor;
490 // update the property tree values
491 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
492 output_nodes[j]->setDoubleValue( scaled_value );
494 } else if ( type == "avionics-resolver" ) {
495 // this type of analog input impliments a
496 // rotational knob. We first caclulate the amount
497 // of knob rotation (slightly complex to work with
498 // hardware specific goofiness) and then multiply
499 // that amount of movement by a scaling factor,
500 // and finally add the result to the original
503 bool do_init = false;
504 float scaled_value = 0.0f;
506 // fetch intermediate values from property tree
508 prop = child->getChild( "is-inited", 0 );
509 if ( prop == NULL ) {
511 prop = child->getChild( "is-inited", 0, true );
512 prop->setBoolValue( true );
516 for ( j = 0; j < 3; ++j ) {
517 prop = child->getChild( "raw", j, true );
519 raw[j] = analog_in_data[index];
521 raw[j] = prop->getIntValue();
525 // do Tony's magic to calculate knob movement
526 // based on current analog input position and
528 int diff = tony_magic( analog_in_data[index], raw );
530 // write raw intermediate values (updated by
531 // tony_magic()) back to property tree
532 for ( j = 0; j < 3; ++j ) {
533 prop = child->getChild( "raw", j, true );
534 prop->setIntValue( raw[j] );
537 // filter knob position
538 prop = child->getChild( "diff-average", 0, true );
539 double diff_ave = prop->getDoubleValue();
540 diff_ave = instr_pot_filter( diff_ave, diff );
541 prop->setDoubleValue( diff_ave );
543 // calculate value adjustment in real world units
544 scaled_value = diff_ave * factor;
546 // update the property tree values
547 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
548 float value = output_nodes[j]->getDoubleValue();
549 value += scaled_value;
551 prop = child->getChild( "min-clamp" );
552 if ( prop != NULL ) {
553 double min = prop->getDoubleValue();
554 if ( value < min ) { value = min; }
557 prop = child->getChild( "max-clamp" );
558 if ( prop != NULL ) {
559 double max = prop->getDoubleValue();
560 if ( value > max ) { value = max; }
563 prop = child->getChild( "compass-heading" );
564 if ( prop != NULL ) {
565 bool compass = prop->getBoolValue();
567 while ( value >= 360.0 ) { value -= 360.0; }
568 while ( value < 0.0 ) { value += 360.0; }
572 output_nodes[j]->setDoubleValue( value );
576 SG_LOG( SG_IO, SG_DEBUG, "Invalid channel type = "
580 SG_LOG( SG_IO, SG_DEBUG,
581 "Input config error, expecting 'channel' but found "
591 /////////////////////////////////////////////////////////////////////
592 // Read the switch positions
593 /////////////////////////////////////////////////////////////////////
595 // decode the packed switch data
596 static void update_switch_matrix(
598 unsigned char switch_data[ATC_SWITCH_BYTES],
599 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
601 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
602 unsigned char switches = switch_data[row];
604 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
605 switch_matrix[board][column][row] = switches & 1;
606 switches = switches >> 1;
611 bool FGATCInput::do_switches() {
613 ATCReadSwitches( switches_fd, switch_data );
615 // unpack the switch data
616 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
617 update_switch_matrix( board, switch_data, switch_matrix );
619 // Process the switch inputs
620 if ( switches_node != NULL ) {
621 for ( int i = 0; i < switches_node->nChildren(); ++i ) {
622 // read the next config entry from the property tree
624 SGPropertyNode *child = switches_node->getChild(i);
625 string cname = child->getName();
628 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
633 float scaled_value = 0.0f;
636 // get common options
638 SGPropertyNode *prop;
639 prop = child->getChild( "name" );
640 if ( prop != NULL ) {
641 name = prop->getStringValue();
643 prop = child->getChild( "type" );
644 if ( prop != NULL ) {
645 type = prop->getStringValue();
648 while ( (prop = child->getChild("prop", j)) != NULL ) {
650 = fgGetNode( prop->getStringValue(), true );
651 output_nodes.push_back( tmp );
654 prop = child->getChild( "factor" );
655 if ( prop != NULL ) {
656 factor = prop->getFloatValue();
658 prop = child->getChild( "invert" );
659 if ( prop != NULL ) {
660 invert = prop->getBoolValue();
662 prop = child->getChild( "steady-state-filter" );
663 if ( prop != NULL ) {
664 filter = prop->getIntValue();
667 // handle different types of switches
669 if ( cname == "switch" ) {
670 prop = child->getChild( "row" );
671 if ( prop != NULL ) {
672 row = prop->getIntValue();
674 prop = child->getChild( "col" );
675 if ( prop != NULL ) {
676 col = prop->getIntValue();
679 // Fetch the raw value
680 int raw_value = switch_matrix[board][row][col];
684 raw_value = !raw_value;
688 scaled_value = (float)raw_value * factor;
690 } else if ( cname == "combo-switch" ) {
691 float combo_value = 0.0f;
695 while ( (pos = child->getChild("position", k++)) != NULL ) {
696 // read the combo position entries from the property tree
698 prop = pos->getChild( "row" );
699 if ( prop != NULL ) {
700 row = prop->getIntValue();
702 prop = pos->getChild( "col" );
703 if ( prop != NULL ) {
704 col = prop->getIntValue();
706 prop = pos->getChild( "value" );
707 if ( prop != NULL ) {
708 combo_value = prop->getFloatValue();
711 // Fetch the raw value
712 int raw_value = switch_matrix[board][row][col];
713 // cout << "sm[" << board << "][" << row << "][" << col
714 // << "] = " << raw_value << endl;
717 // set scaled_value to the first combo_value
718 // that matches and jump out of loop.
719 scaled_value = combo_value;
725 scaled_value *= factor;
726 } else if ( cname == "additive-switch" ) {
727 float additive_value = 0.0f;
728 float increment = 0.0f;
732 while ( (pos = child->getChild("position", k++)) != NULL ) {
733 // read the combo position entries from the property tree
735 prop = pos->getChild( "row" );
736 if ( prop != NULL ) {
737 row = prop->getIntValue();
739 prop = pos->getChild( "col" );
740 if ( prop != NULL ) {
741 col = prop->getIntValue();
743 prop = pos->getChild( "value" );
744 if ( prop != NULL ) {
745 increment = prop->getFloatValue();
748 // Fetch the raw value
749 int raw_value = switch_matrix[board][row][col];
750 // cout << "sm[" << board << "][" << row << "][" << col
751 // << "] = " << raw_value << endl;
754 // set scaled_value to the first combo_value
755 // that matches and jump out of loop.
756 additive_value += increment;
761 scaled_value = additive_value * factor;
764 // handle filter request. The value of the switch must be
765 // steady-state for "n" frames before the property value
768 bool update_prop = true;
771 SGPropertyNode *fv = child->getChild( "filter-value", 0, true );
772 float filter_value = fv->getFloatValue();
773 SGPropertyNode *fc = child->getChild( "filter-count", 0, true );
774 int filter_count = fc->getIntValue();
776 if ( fabs(scaled_value - filter_value) < 0.0001 ) {
782 if ( filter_count < filter ) {
786 fv->setFloatValue( scaled_value );
787 fc->setIntValue( filter_count );
791 if ( type == "engine" || type == "flight" ) {
792 if ( ! ignore_flight_controls->getBoolValue() ) {
793 // update the property tree values
794 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
795 output_nodes[j]->setDoubleValue( scaled_value );
798 } else if ( type == "avionics" ) {
799 // update the property tree values
800 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
801 output_nodes[j]->setDoubleValue( scaled_value );
812 /////////////////////////////////////////////////////////////////////
813 // Read radio switches
814 /////////////////////////////////////////////////////////////////////
816 bool FGATCInput::do_radio_switches() {
818 ATCReadRadios( radios_fd, radio_switch_data );
820 // Process the radio switch/knob inputs
821 if ( radio_in_node != NULL ) {
822 for ( int i = 0; i < radio_in_node->nChildren(); ++i ) {
823 // read the next config entry from the property tree
825 SGPropertyNode *child = radio_in_node->getChild(i);
826 string cname = child->getName();
828 if ( cname == "switch" ) {
831 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
838 int scaled_value = 0;
839 // get common options
841 SGPropertyNode *prop;
842 prop = child->getChild( "name" );
843 if ( prop != NULL ) {
844 name = prop->getStringValue();
846 prop = child->getChild( "type" );
847 if ( prop != NULL ) {
848 type = prop->getStringValue();
851 while ( (prop = child->getChild("prop", j)) != NULL ) {
853 = fgGetNode( prop->getStringValue(), true );
854 output_nodes.push_back( tmp );
857 prop = child->getChild( "byte" );
858 if ( prop != NULL ) {
859 byte_num = prop->getIntValue();
861 prop = child->getChild( "right-shift" );
862 if ( prop != NULL ) {
863 right_shift = prop->getIntValue();
865 prop = child->getChild( "mask" );
866 if ( prop != NULL ) {
867 mask = prop->getIntValue();
869 prop = child->getChild( "factor" );
870 if ( prop != NULL ) {
871 factor = prop->getIntValue();
873 prop = child->getChild( "offset" );
874 if ( prop != NULL ) {
875 offset = prop->getIntValue();
877 prop = child->getChild( "invert" );
878 if ( prop != NULL ) {
879 invert = prop->getBoolValue();
882 // Fetch the raw value
884 = (radio_switch_data[byte_num] >> right_shift) & mask;
888 raw_value = !raw_value;
890 scaled_value = raw_value * factor + offset;
892 // update the property tree values
893 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
894 output_nodes[j]->setIntValue( scaled_value );
904 // process the hardware inputs. This code assumes the calling layer
905 // will lock the hardware.
906 bool FGATCInput::process() {
908 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for input! "
921 bool FGATCInput::close() {
923 #if defined( unix ) || defined( __CYGWIN__ )
931 result = ::close( analog_in_fd );
932 if ( result == -1 ) {
933 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
935 snprintf( msg, 256, "Error closing %s", analog_in_file );
940 result = ::close( radios_fd );
941 if ( result == -1 ) {
942 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
944 snprintf( msg, 256, "Error closing %s", radios_file );
949 result = ::close( switches_fd );
950 if ( result == -1 ) {
951 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
953 snprintf( msg, 256, "Error closing %s", switches_file );