Exemplo n.º 1
0
        /// <summary>
        /// Compare cycle number from BQ to the backup data. Backupdata should have most recent
        /// </summary>
        public StartCyclingArgs CompareCycleNumbers(StartCyclingArgs a)
        {
            for (int i = 0; i < a.TDK.Count; i++)
            {
                var sampleDir = U.CCDataDir + a.TDK[i].SampleName + "\\";
                var directory = new DirectoryInfo(sampleDir);
                var file      = directory.GetFiles()
                                .OrderByDescending(f => f.LastWriteTime)
                                .First().FullName;
                var lastCycle = int.Parse(File.ReadLines(file).Last().Split(',').ToList()[0]);

                if (lastCycle != a.TDK[i].CycleCount)
                {
                    U.Logger.WriteLine($"BQ cycle count {a.TDK[i].CycleCount} does not match local saved cycle count {lastCycle}. Choosing highest cycle!");
                    if (lastCycle > a.TDK[i].CycleCount)
                    {
                        a.TDK[i].CycleCount = lastCycle;
                    }
                }
            }
            return(a);
        }
Exemplo n.º 2
0
        public void StartCycling(StartCyclingArgs args)
        {
            _serTDK = new SerialPort();
            var tdk = args.TDK;

            if (tdk.Count < 1)
            {
                return;
            }
            U.Logger.WriteLine("Waiting for HW interlock");
            NewCoreCommand?.Invoke(this, new CoreCommand()
            {
                Type = U.CmdType.UpdateHeartBeatPacket
            });
            Thread.Sleep(2000); // wait for HW interlock to disengage
            OpenPorts();
            try {
                foreach (var t in tdk)
                {
                    try {
                        SetAddress(t);
                        SetCurrentVoltage(t);
                        t.Connected = true;
                    }
                    catch (TimeoutException exc) {
                        U.Logger.WriteLine($"TIMEOUT ON PORT #{t.Port}");
                        throw new Exception(exc.Message);
                    }
                }
                // start bias cycle
                foreach (var t in tdk)
                {
                    t.CycleCount++;
                    t.PastVoltages = new List <string>();
                }

                // save at a slower interval for China
                var saveTimeON  = Environment.MachineName.ToUpper() == "CH-J7TMTZ1" ? 5000 : U.ResultsSaveTimeON;
                var saveTimeOFF = Environment.MachineName.ToUpper() == "CH-J7TMTZ1" ? 60000 : U.ResultsSaveTimeOFF;

                _TDK        = tdk;
                _totalTimer = new Stopwatch();
                _totalTimer.Start();
                _voltageTimer = new Stopwatch();
                _voltageTimer.Start();
                DataError = false; // assume connection is good at start
                // Loop forever until we get a stop command from main thread
                while (true)
                {
                    _intoCycleTimer = new Stopwatch();
                    _intoCycleTimer.Start();

                    // BIAS ON
                    foreach (var t in tdk)
                    {
                        TurnON(t);
                    }
                    BIASON = true;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOnTime);
                    while (_biasTimer.ElapsedMilliseconds < args.BiasOnTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > saveTimeON)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            if (STOP || SMOKEALARM || TEMPALARM)
                            {
                                break;
                            }
                            SetAddress(tt);
                            _serTDK.DiscardInBuffer();

                            tt.Voltage = MeasureVoltage();
                            tt.Current = MeasureCurrent();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResultsBQ(tt);
                            }

                            if (double.Parse(tt.Voltage) > args.MaxOverVoltage)
                            {
                                STOP           = true;
                                OVERMAXVOLTAGE = true;
                                U.Logger.WriteLine($"{tt.SampleName} Overvoltaged!!");
                            }
                        }
                        if (STOP || SMOKEALARM || TEMPALARM)
                        {
                            break;
                        }
                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }
                    // BIAS OFF
                    TurnOff(tdk);
                    BIASON = false;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOffTime);
                    while (_biasTimer.ElapsedMilliseconds < args.BiasOffTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > saveTimeOFF)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            if (STOP || SMOKEALARM || TEMPALARM)
                            {
                                break;
                            }
                            tt.Voltage = MeasureVoltage();
                            tt.Current = MeasureCurrent();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResultsBQ(tt);
                            }
                        }
                        if (STOP || SMOKEALARM || TEMPALARM)
                        {
                            break;
                        }

                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }

                    // completed a bias on/off cycle
                    foreach (var ttt in tdk)
                    {
                        ttt.CycleCount++;
                    }
                }
            }
            catch (Exception exc) {
                U.Logger.WriteLine($"{exc}");
                BIASON         = false;
                STOP           = false;
                SMOKEALARM     = false;
                TEMPALARM      = false;
                GUISTOP        = false;
                EMSSTOP        = false;
                OVERMAXVOLTAGE = false;
                TurnOffClose(tdk);
                U.Logger.WriteLine($"TDK closing with exception");
            }
            var cmd = EMSSTOP ? "EMSSTOP" : SMOKEALARM ? "SMOKEALARM" : GUISTOP ? "GUI Stop" : OVERMAXVOLTAGE ? "Over Max Voltage" : "TEMPALARM";

            U.Logger.WriteLine($"Encountered {cmd} while Cycling!");
            BIASON  = false;
            STOP    = false;
            EMSSTOP = false;
            GUISTOP = false;
            TurnOffClose(tdk, OVERMAXVOLTAGE, SMOKEALARM, TEMPALARM);
            OVERMAXVOLTAGE = false;
            SMOKEALARM     = false;
            TEMPALARM      = false;
            U.Logger.WriteLine($"TDK Closing Normally");
        }
