예제 #1
0
 private void TurnRelayOff()
 {
     try
     {
         relayCtrl.SetRelay0Status(false);
         relayCtrl.SetRelay1Status(false);
     }
     catch (Exception inner)
     {
         string            errMsg = "Settings.TurnRelayOff : Unable to turn relays off, exception occured.";
         SettingsException ex     = new SettingsException(errMsg, inner);
         log.Error(errMsg, ex);
         throw ex;
     }
 }
예제 #2
0
        private void tsbtnEmergencyStop_Click(object sender, EventArgs e)
        {
            USBRelayController relay = USBRelayController.Instance;

            try
            {
                relay.SetRelay0Status(true);
                relay.SetRelay1Status(true);
            }
            catch (Exception inner)
            {
                string            errMsg = "MainForm.tsbtnEmergencyStop : Relay controller not initialized.";
                MainFormException ex     = new MainFormException(errMsg, inner);
                log.Error(errMsg, ex);
                DisplayError(errMsg, ex);
            }
        }
예제 #3
0
        private void btnOpenRelay_Click(object sender, EventArgs e)
        {
            int portNum;

            try
            {
                portNum = Convert.ToInt32(nudPortNum.Value);
            }
            catch (Exception inner)
            {
                string            errMsg = "Settings.btnOpenRelay_Click : Exception thrown converting nudPortNum.Value to integer.";
                SettingsException ex     = new SettingsException(errMsg, inner);
                log.Error(errMsg, ex);
                DisplayError(errMsg, ex);
                return;
            }

            try
            {
                relayCtrl.Open(portNum);
            }
            catch (Exception inner)
            {
                string            errMsg = "Settings.btnOpenRelay_Click : Error connecting to relay controller.";
                SettingsException ex     = new SettingsException(errMsg, inner);
                log.Error(errMsg, ex);
                DisplayError(errMsg, ex);
                return;
            }

            // turn USB relay off
            USBRelayController usb_relay = USBRelayController.Instance;

            usb_relay.SetRelay0Status(false);
            usb_relay.SetRelay1Status(false);
        }
