X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FInstrumentation%2Fkr_87.cxx;h=79f45809e4907532c239ea8e82b474ea4e8da5ef;hb=ccb890447ae2f950b8032649ca8b8190bdafaf90;hp=404a5e46f048945bf67a772d92660a9221b4c88e;hpb=1254fdea2a2eaa6c8b07a42fe49ee0a23988a52c;p=flightgear.git diff --git a/src/Instrumentation/kr_87.cxx b/src/Instrumentation/kr_87.cxx index 404a5e46f..79f45809e 100644 --- a/src/Instrumentation/kr_87.cxx +++ b/src/Instrumentation/kr_87.cxx @@ -2,7 +2,7 @@ // // Written by Curtis Olson, started April 2002. // -// Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org +// Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -16,9 +16,8 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // -// $Id$ #ifdef HAVE_CONFIG_H @@ -29,14 +28,17 @@ #include #include +#include +#include +#include -#include #include #include "kr_87.hxx" +#include #include -SG_USING_STD(string); +using std::string; static int play_count = 0; static time_t last_time = 0; @@ -69,9 +71,6 @@ static double kludgeRange ( double stationElev, double aircraftElev, // Constructor FGKR_87::FGKR_87( SGPropertyNode *node ) : - lon_node(fgGetNode("/position/longitude-deg", true)), - lat_node(fgGetNode("/position/latitude-deg", true)), - alt_node(fgGetNode("/position/altitude-ft", true)), bus_power(fgGetNode("/systems/electrical/outputs/adf", true)), serviceable(fgGetNode("/instrumentation/adf/serviceable", true)), need_update(true), @@ -102,7 +101,9 @@ FGKR_87::FGKR_87( SGPropertyNode *node ) : needle_deg(0.0), flight_timer(0.0), elapsed_timer(0.0), - tmp_timer(0.0) + tmp_timer(0.0), + _time_before_search_sec(0), + _sgr(NULL) { } @@ -113,137 +114,118 @@ FGKR_87::~FGKR_87() { void FGKR_87::init () { - serviceable->setBoolValue( true ); - morse.init(); + SGSoundMgr *smgr = globals->get_soundmgr(); + _sgr = smgr->find("avionics", true); + _sgr->tie_to_listener(); } +void FGKR_87::reinit () { + _time_before_search_sec = 0; +} void FGKR_87::bind () { + _tiedProperties.setRoot(fgGetNode("/instrumentation/kr-87", true)); // internal values - fgTie("/radios/kr-87/internal/valid", this, &FGKR_87::get_valid); - fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange); - fgTie("/radios/kr-87/internal/dist", this, &FGKR_87::get_dist); - fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading); + _tiedProperties.Tie("internal/valid", this, &FGKR_87::get_valid); + _tiedProperties.Tie("internal/inrange", this, + &FGKR_87::get_inrange); + _tiedProperties.Tie("internal/dist", this, + &FGKR_87::get_dist); + _tiedProperties.Tie("internal/heading", this, + &FGKR_87::get_heading); // modes - fgTie("/radios/kr-87/modes/ant", this, - &FGKR_87::get_ant_mode); - fgTie("/radios/kr-87/modes/stby", this, - &FGKR_87::get_stby_mode); - fgTie("/radios/kr-87/modes/timer", this, - &FGKR_87::get_timer_mode); - fgTie("/radios/kr-87/modes/count", this, - &FGKR_87::get_count_mode); + _tiedProperties.Tie("modes/ant", this, + &FGKR_87::get_ant_mode); + _tiedProperties.Tie("modes/stby", this, + &FGKR_87::get_stby_mode); + _tiedProperties.Tie("modes/timer", this, + &FGKR_87::get_timer_mode); + _tiedProperties.Tie("modes/count", this, + &FGKR_87::get_count_mode); // input and buttons - fgTie("/radios/kr-87/inputs/rotation-deg", this, - &FGKR_87::get_rotation, &FGKR_87::set_rotation); - fgSetArchivable("/radios/kr-87/inputs/rotation-deg"); - fgTie("/radios/kr-87/inputs/power-btn", this, - &FGKR_87::get_power_btn, - &FGKR_87::set_power_btn); - fgSetArchivable("/radios/kr-87/inputs/power-btn"); - fgTie("/radios/kr-87/inputs/audio-btn", this, - &FGKR_87::get_audio_btn, - &FGKR_87::set_audio_btn); - fgSetArchivable("/radios/kr-87/inputs/audio-btn"); - fgTie("/radios/kr-87/inputs/volume", this, - &FGKR_87::get_vol_btn, - &FGKR_87::set_vol_btn); - fgSetArchivable("/radios/kr-87/inputs/volume"); - fgTie("/radios/kr-87/inputs/adf-btn", this, - &FGKR_87::get_adf_btn, - &FGKR_87::set_adf_btn); - fgTie("/radios/kr-87/inputs/bfo-btn", this, - &FGKR_87::get_bfo_btn, - &FGKR_87::set_bfo_btn); - fgTie("/radios/kr-87/inputs/frq-btn", this, - &FGKR_87::get_frq_btn, - &FGKR_87::set_frq_btn); - fgTie("/radios/kr-87/inputs/flt-et-btn", this, - &FGKR_87::get_flt_et_btn, - &FGKR_87::set_flt_et_btn); - fgTie("/radios/kr-87/inputs/set-rst-btn", this, - &FGKR_87::get_set_rst_btn, - &FGKR_87::set_set_rst_btn); + _tiedProperties.Tie("inputs/rotation-deg", this, + &FGKR_87::get_rotation, &FGKR_87::set_rotation); + fgSetArchivable("/instrumentation/kr-87/inputs/rotation-deg"); + _tiedProperties.Tie("inputs/power-btn", this, + &FGKR_87::get_power_btn, + &FGKR_87::set_power_btn); + fgSetArchivable("/instrumentation/kr-87/inputs/power-btn"); + _tiedProperties.Tie("inputs/audio-btn", this, + &FGKR_87::get_audio_btn, + &FGKR_87::set_audio_btn); + fgSetArchivable("/instrumentation/kr-87/inputs/audio-btn"); + _tiedProperties.Tie("inputs/volume", this, + &FGKR_87::get_vol_btn, + &FGKR_87::set_vol_btn); + fgSetArchivable("/instrumentation/kr-87/inputs/volume"); + _tiedProperties.Tie("inputs/adf-btn", this, + &FGKR_87::get_adf_btn, + &FGKR_87::set_adf_btn); + _tiedProperties.Tie("inputs/bfo-btn", this, + &FGKR_87::get_bfo_btn, + &FGKR_87::set_bfo_btn); + _tiedProperties.Tie("inputs/frq-btn", this, + &FGKR_87::get_frq_btn, + &FGKR_87::set_frq_btn); + _tiedProperties.Tie("inputs/flt-et-btn", this, + &FGKR_87::get_flt_et_btn, + &FGKR_87::set_flt_et_btn); + _tiedProperties.Tie("inputs/set-rst-btn", this, + &FGKR_87::get_set_rst_btn, + &FGKR_87::set_set_rst_btn); // outputs - fgTie("/radios/kr-87/outputs/selected-khz", this, - &FGKR_87::get_freq, &FGKR_87::set_freq); - fgSetArchivable("/radios/kr-87/outputs/selected-khz"); - fgTie("/radios/kr-87/outputs/standby-khz", this, - &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq); - fgSetArchivable("/radios/kr-87/outputs/standby-khz"); - fgTie("/radios/kr-87/outputs/needle-deg", this, - &FGKR_87::get_needle_deg); - fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer); - fgTie("/radios/kr-87/outputs/elapsed-timer", this, - &FGKR_87::get_elapsed_timer, - &FGKR_87::set_elapsed_timer); + _tiedProperties.Tie("outputs/selected-khz", this, + &FGKR_87::get_freq, &FGKR_87::set_freq); + fgSetArchivable("/instrumentation/kr-87/outputs/selected-khz"); + _tiedProperties.Tie("outputs/standby-khz", this, + &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq); + fgSetArchivable("/instrumentation/kr-87/outputs/standby-khz"); + _tiedProperties.Tie("outputs/needle-deg", this, + &FGKR_87::get_needle_deg); + _tiedProperties.Tie("outputs/flight-timer", this, + &FGKR_87::get_flight_timer); + _tiedProperties.Tie("outputs/elapsed-timer", this, + &FGKR_87::get_elapsed_timer, + &FGKR_87::set_elapsed_timer); // annunciators - fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann ); - fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann ); - fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann ); - fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann ); - fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann ); - fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann ); + _tiedProperties.Tie("annunciators/ant", this, + &FGKR_87::get_ant_ann ); + _tiedProperties.Tie("annunciators/adf", this, + &FGKR_87::get_adf_ann ); + _tiedProperties.Tie("annunciators/bfo", this, + &FGKR_87::get_bfo_ann ); + _tiedProperties.Tie("annunciators/frq", this, + &FGKR_87::get_frq_ann ); + _tiedProperties.Tie("annunciators/flt", this, + &FGKR_87::get_flt_ann ); + _tiedProperties.Tie("annunciators/et", this, + &FGKR_87::get_et_ann ); } void FGKR_87::unbind () { - // internal values - fgUntie("/radios/kr-87/internal/valid"); - fgUntie("/radios/kr-87/internal/inrange"); - fgUntie("/radios/kr-87/internal/dist"); - fgUntie("/radios/kr-87/internal/heading"); - - // modes - fgUntie("/radios/kr-87/modes/ant"); - fgUntie("/radios/kr-87/modes/stby"); - fgUntie("/radios/kr-87/modes/timer"); - fgUntie("/radios/kr-87/modes/count"); - - // input and buttons - fgUntie("/radios/kr-87/inputs/rotation-deg"); - fgUntie("/radios/kr-87/inputs/power-btn"); - fgUntie("/radios/kr-87/inputs/volume"); - fgUntie("/radios/kr-87/inputs/adf-btn"); - fgUntie("/radios/kr-87/inputs/bfo-btn"); - fgUntie("/radios/kr-87/inputs/frq-btn"); - fgUntie("/radios/kr-87/inputs/flt-et-btn"); - fgUntie("/radios/kr-87/inputs/set-rst-btn"); - fgUntie("/radios/kr-87/inputs/ident-btn"); - - // outputs - fgUntie("/radios/kr-87/outputs/selected-khz"); - fgUntie("/radios/kr-87/outputs/standby-khz"); - fgUntie("/radios/kr-87/outputs/needle-deg"); - fgUntie("/radios/kr-87/outputs/flight-timer"); - fgUntie("/radios/kr-87/outputs/elapsed-timer"); - - // annunciators - fgUntie("/radios/kr-87/annunciators/ant"); - fgUntie("/radios/kr-87/annunciators/adf"); - fgUntie("/radios/kr-87/annunciators/bfo"); - fgUntie("/radios/kr-87/annunciators/frq"); - fgUntie("/radios/kr-87/annunciators/flt"); - fgUntie("/radios/kr-87/annunciators/et"); + _tiedProperties.Untie(); } // Update the various nav values based on position and valid tuned in navs -void FGKR_87::update( double dt ) { - double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS; - double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS; - double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER; +void FGKR_87::update( double dt_sec ) { + SGGeod acft = globals->get_aircraft_position(); need_update = false; - Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) ); - Point3D station; double az1, az2, s; + // On timeout, scan again + _time_before_search_sec -= dt_sec; + if ( _time_before_search_sec < 0 ) { + search(); + } + //////////////////////////////////////////////////////////////////////// // Radio //////////////////////////////////////////////////////////////////////// @@ -283,7 +265,7 @@ void FGKR_87::update( double dt ) { } if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) { // button depressed and was last iteration too - tmp_timer += dt; + tmp_timer += dt_sec; // cout << "tmp_timer = " << tmp_timer << endl; if ( tmp_timer > 2.0 ) { // button held depressed for 2 seconds @@ -308,14 +290,14 @@ void FGKR_87::update( double dt ) { last_set_rst_btn = set_rst_btn; // timers - flight_timer += dt; + flight_timer += dt_sec; if ( set_rst_btn == 0 ) { // only count if set/rst button not depressed if ( count_mode == 0 ) { - elapsed_timer += dt; + elapsed_timer += dt_sec; } else if ( count_mode == 1 ) { - elapsed_timer -= dt; + elapsed_timer -= dt_sec; if ( elapsed_timer < 1.0 ) { count_mode = 0; elapsed_timer = 0.0; @@ -332,7 +314,7 @@ void FGKR_87::update( double dt ) { if ( count_mode < 2 ) { et_ann = stby_mode && timer_mode; } else { - et_flash_time += dt; + et_flash_time += dt_sec; if ( et_ann && et_flash_time > 0.5 ) { et_ann = false; et_flash_time -= 0.5; @@ -345,20 +327,17 @@ void FGKR_87::update( double dt ) { if ( valid ) { // cout << "adf is valid" << endl; // staightline distance - station = Point3D( x, y, z ); - dist = aircraft.distance3D( station ); + // What a hack, dist is a class local variable + dist = sqrt(distSqr(SGVec3d::fromGeod(acft), xyz)); // wgs84 heading - geo_inverse_wgs_84( acft_elev, - acft_lat * SGD_RADIANS_TO_DEGREES, - acft_lon * SGD_RADIANS_TO_DEGREES, - stn_lat, stn_lon, + geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat), &az1, &az2, &s ); heading = az1; // cout << " heading = " << heading // << " dist = " << dist << endl; - effective_range = kludgeRange(stn_elev, acft_elev, range); + effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range); if ( dist < effective_range * SG_NM_TO_METER ) { inrange = true; } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) { @@ -426,7 +405,8 @@ void FGKR_87::update( double dt ) { char formatted_timer[128]; // cout << big << ":" << little << endl; snprintf(formatted_timer, 6, "%02d:%02d", big, little); - fgSetString( "/radios/kr-87/outputs/timer-string", formatted_timer ); + fgSetString( "/instrumentation/kr-87/outputs/timer-string", + formatted_timer ); while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; } while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; } @@ -435,7 +415,7 @@ void FGKR_87::update( double dt ) { while ( diff < -180.0 ) { diff += 360.0; } while ( diff > 180.0 ) { diff -= 360.0; } - needle_deg += diff * dt * 4; + needle_deg += diff * dt_sec * 4; while ( needle_deg < 0.0 ) { needle_deg += 360.0; } while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; } @@ -449,7 +429,7 @@ void FGKR_87::update( double dt ) { // otherwise turn it off if ( vol_btn >= 0.01 && audio_btn ) { SGSoundSample *sound; - sound = globals->get_soundmgr()->find( "adf-ident" ); + sound = _sgr->find( "adf-ident" ); if ( sound != NULL ) { if ( !adf_btn ) { sound->set_volume( vol_btn ); @@ -466,13 +446,13 @@ void FGKR_87::update( double dt ) { } if ( play_count < 4 ) { // play ADF ident - if ( !globals->get_soundmgr()->is_playing("adf-ident") ) { - globals->get_soundmgr()->play_once( "adf-ident" ); + if ( !_sgr->is_playing("adf-ident") && (vol_btn > 0.05) ) { + _sgr->play_once( "adf-ident" ); ++play_count; } } } else { - globals->get_soundmgr()->stop( "adf-ident" ); + _sgr->stop( "adf-ident" ); } } } @@ -480,20 +460,21 @@ void FGKR_87::update( double dt ) { // Update current nav/adf radio stations based on current postition void FGKR_87::search() { - double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS; - double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS; - double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER; - + SGGeod pos = globals->get_aircraft_position(); + // FIXME: the panel should handle this static string last_ident = ""; + // reset search time + _time_before_search_sec = 1.0; + //////////////////////////////////////////////////////////////////////// // ADF. //////////////////////////////////////////////////////////////////////// - FGNavRecord *adf - = globals->get_navlist()->findByFreq( freq, acft_lon, acft_lat, - acft_elev ); + + FGNavList::TypeFilter filter(FGPositioned::NDB); + FGNavRecord *adf = FGNavList::findByFreq( freq, pos, &filter); if ( adf != NULL ) { char sfreq[128]; snprintf( sfreq, 10, "%d", freq ); @@ -509,18 +490,18 @@ void FGKR_87::search() { stn_lat = adf->get_lat(); stn_elev = adf->get_elev_ft(); range = adf->get_range(); - effective_range = kludgeRange(stn_elev, acft_elev, range); - x = adf->get_x(); - y = adf->get_y(); - z = adf->get_z(); + effective_range = kludgeRange(stn_elev, pos.getElevationM(), range); + xyz = adf->cart(); - if ( globals->get_soundmgr()->exists( "adf-ident" ) ) { - globals->get_soundmgr()->remove( "adf-ident" ); + if ( _sgr->exists( "adf-ident" ) ) { + // stop is required! -- remove alone wouldn't stop immediately + _sgr->stop( "adf-ident" ); + _sgr->remove( "adf-ident" ); } SGSoundSample *sound; - sound = morse.make_ident( trans_ident, LO_FREQUENCY ); + sound = FGMorse::instance()->make_ident( trans_ident, FGMorse::LO_FREQUENCY ); sound->set_volume( 0.3 ); - globals->get_soundmgr()->add( sound, "adf-ident" ); + _sgr->add( sound, "adf-ident" ); int offset = (int)(sg_random() * 30.0); play_count = offset / 4; @@ -538,7 +519,7 @@ void FGKR_87::search() { valid = false; ident = ""; trans_ident = ""; - globals->get_soundmgr()->remove( "adf-ident" ); + _sgr->remove( "adf-ident" ); last_ident = ""; // cout << "not picking up adf. :-(" << endl; }