Пример #1
0
        public double CalibrateCurr(RxInfo rx)
        {
            double vp, vn;

            DeviceMgr.Action("turnk", rx.var.iK);
            Thread.Sleep(1000);
            while (DeviceMgr.IsInAction())
            {
                ;
            }
            DeviceMgr.Action("MEAS_RS|KTTP|" + DeviceMgr.ComposeAction(rx), 0);
            Reset();
            this.rx = rx;
            this.rs = Program.lst_rsinfo[0];
            Thread.Sleep(20000);
            bInReading = true;
            Thread.Sleep(500);
            while (IsBusy)
            {
                Thread.Sleep(1000);
            }
            vp = DeviceMgr.reading;

            DeviceMgr.Action("MEAS_RS|KTTN|" + DeviceMgr.ComposeAction(rx), 0);
            Thread.Sleep(20000);
            bInReading = true;
            Thread.Sleep(500);
            while (IsBusy)
            {
                DeviceMgr.IsInAction();
                Thread.Sleep(1000);
            }
            vn = DeviceMgr.reading;
            double val = (Math.Abs((vp - vn) / 2.0)) / rx.var.rRs;

            val = val * RX_VAR.INIT_LOOP / rx.var.iK;
            RunWnd.syscfg.SetCurrent(rx.iIx, rx.bSqrt, val);
            return(val);
        }
