Example #1
0
        public TuningResult calculateMediumTuning(double lastDAC, double lastPhase, int nTime)
        {
            if (state == MediumState.TUNING_SLEEP_1)
            {
                tunningSleepCounter++;
                if (tunningSleepCounter == TUNNING_SLEEP_TIME)
                {
                    resetMediumTuning(); // idemo opet da mjerimo 2 blocka
                    DAC_frame_1 = lastDAC;
                    state       = MediumState.MEASURING_BLOCK_1;
                }
                return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED));
            }
            else if (state == MediumState.MEASURING_BLOCK_1)
            {
                slidingFrame_1.AddPoint(lastPhase);

                if (slidingFrame_1.finished) //   Finished first block. If phase is crossing zero soon, do not measure block2, just set last calculated optimal dac.
                {
                    double crossingZeroTime = getZerroCrosingTime();
                    if (crossingZeroTime >= 0 && crossingZeroTime < TUNNING_SLEEP_TIME + FRAME_SIZE)
                    {
                        if (lastOptimalDac != 0)
                        {
                            calculatedDAC       = lastOptimalDac;             // Crossing zero soon. Set optimal dac and do not make a measure block 2
                            state               = MediumState.TUNING_SLEEP_1; // do not go to tuning state 2 (back to start).
                            tunningSleepCounter = 0;
                            writeServiceFile("Time: " + nTime + ". crossingZeroTime = " + crossingZeroTime + ". Use last Optimal DAC = " + lastOptimalDac);
                            return(new TuningResult(lastOptimalDac, TuningResult.Result.NOT_FINISHED));
                        }
                    }
                    else
                    {
                        writeServiceFile("Time: " + nTime + ". crossingZeroTime not soon = " + crossingZeroTime + ". Measure block 2");
                    }

                    state         = MediumState.TUNING_SLEEP_2;                               //  MEASURING_BLOCK_2;
                    calculatedDAC = lastDAC + calculateDacStep(slidingFrame_1.phaseAvg_stop); //   block_1.phaseAvg_stop);
                    DAC_frame_2   = calculatedDAC;
                }
                else
                {
                    calculatedDAC = lastDAC;
                }
                return(new TuningResult(calculatedDAC, TuningResult.Result.NOT_FINISHED));
            }
            else if (state == MediumState.TUNING_SLEEP_2)
            {
                tunningSleepCounter++;
                if (tunningSleepCounter == TUNNING_SLEEP_TIME)
                {
                    tunningSleepCounter = 0;
                    state = MediumState.MEASURING_BLOCK_2;
                }
                return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED));
            }
            else if (state == MediumState.MEASURING_BLOCK_2)
            {
                slidingFrame_2.AddPoint(lastPhase);

                if (slidingFrame_2.finished)                                                                                      //measure_blocks_2(lastDAC, lastPhase))  // zavrsio oba bloka
                {
                    if (Math.Abs(slidingFrame_2.part_angle) < 2 && Math.Abs(slidingFrame_2.phaseAvg_stop) < 4 * Math.Pow(10, -9)) // blizu nule i lagano se mijenja
                    {
                        writeServiceFile("*********** going to fine tunning. frame2.stop = " + (slidingFrame_2.phaseAvg_stop * Math.Pow(10, 9)) + ", angle: " + slidingFrame_2.part_angle);
                        state = MediumState.FINISHED;
                        return(new TuningResult(lastDAC, TuningResult.Result.FINISHED));
                    }

                    if (DAC_frame_1 < DAC_frame_2)                                 // podigli smo DAC, faza bi trebala da opada u part 2
                    {
                        if (slidingFrame_2.part_angle > slidingFrame_1.part_angle) // !!! NIJE KAO OCEKIVANO, faza porasla
                        {
                            calculatedDAC = lastDAC + calculateDacStep(slidingFrame_2.phaseAvg_stop);

                            writeServiceFile("!!! GRESKA_1: Time: " + nTime +
                                             ".\tDAC_frame_1 = " + DAC_frame_1 + " start: " + slidingFrame_1.phaseAvg_start + ", stop: " + slidingFrame_1.phaseAvg_stop + ", angle: " + slidingFrame_1.part_angle +
                                             "\t\tDAC_frame_2 = " + DAC_frame_2 + " start: " + slidingFrame_2.phaseAvg_start + ", stop: " + slidingFrame_2.phaseAvg_stop + ", angle: " + slidingFrame_2.part_angle);

                            writeServiceFile("GRESKA_1 novi DAC: " + calculatedDAC);
                            state = MediumState.TUNING_SLEEP_1;

                            return(new TuningResult(calculatedDAC, TuningResult.Result.NOT_FINISHED));
                        }
                    }
                    else if (DAC_frame_1 > DAC_frame_2)
                    {                                                              // smanjili smo DAC, faza bi trebala da raste u part 2
                        if (slidingFrame_2.part_angle < slidingFrame_1.part_angle) // !!! NIJE KAO OCEKIVANO, faza opala
                        {
                            calculatedDAC = lastDAC + calculateDacStep(slidingFrame_2.phaseAvg_stop);

                            writeServiceFile("!!! GRESKA_2: Time: " + nTime +
                                             ".\tDAC_frame_1 = " + DAC_frame_1 + " start: " + slidingFrame_1.phaseAvg_start + ", stop: " + slidingFrame_1.phaseAvg_stop + ", angle: " + slidingFrame_1.part_angle +
                                             "\t\tDAC_frame_2 = " + DAC_frame_2 + " start: " + slidingFrame_2.phaseAvg_start + ", stop: " + slidingFrame_2.phaseAvg_stop + ", angle: " + slidingFrame_2.part_angle);

                            writeServiceFile("GRESKA_2 novi DAC: " + calculatedDAC);
                            state = MediumState.TUNING_SLEEP_1;

                            return(new TuningResult(calculatedDAC, TuningResult.Result.NOT_FINISHED));
                        }
                    }

                    calculateNewDac(nTime);
                    state = MediumState.TUNING_SLEEP_1;
                }

                return(new TuningResult(calculatedDAC, TuningResult.Result.NOT_FINISHED));
            }
            else
            {
                writeServiceFile("!!!! NE BI SMJELO DOCI OVDJE, State: " + state.ToString());
                return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED));
            }
        }
