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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/compiler.h>
30 #if defined( unix ) || defined( __CYGWIN__ )
31 # include <sys/types.h>
32 # include <sys/stat.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/props/props_io.hxx>
47 #include <Main/fg_props.hxx>
49 #include "ATC-Inputs.hxx"
55 // Constructor: The _board parameter specifies which board to
56 // reference. Possible values are 0 or 1. The _config_file parameter
57 // specifies the location of the input config file (xml)
58 FGATCInput::FGATCInput( const int _board, const SGPath &_config_file ) :
60 ignore_flight_controls(NULL),
61 ignore_pedal_controls(NULL),
67 config = _config_file;
72 static void ATCReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
73 #if defined( unix ) || defined( __CYGWIN__ )
75 lseek( fd, 0, SEEK_SET );
77 int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
78 if ( result != ATC_ANAL_IN_BYTES ) {
79 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
86 // Read status of radio switches and knobs
87 static void ATCReadRadios( int fd, unsigned char *switch_data ) {
88 #if defined( unix ) || defined( __CYGWIN__ )
90 lseek( fd, 0, SEEK_SET );
92 int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
93 if ( result != ATC_RADIO_SWITCH_BYTES ) {
94 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
101 // Read switch inputs
102 static void ATCReadSwitches( int fd, unsigned char *switch_bytes ) {
103 #if defined( unix ) || defined( __CYGWIN__ )
105 lseek( fd, 0, SEEK_SET );
107 int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
108 if ( result != ATC_SWITCH_BYTES ) {
109 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
116 void FGATCInput::init_config() {
117 #if defined( unix ) || defined( __CYGWIN__ )
118 if ( config.str()[0] != '/' ) {
119 // not an absolute path, prepend the standard location
121 char *envp = ::getenv( "HOME" );
122 if ( envp != NULL ) {
124 tmp.append( ".atcflightsim" );
125 tmp.append( config.str() );
129 readProperties( config.str(), globals->get_props() );
134 // Open and initialize the ATC hardware
135 bool FGATCInput::open() {
137 SG_LOG( SG_IO, SG_ALERT, "This board is already open for input! "
142 // This loads the config parameters generated by "simcal"
145 SG_LOG( SG_IO, SG_ALERT,
146 "Initializing ATC input hardware, please wait ..." );
148 snprintf( analog_in_file, 256,
149 "/proc/atcflightsim/board%d/analog_in", board );
150 snprintf( radios_file, 256,
151 "/proc/atcflightsim/board%d/radios", board );
152 snprintf( switches_file, 256,
153 "/proc/atcflightsim/board%d/switches", board );
155 #if defined( unix ) || defined( __CYGWIN__ )
157 /////////////////////////////////////////////////////////////////////
158 // Open the /proc files
159 /////////////////////////////////////////////////////////////////////
161 analog_in_fd = ::open( analog_in_file, O_RDONLY );
162 if ( analog_in_fd == -1 ) {
163 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
165 snprintf( msg, 256, "Error opening %s", analog_in_file );
170 radios_fd = ::open( radios_file, O_RDWR );
171 if ( radios_fd == -1 ) {
172 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
174 snprintf( msg, 256, "Error opening %s", radios_file );
179 switches_fd = ::open( switches_file, O_RDONLY );
180 if ( switches_fd == -1 ) {
181 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
183 snprintf( msg, 256, "Error opening %s", switches_file );
190 /////////////////////////////////////////////////////////////////////
191 // Finished initing hardware
192 /////////////////////////////////////////////////////////////////////
194 SG_LOG( SG_IO, SG_ALERT,
195 "Done initializing ATC input hardware." );
199 /////////////////////////////////////////////////////////////////////
200 // Connect up to property values
201 /////////////////////////////////////////////////////////////////////
203 ignore_flight_controls
204 = fgGetNode( "/input/atcsim/ignore-flight-controls", true );
205 ignore_pedal_controls
206 = fgGetNode( "/input/atcsim/ignore-pedal-controls", true );
210 snprintf( base_name, 256, "/input/atc-board[%d]/analog-in", board );
211 analog_in_node = fgGetNode( base_name );
213 snprintf( base_name, 256, "/input/atc-board[%d]/radio-switches", board );
214 radio_in_node = fgGetNode( base_name );
216 snprintf( base_name, 256, "/input/atc-board[%d]/switches", board );
217 switches_node = fgGetNode( base_name );
223 /////////////////////////////////////////////////////////////////////
224 // Read analog inputs
225 /////////////////////////////////////////////////////////////////////
227 // scale a number between min and max (with center defined) to a scale
228 // from -1.0 to 1.0. The deadband value is symmetric, so specifying
229 // '1' will give you a deadband of +/-1
230 static double scale( int center, int deadband, int min, int max, int value ) {
231 // cout << center << " " << min << " " << max << " " << value << " ";
235 if ( value <= (center - deadband) ) {
236 range = (center - deadband) - min;
237 result = (value - (center - deadband)) / range;
238 } else if ( value >= (center + deadband) ) {
239 range = max - (center + deadband);
240 result = (value - (center + deadband)) / range;
245 if ( result < -1.0 ) result = -1.0;
246 if ( result > 1.0 ) result = 1.0;
248 // cout << result << endl;
254 // scale a number between min and max to a scale from 0.0 to 1.0
255 static double scale( int min, int max, int value ) {
256 // cout << center << " " << min << " " << max << " " << value << " ";
261 result = (value - min) / range;
263 if ( result < 0.0 ) result = 0.0;
264 if ( result > 1.0 ) result = 1.0;
266 // cout << result << endl;
272 static double clamp( double min, double max, double value ) {
273 double result = value;
275 if ( result < min ) result = min;
276 if ( result > max ) result = max;
278 // cout << result << endl;
284 static int tony_magic( int raw, int obs[3] ) {
290 if ( obs[2] >= 68 && obs[2] < 480 ) {
292 } else if ( obs[2] >= 480 ) {
297 } else if ( obs[1] < 68 ) {
300 } else if ( obs[2] < 30 ) {
301 if ( obs[1] >= 68 && obs[1] < 480 ) {
305 } else if ( obs[1] >= 480 ) {
307 if ( obs[0] < obs[1] ) {
315 } else if ( obs[1] > 980 ) {
316 if ( obs[2] <= 956 && obs[2] > 480 ) {
318 } else if ( obs[2] <= 480 ) {
323 } else if ( obs[1] > 956 ) {
326 } else if ( obs[2] > 980 ) {
327 if ( obs[1] <= 956 && obs[1] > 480 ) {
331 } else if ( obs[1] <= 480 ) {
333 if ( obs[0] > obs[1] ) {
342 if ( obs[1] < 480 && obs[2] > 480 ) {
343 // crossed gap going up
344 if ( obs[0] < obs[1] ) {
345 // caught a bogus intermediate value coming out of the gap
348 } else if ( obs[1] > 480 && obs[2] < 480 ) {
349 // crossed gap going down
350 if ( obs[0] > obs[1] ) {
351 // caught a bogus intermediate value coming out of the gap
354 } else if ( obs[0] > 480 && obs[1] < 480 && obs[2] < 480 ) {
355 // crossed the gap going down
356 if ( obs[1] > obs[2] ) {
357 // caught a bogus intermediate value coming out of the gap
360 } else if ( obs[0] < 480 && obs[1] > 480 && obs[2] > 480 ) {
361 // crossed the gap going up
362 if ( obs[1] < obs[2] ) {
363 // caught a bogus intermediate value coming out of the gap
367 result = obs[1] - obs[2];
368 if ( abs(result) > 400 ) {
376 // cout << " result = " << result << endl;
377 if ( result < -500 ) { result += 1024; }
378 if ( result > 500 ) { result -= 1024; }
384 static double instr_pot_filter( double ave, double val ) {
385 if ( fabs(ave - val) < 400 || fabs(val) < fabs(ave) ) {
386 return 0.5 * ave + 0.5 * val;
393 bool FGATCInput::do_analog_in() {
394 // Read raw data in byte form
395 ATCReadAnalogInputs( analog_in_fd, analog_in_bytes );
397 // Convert to integer values
398 for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
399 unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
400 unsigned char lo = analog_in_bytes[2 * channel + 1];
401 analog_in_data[channel] = hi * 256 + lo;
403 // printf("%02x %02x ", hi, lo );
404 // printf("%04d ", value );
407 // Process analog inputs
408 if ( analog_in_node != NULL ) {
409 for ( int i = 0; i < analog_in_node->nChildren(); ++i ) {
410 // read the next config entry from the property tree
412 SGPropertyNode *child = analog_in_node->getChild(i);
413 string cname = child->getName();
414 int index = child->getIndex();
418 vector <SGPropertyNode *> output_nodes;
425 if ( cname == "channel" ) {
426 SGPropertyNode *prop;
427 prop = child->getChild( "name" );
428 if ( prop != NULL ) {
429 name = prop->getStringValue();
431 prop = child->getChild( "type", 0 );
432 if ( prop != NULL ) {
433 type = prop->getStringValue();
435 prop = child->getChild( "type", 1 );
436 if ( prop != NULL ) {
437 subtype = prop->getStringValue();
440 while ( (prop = child->getChild("prop", j)) != NULL ) {
442 = fgGetNode( prop->getStringValue(), true );
443 output_nodes.push_back( tmp );
446 prop = child->getChild( "center" );
447 if ( prop != NULL ) {
448 center = prop->getIntValue();
450 prop = child->getChild( "min" );
451 if ( prop != NULL ) {
452 min = prop->getIntValue();
454 prop = child->getChild( "max" );
455 if ( prop != NULL ) {
456 max = prop->getIntValue();
458 prop = child->getChild( "deadband" );
459 if ( prop != NULL ) {
460 deadband = prop->getIntValue();
462 prop = child->getChild( "offset" );
463 if ( prop != NULL ) {
464 offset = prop->getFloatValue();
466 prop = child->getChild( "factor" );
467 if ( prop != NULL ) {
468 factor = prop->getFloatValue();
471 // Fetch the raw value
473 int raw_value = analog_in_data[index];
475 // Update the target properties
477 if ( type == "flight"
478 && !ignore_flight_controls->getBoolValue() )
480 if ( subtype != "pedals" ||
481 ( subtype == "pedals"
482 && !ignore_pedal_controls->getBoolValue() ) )
484 // "Cook" the raw value
485 float scaled_value = 0.0f;
487 scaled_value = scale( center, deadband,
488 min, max, raw_value );
490 scaled_value = scale( min, max, raw_value );
492 scaled_value *= factor;
493 scaled_value += offset;
495 // final sanity clamp
497 scaled_value = clamp( -1.0, 1.0, scaled_value );
499 scaled_value = clamp( 0.0, 1.0, scaled_value );
502 // update the property tree values
503 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
504 output_nodes[j]->setDoubleValue( scaled_value );
507 } else if ( type == "avionics-simple" ) {
508 // "Cook" the raw value
509 float scaled_value = 0.0f;
511 scaled_value = scale( center, deadband,
512 min, max, raw_value );
514 scaled_value = scale( min, max, raw_value );
516 scaled_value *= factor;
517 scaled_value += offset;
519 // final sanity clamp
521 scaled_value = clamp( -1.0, 1.0, scaled_value );
523 scaled_value = clamp( 0.0, 1.0, scaled_value );
526 // update the property tree values
527 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
528 output_nodes[j]->setDoubleValue( scaled_value );
530 } else if ( type == "avionics-resolver" ) {
531 // this type of analog input impliments a
532 // rotational knob. We first caclulate the amount
533 // of knob rotation (slightly complex to work with
534 // hardware specific goofiness) and then multiply
535 // that amount of movement by a scaling factor,
536 // and finally add the result to the original
539 bool do_init = false;
540 float scaled_value = 0.0f;
542 // fetch intermediate values from property tree
544 prop = child->getChild( "is-inited", 0 );
545 if ( prop == NULL ) {
547 prop = child->getChild( "is-inited", 0, true );
548 prop->setBoolValue( true );
552 for ( j = 0; j < 3; ++j ) {
553 prop = child->getChild( "raw", j, true );
555 raw[j] = analog_in_data[index];
557 raw[j] = prop->getIntValue();
561 // do Tony's magic to calculate knob movement
562 // based on current analog input position and
564 int diff = tony_magic( analog_in_data[index], raw );
566 // write raw intermediate values (updated by
567 // tony_magic()) back to property tree
568 for ( j = 0; j < 3; ++j ) {
569 prop = child->getChild( "raw", j, true );
570 prop->setIntValue( raw[j] );
573 // filter knob position
574 prop = child->getChild( "diff-average", 0, true );
575 double diff_ave = prop->getDoubleValue();
576 diff_ave = instr_pot_filter( diff_ave, diff );
577 prop->setDoubleValue( diff_ave );
579 // calculate value adjustment in real world units
580 scaled_value = diff_ave * factor;
582 // update the property tree values
583 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
584 float value = output_nodes[j]->getDoubleValue();
585 value += scaled_value;
587 prop = child->getChild( "min-clamp" );
588 if ( prop != NULL ) {
589 double min = prop->getDoubleValue();
590 if ( value < min ) { value = min; }
593 prop = child->getChild( "max-clamp" );
594 if ( prop != NULL ) {
595 double max = prop->getDoubleValue();
596 if ( value > max ) { value = max; }
599 prop = child->getChild( "compass-heading" );
600 if ( prop != NULL ) {
601 bool compass = prop->getBoolValue();
603 while ( value >= 360.0 ) { value -= 360.0; }
604 while ( value < 0.0 ) { value += 360.0; }
608 output_nodes[j]->setDoubleValue( value );
612 SG_LOG( SG_IO, SG_DEBUG, "Invalid channel type = "
616 SG_LOG( SG_IO, SG_DEBUG,
617 "Input config error, expecting 'channel' but found "
627 /////////////////////////////////////////////////////////////////////
628 // Read the switch positions
629 /////////////////////////////////////////////////////////////////////
631 // decode the packed switch data
632 static void update_switch_matrix(
634 unsigned char switch_data[ATC_SWITCH_BYTES],
635 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
637 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
638 unsigned char switches = switch_data[row];
640 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
641 switch_matrix[board][column][row] = switches & 1;
642 switches = switches >> 1;
647 bool FGATCInput::do_switches() {
649 ATCReadSwitches( switches_fd, switch_data );
651 // unpack the switch data
652 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
653 update_switch_matrix( board, switch_data, switch_matrix );
655 // Process the switch inputs
656 if ( switches_node != NULL ) {
657 for ( int i = 0; i < switches_node->nChildren(); ++i ) {
658 // read the next config entry from the property tree
660 SGPropertyNode *child = switches_node->getChild(i);
661 string cname = child->getName();
664 vector <SGPropertyNode *> output_nodes;
669 float scaled_value = 0.0f;
672 // get common options
674 SGPropertyNode *prop;
675 prop = child->getChild( "name" );
676 if ( prop != NULL ) {
677 name = prop->getStringValue();
679 prop = child->getChild( "type" );
680 if ( prop != NULL ) {
681 type = prop->getStringValue();
684 while ( (prop = child->getChild("prop", j)) != NULL ) {
686 = fgGetNode( prop->getStringValue(), true );
687 output_nodes.push_back( tmp );
690 prop = child->getChild( "factor" );
691 if ( prop != NULL ) {
692 factor = prop->getFloatValue();
694 prop = child->getChild( "invert" );
695 if ( prop != NULL ) {
696 invert = prop->getBoolValue();
698 prop = child->getChild( "steady-state-filter" );
699 if ( prop != NULL ) {
700 filter = prop->getIntValue();
703 // handle different types of switches
705 if ( cname == "switch" ) {
706 prop = child->getChild( "row" );
707 if ( prop != NULL ) {
708 row = prop->getIntValue();
710 prop = child->getChild( "col" );
711 if ( prop != NULL ) {
712 col = prop->getIntValue();
715 // Fetch the raw value
716 int raw_value = switch_matrix[board][row][col];
720 raw_value = !raw_value;
724 scaled_value = (float)raw_value * factor;
726 } else if ( cname == "combo-switch" ) {
727 float combo_value = 0.0f;
731 while ( (pos = child->getChild("position", k++)) != NULL ) {
732 // read the combo position entries from the property tree
734 prop = pos->getChild( "row" );
735 if ( prop != NULL ) {
736 row = prop->getIntValue();
738 prop = pos->getChild( "col" );
739 if ( prop != NULL ) {
740 col = prop->getIntValue();
742 prop = pos->getChild( "value" );
743 if ( prop != NULL ) {
744 combo_value = prop->getFloatValue();
747 // Fetch the raw value
748 int raw_value = switch_matrix[board][row][col];
749 // cout << "sm[" << board << "][" << row << "][" << col
750 // << "] = " << raw_value << endl;
753 // set scaled_value to the first combo_value
754 // that matches and jump out of loop.
755 scaled_value = combo_value;
761 scaled_value *= factor;
762 } else if ( cname == "additive-switch" ) {
763 float additive_value = 0.0f;
764 float increment = 0.0f;
768 while ( (pos = child->getChild("position", k++)) != NULL ) {
769 // read the combo position entries from the property tree
771 prop = pos->getChild( "row" );
772 if ( prop != NULL ) {
773 row = prop->getIntValue();
775 prop = pos->getChild( "col" );
776 if ( prop != NULL ) {
777 col = prop->getIntValue();
779 prop = pos->getChild( "value" );
780 if ( prop != NULL ) {
781 increment = prop->getFloatValue();
784 // Fetch the raw value
785 int raw_value = switch_matrix[board][row][col];
786 // cout << "sm[" << board << "][" << row << "][" << col
787 // << "] = " << raw_value << endl;
790 // set scaled_value to the first combo_value
791 // that matches and jump out of loop.
792 additive_value += increment;
797 scaled_value = additive_value * factor;
800 // handle filter request. The value of the switch must be
801 // steady-state for "n" frames before the property value
804 bool update_prop = true;
807 SGPropertyNode *fv = child->getChild( "filter-value", 0, true );
808 float filter_value = fv->getFloatValue();
809 SGPropertyNode *fc = child->getChild( "filter-count", 0, true );
810 int filter_count = fc->getIntValue();
812 if ( fabs(scaled_value - filter_value) < 0.0001 ) {
818 if ( filter_count < filter ) {
822 fv->setFloatValue( scaled_value );
823 fc->setIntValue( filter_count );
827 if ( type == "engine" || type == "flight" ) {
828 if ( ! ignore_flight_controls->getBoolValue() ) {
829 // update the property tree values
830 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
831 output_nodes[j]->setDoubleValue( scaled_value );
834 } else if ( type == "avionics" ) {
835 // update the property tree values
836 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
837 output_nodes[j]->setDoubleValue( scaled_value );
848 /////////////////////////////////////////////////////////////////////
849 // Read radio switches
850 /////////////////////////////////////////////////////////////////////
852 bool FGATCInput::do_radio_switches() {
854 ATCReadRadios( radios_fd, radio_switch_data );
856 // Process the radio switch/knob inputs
857 if ( radio_in_node != NULL ) {
858 for ( int i = 0; i < radio_in_node->nChildren(); ++i ) {
859 // read the next config entry from the property tree
861 SGPropertyNode *child = radio_in_node->getChild(i);
862 string cname = child->getName();
864 if ( cname == "switch" ) {
867 vector <SGPropertyNode *> output_nodes;
874 int scaled_value = 0;
875 // get common options
877 SGPropertyNode *prop;
878 prop = child->getChild( "name" );
879 if ( prop != NULL ) {
880 name = prop->getStringValue();
882 prop = child->getChild( "type" );
883 if ( prop != NULL ) {
884 type = prop->getStringValue();
887 while ( (prop = child->getChild("prop", j)) != NULL ) {
889 = fgGetNode( prop->getStringValue(), true );
890 output_nodes.push_back( tmp );
893 prop = child->getChild( "byte" );
894 if ( prop != NULL ) {
895 byte_num = prop->getIntValue();
897 prop = child->getChild( "right-shift" );
898 if ( prop != NULL ) {
899 right_shift = prop->getIntValue();
901 prop = child->getChild( "mask" );
902 if ( prop != NULL ) {
903 mask = prop->getIntValue();
905 prop = child->getChild( "factor" );
906 if ( prop != NULL ) {
907 factor = prop->getIntValue();
909 prop = child->getChild( "offset" );
910 if ( prop != NULL ) {
911 offset = prop->getIntValue();
913 prop = child->getChild( "invert" );
914 if ( prop != NULL ) {
915 invert = prop->getBoolValue();
918 // Fetch the raw value
920 = (radio_switch_data[byte_num] >> right_shift) & mask;
924 raw_value = !raw_value;
926 scaled_value = raw_value * factor + offset;
928 // update the property tree values
929 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
930 output_nodes[j]->setIntValue( scaled_value );
940 // process the hardware inputs. This code assumes the calling layer
941 // will lock the hardware.
942 bool FGATCInput::process() {
944 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for input! "
957 bool FGATCInput::close() {
959 #if defined( unix ) || defined( __CYGWIN__ )
967 result = ::close( analog_in_fd );
968 if ( result == -1 ) {
969 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
971 snprintf( msg, 256, "Error closing %s", analog_in_file );
976 result = ::close( radios_fd );
977 if ( result == -1 ) {
978 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
980 snprintf( msg, 256, "Error closing %s", radios_file );
985 result = ::close( switches_fd );
986 if ( result == -1 ) {
987 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
989 snprintf( msg, 256, "Error closing %s", switches_file );