Advertisement
DL5SMB

Untitled

May 5th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 26.46 KB | None | 0 0
  1. //*********************************************************************************
  2. //**
  3. //** Project.........: Magnetic Transmitting Loop Antenna Controller
  4. //**
  5. //**
  6. //** Copyright (C) 2015  Loftur E. Jonasson  (tf3lj [at] arrl [dot] net)
  7. //**
  8. //** This program is free software: you can redistribute it and/or modify
  9. //** it under the terms of the GNU General Public License as published by
  10. //** the Free Software Foundation, either version 3 of the License, or
  11. //** (at your option) any later version.
  12. //**
  13. //** This program is distributed in the hope that it will be useful,
  14. //** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. //** GNU General Public License for more details.
  17. //**
  18. //** You should have received a copy of the GNU General Public License
  19. //** along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20. //**
  21. //** Platform........: Teensy 3.1 & 3.2 (http://www.pjrc.com)
  22. //**                   (It may be possible to adapt this code to other
  23. //**                    Arduino compatible platforms, however this will
  24. //**                    require extensive rewriting of some portions of
  25. //**                    the code)
  26. //**
  27. //** Initial version.: 0.00, 2012-10-20  Loftur Jonasson, TF3LJ / VE2LJX
  28. //**                   (pre-alpha version)
  29. //**
  30. //**
  31. //*********************************************************************************
  32.  
  33. #if PSWR_AUTOTUNE
  34. //
  35. //-----------------------------------------------------------------------------------------
  36. //      SWR Tune routines
  37. //-----------------------------------------------------------------------------------------
  38. //
  39.  
  40. // Token passing flagsfor SWR tune process
  41. struct  {
  42.           unsigned Prepare  : 1;    // Do initial preparations
  43.           unsigned Progress : 2;    // Track tune progress
  44.           unsigned Ask_SWR  : 1;    // Request new SWR measurement - Yaesu special case...
  45.           unsigned InitPWR  : 1;    // Init Power Setting for Tune
  46.           unsigned InitMode : 1;    // Init Mode Setting for Tune
  47.          } tune;
  48.  
  49. int32_t sample[SWR_SAMPLEBUF];      // Ringbuffer for SWR averaging
  50. int32_t mid_swr;                    // The SWR at midpoint, SWR_SAMPLEBUF/2 samples ago
  51.  
  52. //
  53. //-----------------------------------------------------------------------------------------
  54. //      Find Best SWR - by sampling, summing and squaring a number of SWR values and
  55. //      comparing with a previous similar sum-square, advancing one sample at a time.
  56. //      once this sum-square value is larger than the previous one, and midpoint SWR
  57. //      (SWR value measured at sample_buffer_size/2 samples earlier) is at an acceptable
  58. //      level, then return TRUE, else return FALSE
  59. //      Relies on sample[] being seeded with 999 values before initial SWR search.
  60. //-----------------------------------------------------------------------------------------
  61. //
  62. int8_t find_best_swr(void)
  63. {
  64.   static int32_t sum_sq_swr;           // [SWR x 10] is summed over SWR_SAMPLEBUF number of steps
  65.   static int32_t old_sum_sq_swr=99999; // Previous sum
  66.   static int16_t a=0;                  // [SWR x 10] ring buffer counter
  67.   int16_t        b;
  68.   double         max_acceptable_swr;
  69.  
  70.   max_acceptable_swr = (controller_settings.swr_ok + 10)/10.0;
  71.  
  72.   // Find average of last SWR_SAMPLEBUF (10 - 50) measurements with two decimal point accuracy
  73.   // and compare with previous average
  74.   sample[a] = measured_swr*100;     // Get precision of 2 subdecimals.
  75.   a++;                              // Advance ringbuffer counter
  76.   if (a >= SWR_SAMPLEBUF) a = 0;
  77.  
  78.   // Derive the SWR measured SWR_SAMPLEBUF/2 + 1 times ago
  79.   b = a - (SWR_SAMPLEBUF/2 + 1);
  80.   if (b < 0) b = b + SWR_SAMPLEBUF;
  81.   mid_swr = sample[b];
  82.  
  83.   // Add up the measurements of the ringbuffer and find an average
  84.   sum_sq_swr = 0;
  85.   for (uint16_t b = 0; b < SWR_SAMPLEBUF; b++)
  86.   {
  87.     // Sum up the squares of the samples
  88.     sum_sq_swr += sample[b]*sample[b];
  89.   }
  90.  
  91.   // Determine if we have found a dip, return FALSE while not
  92.   if ((sum_sq_swr <= old_sum_sq_swr) || (mid_swr >= (max_acceptable_swr * 100)))
  93.   {
  94.     // Debug SWR dip, USB print one out of every SWR_SAMPLEBUF measurements made
  95.     if (meter.debug_swrdip && (a == 0))
  96.     {
  97.       sprintf(print_buf,"%1.01f ",sample[a]/100.0);
  98.       Serial.print(print_buf);
  99.     }
  100.  
  101.     old_sum_sq_swr = sum_sq_swr;
  102.     return false;
  103.   }
  104.   // We have found a dip, tidy up and report
  105.   else
  106.   {
  107.     // Debug SWR dip, USB print the last SWR_SAMPLEBUF measurements upon success
  108.     if (meter.debug_swrdip)
  109.     {
  110.       sprintf(print_buf,"\r\nmidSWR: %1.02f\r\n",(double) mid_swr/100);
  111.       Serial.print(print_buf);
  112.       b = a - SWR_SAMPLEBUF;
  113.       if (b < 0) b = b + SWR_SAMPLEBUF;
  114.       for (uint16_t x=0; x<SWR_SAMPLEBUF; x++)
  115.       {
  116.         sprintf(print_buf,"%1.02f ",(double) sample[b]/100);
  117.         Serial.print(print_buf);
  118.         b++;
  119.         if (b >= SWR_SAMPLEBUF) b = 0;
  120.       }
  121.       Serial.println();
  122.     }
  123.     snapshot_swr = (double) mid_swr/100.0;   // Indicate best SWR found
  124.     snapshot_swr_bar = 1000.0 * log10(snapshot_swr);  
  125.     old_sum_sq_swr = 99999;                 // Make ready for next time
  126.     return true;                            // Return success
  127.   }
  128. }
  129.      
  130.  
  131. //
  132. //-----------------------------------------------------------------------------------------
  133. //  SWR Tune functions, to be polled as rapidly as the stepper is serviced
  134. //            return 1 (TRUE) if autotune success (a minimum SWR found)
  135. //            return 2 (FAIL) if no minimum SWR was found
  136. //            return 0 (WORKING) while still hunting
  137. //
  138. // SWR_Hunt() for hunting around current position.  Default for SWR Tune
  139. // SWR_TuneUp()   for finding best SWR while tuning upwards.  Tunes until timeout or endstop
  140. // SWR_TuneDown() for finding best SWR while tuning downward. Tunes until timeout or endstop
  141. //-----------------------------------------------------------------------------------------
  142. //
  143.  
  144. //-----------------------------------------------------------------------------------------
  145. // SWR_Hunt() for hunting around current position.  Default for SWR Tune
  146. //-----------------------------------------------------------------------------------------
  147. int8_t SWR_Hunt(void)
  148. {
  149.   int32_t step_size;                      // Number of Microsteps to advance at a time
  150.                                  
  151.   step_size = pow(2,microstep_resolution);// 1 microstep is our smallest unit.
  152.                                           // 8 microsteps is our largest unit                                    
  153.   // First time run: Set up initial Tune Offset  
  154.   if (!tune.Prepare)
  155.   {
  156.     tune.Prepare = true;
  157.     delta_Pos[ant] = delta_Pos[ant] - SWR_HUNT_RANGE * 8; // Set up start position for SWR Hunt
  158.     tune.Progress = WORKING;
  159.     tune.Ask_SWR = false;
  160.     radio.swr = false;
  161.     // Prepare SWR average buffer for use
  162.     for (uint16_t a = 0; a < SWR_SAMPLEBUF; a++) sample[a] = 99999;
  163.   }
  164.  
  165.   #if ENDSTOP_OPT == 1
  166.   // If at lower endstop, then modify the delta_Pos until resolved
  167.   if (flag.cap_lower_pos || (abs(stepper_track[ant] - min_preset[ant].Pos) < step_size))
  168.   {
  169.     delta_Pos[ant] += step_size;          // Increase by one step
  170.   }
  171.   // If at upper endstop, then we have reached the end of the line without resoution
  172.   else if (flag.cap_upper_pos)
  173.   {
  174.     tune.Prepare = false;
  175.     tune.Progress = FAIL;
  176.   }
  177.   else
  178.   #elif ENDSTOP_OPT == 2
  179.   // If at lower endstop, then modify the delta_Pos until resolved
  180.   if (flag.endstop_lower)
  181.   {
  182.     delta_Pos[ant] += step_size;          // Increase by one step
  183.   }
  184.   // If at upper endstop, then we have reached the end of the line without resoution
  185.   else if (flag.endstop_upper)
  186.   {
  187.     tune.Prepare = false;
  188.     tune.Progress = FAIL;
  189.   }
  190.   else
  191.   #endif
  192.   // Whenever we are at the expected position, then do stuff
  193.   //The below is equivalent of this if(stepper_track == running.Pos + delta_Pos)
  194.   // taking into account microstep settings:
  195.   if (abs(stepper_track[ant] - (running[ant].Pos + delta_Pos[ant])) < step_size)
  196.   {
  197.     if (fwd_power_mw > MIN_PWR_FOR_SWR_CALC)    // Do we have sufficient power from Radio?
  198.     {
  199.       // Advance while unacceptably high SWR and until Old average lower than new average
  200.       // (gone SWR_SAMPLEBUF/2 number of steps beyond best resonance)
  201.       if (!find_best_swr() )                    // Returns TRUE if we find a good SWR dip
  202.       {
  203.         delta_Pos[ant] += step_size;            // Increase by one step
  204.      
  205.         // If we're here, then the tune process failed.
  206.         if (delta_Pos[ant] >= (tmp_delta_Pos + SWR_HUNT_RANGE * 8))
  207.         {
  208.           tune.Prepare = false;                 // Clean up for next time
  209.           tune.Progress = FAIL;
  210.         }
  211.       }
  212.       // We found the the lowest SWR and passed it, back up and exit      
  213.       else
  214.       {
  215.         // Back off by half the size of the averaging buffer to find best tune
  216.         delta_Pos[ant] = (int32_t) delta_Pos[ant] - step_size * (SWR_SAMPLEBUF/2 + 1);              
  217.         tune.Prepare = false;                   // Clean up for next time
  218.         tune.Progress = SUCCESS;
  219.       }
  220.     }
  221.     // FAIL - Not enough Transmit Power to determine SWR
  222.     else
  223.     {
  224.       tune.Prepare = false;                     // Clean up for next time
  225.       tune.Progress = NOPWR;
  226.     }
  227.   }  
  228.   return tune.Progress;  
  229. }
  230.  
  231. //-----------------------------------------------------------------------------------------
  232. // SWR_TuneUp() for finding best SWR while tuning upwards
  233. //-----------------------------------------------------------------------------------------
  234. int8_t SWR_TuneUp(void)
  235. {
  236.   int32_t step_size;                      // Number of Microsteps to advance at a time
  237.   int32_t butterfly_travel;               // keep tabs of number of steps scanned while tuning,
  238.                                           // used with Butterfly cap to impose max boundary
  239.                                  
  240.   step_size = pow(2,microstep_resolution);// 1 microstep is our smallest unit.
  241.                                           // 8 microsteps is our largest unit                                    
  242.   // First time run: Set up initial Tune Parameters  
  243.   if (!tune.Prepare)
  244.   {
  245.     tune.Prepare = true;
  246.     tune.Progress = WORKING;
  247.     tune.Ask_SWR = false;
  248.     radio.swr = false;
  249.     butterfly_travel = 0;                 // Only used with butterfly caps
  250.     // Prepare SWR average buffer for use
  251.     for (uint16_t a = 0; a < SWR_SAMPLEBUF; a++) sample[a] = 99999;
  252.   }
  253.  
  254.   #if ENDSTOP_OPT == 1
  255.   // If at upper endstop, then we have reached the end of the line without resoution
  256.   if (flag.cap_upper_pos || (abs(stepper_track[ant] - max_preset[ant].Pos) < step_size))
  257.   #elif ENDSTOP_OPT == 2
  258.   // If at upper endstop, then we have reached the end of the line without resoution
  259.   if (flag.endstop_upper)
  260.   #elif ENDSTOP_OPT == 3
  261.   // Butterfly, no endstops - but need to add some max distance for sanity
  262.   if (butterfly_travel >= BUTTERFLY_MAX_TRAVEL)
  263.   #endif
  264.   {
  265.     tune.Prepare = false;
  266.     tune.Progress = FAIL;
  267.   }
  268.   // Whenever we are at the expected position, then do stuff
  269.   //The below is equivalent of this if(stepper_track == running.Pos + delta_Pos)
  270.   // taking into account microstep settings:
  271.   else if (abs(stepper_track[ant] - (running[ant].Pos + delta_Pos[ant])) < step_size)
  272.   {
  273.     if (fwd_power_mw > MIN_PWR_FOR_SWR_CALC)    // Do we have sufficient power from Radio?
  274.     {
  275.       // Advance while unacceptably high SWR and until Old average lower than new average
  276.       // (gone SWR_SAMPLEBUF/2 number of steps beyond best resonance)
  277.       if (!find_best_swr() )                    // Returns TRUE if we find a good SWR dip
  278.       {
  279.         delta_Pos[ant] += step_size;            // Increase by one step    
  280.         butterfly_travel++;                     // Increment tune travel for Butterfly Cap special case
  281.       }
  282.       // We found the the lowest SWR and passed it, back up and exit      
  283.       else
  284.       {
  285.         // Back off by half the size of the averaging buffer to find best tune
  286.         delta_Pos[ant] = (int32_t) delta_Pos[ant] - step_size * (SWR_SAMPLEBUF/2 + 1);              
  287.         tune.Prepare = false;                   // Clean up for next time
  288.         tune.Progress = SUCCESS;
  289.       }
  290.     }
  291.     // FAIL - Not enough Transmit Power to determine SWR
  292.     else
  293.     {
  294.       tune.Prepare = false;                     // Clean up for next time
  295.       tune.Progress = NOPWR;
  296.     }
  297.   }  
  298.   return tune.Progress;  
  299. }
  300.  
  301. //-----------------------------------------------------------------------------------------
  302. // SWR_TuneDown() for finding best SWR while tuning downward
  303. //-----------------------------------------------------------------------------------------
  304. int8_t SWR_TuneDown(void)
  305. {
  306.   int32_t step_size;                      // Number of Microsteps to advance at a time
  307.   int32_t butterfly_travel;               // keep tabs of number of steps scanned while tuning,
  308.                                           // used with Butterfly cap to impose max boundary
  309.                                  
  310.   step_size = pow(2,microstep_resolution);// 1 microstep is our smallest unit.
  311.                                           // 8 microsteps is our largest unit                                    
  312.   // First time run: Set up initial Tune Parameters
  313.   if (!tune.Prepare)
  314.   {
  315.     tune.Prepare = true;
  316.     tune.Progress = WORKING;
  317.     tune.Ask_SWR = false;
  318.     radio.swr = false;
  319.     butterfly_travel = 0;                 // Only used with butterfly caps
  320.     // Prepare SWR average buffer for use
  321.     for (uint16_t a = 0; a < SWR_SAMPLEBUF; a++) sample[a] = 99999;
  322.   }
  323.  
  324.   #if ENDSTOP_OPT == 1
  325.   // If at lower endstop, then we have reached the end of the line without resoution
  326.   if (flag.cap_lower_pos || (abs(stepper_track[ant] - min_preset[ant].Pos) < step_size))
  327.   #elif ENDSTOP_OPT == 2
  328.   // If at lower endstop, then we have reached the end of the line without resoution
  329.   if (flag.endstop_lower)
  330.   #elif ENDSTOP_OPT == 3
  331.   // Butterfly, no endstops - but need to add some max distance for sanity
  332.   if (butterfly_travel >= BUTTERFLY_MAX_TRAVEL)
  333.   #endif
  334.   {
  335.     tune.Prepare = false;
  336.     tune.Progress = FAIL;
  337.   }
  338.   // Whenever we are at the expected position, then do stuff
  339.   //The below is equivalent of this if(stepper_track == running.Pos + delta_Pos)
  340.   // taking into account microstep settings:
  341.   else if (abs(stepper_track[ant] - (running[ant].Pos + delta_Pos[ant])) < step_size)
  342.   {
  343.     if (fwd_power_mw > MIN_PWR_FOR_SWR_CALC)    // Do we have sufficient power from Radio?
  344.     {
  345.       // Advance while unacceptably high SWR and until Old average lower than new average
  346.       // (gone SWR_SAMPLEBUF/2 number of steps beyond best resonance)
  347.       if (!find_best_swr() )                    // Returns TRUE if we find a good SWR dip
  348.       {
  349.         delta_Pos[ant] -= step_size;            // Decrease by one step
  350.         butterfly_travel++;                     // Increment tune travel for Butterfly Cap special case
  351.       }
  352.       // We found the the lowest SWR and passed it, back up and exit      
  353.       else
  354.       {
  355.         // Back off by half the size of the averaging buffer to find best tune
  356.         delta_Pos[ant] = (int32_t) delta_Pos[ant] - step_size * (SWR_SAMPLEBUF/2 + 1);              
  357.         tune.Prepare = false;                   // Clean up for next time
  358.         tune.Progress = SUCCESS;
  359.       }
  360.     }
  361.     // FAIL - Not enough Transmit Power to determine SWR
  362.     else
  363.     {
  364.       tune.Prepare = false;                     // Clean up for next time
  365.       tune.Progress = NOPWR;
  366.     }
  367.   }  
  368.   return tune.Progress;  
  369. }
  370.  
  371.  
  372. //
  373. //-----------------------------------------------------------------------------------------
  374. //      Setup for SWR tune.
  375. //      If Transceiver is fully controllable, then set it up to transmit a tune carrier
  376. //      Poll this routine once every 100ms until success.
  377. //      Returns 0 (WORKING) while still working, 1 (SUCCESS) when Transceiver is ready
  378. //      to go,and 2 (FAIL) if transceiver did not respond to commands.
  379. //-----------------------------------------------------------------------------------------
  380. //
  381. int8_t Radio_TuneInit(void)
  382. {
  383.   static uint8_t progress;            // progress: 0=WORKING, 1=SUCCESS, 2=FAIL
  384.   static uint16_t timer;              // response timer
  385.   if (!radio.tunefirst)
  386.   {
  387.     radio.tunefirst = true;           // Init everything once
  388.     radio.pwr = false;                // Ensure we do not have stale Power Level data from Radio
  389.     radio.pwr_available = true;       // Init Power Available Command Supported flag
  390.     radio.mode = false;               // Ensure we do not have stale Mode data from Radio
  391.     tune.InitPWR = false;             // Prepare to ask for Power Level data
  392.     tune.InitMode = false;            // Prepare to ask for Mode data
  393.     radio.ack = WORKING;              // Only used with ICOM Radios
  394.     progress = WORKING;               // Indicate that we are working to retrieve data
  395.   }
  396.  
  397.   // Check whether Transmitter is already puming out RF
  398.   measure_power_and_swr();            // Ensure we have one measurement available
  399.   if (fwd_power_mw >= MIN_PWR_FOR_SWR_CALC)
  400.   {
  401.     swr.rfactive_mode = true;         // We're good to go, no setup required
  402.     progress = SUCCESS;               // Setup for Autotune is initiated  
  403.   }
  404.  
  405.   if (!swr.rfactive_mode)             // We need to ask the Radio into Tune Mode
  406.   {
  407.     // Request Power level only once
  408.     if (!radio.pwr && !tune.InitPWR)
  409.     {
  410.       tune.InitPWR = true;
  411.       timer = 10;                     // Give max 1 second for command rsponse
  412.       progress = trx_request_pwr();   // Returns FAIL if not implemented for this radio
  413.     }
  414.     // Only ICOM: If Radio does not support the Power request command, then capture this (some older ICOM)
  415.     else if  (tune.InitPWR && (radio.ack == FAIL))
  416.     {
  417.       radio.pwr = true;
  418.       radio.pwr_available = false;
  419.     }
  420.     // Request Mode only once, if Power level has been received
  421.     if (radio.pwr && !radio.mode && !tune.InitMode)
  422.     {
  423.       tune.InitMode = true;
  424.       timer = 10;                     // Give max 1 second for command rsponse
  425.       progress = trx_request_mode();  // Returns FAIL if not implemented for this radio
  426.     }
  427.    
  428.     timer--;
  429.    
  430.     // If we have received Mode data, then we're good to go
  431.     if (radio.mode)
  432.     {
  433.       trx_set_am_mode();              // AM mode
  434.       delayloop(50);
  435.       if (radio.pwr_available) trx_set_min_pwr();  // Set Minimal power
  436.       delayloop(50);
  437.       trx_set_tx();                   // TX mode
  438.       delayloop(250);                 // Give time for Power to come up
  439.       measure_power_and_swr();        // Ensure we have one measurement available
  440.       progress = SUCCESS;             // Setup for Autotune is initiated  
  441.     }
  442.    
  443.     // Timer has expired without receiving response from radio - FAIL
  444.     else if (timer == 0)
  445.     {
  446.       progress = FAIL;
  447.     }
  448.   }
  449.   return progress;
  450. }
  451.  
  452. //
  453. //-----------------------------------------------------------------------------------------
  454. //      Return Transceiver from Autotune.
  455. //      (No test for radio response)
  456. //-----------------------------------------------------------------------------------------
  457. //
  458. void Radio_TuneEnd(void)
  459. {
  460.   if (!swr.rfactive_mode)          // Only do this if required
  461.   {    
  462.     trx_set_rx();                  // Back to RX mode
  463.     delayloop(50);
  464.     trx_restore_mode();            // Restore previous mode setting
  465.     delayloop(50);
  466.     if (radio.pwr_available) trx_restore_pwr();   // Restore previous Power Level setting
  467.     radio.mode = false;            // Clean up for next Auto Tune
  468.     radio.pwr = false;
  469.   }
  470.   swr.tune_request = false;
  471.   swr.tune = false;                // Drop out of SWR Tune Mode
  472.   swr.up_mode = false;
  473.   swr.down_mode = false;
  474.   swr.rfactive_mode = false;
  475.  
  476. }
  477.  
  478. //
  479. //-----------------------------------------------------------------------------------------
  480. //      High Level SWR Tune Mode stuff - polled every 100ms
  481. //-----------------------------------------------------------------------------------------
  482. //
  483. void swr_tune_functions(void)
  484. {
  485.   // Used to prevent an automatic retune before stepper motor has settled from last time
  486.   if (SWRautoretune_timer > 0)
  487.   {
  488.     SWRautoretune_timer--;                   // Count down the Autoretune timer
  489.   }
  490.  
  491.   // New Request, things to do once...
  492.   if (swr.tune_request && !swr.tune)         // New request
  493.   {
  494.     swr.tune = true;                         // Init everything once
  495.     radio.tuneinit = WORKING;                // Flag to indicate we need to Set up Radio
  496.     radio.tunefirst = false;                 // Flag to indicate whether first Radio TuneInit poll
  497.     tmp_delta_Pos = delta_Pos[ant];          // Offset the delta_Pos for autotune scan      
  498.     swr.tune_status = WORKING;               // Indicate SWR tune is in Progress
  499.     virt_lcd_clear();                        // Announce on LCD
  500.     virt_lcd_setCursor(0, 0);
  501.     virt_lcd_print("SWR Tune!!!");        
  502.     Menu_exit_timer = 0;                     // Zero Menu Display timer, if previously running
  503.     // Force a reprint of Config Menu upon return, if we went here from there
  504.     flag.menu_lcd_upd = false;
  505.  
  506.   }
  507.  
  508.   // SWR Tune is indicated as being in progress, do stuff once every 100ms
  509.   //
  510.   if (swr.tune)
  511.   {
  512.     // Stuff to do while SWR Tune timer does not indicate timeout
  513.     //
  514.     if (SWRtune_timer > 0)
  515.     {
  516.       SWRtune_timer--;                       // Count down the SWR tune timer
  517.      
  518.       // Radio Tune Init and special cases for Up tune and Down tune
  519.       //
  520.       if (radio.tuneinit == WORKING)         // Setup Transceiver for SWR tune
  521.       {
  522.         // Instruct Radio to go into Tune Mode (Minimum Power Out and AM or similar)
  523.         radio.tuneinit = Radio_TuneInit();   // If radio.tuneinit == SUCCESS, then SWR tuning enabled  
  524.       }      
  525.       else if((radio.tuneinit == SUCCESS) && swr.up_mode_request && !swr.up_mode)
  526.       {
  527.         swr.up_mode_request = false;
  528.         delta_Pos[ant] = tmp_delta_Pos;      // Restore original position
  529.         swr.up_mode = true;                  // Indicate up mode active (normally hunt mode active first)
  530.         swr.down_mode = false;               // Deactivate down mode, if previously active
  531.         virt_lcd_setCursor(0, 0);            // Announce on LCD
  532.         virt_lcd_print("SWR Tune UP!!!");        
  533.         Menu_exit_timer = 0;                 // Zero Menu Display timer, if previously running
  534.       }
  535.       else if((radio.tuneinit == SUCCESS) && swr.down_mode_request && !swr.down_mode)
  536.       {
  537.         swr.down_mode_request = false;
  538.         delta_Pos[ant] = tmp_delta_Pos;      // Restore original position
  539.         swr.down_mode = true;
  540.         swr.up_mode = false;
  541.         virt_lcd_setCursor(0, 0);            // Announce on LCD
  542.         virt_lcd_print("SWR Tune DOWN!!!");
  543.         Menu_exit_timer = 0;                 // Zero Menu Display timer, if previously running
  544.       }
  545.            
  546.       // Radio Tune end, success and fail conditions
  547.       //
  548.       else if (swr.tune_status == SUCCESS)   // Was the SWR tune Successful?
  549.       {
  550.         virt_lcd_setCursor(0, 0);
  551.         virt_lcd_print("SWR Tune Success!!  ");
  552.         swr.lcd_snapshot = true;             // Allow one last print to LCD, even though
  553.                                              // Menu_exit_timer is seeded.        
  554.         Menu_exit_timer = 30;                // Show on LCD for 3 seconds
  555.         lcd_display();                       // Force display of best SWR
  556.         Radio_TuneEnd();                     // Instruct Radio back to normal mode
  557.         swr.fail_counter = 0;                // Indicate that last tune operation was a success
  558.         SWRautoretune_timer = 20;            // Set SWR Autoretune timer at 2 seconds
  559.       }
  560.       else if (radio.tuneinit == FAIL)       // Radio failed to respond to setup commands
  561.       {
  562.         Radio_TuneEnd();                     // Instruct Radio back to normal mode (just to make sure)
  563.         delta_Pos[ant] = tmp_delta_Pos;      // Restore the original position
  564.         virt_lcd_setCursor(0, 0);
  565.         if (controller_settings.trx[controller_settings.radioprofile].radio == 10) // Special case, Pseudo-VFO
  566.         {
  567.           virt_lcd_print("Xmit Carrier to Tune");
  568.         }
  569.         else
  570.         {
  571.           virt_lcd_print("Radio not responding");
  572.         }        
  573.         Menu_exit_timer = 30;                // Show on LCD for 3 seconds
  574.         SWRautoretune_timer = 20;            // Set SWR Autoretune timer at 2 seconds
  575.         if (swr.fail_counter < 3) swr.fail_counter++; // Indicate that last tune operation failed
  576.       }
  577.       else if (swr.tune_status == FAIL)      // Did the the SWR tune fail to find a dip?
  578.       {
  579.         Radio_TuneEnd();                     // Instruct Radio back to normal mode
  580.         delta_Pos[ant] = tmp_delta_Pos;      // Restore the original position
  581.         virt_lcd_setCursor(0, 0);
  582.         virt_lcd_print("SWR Tune Failed!!   ");        
  583.         Menu_exit_timer = 30;                // Show on LCD for 3 seconds
  584.         SWRautoretune_timer = 20;            // Set SWR Autoretune timer at 2 seconds
  585.         if (swr.fail_counter < 3) swr.fail_counter++; // Indicate that last tune operation failed
  586.       }
  587.       else if (swr.tune_status == NOPWR)     // Insufficient power for SWR measurement
  588.       {
  589.         Radio_TuneEnd();                     // Instruct Radio back to normal mode
  590.         delta_Pos[ant] = tmp_delta_Pos;      // Restore the original position
  591.         virt_lcd_setCursor(0, 0);
  592.         virt_lcd_print("No RF Detected!!    ");        
  593.         SWRautoretune_timer = 20;            // Set SWR Autoretune timer at 2 seconds
  594.         Menu_exit_timer = 30;                // Show on LCD for 3 seconds
  595.         if (swr.fail_counter < 3) swr.fail_counter++; // Indicate that last tune operation failed
  596.       }
  597.     }
  598.     // SWR Tune timer timeout
  599.     //
  600.     else                                     // Timeout - Indicate SWRtune fail
  601.     {
  602.       delta_Pos[ant] = tmp_delta_Pos;        // Restore the original position
  603.       Radio_TuneEnd();
  604.       virt_lcd_setCursor(0, 0);
  605.       virt_lcd_print("SWR Tune Timeout!!! ");
  606.       Menu_exit_timer = 30;                   // Show on LCD for 3 seconds      
  607.       SWRautoretune_timer = 20;            // Set SWR Autoretune timer at 2 seconds
  608.       if (swr.fail_counter < 3) swr.fail_counter++; // Indicate that last tune operation failed
  609.     }
  610.   }  
  611. }
  612. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement