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 hardware, please wait ..." );
141 snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
142 snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
143 snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
145 #if defined( unix ) || defined( __CYGWIN__ )
147 /////////////////////////////////////////////////////////////////////
148 // Open the /proc files
149 /////////////////////////////////////////////////////////////////////
151 analog_in_fd = ::open( analog_in_file, O_RDONLY );
152 if ( analog_in_fd == -1 ) {
153 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
155 snprintf( msg, 256, "Error opening %s", analog_in_file );
160 radios_fd = ::open( radios_file, O_RDWR );
161 if ( radios_fd == -1 ) {
162 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
164 snprintf( msg, 256, "Error opening %s", radios_file );
169 switches_fd = ::open( switches_file, O_RDONLY );
170 if ( switches_fd == -1 ) {
171 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
173 snprintf( msg, 256, "Error opening %s", switches_file );
180 /////////////////////////////////////////////////////////////////////
181 // Finished initing hardware
182 /////////////////////////////////////////////////////////////////////
184 SG_LOG( SG_IO, SG_ALERT,
185 "Done initializing ATC hardware." );
189 /////////////////////////////////////////////////////////////////////
190 // Connect up to property values
191 /////////////////////////////////////////////////////////////////////
193 ignore_flight_controls
194 = fgGetNode( "/input/atcsim/ignore-flight-controls", true );
195 ignore_pedal_controls
196 = fgGetNode( "/input/atcsim/ignore-pedal-controls", true );
200 snprintf( base_name, 256, "/input/atc-board[%d]/analog-in", board );
201 analog_in_node = fgGetNode( base_name );
203 snprintf( base_name, 256, "/input/atc-board[%d]/radio-switches", board );
204 radio_in_node = fgGetNode( base_name );
206 snprintf( base_name, 256, "/input/atc-board[%d]/switches", board );
207 switches_node = fgGetNode( base_name );
213 /////////////////////////////////////////////////////////////////////
214 // Read analog inputs
215 /////////////////////////////////////////////////////////////////////
217 // scale a number between min and max (with center defined) to a scale
219 static double scale( int center, int min, int max, int value ) {
220 // cout << center << " " << min << " " << max << " " << value << " ";
224 if ( value <= center ) {
225 range = center - min;
226 result = (value - center) / range;
228 range = max - center;
229 result = (value - center) / range;
232 if ( result < -1.0 ) result = -1.0;
233 if ( result > 1.0 ) result = 1.0;
235 // cout << result << endl;
241 // scale a number between min and max to a scale from 0.0 to 1.0
242 static double scale( int min, int max, int value ) {
243 // cout << center << " " << min << " " << max << " " << value << " ";
248 result = (value - min) / range;
250 if ( result < 0.0 ) result = 0.0;
251 if ( result > 1.0 ) result = 1.0;
253 // cout << result << endl;
259 static int tony_magic( int raw, int obs[3] ) {
265 if ( obs[2] >= 68 && obs[2] < 480 ) {
267 } else if ( obs[2] >= 480 ) {
272 } else if ( obs[1] < 68 ) {
275 } else if ( obs[2] < 30 ) {
276 if ( obs[1] >= 68 && obs[1] < 480 ) {
280 } else if ( obs[1] >= 480 ) {
282 if ( obs[0] < obs[1] ) {
290 } else if ( obs[1] > 980 ) {
291 if ( obs[2] <= 956 && obs[2] > 480 ) {
293 } else if ( obs[2] <= 480 ) {
298 } else if ( obs[1] > 956 ) {
301 } else if ( obs[2] > 980 ) {
302 if ( obs[1] <= 956 && obs[1] > 480 ) {
306 } else if ( obs[1] <= 480 ) {
308 if ( obs[0] > obs[1] ) {
317 if ( obs[1] < 480 && obs[2] > 480 ) {
318 // crossed gap going up
319 if ( obs[0] < obs[1] ) {
320 // caught a bogus intermediate value coming out of the gap
323 } else if ( obs[1] > 480 && obs[2] < 480 ) {
324 // crossed gap going down
325 if ( obs[0] > obs[1] ) {
326 // caught a bogus intermediate value coming out of the gap
329 } else if ( obs[0] > 480 && obs[1] < 480 && obs[2] < 480 ) {
330 // crossed the gap going down
331 if ( obs[1] > obs[2] ) {
332 // caught a bogus intermediate value coming out of the gap
335 } else if ( obs[0] < 480 && obs[1] > 480 && obs[2] > 480 ) {
336 // crossed the gap going up
337 if ( obs[1] < obs[2] ) {
338 // caught a bogus intermediate value coming out of the gap
342 result = obs[1] - obs[2];
343 if ( abs(result) > 400 ) {
351 // cout << " result = " << result << endl;
352 if ( result < -500 ) { result += 1024; }
353 if ( result > 500 ) { result -= 1024; }
359 static double instr_pot_filter( double ave, double val ) {
360 if ( fabs(ave - val) < 400 || fabs(val) < fabs(ave) ) {
361 return 0.5 * ave + 0.5 * val;
368 bool FGATCInput::do_analog_in() {
369 // Read raw data in byte form
370 ATCReadAnalogInputs( analog_in_fd, analog_in_bytes );
372 // Convert to integer values
373 for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
374 unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
375 unsigned char lo = analog_in_bytes[2 * channel + 1];
376 analog_in_data[channel] = hi * 256 + lo;
378 // printf("%02x %02x ", hi, lo );
379 // printf("%04d ", value );
382 // Process analog inputs
383 if ( analog_in_node != NULL ) {
384 for ( int i = 0; i < analog_in_node->nChildren(); ++i ) {
385 // read the next config entry from the property tree
387 SGPropertyNode *child = analog_in_node->getChild(i);
388 string cname = child->getName();
389 int index = child->getIndex();
393 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
398 if ( cname == "channel" ) {
399 SGPropertyNode *prop;
400 prop = child->getChild( "name" );
401 if ( prop != NULL ) {
402 name = prop->getStringValue();
404 prop = child->getChild( "type", 0 );
405 if ( prop != NULL ) {
406 type = prop->getStringValue();
408 prop = child->getChild( "type", 1 );
409 if ( prop != NULL ) {
410 subtype = prop->getStringValue();
413 while ( (prop = child->getChild("prop", j)) != NULL ) {
415 = fgGetNode( prop->getStringValue(), true );
416 output_nodes.push_back( tmp );
419 prop = child->getChild( "center" );
420 if ( prop != NULL ) {
421 center = prop->getIntValue();
423 prop = child->getChild( "min" );
424 if ( prop != NULL ) {
425 min = prop->getIntValue();
427 prop = child->getChild( "max" );
428 if ( prop != NULL ) {
429 max = prop->getIntValue();
431 prop = child->getChild( "factor" );
432 if ( prop != NULL ) {
433 factor = prop->getFloatValue();
436 // Fetch the raw value
438 int raw_value = analog_in_data[index];
440 // Update the target properties
442 if ( type == "flight"
443 && !ignore_flight_controls->getBoolValue() )
445 if ( subtype != "pedals" ||
446 ( subtype == "pedals"
447 && !ignore_pedal_controls->getBoolValue() ) )
449 // "Cook" the raw value
450 float scaled_value = 0.0f;
452 scaled_value = scale( center, min, max, raw_value );
454 scaled_value = scale( min, max, raw_value );
456 scaled_value *= factor;
458 // update the property tree values
459 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
460 output_nodes[j]->setDoubleValue( scaled_value );
463 } else if ( type == "avionics-simple" ) {
464 // "Cook" the raw value
465 float scaled_value = 0.0f;
467 scaled_value = scale( center, min, max, raw_value );
469 scaled_value = scale( min, max, raw_value );
471 scaled_value *= factor;
473 // update the property tree values
474 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
475 output_nodes[j]->setDoubleValue( scaled_value );
477 } else if ( type == "avionics-resolver" ) {
478 // this type of analog input impliments a
479 // rotational knob. We first caclulate the amount
480 // of knob rotation (slightly complex to work with
481 // hardware specific goofiness) and then multiply
482 // that amount of movement by a scaling factor,
483 // and finally add the result to the original
486 bool do_init = false;
487 float scaled_value = 0.0f;
489 // fetch intermediate values from property tree
491 prop = child->getChild( "is-inited", 0 );
492 if ( prop == NULL ) {
494 prop = child->getChild( "is-inited", 0, true );
495 prop->setBoolValue( true );
499 for ( j = 0; j < 3; ++j ) {
500 prop = child->getChild( "raw", j, true );
502 raw[j] = analog_in_data[index];
504 raw[j] = prop->getIntValue();
508 // do Tony's magic to calculate knob movement
509 // based on current analog input position and
511 int diff = tony_magic( analog_in_data[index], raw );
513 // write raw intermediate values (updated by
514 // tony_magic()) back to property tree
515 for ( j = 0; j < 3; ++j ) {
516 prop = child->getChild( "raw", j, true );
517 prop->setIntValue( raw[j] );
520 // filter knob position
521 prop = child->getChild( "diff-average", 0, true );
522 double diff_ave = prop->getDoubleValue();
523 diff_ave = instr_pot_filter( diff_ave, diff );
524 prop->setDoubleValue( diff_ave );
526 // calculate value adjustment in real world units
527 scaled_value = diff_ave * factor;
529 // update the property tree values
530 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
531 float value = output_nodes[j]->getDoubleValue();
532 value += scaled_value;
534 prop = child->getChild( "min-clamp" );
535 if ( prop != NULL ) {
536 double min = prop->getDoubleValue();
537 if ( value < min ) { value = min; }
540 prop = child->getChild( "max-clamp" );
541 if ( prop != NULL ) {
542 double max = prop->getDoubleValue();
543 if ( value > max ) { value = max; }
546 prop = child->getChild( "compass-heading" );
547 if ( prop != NULL ) {
548 bool compass = prop->getBoolValue();
550 while ( value >= 360.0 ) { value -= 360.0; }
551 while ( value < 0.0 ) { value += 360.0; }
555 output_nodes[j]->setDoubleValue( value );
559 SG_LOG( SG_IO, SG_DEBUG, "Invalid channel type = "
563 SG_LOG( SG_IO, SG_DEBUG,
564 "Input config error, expecting 'channel' but found "
574 /////////////////////////////////////////////////////////////////////
575 // Read the switch positions
576 /////////////////////////////////////////////////////////////////////
578 // decode the packed switch data
579 static void update_switch_matrix(
581 unsigned char switch_data[ATC_SWITCH_BYTES],
582 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
584 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
585 unsigned char switches = switch_data[row];
587 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
588 switch_matrix[board][column][row] = switches & 1;
589 switches = switches >> 1;
594 bool FGATCInput::do_switches() {
596 ATCReadSwitches( switches_fd, switch_data );
598 // unpack the switch data
599 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
600 update_switch_matrix( board, switch_data, switch_matrix );
602 // Process the switch inputs
603 if ( switches_node != NULL ) {
604 for ( int i = 0; i < switches_node->nChildren(); ++i ) {
605 // read the next config entry from the property tree
607 SGPropertyNode *child = switches_node->getChild(i);
608 string cname = child->getName();
611 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
616 float scaled_value = 0.0f;
618 // get common options
620 SGPropertyNode *prop;
621 prop = child->getChild( "name" );
622 if ( prop != NULL ) {
623 name = prop->getStringValue();
625 prop = child->getChild( "type" );
626 if ( prop != NULL ) {
627 type = prop->getStringValue();
630 while ( (prop = child->getChild("prop", j)) != NULL ) {
632 = fgGetNode( prop->getStringValue(), true );
633 output_nodes.push_back( tmp );
636 prop = child->getChild( "factor" );
637 if ( prop != NULL ) {
638 factor = prop->getFloatValue();
640 prop = child->getChild( "steady-state-filter" );
641 if ( prop != NULL ) {
642 filter = prop->getIntValue();
645 // handle different types of switches
647 if ( cname == "switch" ) {
648 prop = child->getChild( "row" );
649 if ( prop != NULL ) {
650 row = prop->getIntValue();
652 prop = child->getChild( "col" );
653 if ( prop != NULL ) {
654 col = prop->getIntValue();
657 // Fetch the raw value
658 int raw_value = switch_matrix[board][row][col];
661 scaled_value = (float)raw_value * factor;
663 } else if ( cname == "combo-switch" ) {
664 float combo_value = 0.0f;
668 while ( (pos = child->getChild("position", k++)) != NULL ) {
669 // read the combo position entries from the property tree
671 prop = pos->getChild( "row" );
672 if ( prop != NULL ) {
673 row = prop->getIntValue();
675 prop = pos->getChild( "col" );
676 if ( prop != NULL ) {
677 col = prop->getIntValue();
679 prop = pos->getChild( "value" );
680 if ( prop != NULL ) {
681 combo_value = prop->getFloatValue();
684 // Fetch the raw value
685 int raw_value = switch_matrix[board][row][col];
686 // cout << "sm[" << board << "][" << row << "][" << col
687 // << "] = " << raw_value << endl;
690 // set scaled_value to the first combo_value
691 // that matches and jump out of loop.
692 scaled_value = combo_value;
698 scaled_value *= factor;
701 // handle filter request. The value of the switch must be
702 // steady-state for "n" frames before the property value
705 bool update_prop = true;
708 SGPropertyNode *fv = child->getChild( "filter-value", 0, true );
709 float filter_value = fv->getFloatValue();
710 SGPropertyNode *fc = child->getChild( "filter-count", 0, true );
711 int filter_count = fc->getIntValue();
713 if ( fabs(scaled_value - filter_value) < 0.0001 ) {
719 if ( filter_count < filter ) {
723 fv->setFloatValue( scaled_value );
724 fc->setIntValue( filter_count );
728 if ( type == "engine" || type == "flight" ) {
729 if ( ! ignore_flight_controls->getBoolValue() ) {
730 // update the property tree values
731 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
732 output_nodes[j]->setDoubleValue( scaled_value );
735 } else if ( type == "avionics" ) {
736 // update the property tree values
737 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
738 output_nodes[j]->setDoubleValue( scaled_value );
749 /////////////////////////////////////////////////////////////////////
750 // Read radio switches
751 /////////////////////////////////////////////////////////////////////
753 bool FGATCInput::do_radio_switches() {
755 ATCReadRadios( radios_fd, radio_switch_data );
757 // Process the radio switch/knob inputs
758 if ( radio_in_node != NULL ) {
759 for ( int i = 0; i < radio_in_node->nChildren(); ++i ) {
760 // read the next config entry from the property tree
762 SGPropertyNode *child = radio_in_node->getChild(i);
763 string cname = child->getName();
765 if ( cname == "switch" ) {
768 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
775 int scaled_value = 0;
776 // get common options
778 SGPropertyNode *prop;
779 prop = child->getChild( "name" );
780 if ( prop != NULL ) {
781 name = prop->getStringValue();
783 prop = child->getChild( "type" );
784 if ( prop != NULL ) {
785 type = prop->getStringValue();
788 while ( (prop = child->getChild("prop", j)) != NULL ) {
790 = fgGetNode( prop->getStringValue(), true );
791 output_nodes.push_back( tmp );
794 prop = child->getChild( "byte" );
795 if ( prop != NULL ) {
796 byte_num = prop->getIntValue();
798 prop = child->getChild( "right-shift" );
799 if ( prop != NULL ) {
800 right_shift = prop->getIntValue();
802 prop = child->getChild( "mask" );
803 if ( prop != NULL ) {
804 mask = prop->getIntValue();
806 prop = child->getChild( "factor" );
807 if ( prop != NULL ) {
808 factor = prop->getIntValue();
810 prop = child->getChild( "offset" );
811 if ( prop != NULL ) {
812 offset = prop->getIntValue();
814 prop = child->getChild( "invert" );
815 if ( prop != NULL ) {
816 invert = prop->getBoolValue();
819 // Fetch the raw value
821 = (radio_switch_data[byte_num] >> right_shift) & mask;
825 raw_value = !raw_value;
827 scaled_value = raw_value * factor + offset;
829 // update the property tree values
830 for ( j = 0; j < (int)output_nodes.size(); ++j ) {
831 output_nodes[j]->setIntValue( scaled_value );
841 // process the hardware inputs. This code assumes the calling layer
842 // will lock the hardware.
843 bool FGATCInput::process() {
845 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for input! "
858 bool FGATCInput::close() {
860 #if defined( unix ) || defined( __CYGWIN__ )
864 result = ::close( analog_in_fd );
865 if ( result == -1 ) {
866 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
868 snprintf( msg, 256, "Error closing %s", analog_in_file );
873 result = ::close( radios_fd );
874 if ( result == -1 ) {
875 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
877 snprintf( msg, 256, "Error closing %s", radios_file );
882 result = ::close( switches_fd );
883 if ( result == -1 ) {
884 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
886 snprintf( msg, 256, "Error closing %s", switches_file );