Exemplo n.º 3
0
        private void BtnStart_Click(object sender, EventArgs e)
        {
            if (!_TDKconnection.Any(b => b == true))
            {
                U.Logger.WriteLine($"TDK has no connections!");
                MessageBox.Show($"TDK has no connections!");
                return;
            }
            if (!_arduino.Connected)
            {
                U.Logger.WriteLine($"Arduino not connected!");
                MessageBox.Show($"Arduino not connected!");
                return;
            }

            CheckPorts();
            var tdks = _TDKS.Where(t => t.Cycling == true).ToList();

            if (tdks.Select(x => x.SampleName).Distinct().Count() != tdks.Count() && tdks.Count() > 1)
            {
                U.Logger.WriteLine($"Duplicate Sample Names Choosen!");
                MessageBox.Show($"Duplicate Sample Names Choosen!");
                return;
            }

            var startargs = new StartCyclingArgs(_TDKS.Where(t => t.Cycling == true).ToList(),
                                                 Double.Parse(txtBiasOn.Text), Double.Parse(txtBiasOff.Text),
                                                 Double.Parse(textBoxOverVoltage.Text));

            var start = new CoreCommand {
                Type      = U.CmdType.StartCycling,
                StartArgs = startargs
            };

            NewCoreCommand(this, start);

            // Disable GUI buttons from the GUI thread
            foreach (var chk in _checkBoxes)
            {
                chk.Enabled = false;
            }
            foreach (var temp in _tempSensors)
            {
                temp.Enabled = false;
            }
            foreach (var curr in _setCurrents)
            {
                curr.Enabled = false;
            }
            foreach (var load in _loadButtons)
            {
                load.Enabled = false;
            }
            foreach (var neww in _voc)
            {
                neww.Enabled = false;
            }
            foreach (var neww in _numCells)
            {
                neww.Enabled = false;
            }
            chkTemp.Enabled            = false;
            chkSmoke.Enabled           = false;
            btnStart.Enabled           = false;
            btnCheckConnection.Enabled = false;
            btnLoadSamples.Enabled     = false;
            btnClearSamples.Enabled    = false;
            buttonClearAlarms.Enabled  = false;

            // save GUI inputs to default settings
            Properties.Settings.Default.Operator       = txtOperator.Text;
            Properties.Settings.Default.BiasON         = txtBiasOn.Text;
            Properties.Settings.Default.BiasOFF        = txtBiasOff.Text;
            Properties.Settings.Default.BiasONTempSet  = txtCurrOnTempSet.Text;
            Properties.Settings.Default.BiasOFFTempSet = txtCurrOffTempSet.Text;
            Properties.Settings.Default.OverTempSet    = txtOverTempSet.Text;
            Properties.Settings.Default.OverSmokeSet   = txtSmokeOverSet.Text;
            Properties.Settings.Default.OverVoltage    = textBoxOverVoltage.Text;
            Properties.Settings.Default.PauseFanTime   = txtPauseFans.Text;
            var iii = 0;

            foreach (var chk in _checkBoxes)
            {
                Properties.Settings.Default.CheckBoxes[iii] = chk.Checked;
                iii++;
            }
            var ii = 0;

            foreach (object chk in chkTemp.Items)
            {
                Properties.Settings.Default.ActiveTemps[ii] = chkTemp.GetItemChecked(chkTemp.Items.IndexOf(chk));
                ii++;
            }
            ii = 0;
            foreach (object chk in chkSmoke.Items)
            {
                Properties.Settings.Default.ActiveSmokes[ii] = chkSmoke.GetItemChecked(chkSmoke.Items.IndexOf(chk));
                ii++;
            }
            Properties.Settings.Default.Save();
        }