예제 #4
0
        private void Process()
        {
            // get a local copy of metadata singleton
            MetaData metadata = MetaData.Instance;

            // location of folders in saving directory structure
            string rootLocation      = metadata.SaveLocation;
            string cam1TestLocation  = "";
            string cam2TestLocation  = "";
            string cam1DebugLocation = "";
            string cam2DebugLocation = "";

            // list to keep image data in
            IPData[] cam1History            = new IPData[DEFAULT_TEST_HISTORY_SIZE];
            IPData[] cam2History            = new IPData[DEFAULT_TEST_HISTORY_SIZE];
            int      cam1HistoryInsertIndex = 0;
            int      cam2HistoryInsertIndex = 0;

            // tracks consecutive cracks in data and a flag to confirm a crack
            int  cam1ConsecutiveCrackedSampleCount = 0;
            int  cam2ConsecutiveCrackedSampleCount = 0;
            bool crackConfirmed = false;

            // timers for throttling thread, and updating save requests
            Stopwatch _threadTimer          = new Stopwatch();
            Stopwatch _debugSaveUpdateTimer = new Stopwatch();
            Stopwatch _testSaveUpdateTimer  = new Stopwatch();

            // flags to reset when timers trigger indicating corresponding data needs to be updated
            bool updateCam1DebugData = true;
            bool updateCam2DebugData = true;
            bool updateCam1TestData  = true;
            bool updateCam2TestData  = true;

            // attempt to create root directory and unique identify folder
            try
            {
                // updates the rootLocation to include the unique identifier folder generated inside function call
                rootLocation = initRootDirectory(rootLocation);
            }
            catch (Exception inner)
            {
                string errMsg = "ImageHistoryBuffer.Process : Unable to create root directory and unique identifier folder.";
                ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner);
                log.Error(errMsg, ex);

                // shutdown thread here
                ThreadErrorEventArgs er = new ThreadErrorEventArgs(errMsg, ex, true);
                ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
            }

            // attempt to create directory structure for test data
            try
            {
                initTestDirectory(rootLocation, ref cam1TestLocation, ref cam2TestLocation);
            }
            catch (Exception inner)
            {
                string errMsg = "ImageHistoryBuffer.Process : Unable to create directory structure for test data.";
                ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner);
                log.Error(errMsg, ex);

                // shutdown thread here
                ThreadErrorEventArgs er = new ThreadErrorEventArgs(errMsg, ex, true);
                ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
            }

            // attempt to create directory structure for debug saving if enabled
            if (metadata.EnableDebugSaving)
            {
                try
                {
                    initDebugDirectory(rootLocation, ref cam1DebugLocation, ref cam2DebugLocation);
                }
                catch (Exception inner)
                {
                    string errMsg = "ImageHistoryBuffer.Process : Unable to create directory structure for debug saving.";
                    ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner);
                    log.Error(errMsg, ex);

                    // shutdown thread here
                    ThreadErrorEventArgs er = new ThreadErrorEventArgs(errMsg, ex, true);
                    ThreadErrorEventArgs.OnThreadError(this, ThreadError, er);
                }
            }

            // start threading timers
            _threadTimer.Start();
            _debugSaveUpdateTimer.Start();
            _testSaveUpdateTimer.Start();

            while (isRunning)
            {
                // throttles the thread to prevent it from consuming 100% of processor it is running on
                int timeToSleep = threadThrottlePeriod - Convert.ToInt32(_threadTimer.ElapsedMilliseconds);
                if (timeToSleep > 0)
                {
                    Thread.Sleep(timeToSleep);
                }
                _threadTimer.Restart();

                // check if debug data needs to be updated
                if (_debugSaveUpdateTimer.ElapsedMilliseconds >= metadata.DebugSaveFrequency * 1000)
                {
                    updateCam1DebugData = true;
                    updateCam2DebugData = true;
                    _debugSaveUpdateTimer.Restart();
                }

                // check to see if test data needs to be updated
                if (_testSaveUpdateTimer.ElapsedMilliseconds >= 250)
                {
                    updateCam1TestData = true;
                    updateCam2TestData = true;
                    _testSaveUpdateTimer.Restart();
                }

                // pop everything off of savequeue
                List <QueueElement> imageElements = new List <QueueElement>();
                consumerQueue.popAll(ref imageElements);
                if (imageElements.Count > 0)
                {
                    for (int i = 0; i < imageElements.Count; i++)
                    {
                        //figure our where the iamge is coming from
                        IPData data = (IPData)imageElements[i].Data;
                        string type = imageElements[i].Type;
                        if (type.Contains("1"))
                        {
                            // see if image needs to be stored in recent history buffer 1
                            if (updateCam1TestData)
                            {
                                // requires a slot in buffer
                                cam1History[cam1HistoryInsertIndex] = data;
                                cam1HistoryInsertIndex = (cam1HistoryInsertIndex + 1) % DEFAULT_TEST_HISTORY_SIZE;
                                updateCam1TestData     = false;
                            }
                            if (metadata.EnableDebugSaving)
                            {
                                if (updateCam1DebugData)
                                {
                                    // need to save image to debug slot
                                    SaveIPData(cam1DebugLocation, ref data);
                                    updateCam1DebugData = false;
                                }
                            }
                            if (data.ContainsCrack)
                            {
                                cam1ConsecutiveCrackedSampleCount++;
                                if (cam1ConsecutiveCrackedSampleCount > CONSECUTIVE_CRACKS)
                                {
                                    crackConfirmed = true;
                                }
                            }
                            else
                            {
                                cam1ConsecutiveCrackedSampleCount = 0;
                                crackConfirmed = false;
                            }
                        }
                        else if (type.Contains("2"))
                        {
                            // see if image needs to be stored in recent history buffer 2
                            if (updateCam2TestData)
                            {
                                // requires a slot in buffer
                                cam2History[cam2HistoryInsertIndex] = data;
                                cam2HistoryInsertIndex = (cam2HistoryInsertIndex + 1) % DEFAULT_TEST_HISTORY_SIZE;
                                updateCam2TestData     = false;
                            }
                            if (metadata.EnableDebugSaving)
                            {
                                if (updateCam2DebugData)
                                {
                                    // need to save image to debug slot
                                    SaveIPData(cam2DebugLocation, ref data);
                                    updateCam2DebugData = false;
                                }
                            }
                            if (data.ContainsCrack)
                            {
                                cam2ConsecutiveCrackedSampleCount++;
                                if (cam2ConsecutiveCrackedSampleCount > CONSECUTIVE_CRACKS)
                                {
                                    crackConfirmed = true;
                                }
                            }
                            else
                            {
                                cam2ConsecutiveCrackedSampleCount = 0;
                                crackConfirmed = false;
                            }
                        }
                        else
                        {
                            // unknown data type
                            log.Info("ImageHistoryBuffer.Process : Received unknown data type.");
                        }
                        // check to see if we have confirmed a crack existing
                        if (crackConfirmed)
                        {
                            // trigger the USB Relay
                            USBRelayController usb_relay = USBRelayController.Instance;
                            if (usb_relay.IsOpen)
                            {
                                usb_relay.SetRelay0Status(true);
                                usb_relay.SetRelay1Status(true);
                            }

                            // save all images in history buffer
                            saveHistoryBuffer(cam1TestLocation, cam1History);
                            saveHistoryBuffer(cam2TestLocation, cam2History);

                            break;
                        }
                    }
                }
            }
            return;
        }