Пример #2
0
        public void Step()
        {
            if (DeviceMgr.IsInAction()) //communication action is going on
            {
                return;
            }

            if (currRx.i_State == RUN_STATE.STOPPING)   //user choose to stop
            {
                DeviceMgr.Reset();
                act_mgr.Reset();
                bPaused        = false;
                bRunning       = false;
                currRx.i_State = RUN_STATE.IDLE;
            }
            if (bPaused || !bRunning || act_mgr.IsBusy)
            {
                return;
            }


            if (stm == ACTION_STATE.INIT_FOR_START)//search the next valid channel
            {
                currRx.i_State = RUN_STATE.SEARCHING;
                StatusChanged(this, "search");
                stm = act_mgr.Do(ACTION_REQUEST.INIT_NAV_RANGE, stm);
                return;
            }
            if (stm == ACTION_STATE.INIT_FOR_SEARCH) //prepare the capture with 1:1 mode
            {
                currRx.i_State = RUN_STATE.SEARCHING;
                StatusChanged(this, "search");
                stm = act_mgr.Do(ACTION_REQUEST.INITKI_FOR_SEARCH, stm);
                return;
            }
            if (stm == ACTION_STATE.INIT_FOR_RS_KTTP) //capture once //set to KTTP mode and delay 1s
            {
                currRx.i_State = RUN_STATE.SEARCHING;
                StatusChanged(this, "search");
                stm = act_mgr.Do(ACTION_REQUEST.SET_FOR_RS_KTTP, stm);
                return;
            }
            if (stm == ACTION_STATE.INIT_FOR_RS_KTTN)
            {
                currRx.i_State = RUN_STATE.SEARCHING;
                StatusChanged(this, "search");

                double val = act_mgr.dStableReading;
                if (val < -999)
                {
                    currRx.i_State = RUN_STATE.STOPPING;
                    StatusChanged(this, "stopping");
                    return;
                }
                currRx.var.vRs = val;
                stm            = act_mgr.Do(ACTION_REQUEST.SET_FOR_RS_KTTN, stm);
                return;
            }
            if (stm == ACTION_STATE.COMPLETE_ISRC) //capture once //set to KTTP mode
            {
                currRx.i_State = RUN_STATE.SEARCHING;
                StatusChanged(this, "search");
                double val = act_mgr.dStableReading;
                if (val < -999)
                {
                    currRx.i_State = RUN_STATE.STOPPING;
                    StatusChanged(this, "stopping");
                    return;
                }
                currRx.var.vRs  = Math.Abs(currRx.var.vRs - val) / 2.0;
                currRx.var.iSrc = currRx.var.vRs / currRx.var.rRs;
                currRx.var.iSrc = currRx.var.iSrc * RX_VAR.INIT_LOOP / (double)currRx.var.iK;
                double stdcurr = RunWnd.syscfg.GetStdCurrent(currRx.iIx, currRx.bSqrt);
                if (currRx.var.iSrc < 0 || (Math.Abs(stdcurr - currRx.var.iSrc) > stdcurr * 0.5))
                {
                    if (retry == 0)
                    {
                        retry++;
                        stm = ACTION_STATE.INIT_FOR_SEARCH;
                        DeviceMgr.SysLog(String.Format("Incorrect current {0} vs std:{1}, bSqrt is {2}, iIx is {3}",
                                                       currRx.var.iSrc.ToString(), stdcurr.ToString(), currRx.bSqrt.ToString(), currRx.iIx.ToString()));
                    }
                    else
                    {
                        currRx.i_State = RUN_STATE.STOPPING;
                        StatusChanged(this, "stopping");
                        return;
                    }
                }
                else
                {
                    stm = act_mgr.Do(ACTION_REQUEST.SET_FOR_TRACKP, stm);
                }
                return;
            }
            #region  no use

            /*
             * if (stm == ACTION_STATE.INIT_FOR_RX_KTTP) //capture once //set to KTTP mode
             * {
             *  currRx.i_State = RUN_STATE.SEARCHING;
             *  StatusChanged(this, "search");
             *  double val = act_mgr.dStableReading;
             *  if (val < -999)
             *  {
             *      currRx.i_State = RUN_STATE.STOPPING;
             *      StatusChanged(this, "stopping");
             *      return;
             *  }
             *
             *  currRx.var.vRs = Math.Abs(currRx.var.vRs - val) / 2.0;
             *  currRx.var.iSrc = currRx.var.vRs / currRx.var.rRs;
             *  stm = act_mgr.Do(ACTION_REQUEST.SET_FOR_RX_KTTP, stm);
             *  return;
             * }
             * if (stm == ACTION_STATE.INIT_FOR_RX_KTTN)
             * {
             *  currRx.i_State = RUN_STATE.SEARCHING;
             *  StatusChanged(this, "search");
             *  double val = act_mgr.dStableReading;
             *  if (val < -999)
             *  {
             *      currRx.i_State = RUN_STATE.STOPPING;
             *      StatusChanged(this, "stopping");
             *      return;
             *  }
             *  currRx.var.vRx = val;
             *  stm = act_mgr.Do(ACTION_REQUEST.SET_FOR_RX_KTTN, stm);
             *  return;
             * }
             * if (stm == ACTION_STATE.INIT_K_ADJUST) //adjust K
             * {
             *  double val = act_mgr.dStableReading;
             *  if (val < -999)
             *  {
             *      currRx.i_State = RUN_STATE.STOPPING;
             *      StatusChanged(this, "stopping");
             *      return;
             *  }
             *
             *  currRx.var.vRx = Math.Abs(currRx.var.vRx - val)/ 2;
             *  currRx.var.rRx = currRx.var.vRx / currRx.var.iSrc;
             *
             *  currRx.var.iK = currRx.var.calc_capture_nextk();
             *  DeviceMgr.Action("turnk", Convert.ToUInt32(currRx.var.iK));
             *  stm = ACTION_STATE.INIT_FOR_TRACKP;
             * }
             */
            #endregion
            if (stm == ACTION_STATE.INIT_FOR_TRACKP) //capture once //set to KTTP mode
            {
                currRx.i_State = RUN_STATE.TRACKING;
                StatusChanged(this, "tracking");

                stm = act_mgr.Do(ACTION_REQUEST.SET_FOR_TRACKP, stm);
                return;
            }
            if (stm == ACTION_STATE.INIT_FOR_TRACKN)
            {
                currRx.i_State = RUN_STATE.TRACKING;
                StatusChanged(this, "tracking");
                double val = act_mgr.dStableReading;
                if (val < -999)
                {
                    currRx.i_State = RUN_STATE.STOPPING;
                    StatusChanged(this, "stopping");
                    return;
                }
                currRx.var.vCrossP = val;
                stm = act_mgr.Do(ACTION_REQUEST.SET_FOR_TRACKN, stm);
                return;
            }
            if (stm == ACTION_STATE.UPDATE_K_TRACK)
            {
                currRx.i_State = RUN_STATE.TRACKING;
                StatusChanged(this, "tracking");

                double val = act_mgr.dStableReading;
                if (val < -999)
                {
                    currRx.i_State = RUN_STATE.STOPPING;
                    StatusChanged(this, "stopping");
                    return;
                }
                currRx.var.vCrossN = val;

                stm = act_mgr.Do(ACTION_REQUEST.SET_NEXT_K, stm);
                if (stm == ACTION_STATE.SHOW_NEW_VALUE)
                {
                    currRx.var.log_start(currRx.iRRange);
                }
                return;
            }
            if (stm == ACTION_STATE.SHOW_NEW_VALUE)
            {
                if (data_mgr.AddNewValue(ref currRx.var.rRx))
                {
                    StatusChanged(this, "newvalue");
                }
                if (data_mgr.iMeasCnt == RunWnd.syscfg.iMeasTimes)
                {
                    stm = ACTION_STATE.COMPLETE;
                }
                else
                {
                    stm = ACTION_STATE.INIT_FOR_TRACKP;
                }
                return;
            }

            if (stm == ACTION_STATE.COMPLETE) //update the display and try the next channel
            {
                currRx.i_State = RUN_STATE.IDLE;
                bRunning       = false;
                StatusChanged(this, "complete");
                act_mgr.Do(ACTION_REQUEST.INIT_NAV_RANGE, stm);
                DeviceMgr.Reset();
                while (DeviceMgr.IsInAction())
                {
                    ;
                }
                stm = ACTION_STATE.IDLE;
            }
        }