Exemplo n.º 4
0
        public void StartCycling(StartCyclingArgs args)
        {
            _serTDK = new SerialPort();
            OpenPorts();
            var tdk = args.TDK;

            try {
                foreach (var t in tdk)
                {
                    try {
                        SetAddress(t);
                        SetCurrentVoltage(t);
                        t.Connected = true;
                    }
                    catch (TimeoutException exc) {
                        Console.WriteLine($"TIMEOUT ON PORT #{t.Port}");
                        throw new Exception(exc.Message);
                    }
                }
                _TDK        = tdk;
                _resultsDir = args.ResultsDirectory;
                _totalTimer = new Stopwatch();
                _totalTimer.Start();

                // Loop forever until we get a stop command from main thread
                while (true)
                {
                    _intoCycleTimer = new Stopwatch();
                    _intoCycleTimer.Start();

                    // BIAS ON
                    foreach (var t in tdk)
                    {
                        TurnON(t);
                    }
                    BIASON = true;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOnTime);
                    while (_timer.ElapsedMilliseconds < args.BiasOnTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > 1000)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            SetAddress(tt);

                            _serTDK.Write("MV?\r\n");
                            Wait(50); // lag in measured value
                            tt.Voltage = _serTDK.ReadLine();

                            _serTDK.Write("MC?\r\n");
                            Wait(50);
                            tt.Current = _serTDK.ReadLine();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResults(tt);
                            }
                        }
                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }
                    // BIAS OFF
                    TurnOff(tdk);
                    BIASON = false;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOffTime);
                    while (_timer.ElapsedMilliseconds < args.BiasOffTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > 1000)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            _serTDK.Write("MV?\r\n");
                            Wait(50); // lag in measured value
                            tt.Voltage = _serTDK.ReadLine();

                            _serTDK.Write("MC?\r\n");
                            Wait(50);
                            tt.Current = _serTDK.ReadLine();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResults(tt);
                            }
                        }

                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }

                    // completed a bias on/off cycle
                    foreach (var ttt in tdk)
                    {
                        ttt.CycleCount++;
                    }
                }
            }
            catch (Exception exc) {
                Console.WriteLine($"{exc}");
                TurnOffClose(tdk);
                STOP       = false;
                SMOKEALARM = false;
                TEMPALARM  = false;
            }
            STOP       = false;
            SMOKEALARM = false;
            TEMPALARM  = false;
            TurnOffClose(tdk);
        }