Example #2
0
        /*
         * private void updateConstandDacArray(int lastDAC, int nTime) // u ovu array ubacujemo DAC za koji imamo konstantnu fazu (bez obzira da li je trenutna faza daleko od nule).
         * {
         *  if (nTimeFromPreviousConstDAC >= IGNORING_CONST_DAC_COUNT) // ako je od proslog "idealnog DAC-a" proslo dovoljno vremena (minimalno averaging time), onda cemo prihvatiti novi "idealni DAC" ako se desi
         *  {
         *      if (Math.Abs(phaseAverageExp.part_angle) < GOOD_ANGLE)
         *      {
         *          nTimeFromPreviousConstDAC = 0; // prihvaticemo ovu vrijednost tako da ce morati sacekati prije novog prihvacanja
         *
         *          lastConstDAC[lastConstDAC_index] = lastDAC;
         *          if (lastConstDAC_index == 0 && lastConstDAC[1] == 0) // ako punimo prvi elemenat i ako drugi jos nije postavljen, stavimo i drugi na istu vrjednost (tako da prihvatimo ovu vrijednost kao idealnu)
         *          {
         *                  lastConstDAC[1] = lastDAC;
         *          }
         *
         *          if (Math.Abs(lastConstDAC[0] - lastConstDAC[1]) <= 3)
         *          {
         *              optimalDac = (lastConstDAC[0] + lastConstDAC[1]) / 2; // zlatna sredia (mada ce se zaokruziti na manji broj)
         *              writeServiceFile("Izracunati optimal DAC: " + optimalDac.ToString() + ". Time: " + nTime);
         *          }
         *
         *      }
         *  }
         *  else
         *  {
         *      nTimeFromPreviousConstDAC++;
         *  }
         * }
         */

        public TuningResult tune(double lastDAC, double lastPhase, int nTime)
        {
            phaseAverageExp.AddPoint(lastPhase);
            nCounter++;

            // writeServiceFile("C: " + nCounter);

            if (nCounter < TUNNING_SLEEP_TIME)
            {
                return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // nemoj nista mijenjati
            }

            if (state == FineState.NORMAL)  // u normalnom state, cekaj da napuni frame size i onda reaguj
            {
                if (nCounter == TUNNING_SLEEP_TIME + FRAME_SIZE)
                {
                    nCounter = 0;

                    if (Math.Abs(phaseAverageExp.phaseAvg_stop) < 1 * Math.Pow(10, -9))  // TODO: ovo nam mozda ne treba? Is normal state je trebao preci u gooing to zero ako se priblizuje nuli. Ovdje ispada da je iznenada skocio blizu nule.
                    {
                        state = FineState.CLOSE_TO_ZERO;
                        writeServiceFile("Time: " + nTime + ". FT*1: Unexpected |phase| <1. Phase: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ", new state = CLOSE_TO_ZERO. DAC not changed");
                        return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // nemoj nista mijenjati
                    }
                    // PHASE IS POSITIVE
                    else if (phaseAverageExp.phaseAvg_stop > 1 * Math.Pow(10, -9))
                    {
                        if (phaseAverageExp.part_angle < 0) // ide prema nuli
                        {
                            writeServiceFile("Time: " + nTime + ". FT*2: phase positive: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ". going to zero, new state = GOOING_ZERO_POSITIV. DAC not changed: " + lastDAC);
                            state = FineState.GOOING_ZERO_POSITIV;
                            return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED));
                        }
                        else
                        {
                            if (phaseAverageExp.phaseAvg_stop < 3.8 * Math.Pow(10, -9))   // faza manja od 2.5, povecaj DAC za 1. // TODO: ne bi trebali gledati fazu nego ugao? Ako je ugao < xx, +1 ili +2?
                            {
                                writeServiceFile("Time: " + nTime + ". FT*3: phase positive: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ".  not going to zero, increase one New dac: " + (lastDAC + 1));
                                return(new TuningResult(lastDAC + 1 * ccoef, TuningResult.Result.NOT_FINISHED));
                            }
                            else
                            {   // faza je veca od 2.5, povecaj za 2
                                writeServiceFile("Time: " + nTime + ". FT*4: phase positive: " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ".  not going to zero, increase two New dac: " + (lastDAC + 2));
                                return(new TuningResult(lastDAC + 2, TuningResult.Result.NOT_FINISHED));
                            }
                        }
                    }
                    else // phase < -1 ns
                    {
                        if (phaseAverageExp.part_angle > 0) // ide prema nuli
                        {
                            writeServiceFile("Time: " + nTime + ". FT*5: phase negative: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ".  going to zero, new state = GOOING_ZERO_NEGATIVE. DAC not changed: " + lastDAC);
                            state = FineState.GOOING_ZERO_NEGATIVE;
                            return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED));
                        }
                        else
                        {
                            if (phaseAverageExp.phaseAvg_stop > -3.8 * Math.Pow(10, -9))
                            {
                                writeServiceFile("Time: " + nTime + ". FT*6: phase negative: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ".  not going to zero, decrease one. New dac: " + (lastDAC - 1));
                                return(new TuningResult(lastDAC - 1 * ccoef, TuningResult.Result.NOT_FINISHED));
                            }
                            else
                            {
                                writeServiceFile("Time: " + nTime + ". FT*7: phase negative: " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ".  not going to zero, decrease two. New dac: " + (lastDAC - 2));
                                return(new TuningResult(lastDAC - 2, TuningResult.Result.NOT_FINISHED));
                            }
                        }
                    }
                }
                else
                {
                    return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // nemoj nista mijenjati, cekamo da zavrsi sliding frame
                }
            }
            else if (state == FineState.GOOING_ZERO_POSITIV)
            {
                if (phaseAverageExp.phaseAvg_stop < 1 * Math.Pow(10, -9))  // usli blizu nule, zaustavi opadanje
                {
                    state = FineState.CLOSE_TO_ZERO;
                    writeServiceFile("Time: " + nTime + ". FT1: phase positive, gooing zero. Phase: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ". New state: CLOSE_TO_ZERO, New dac -1: " + lastDAC);
                    return(new TuningResult(lastDAC - 1 * ccoef, TuningResult.Result.NOT_FINISHED));
                }
                else if (phaseAverageExp.part_angle > 0)                                 // ne ide vise prema nuli
                {
                    state    = FineState.NORMAL;                                         // vrati na normalnu regulaciju
                    nCounter = TUNNING_SLEEP_TIME + FRAME_SIZE - 1;                      // Stavi counter tako da sljedeci put izracuna kao da je normalna regulacija
                    writeServiceFile("Time: " + nTime + ". FT2: phase positive, gooing zero. !!! Now is gooing up " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ". New state: NORMAL. DAC not changed: " + lastDAC);
                    return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // ??? nemoj mijenjati DAC? jer je mozda privremeni skok?
                }
                else
                {
                    return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // do nothing
                }
            }
            else if (state == FineState.GOOING_ZERO_NEGATIVE)
            {
                if (phaseAverageExp.phaseAvg_stop > -1 * Math.Pow(10, -9))  // usli blizu nule, zaustavi rast
                {
                    state = FineState.CLOSE_TO_ZERO;
                    writeServiceFile("Time: " + nTime + ". FT3: phase negative, gooing zero. Phase: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ". New state: CLOSE_TO_ZERO, New dac +1: " + lastDAC);
                    return(new TuningResult(lastDAC + 1 * ccoef, TuningResult.Result.NOT_FINISHED));
                }
                else if (phaseAverageExp.part_angle < 0)                                 // ne ide vise prema nuli
                {
                    state    = FineState.NORMAL;                                         // vrati na normalnu regulaciju
                    nCounter = TUNNING_SLEEP_TIME + FRAME_SIZE - 1;                      // Stavi counter tako da sljedeci put izracuna kao da je normalna regulacija
                    writeServiceFile("Time: " + nTime + ". FT4: phase negative, gooing zero. !!! Now is gooing down " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ". New state: NORMAL. DAC not changed: " + lastDAC);
                    return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // ??? nemoj mijenjati DAC? jer je mozda privremeni skok?
                }
                else
                {
                    return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // do nothing
                }
            }
            else // if (state == FineState.CLOSE_TO_ZERO)
            {
                if (Math.Abs(phaseAverageExp.phaseAvg_stop) > 1 * Math.Pow(10, -9)) // iskocio iz zone blizu nule
                {
                    state    = FineState.NORMAL;                              // vrati na normalnu regulaciju
                    nCounter = TUNNING_SLEEP_TIME + FRAME_SIZE - 1;           // Stavi counter tako da sljedeci put izracuna kao da je normalna regulacija
                    writeServiceFile("Time: " + nTime + ". FT5: phase was close to zero, but now out of limit: " + phaseAverageExp.phaseAvg_start + "; " + phaseAverageExp.phaseAvg_stop + ", angle: " + phaseAverageExp.part_angle + ". New state: NORMAL. DAC not changed: " + lastDAC);
                    if (phaseAverageExp.phaseAvg_stop > 1 * Math.Pow(10, -9)) // ide u plus
                    {
                        return(new TuningResult(lastDAC + 1 * ccoef, TuningResult.Result.NOT_FINISHED));
                    }
                    else
                    {
                        return(new TuningResult(lastDAC - 1 * ccoef, TuningResult.Result.NOT_FINISHED));
                    }
                }
                else
                {
                    return(new TuningResult(lastDAC, TuningResult.Result.NOT_FINISHED)); // do nothing
                }
            }
        }