Пример #3
0
        public void pc_cmd(string cmd)
        {
            DeviceMgr.SysLog(cmd);
            if (cmd == "ECHO")
            {
                DeviceMgr.Resend();
                return;
            }
            if (cmd == "S")
            {
                if (task.bRunning)
                {
                    statusBar1_StartStopClick(null, null); //click stop
                }
                return;
            }

            if (cmd == "H")
            {
                if ((!task.bRunning) && (!DeviceMgr.IsInAction()))
                {
                    DeviceMgr.Reset();//reset
                }
                return;
            }
            Match m;

            m = reg_mode.Match(cmd);
            if (m.Success)
            {
                oper_mode = m.Groups[1].Value;
                return;
            }
            m = reg_statistic.Match(cmd);
            if (m.Success)
            {
                syscfg.iFilter = Convert.ToInt32(m.Groups[1].Value);
                return;
            }
            m = reg_filter.Match(cmd);
            if (m.Success)
            {
                if (m.Groups[1].Value == "2")
                {
                    syscfg.sFilterType = "filtertype3";
                }
                else if (m.Groups[1].Value == "1")
                {
                    syscfg.sFilterType = "filtertype2";
                }
                else if (m.Groups[1].Value == "3")
                {
                    syscfg.sFilterType = "filtertype4";
                }
                else
                {
                    syscfg.sFilterType = "filtertype1";
                }
                return;
            }
            m = reg_ix.Match(cmd);
            if (m.Success)
            {
                #region current convert
                double myIx = double.Parse(m.Groups[1].Value) / 1000;
                if (myIx >= 5.5)
                {
                    Program.lst_rxinfo[0].iIx   = 5; //5A x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                if (myIx >= 2.2)
                {
                    Program.lst_rxinfo[0].iIx   = 5; //5A
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }
                if (myIx >= 1.1)
                {
                    Program.lst_rxinfo[0].iIx   = 4; //1A x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                if (myIx >= 0.66)
                {
                    Program.lst_rxinfo[0].iIx   = 4; //1A x 2
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }
                if (myIx >= 0.33)
                {
                    Program.lst_rxinfo[0].iIx   = 3; //0.3A x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                if (myIx >= 0.22)
                {
                    Program.lst_rxinfo[0].iIx   = 3; //0.3A
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }
                if (myIx >= 0.11)
                {
                    Program.lst_rxinfo[0].iIx   = 2; //0.1A x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                if (myIx >= 0.022)
                {
                    Program.lst_rxinfo[0].iIx   = 2; //0.1A
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }
                if (myIx >= 0.011)
                {
                    Program.lst_rxinfo[0].iIx   = 1; //10mA x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                if (myIx >= 0.0022)
                {
                    Program.lst_rxinfo[0].iIx   = 1; //10mA
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }

                if (myIx >= 0.0011)
                {
                    Program.lst_rxinfo[0].iIx   = 0; //1mA x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                if (myIx >= 0.00022)
                {
                    Program.lst_rxinfo[0].iIx   = 0; //1mA
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }
                if (myIx >= 0.00011)
                {
                    Program.lst_rxinfo[0].iIx   = -1; //0.1mA x 2
                    Program.lst_rxinfo[0].bSqrt = true;
                    return;
                }
                {
                    Program.lst_rxinfo[0].iIx   = -1; //0.1mA
                    Program.lst_rxinfo[0].bSqrt = false;
                    return;
                }
                #endregion
            }
            m = reg_delay.Match(cmd);
            if (m.Success)
            {
                syscfg.iMeasDelay = Convert.ToInt32(m.Groups[1].Value);
                return;
            }
            m = reg_meastimes.Match(cmd);
            if (m.Success)
            {
                syscfg.iMeasTimes = Convert.ToInt32(m.Groups[1].Value);
                return;
            }
            m = reg_switch.Match(cmd);
            if (m.Success)
            {
                syscfg.iKTT = Convert.ToInt32(m.Groups[1].Value);
                return;
            }
            m = reg_measure.Match(cmd);
            if (m.Success)
            {
                syscfg.iMeasTimes = Convert.ToInt32(m.Groups[1].Value);
                RsInfo rs = Program.lst_rsinfo[Program.mainwnd.selectedRs];
                RxInfo rx = Program.lst_rxinfo[Program.mainwnd.selectedRx];
                if (!task.bRunning)
                {
                    statusBar1_StartStopClick(null, null); //click start
                }
                return;
            }
            m = reg_sampletimes.Match(cmd);
            if (m.Success)
            {
                syscfg.iSampleTimes = Convert.ToInt32(m.Groups[1].Value);
                return;
            }
            m = reg_alpha.Match(cmd);
            if (m.Success)
            {
                Program.lst_rsinfo[0].iRRange = ActionMgr.RNG_INVALID;
                Program.lst_rsinfo[0].dAlpha  = Convert.ToDouble(m.Groups[1].Value);
                return;
            }
            m = reg_beta.Match(cmd);
            if (m.Success)
            {
                Program.lst_rsinfo[0].iRRange = ActionMgr.RNG_INVALID;
                Program.lst_rsinfo[0].dBeta   = Convert.ToDouble(m.Groups[1].Value);

                return;
            }
            m = reg_temp.Match(cmd);
            if (m.Success)
            {
                syscfg.dTemp = Convert.ToDouble(m.Groups[1].Value);
                return;
            }
            m = reg_rsa.Match(cmd);
            if (m.Success)
            {
                Program.lst_rsinfo[0].iRRange  = ActionMgr.RNG_INVALID;
                Program.lst_rsinfo[0].dValue   = Convert.ToDouble(m.Groups[1].Value);
                Program.lst_rxinfo[0].cStdChan = 'A';
                return;
            }
            m = reg_rsid.Match(cmd);
            if (m.Success)
            {
                Program.lst_rsinfo[0].iRRange = ActionMgr.RNG_INVALID;
                Program.lst_rsinfo[0].sSerial = m.Groups[1].Value;
                return;
            }
            m = reg_rxid.Match(cmd);
            if (m.Success)
            {
                Program.lst_rxinfo[0].sSerial = m.Groups[1].Value;
                return;
            }

            m = reg_rsb.Match(cmd);
            if (m.Success)
            {
                Program.lst_rxinfo[0].dRxInput = Convert.ToDouble(m.Groups[1].Value);
                ActionMgr.SetIxRange(0, Program.lst_rxinfo[0].dRxInput, false);
                Program.lst_rxinfo[0].cStdChan = 'B';
                return;
            }
        }
Пример #4
0
        private void CollectReading()
        {
            if (DeviceMgr.IsInAction())
            {
                return;
            }
            if (!bFirstReadSent)
            {
                DeviceMgr.Action("navread", 0);
                bFirstReadSent = true;
                return;
            }

            if (DeviceMgr.reading < -999)
            {
                badReadingCount++;
            }
            else
            {
                badReadingCount = 0;
                if (RunWnd.syscfg.sNavmeter != NAV_TYPE_2182 && nav_range != "navto1v")
                {
                    datafilter.Add(DeviceMgr.reading / 1000);
                }
                else
                {
                    datafilter.Add(DeviceMgr.reading);
                }
            }
            if (badReadingCount > 5) //to much invalid readings
            {
                dStableReading = -9999;
                bInReading     = false;
                return;
            }
            if (datafilter.Count < RunWnd.syscfg.iSampleTimes)
            {
                DeviceMgr.Action("navread", 0);
                return;
            }
            int cnt0  = 0; //count for no change
            int cnt1  = 0; //count for going up
            int cnt_1 = 0; //count for going down

            for (int i = 1; i < datafilter.Count; i++)
            {
                if (Math.Abs(datafilter[i] - datafilter[i - 1]) < 1e-12)
                {
                    cnt0++;
                }
                else if (datafilter[i] > datafilter[i - 1])
                {
                    cnt1++;
                }
                else
                {
                    cnt_1++;
                }
            }
            if ((cnt1 == datafilter.Count - 1) || (cnt_1 == datafilter.Count - 1)) //all up or all down
            {
//no jump check                datafilter.Clear();
//                return;
            }


            {
                double sqr;
                if (rx.iRRange == RNG_P001) //0.001
                {
                    sqr = GetSqrt3(10);
                }
                else if (rx.iRRange == RNG_P01) //0.01
                {
                    sqr = GetSqrt3(10);
                }
                else if (rx.iRRange == RNG_P1) //0.1
                {
                    sqr = GetSqrt3(5);
                }
                else
                {
                    sqr = GetSqrt3(3);
                }
                double avg     = datafilter.Average();
                int    removed = datafilter.RemoveAll(x => Math.Abs(x - avg) > sqr);

                string type = RunWnd.syscfg.sFilterType;

                if (type != "filtertype4" && removed > 0)
                {
                    DeviceMgr.reading = -9999; //clear the current reading
                    return;                    //not enough data now
                }

                if (type == "filtertype1")  //中位数平均值
                {
                    datafilter.Sort();
                    dStableReading = datafilter.Skip(datafilter.Count * 3 / 5).Take(datafilter.Count / 4).Average();
                    bInReading     = false;
                    return;
                }
                if (type == "filtertype2")  //最后值平均
                {
                    int len = 10;
                    dStableReading = datafilter.Skip(datafilter.Count - len).Take(len).Average();
                    bInReading     = false;
                }
                if (type == "filtertype3") //滑动滤波平均
                {
                    int len = datafilter.Count - 5;
                    if (len > 10)
                    {
                        len = 10;
                    }
                    if (len <= 0)
                    {
                        len = 2;
                    }
                    for (int i = (datafilter.Count - 1); i >= len; i--)
                    {
                        datafilter[i] = datafilter.Skip(i - len).Take(len).Average();
                    }
                    datafilter.RemoveRange(0, len);
                    datafilter.Sort();
                    dStableReading = datafilter.Skip(datafilter.Count / 3).Take(datafilter.Count / 3).Average();
                    bInReading     = false;
                }
                if (type == "filtertype4") //看分布平均
                {
                    int[]  bins = new int[] { 0, 0, 0, 0, 0 };
                    double max  = datafilter.Max();
                    double min  = datafilter.Min();
                    double step = (max - min) / (bins.Length - 2);
                    int    i;
                    foreach (double v in datafilter)
                    {
                        for (i = 0; i < bins.Length - 1; i++)
                        {
                            if (v < (min + i * step))
                            {
                                bins[i]++;
                                break;
                            }
                        }
                        if (i >= bins.Length)
                        {
                            bins[bins.Length - 1]++;
                        }
                    }

                    double bottom = -99999;
                    double top    = -9999;
                    for (i = 0; i < bins.Length - 1; i++)
                    {
                        bottom = top;
                        top    = min + i * step;
                        if (bins[i] == bins.Max())
                        {
                            break;
                        }
                    }
                    if (i == bins.Length - 1)
                    {
                        bottom = top;
                        top    = 10e9;
                    }
                    dStableReading = datafilter.Where(x => (x <= top && x >= bottom)).Average();
                    bInReading     = false;
                }

                return;
            }
            throw new NotImplementedException("unexpected action state in data processing");
        }
Пример #5
0
        private void RangeAdjust()
        {
            if (DeviceMgr.IsInAction())
            {
                return;
            }
            if (!bFirstReadSent)
            {
                DeviceMgr.Action("navread", 0);
                bFirstReadSent = true;
                return;
            }
            if (RunWnd.syscfg.sNavmeter != NAV_TYPE_2182 && nav_range != "navto1v")
            {
                datafilter.Add(DeviceMgr.reading / 1000);
            }
            else
            {
                datafilter.Add(DeviceMgr.reading);
            }
            if (datafilter.Count < 4)
            {
                DeviceMgr.Action("navread", 0);
                return;
            }
            int total = datafilter.Count(x => Math.Abs(x) < 999);

            datafilter.Clear();
            if (range_dir == 1)
            {
                if (total > 0 || IsTopRange())
                {
                    range_dir = 0;
                    return;
                }
                if (RunWnd.syscfg.sNavmeter == NAV_TYPE_2182)
                {
                    if (nav_range == "navto1v")
                    {
                        nav_range = "navto30v";
                    }
                    else
                    {
                        nav_range = "navto1v";
                    }
                }
                else
                {
                    if (nav_range == "navto10mv")
                    {
                        nav_range = "navto120mv";
                    }
                    else
                    {
                        nav_range = "navto1v";
                    }
                }
                delay = 4;
                DeviceMgr.Action(nav_range, 0);
                return;
            }
            else
            {
                throw new NotImplementedException("unexpected down range");
            }
        }