private void TuneATU() { //console.FWCATUTuned(); Debug.WriteLine("***Entering TuneATU()*****"); HiPerfTimer t1 = new HiPerfTimer(); t1.Start(); Thread t2 = new Thread(new ThreadStart(progressBarTuning)); t2.Name = "ATU Progress Bar Thread"; t2.IsBackground = true; t2.Priority = ThreadPriority.Normal; t2.Start(); double start_swr = 99; int SLEEP_TIME = (int)udSleepTime.Value; int OFF_SLEEP = (int)udOffSleep.Value; int TUN_LVL = (int)udTunPower.Value; int old_power = console.TunePower; if (abort) { last_tune_result = TuneResult.TuneAborted; goto end; } string last_tune_result_string = last_tune_result.ToString(); string version = console.getVersion(); button1.Focus(); try { #if(!NO_NEW_ATU) ATUClass obj = new ATUClass(); MemoryTune.Hide(); console.TunePower = 10; //Thread.Sleep(OFF_SLEEP); // Audio_RadioVolumeSaved = Audio.RadioVolume; version = console.getVersion(); ////////////***load atu profiles if ATU button is pressed first if (comboAntProfileName.Items.Count == 0) { profileList = obj.Init(version, ref currentProfileName, true, ref high_swr, ref high_swr_no_limit, ref swr_thresh, ref swr_retune_target); } else { profileList = obj.Init(version, ref currentProfileName, false, ref high_swr, ref high_swr_no_limit, ref swr_thresh, ref swr_retune_target); } if (profileList.Count == 0 && comboAntProfileName.Items.Count == 0) { currentProfileName = "Default Profile"; comboAntProfileName.Items.Add(currentProfileName); comboAntProfileName.SelectedItem = currentProfileName; } else { foreach (string s in profileList) { if (!comboAntProfileName.Items.Contains(s)) { comboAntProfileName.Items.Add(s); } } comboAntProfileName.SelectedItem = currentProfileName; //last saved profile (from database) } ///////*** obj.DoWork( (int)console.RX1Band, autoMode, ref high_swr, ref high_swr_no_limit, ref swr_retune_target, ref swr_thresh, force_retune, ref currentProfileName, ref deletedProfileList, ref start_swr, console.atu_tuning, ref version, ref SLEEP_TIME, ref OFF_SLEEP, ref TUN_LVL, ref byp_swr, ref tun_swr, ref byp_fwd_pow, ref tun_fwd_pow, ref byp_ref_pow, ref tun_ref_pow, ref abort, ref tuning, console.VFOAFreq, ref last_tune_result_string, FWC.SetATUEnable, FWC.SetHiZ, SetL, SetC, getSWR, console_TunePower, //console_TunePower, Audio_RadioVolume, ref Audio_RadioVolumeSaved, console_TUN, console_TUN, rdBypass_Checked, rdBypass_Checked, rdTune_Checked, rdTune_Checked, setlblHiZ_Visible); //autoMode = false; if (start_swr == 99 && !rdBypass.Checked) { MemoryTune.Show(); } #endif } catch (Exception) { } if (last_tune_result_string == "TuneSuccessful") last_tune_result = TuneResult.TuneSuccessful; else if (last_tune_result_string == "TuneOK") last_tune_result = TuneResult.TuneOK; else if (last_tune_result_string == "TuneFailedHighSWR") last_tune_result = TuneResult.TuneFailedHighSWR; else if (last_tune_result_string == "TuneAborted") last_tune_result = TuneResult.TuneAborted; else if (last_tune_result_string == "TunerNotNeeded") last_tune_result = TuneResult.TunerNotNeeded; else if (last_tune_result_string == "TuneFailedNoRF") last_tune_result = TuneResult.TuneFailedNoRF; else if (last_tune_result_string == "TuneAutoModeTune") last_tune_result = TuneResult.TuneAutoModeTune; else if (last_tune_result_string == "TuneAutoModeBypass") last_tune_result = TuneResult.TuneAutoModeBypass; end: t1.Stop(); Debug.WriteLine("The SWR has been reduced to " + tun_swr + " from " + byp_swr + "(" + start_swr + ") in " + t1.Duration.ToString("f1") + " seconds"); LogMessageToFile("****The SWR at " + console.VFOAFreq + " MHz has been reduced to " + tun_swr + " from " + byp_swr + "(" + start_swr + ") in " + t1.Duration.ToString("f1") + " seconds***"); tuning = false; console.TUN = false; console.TunePower = old_power; rdTune.Checked = false; console.atu_tuning = false; abort = false; lblTime.Text = "Time: " + t1.Duration.ToString("f1"); UpdateFeedbackNew(); tuningDLL = false; autoMode = false; }
private void Tune() { int fwd, rev; HiPerfTimer t1 = new HiPerfTimer(); t1.Start(); //const int SLEEP_TIME = 300; int SLEEP_TIME = (int)udSleepTime.Value; double HIGH_SWR = (double)udHighSWR.Value; double swr_thresh = 1.1; // start at 1.1, switch to 1.5 after 5 sec. int OFF_SLEEP = (int)udOffSleep.Value; int MaxL = 256; int MaxC = 128; int TUN_LVL = (int)udTunPower.Value; console.atu_tuning = true; // check in bypass FWC.SetATUEnable(false); FWC.SetHiZ(false); Thread.Sleep(50); lblHiZ.Visible = false; int old_power = console.TunePower; console.TunePower = TUN_LVL; console.TUN = true; tuning = true; Thread.Sleep(500); if (abort) { last_tune_result = TuneResult.TuneAborted; goto end; } // if the match is good enough, then just leave it bypassed FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; byp_fwd_pow = console.FWCPAPower(fwd); byp_ref_pow = console.FWCPAPower(rev) * console.atu_swr_table[(int)console.TXBand]; byp_swr = SWR(fwd, rev); tun_swr = byp_swr; if (byp_fwd_pow < 1) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneFailedNoRF; goto end; } if (byp_swr < swr_thresh) { rdBypass.Checked = true; last_tune_result = TuneResult.TunerNotNeeded; goto end; } else if (byp_swr > HIGH_SWR) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneFailedHighSWR; goto end; } // check HiZ console.TunePower = 0; Thread.Sleep(OFF_SLEEP); FWC.SetATUEnable(true); Thread.Sleep(OFF_SLEEP); SetC(8); SetL(8); FWC.SetHiZ(false); console.TunePower = TUN_LVL; Thread.Sleep(SLEEP_TIME); if (abort) { last_tune_result = TuneResult.TuneAborted; goto end; } FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; double lo_z_swr = SWR(fwd, rev); tun_swr = lo_z_swr; console.TunePower = 0; Thread.Sleep(OFF_SLEEP); FWC.SetHiZ(true); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; Thread.Sleep(SLEEP_TIME); if (abort) { last_tune_result = TuneResult.TuneAborted; goto end; } FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; double hi_z_swr = SWR(fwd, rev); tun_swr = hi_z_swr; Debug.WriteLine("lo: " + lo_z_swr.ToString("f1") + " hi: " + hi_z_swr.ToString("f1")); double min_swr = double.MaxValue; if (hi_z_swr < lo_z_swr) { lblHiZ.Visible = true; min_swr = hi_z_swr; } else { lblHiZ.Visible = false; console.TunePower = 0; Thread.Sleep(OFF_SLEEP); FWC.SetHiZ(false); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; min_swr = lo_z_swr; } tun_fwd_pow = console.FWCPAPower(console.pa_fwd_power); if (byp_fwd_pow < 1) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneFailedLostRF; goto end; } if (min_swr < swr_thresh) { rdTune.Checked = false; last_tune_result = TuneResult.TuneSuccessful; goto end; } console.TunePower = 0; Thread.Sleep(OFF_SLEEP); SetL(0); SetC(0); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; Thread.Sleep(SLEEP_TIME); if (abort) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneAborted; goto end; } int count = 0; int l_step = 8; int c_step = 8; int min_l = 0, L = 0; int min_c = 0, C = 0; int no_progress = 0; FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; min_swr = SWR(fwd, rev); bool first_time = true; while (rdTune.Checked) { double start_swr = min_swr; while (L >= 0 && L <= MaxL && rdTune.Checked) { if (!first_time) Thread.Sleep(SLEEP_TIME); else first_time = false; if (abort) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneAborted; goto end; } t1.Stop(); if (t1.Duration > 5.0 && swr_thresh < 1.5) swr_thresh = 1.5; if (t1.Duration > 15.0) no_progress = 100; FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; tun_fwd_pow = console.FWCPAPower(fwd); tun_ref_pow = console.FWCPAPower(rev) * console.atu_swr_table[(int)console.TXBand]; if (byp_fwd_pow < 1) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneFailedLostRF; goto end; } tun_swr = SWR(fwd, rev); Debug.WriteLine("swr (" + L + ", " + C + "): " + tun_swr.ToString("f1") + " min: " + min_swr.ToString("f1") + " start: " + start_swr.ToString("f1")); if (tun_swr < swr_thresh) { rdTune.Checked = false; last_tune_result = TuneResult.TuneSuccessful; goto end; } if (tun_swr < min_swr) { min_swr = tun_swr; min_l = L; } if (tun_swr > min_swr + 0.3) { l_step *= -1; break; } if (count++ * Math.Abs(l_step) > 32 && min_swr >= start_swr - 0.05) break; if (!rdTune.Checked) break; console.TunePower = 0; Thread.Sleep(OFF_SLEEP); SetL(L += l_step); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; } console.TunePower = 0; Thread.Sleep(OFF_SLEEP); SetL(min_l); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; L = min_l; if (min_swr >= start_swr - 0.05) no_progress++; if (no_progress > 6) { rdTune.Checked = false; last_tune_result = TuneResult.TuneOK; goto end; } Debug.Write("C"); count = 0; start_swr = min_swr; while (C >= 0 && C <= MaxC && rdTune.Checked) { Thread.Sleep(SLEEP_TIME); if (abort) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneAborted; goto end; } t1.Stop(); if (t1.Duration > 5.0 && swr_thresh < 1.5) swr_thresh = 1.5; if (t1.Duration > 15.0) no_progress = 100; FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; tun_fwd_pow = console.FWCPAPower(fwd); tun_ref_pow = console.FWCPAPower(rev) * console.atu_swr_table[(int)console.TXBand]; if (byp_fwd_pow < 1) { rdBypass.Checked = true; last_tune_result = TuneResult.TuneFailedLostRF; goto end; } tun_swr = SWR(fwd, rev); Debug.WriteLine("swr (" + L + ", " + C + "): " + tun_swr.ToString("f1") + " min: " + min_swr.ToString("f1") + " start: " + start_swr.ToString("f1")); if (tun_swr < swr_thresh) { rdTune.Checked = false; last_tune_result = TuneResult.TuneSuccessful; goto end; } if (tun_swr < min_swr) { min_swr = tun_swr; min_c = C; } if (tun_swr > min_swr + 0.3) { c_step *= -1; break; } if (count++ * Math.Abs(c_step) > 32 && min_swr >= start_swr - 0.05) break; if (!rdTune.Checked) break; console.TunePower = 0; Thread.Sleep(OFF_SLEEP); SetC(C += c_step); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; } console.TunePower = 0; Thread.Sleep(OFF_SLEEP); SetC(min_c); Thread.Sleep(OFF_SLEEP); console.TunePower = TUN_LVL; C = min_c; count = 0; if (min_swr >= start_swr - 0.05) no_progress++; if (no_progress > 6) { rdTune.Checked = false; if (byp_swr > tun_swr) { last_tune_result = TuneResult.TuneOK; } else { last_tune_result = TuneResult.TuneFailedHighSWR; rdBypass.Checked = true; } goto end; } if (Math.Abs(l_step) > 1) l_step /= 2; if (Math.Abs(c_step) > 1) c_step /= 2; Debug.Write("L"); } end: switch (last_tune_result) { case TuneResult.TuneSuccessful: case TuneResult.TuneOK: Thread.Sleep(SLEEP_TIME); FWC.ReadPAADC(5, out fwd); console.pa_fwd_power = fwd; FWC.ReadPAADC(4, out rev); console.pa_rev_power = rev; tun_fwd_pow = console.FWCPAPower(fwd); tun_ref_pow = console.FWCPAPower(rev) * console.atu_swr_table[(int)console.TXBand]; tun_swr = SWR(fwd, rev); if (tun_swr > byp_swr) { last_tune_result = TuneResult.TunerNotNeeded; rdBypass.Checked = true; } Thread.Sleep(100); break; } //Debug.WriteLine("swr: "+swr.ToString("f1")); // cleanup tuning = false; console.TUN = false; console.TunePower = old_power; rdTune.Checked = false; console.atu_tuning = false; abort = false; //Invoke(new MethodInvoker(UpdateFeedback)); UpdateFeedback(); t1.Stop(); lblTime.Text = "Time: " + t1.Duration.ToString("f1"); }