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>
38 #include <simgear/debug/logstream.hxx>
40 #include <Main/fg_props.hxx>
42 #include "ATC-Inputs.hxx"
48 // Constructor: The _board parameter specifies which board to
49 // reference. Possible values are 0 or 1. The _config_file parameter
50 // specifies the location of the input config file (xml)
51 FGATCInput::FGATCInput( const int _board, const SGPath &_config_file ) :
53 ignore_flight_controls(NULL),
54 ignore_pedal_controls(NULL),
60 config = _config_file;
65 static void ATCReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
66 #if defined( unix ) || defined( __CYGWIN__ )
68 lseek( fd, 0, SEEK_SET );
70 int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
71 if ( result != ATC_ANAL_IN_BYTES ) {
72 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
79 // Read status of radio switches and knobs
80 static void ATCReadRadios( int fd, unsigned char *switch_data ) {
81 #if defined( unix ) || defined( __CYGWIN__ )
83 lseek( fd, 0, SEEK_SET );
85 int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
86 if ( result != ATC_RADIO_SWITCH_BYTES ) {
87 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
95 static void ATCReadSwitches( int fd, unsigned char *switch_bytes ) {
96 #if defined( unix ) || defined( __CYGWIN__ )
98 lseek( fd, 0, SEEK_SET );
100 int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
101 if ( result != ATC_SWITCH_BYTES ) {
102 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
109 void FGATCInput::init_config() {
110 #if defined( unix ) || defined( __CYGWIN__ )
111 if ( config.str()[0] != '/' ) {
112 // not an absolute path, prepend the standard location
114 char *envp = ::getenv( "HOME" );
115 if ( envp != NULL ) {
117 tmp.append( ".atcflightsim" );
118 tmp.append( config.str() );
122 readProperties( config.str(), globals->get_props() );
127 // Open and initialize the ATC hardware
128 bool FGATCInput::open() {
130 SG_LOG( SG_IO, SG_ALERT, "This board is already open for input! "
135 // This loads the config parameters generated by "simcal"
138 SG_LOG( SG_IO, SG_ALERT,
139 "Initializing ATC 610x hardware, please wait ..." );
141 snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
142 snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
143 snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
144 snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
146 #if defined( unix ) || defined( __CYGWIN__ )
148 /////////////////////////////////////////////////////////////////////
149 // Open the /proc files
150 /////////////////////////////////////////////////////////////////////
152 lock_fd = ::open( lock_file, O_RDWR );
153 if ( lock_fd == -1 ) {
154 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
156 snprintf( msg, 256, "Error opening %s", lock_file );
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 610x 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
229 static double scale( int center, int min, int max, int value ) {
230 // cout << center << " " << min << " " << max << " " << value << " ";
234 if ( value <= center ) {
235 range = center - min;
236 result = (value - center) / range;
238 range = max - center;
239 result = (value - center) / 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();
408 if ( cname == "channel" ) {
409 SGPropertyNode *prop;
410 prop = child->getChild( "name" );
411 if ( prop != NULL ) {
412 name = prop->getStringValue();
414 prop = child->getChild( "type", 0 );
415 if ( prop != NULL ) {
416 type = prop->getStringValue();
418 prop = child->getChild( "type", 1 );
419 if ( prop != NULL ) {
420 subtype = prop->getStringValue();
423 while ( (prop = child->getChild("prop", j)) != NULL ) {
425 = fgGetNode( prop->getStringValue(), true );
426 output_nodes.push_back( tmp );
429 prop = child->getChild( "center" );
430 if ( prop != NULL ) {
431 center = prop->getIntValue();
433 prop = child->getChild( "min" );
434 if ( prop != NULL ) {
435 min = prop->getIntValue();
437 prop = child->getChild( "max" );
438 if ( prop != NULL ) {
439 max = prop->getIntValue();
441 prop = child->getChild( "factor" );
442 if ( prop != NULL ) {
443 factor = prop->getFloatValue();
446 // Fetch the raw value
448 int raw_value = analog_in_data[index];
450 // Update the target properties
452 if ( type == "flight"
453 && !ignore_flight_controls->getBoolValue() )
455 if ( subtype != "pedals" ||
456 ( subtype == "pedals"
457 && !ignore_pedal_controls->getBoolValue() ) )
459 // "Cook" the raw value
460 float scaled_value = 0.0f;
462 scaled_value = scale( center, min, max, raw_value );
464 scaled_value = scale( min, max, raw_value );
466 scaled_value *= factor;
468 // update the property tree values
469 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
470 output_nodes[j]->setDoubleValue( scaled_value );
473 } else if ( type == "avionics-simple" ) {
474 // "Cook" the raw value
475 float scaled_value = 0.0f;
477 scaled_value = scale( center, min, max, raw_value );
479 scaled_value = scale( min, max, raw_value );
481 scaled_value *= factor;
483 // update the property tree values
484 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
485 output_nodes[j]->setDoubleValue( scaled_value );
487 } else if ( type == "avionics-resolver" ) {
488 // this type of analog input impliments a
489 // rotational knob. We first caclulate the amount
490 // of knob rotation (slightly complex to work with
491 // hardware specific goofiness) and then multiply
492 // that amount of movement by a scaling factor,
493 // and finally add the result to the original
496 bool do_init = false;
497 float scaled_value = 0.0f;
499 // fetch intermediate values from property tree
501 prop = child->getChild( "is-inited", 0 );
502 if ( prop == NULL ) {
504 prop = child->getChild( "is-inited", 0, true );
505 prop->setBoolValue( true );
509 for ( j = 0; j < 3; ++j ) {
510 prop = child->getChild( "raw", j, true );
512 raw[j] = analog_in_data[index];
514 raw[j] = prop->getIntValue();
518 // do Tony's magic to calculate knob movement
519 // based on current analog input position and
521 int diff = tony_magic( analog_in_data[index], raw );
523 // write raw intermediate values (updated by
524 // tony_magic()) back to property tree
525 for ( j = 0; j < 3; ++j ) {
526 prop = child->getChild( "raw", j, true );
527 prop->setIntValue( raw[j] );
530 // filter knob position
531 prop = child->getChild( "diff-average", 0, true );
532 double diff_ave = prop->getDoubleValue();
533 diff_ave = instr_pot_filter( diff_ave, diff );
534 prop->setDoubleValue( diff_ave );
536 // calculate value adjustment in real world units
537 scaled_value = diff_ave * factor;
539 // update the property tree values
540 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
541 float value = output_nodes[j]->getDoubleValue();
542 value += scaled_value;
544 prop = child->getChild( "min-clamp" );
545 if ( prop != NULL ) {
546 double min = prop->getDoubleValue();
547 if ( value < min ) { value = min; }
550 prop = child->getChild( "max-clamp" );
551 if ( prop != NULL ) {
552 double max = prop->getDoubleValue();
553 if ( value > max ) { value = max; }
556 prop = child->getChild( "compass-heading" );
557 if ( prop != NULL ) {
558 bool compass = prop->getBoolValue();
560 while ( value >= 360.0 ) { value -= 360.0; }
561 while ( value < 0.0 ) { value += 360.0; }
565 output_nodes[j]->setDoubleValue( value );
569 SG_LOG( SG_IO, SG_DEBUG, "Invalid channel type = "
573 SG_LOG( SG_IO, SG_DEBUG,
574 "Input config error, expecting 'channel' but found "
584 /////////////////////////////////////////////////////////////////////
585 // Read the switch positions
586 /////////////////////////////////////////////////////////////////////
588 // decode the packed switch data
589 static void update_switch_matrix(
591 unsigned char switch_data[ATC_SWITCH_BYTES],
592 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
594 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
595 unsigned char switches = switch_data[row];
597 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
598 switch_matrix[board][column][row] = switches & 1;
599 switches = switches >> 1;
604 bool FGATCInput::do_switches() {
606 ATCReadSwitches( switches_fd, switch_data );
608 // unpack the switch data
609 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
610 update_switch_matrix( board, switch_data, switch_matrix );
612 // Process the switch inputs
613 if ( switches_node != NULL ) {
614 for ( int i = 0; i < switches_node->nChildren(); ++i ) {
615 // read the next config entry from the property tree
617 SGPropertyNode *child = switches_node->getChild(i);
618 string cname = child->getName();
621 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
626 float scaled_value = 0.0f;
628 // get common options
630 SGPropertyNode *prop;
631 prop = child->getChild( "name" );
632 if ( prop != NULL ) {
633 name = prop->getStringValue();
635 prop = child->getChild( "type" );
636 if ( prop != NULL ) {
637 type = prop->getStringValue();
640 while ( (prop = child->getChild("prop", j)) != NULL ) {
642 = fgGetNode( prop->getStringValue(), true );
643 output_nodes.push_back( tmp );
646 prop = child->getChild( "factor" );
647 if ( prop != NULL ) {
648 factor = prop->getFloatValue();
650 prop = child->getChild( "steady-state-filter" );
651 if ( prop != NULL ) {
652 filter = prop->getIntValue();
655 // handle different types of switches
657 if ( cname == "switch" ) {
658 prop = child->getChild( "row" );
659 if ( prop != NULL ) {
660 row = prop->getIntValue();
662 prop = child->getChild( "col" );
663 if ( prop != NULL ) {
664 col = prop->getIntValue();
667 // Fetch the raw value
668 int raw_value = switch_matrix[board][row][col];
671 scaled_value = (float)raw_value * factor;
673 } else if ( cname == "combo-switch" ) {
674 float combo_value = 0.0f;
678 while ( (pos = child->getChild("position", k++)) != NULL ) {
679 // read the combo position entries from the property tree
681 prop = pos->getChild( "row" );
682 if ( prop != NULL ) {
683 row = prop->getIntValue();
685 prop = pos->getChild( "col" );
686 if ( prop != NULL ) {
687 col = prop->getIntValue();
689 prop = pos->getChild( "value" );
690 if ( prop != NULL ) {
691 combo_value = prop->getFloatValue();
694 // Fetch the raw value
695 int raw_value = switch_matrix[board][row][col];
696 // cout << "sm[" << board << "][" << row << "][" << col
697 // << "] = " << raw_value << endl;
700 // set scaled_value to the first combo_value
701 // that matches and jump out of loop.
702 scaled_value = combo_value;
708 scaled_value *= factor;
711 // handle filter request. The value of the switch must be
712 // steady-state for "n" frames before the property value
715 bool update_prop = true;
718 SGPropertyNode *fv = child->getChild( "filter-value", 0, true );
719 float filter_value = fv->getFloatValue();
720 SGPropertyNode *fc = child->getChild( "filter-count", 0, true );
721 int filter_count = fc->getIntValue();
723 if ( fabs(scaled_value - filter_value) < 0.0001 ) {
729 if ( filter_count < filter ) {
733 fv->setFloatValue( scaled_value );
734 fc->setIntValue( filter_count );
738 if ( type == "engine" || type == "flight" ) {
739 if ( ! ignore_flight_controls->getBoolValue() ) {
740 // update the property tree values
741 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
742 output_nodes[j]->setDoubleValue( scaled_value );
745 } else if ( type == "avionics" ) {
746 // update the property tree values
747 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
748 output_nodes[j]->setDoubleValue( scaled_value );
759 /////////////////////////////////////////////////////////////////////
760 // Read radio switches
761 /////////////////////////////////////////////////////////////////////
763 bool FGATCInput::do_radio_switches() {
765 ATCReadRadios( radios_fd, radio_switch_data );
767 // Process the radio switch/knob inputs
768 if ( radio_in_node != NULL ) {
769 for ( int i = 0; i < radio_in_node->nChildren(); ++i ) {
770 // read the next config entry from the property tree
772 SGPropertyNode *child = radio_in_node->getChild(i);
773 string cname = child->getName();
775 if ( cname == "switch" ) {
778 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
785 int scaled_value = 0;
786 // get common options
788 SGPropertyNode *prop;
789 prop = child->getChild( "name" );
790 if ( prop != NULL ) {
791 name = prop->getStringValue();
793 prop = child->getChild( "type" );
794 if ( prop != NULL ) {
795 type = prop->getStringValue();
798 while ( (prop = child->getChild("prop", j)) != NULL ) {
800 = fgGetNode( prop->getStringValue(), true );
801 output_nodes.push_back( tmp );
804 prop = child->getChild( "byte" );
805 if ( prop != NULL ) {
806 byte_num = prop->getIntValue();
808 prop = child->getChild( "right-shift" );
809 if ( prop != NULL ) {
810 right_shift = prop->getIntValue();
812 prop = child->getChild( "mask" );
813 if ( prop != NULL ) {
814 mask = prop->getIntValue();
816 prop = child->getChild( "factor" );
817 if ( prop != NULL ) {
818 factor = prop->getIntValue();
820 prop = child->getChild( "offset" );
821 if ( prop != NULL ) {
822 offset = prop->getIntValue();
824 prop = child->getChild( "invert" );
825 if ( prop != NULL ) {
826 invert = prop->getBoolValue();
829 // Fetch the raw value
831 = (radio_switch_data[byte_num] >> right_shift) & mask;
835 raw_value = !raw_value;
837 scaled_value = raw_value * factor + offset;
839 // update the property tree values
840 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
841 output_nodes[j]->setIntValue( scaled_value );
851 // process the hardware inputs. This code assumes the calling layer
852 // will lock the hardware.
853 bool FGATCInput::process() {
855 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for input! "
868 bool FGATCInput::close() {
870 #if defined( unix ) || defined( __CYGWIN__ )
874 result = ::close( lock_fd );
875 if ( result == -1 ) {
876 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
878 snprintf( msg, 256, "Error closing %s", lock_file );
883 result = ::close( analog_in_fd );
884 if ( result == -1 ) {
885 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
887 snprintf( msg, 256, "Error closing %s", analog_in_file );
892 result = ::close( radios_fd );
893 if ( result == -1 ) {
894 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
896 snprintf( msg, 256, "Error closing %s", radios_file );
901 result = ::close( switches_fd );
902 if ( result == -1 ) {
903 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
905 snprintf( msg, 256, "Error closing %s", switches_file );