private RFData AddToRFDataDatabase(SpectraCyberResponse spectraCyberResponse, Appointment appt)
        {
            RFData rfData = RFData.GenerateFrom(spectraCyberResponse);

            appt = DatabaseOperations.GetUpdatedAppointment(appt);
            rfData.Appointment = appt;
            rfData.Intensity   = rfData.Intensity * MiscellaneousHardwareConstants.SPECTRACYBER_VOLTS_PER_STEP;

            // Add to database
            DatabaseOperations.AddRFData(rfData);

            configVals.rfData = rfData.Intensity;

            if (configVals.spectraCyberMode == SpectraCyberModeTypeEnum.SPECTRAL)
            {
                if (configVals.bandscan > configVals.frequency / 2)
                {
                    configVals.bandscan = -1 * (configVals.frequency / 2);
                }
                else
                {
                    configVals.bandscan = configVals.bandscan + MiscellaneousHardwareConstants.SPECTRACYBER_BANDWIDTH_STEP;
                }
            }
            else if (configVals.spectraCyberMode == SpectraCyberModeTypeEnum.CONTINUUM)
            {
                configVals.scanTime = configVals.scanTime + configVals.integrationStep;
            }

            return(rfData);
        }
        private bool SetSomeOffsetVoltage(double offset, char identifier)
        {
            if ((offset < 0.0) || (offset > 4.095))
            {
                logger.Info(Utilities.GetTimeStamp() + ": [AbstractSpectraCyberController] ERROR: input voltage outside of range [0, 4.095]");
                return(false);
            }

            configVals.offsetVoltage = offset;

            int    Magnitude = (int)(offset * 1000);
            string Command   = "!" + identifier + IntToHexString(Magnitude);

            SpectraCyberRequest Request = new SpectraCyberRequest(
                SpectraCyberCommandTypeEnum.CHANGE_SETTING,
                Command,
                false,
                4
                );

            SpectraCyberResponse Response = new SpectraCyberResponse();

            SendCommand(Request, ref Response);

            return(Response.RequestSuccessful);
        }
        public bool SetSpectraCyberIFGain(double ifGain)
        {
            if (ifGain < 10.0 || ifGain > 25.75)
            {
                logger.Info(Utilities.GetTimeStamp() + ": [AbstractSpectraCyberController] ERROR: invalid IF Gain value: " + ifGain);
                return(false);
            }

            double adjustedGain = (ifGain - 10.0) / 0.25;

            string Command = "!A" + IntToHexString(Convert.ToInt32(adjustedGain));

            SpectraCyberRequest Request = new SpectraCyberRequest(
                SpectraCyberCommandTypeEnum.CHANGE_SETTING,
                Command,
                false,
                4
                );

            SpectraCyberResponse Response = new SpectraCyberResponse();

            SendCommand(Request, ref Response);

            configVals.IFGain = ifGain;

            return(Response.RequestSuccessful);
        }
        private static RFData GenerateRFData(SpectraCyberResponse spectraCyberResponse)
        {
            RFData rfData = new RFData();

            rfData.TimeCaptured = spectraCyberResponse.DateTimeCaptured;
            rfData.Intensity    = spectraCyberResponse.DecimalData;
            return(rfData);
        }
        // Perform a single scan, based on current mode
        public SpectraCyberResponse DoSpectraCyberScan()
        {
            SpectraCyberResponse Response = new SpectraCyberResponse();

            CommunicationMutex.WaitOne();
            SendCommand(GenerateCurrentDataRequest(), ref Response);
            CommunicationMutex.ReleaseMutex();

            return(Response);
        }
Ejemplo n.º 6
0
        private RFData AddToRFDataDatabase(SpectraCyberResponse spectraCyberResponse, int appId)
        {
            RFData rfData = RFData.GenerateFrom(spectraCyberResponse);

            //
            // Add to database
            //
            DatabaseOperations.CreateRFData(appId, rfData);

            return(rfData);
        }
Ejemplo n.º 7
0
        public void TestGenerateListFrom()
        {
            SpectraCyberResponse response1 = new SpectraCyberResponse();
            SpectraCyberResponse response2 = new SpectraCyberResponse();
            SpectraCyberResponse response3 = new SpectraCyberResponse();
            DateTime             date1     = DateTime.UtcNow;
            DateTime             date2     = date1.AddHours(1);
            DateTime             date3     = date2.AddMinutes(30);

            response1.DateTimeCaptured  = date1;
            response1.DecimalData       = 15;
            response1.RequestSuccessful = true;
            response1.SerialIdentifier  = 'c';
            response1.Valid             = true;

            response2.DateTimeCaptured  = date2;
            response2.DecimalData       = 10;
            response2.RequestSuccessful = true;
            response2.SerialIdentifier  = 'c';
            response2.Valid             = true;

            response3.DateTimeCaptured  = date3;
            response3.DecimalData       = 1500;
            response3.RequestSuccessful = true;
            response3.SerialIdentifier  = 'c';
            response3.Valid             = true;

            List <SpectraCyberResponse> responses = new List <SpectraCyberResponse>
            {
                response1,
                response2,
                response3
            };

            List <RFData> rfDatas = RFData.GenerateListFrom(responses);

            Assert.IsTrue(rfDatas != null);

            Assert.IsTrue(rfDatas[0] != null);
            Assert.AreEqual(date1.Date, rfDatas[0].TimeCaptured.Date);
            Assert.AreEqual(response1.DecimalData, rfDatas[0].Intensity);

            Assert.IsTrue(rfDatas[1] != null);
            Assert.AreEqual(date2.Date, rfDatas[1].TimeCaptured.Date);
            Assert.AreEqual(response2.DecimalData, rfDatas[1].Intensity);

            Assert.IsTrue(rfDatas[2] != null);
            Assert.AreEqual(date3.Date, rfDatas[2].TimeCaptured.Date);
            Assert.AreEqual(response3.DecimalData, rfDatas[2].Intensity);
        }
        private bool SetSomeIntegrationTime(SpectraCyberIntegrationTimeEnum time, char identifier)
        {
            string Command = "!" + identifier + "00" + SpectraCyberIntegrationTimeEnumHelper.GetValue(time);

            SpectraCyberRequest Request = new SpectraCyberRequest(
                SpectraCyberCommandTypeEnum.CHANGE_SETTING,
                Command,
                false,
                4
                );

            SpectraCyberResponse Response = new SpectraCyberResponse();

            SendCommand(Request, ref Response);

            return(Response.RequestSuccessful);
        }
        public void TestCommunication()
        {
            SpectraCyberRequest Request = new SpectraCyberRequest(
                SpectraCyberCommandTypeEnum.RESET,
                "!R000",
                true,
                4
                );

            SpectraCyberResponse Response = new SpectraCyberResponse();

            SendCommand(Request, ref Response);

            string ResponseData = Response.SerialIdentifier + Response.DecimalData.ToString("X3");

            logger.Info(Utilities.GetTimeStamp() + ": [AbstractSpectraCyberController] Attempted RESET with command \"!R000\", heard back: " + ResponseData);
        }
        public bool SetSpectraCyberDCGain(int dcgain, string identifier)
        {
            string Command = "!" + identifier + IntToHexString(dcgain);

            SpectraCyberRequest Request = new SpectraCyberRequest(
                SpectraCyberCommandTypeEnum.CHANGE_SETTING,
                Command,
                false,
                4
                );

            SpectraCyberResponse Response = new SpectraCyberResponse();

            SendCommand(Request, ref Response);

            return(Response.RequestSuccessful);
        }
Ejemplo n.º 11
0
        public void TestGenerateFrom()
        {
            SpectraCyberResponse response = new SpectraCyberResponse();
            DateTime             date     = DateTime.UtcNow;

            response.DateTimeCaptured  = date;
            response.DecimalData       = 15;
            response.RequestSuccessful = true;
            response.SerialIdentifier  = 'c';
            response.Valid             = true;

            RFData data = RFData.GenerateFrom(response);

            Assert.IsTrue(data != null);
            Assert.AreEqual(response.DateTimeCaptured, data.TimeCaptured);
            Assert.AreEqual(response.DecimalData, data.Intensity);
        }
Ejemplo n.º 12
0
        // Submit a command and return a response
        protected override void SendCommand(SpectraCyberRequest request, ref SpectraCyberResponse response)
        {
            // Here is where the request would be sent through serial if this were a physical device

            // Assume it is successfully sent
            response.RequestSuccessful = true;

            // Give the simulated SpectraCyber some time to process the command
            Thread.Sleep(AbstractSpectraCyberConstants.WAIT_TIME_MS);

            // Check for any significant cases
            switch (request.CommandType)
            {
            // Termination, safely end communication
            case SpectraCyberCommandTypeEnum.TERMINATE:
                BringDown();
                break;

                //
                // Do nothing by default
                //
            }

            // If the request expects a reply back, capture the data and attach it to the response
            if (request.WaitForReply)
            {
                // Reponse's data is valid
                response.Valid = true;

                // Set the SerialIdentifier, assuming the correct type of response is heard back
                response.SerialIdentifier = request.ResponseIdentifier;

                // Generate random data
                // TODO: may need to update to more accurately match the data seen from the real spectra cyber, or use a CSV file (issue #409)
                int minIntensityScaled = (int)(AbstractSpectraCyberConstants.SIMULATED_RF_INTENSITY_MINIMUM / AbstractSpectraCyberConstants.SIMULATED_RF_INTENSITY_DISCRETIZATION);
                int maxIntensityScaled = (int)(AbstractSpectraCyberConstants.SIMULATED_RF_INTENSITY_MAXIMUM / AbstractSpectraCyberConstants.SIMULATED_RF_INTENSITY_DISCRETIZATION);
                response.DecimalData = random.Next(minIntensityScaled, maxIntensityScaled + 1);

                // Set the time captured to be as close to the (simulated) read as possible
                response.DateTimeCaptured = DateTime.UtcNow;
            }

            // Do nothing to purge a simulated buffer
        }
        public bool SetFrequency(double frequency)
        {
            string Command = "!F" + IntToHexString(Convert.ToInt32(frequency));

            SpectraCyberRequest Request = new SpectraCyberRequest(
                SpectraCyberCommandTypeEnum.CHANGE_SETTING,
                Command,
                false,
                4
                );

            SpectraCyberResponse Response = new SpectraCyberResponse();

            SendCommand(Request, ref Response);

            configVals.frequency = frequency;
            configVals.bandscan  = -1 * (configVals.frequency / 2);

            return(Response.RequestSuccessful);
        }
Ejemplo n.º 14
0
        // Submit a command and return a response
        protected override void SendCommand(SpectraCyberRequest request, ref SpectraCyberResponse response)
        {
            // If the request is empty, don't process
            if (request.IsEmpty())
            {
                return;
            }

            try
            {
                // Attempt to write the command to the serial port
                ((SpectraCyber)SpectraCyber).SerialPort.Write(request.CommandString);
            }
            catch (Exception)
            {
                // Something went wrong, return the response
                SerialCommsFailed = true;
                return;
            }

            // Command was successfully sent through serial communication
            response.RequestSuccessful = true;

            // Give the SpectraCyber some time to process the command
            Thread.Sleep(AbstractSpectraCyberConstants.WAIT_TIME_MS);

            // Check for any significant cases
            switch (request.CommandType)
            {
                // Termination, safely end communication
                case SpectraCyberCommandTypeEnum.TERMINATE:
                    BringDown();
                    break;

                // TODO: implement this case further probably
                case SpectraCyberCommandTypeEnum.SCAN_STOP:
                    break;

                // Purge the serial buffer
                case SpectraCyberCommandTypeEnum.DATA_DISCARD:
                    ((SpectraCyber)SpectraCyber).SerialPort.DiscardInBuffer();
                    break;

                //
                // Do nothing by default
                //
            }

            // If the request expects a reply back, capture the data and attach it to the response
            if (request.WaitForReply)
            {
                // Reponse's data is valid (assuming no exceptions are thrown)
                response.Valid = true;

                try
                {
                    // Create a character array in which to store the buffered characters
                    string hexString;

                    // Read a number of characters in the buffer
                    char[] charInBuffer = new char[AbstractSpectraCyberConstants.BUFFER_SIZE];

                    int length = -1;
                    try
                    {
                        length = ((SpectraCyber)SpectraCyber).SerialPort.Read(charInBuffer, 0, request.CharsToRead);
                    }
                    catch (Exception)
                    {
                        // Something went wrong, return the response
                        SerialCommsFailed = true;
                        return;
                    }

                    // Set the time captured to be as close to the read as possible, in case it's valid
                    response.DateTimeCaptured = DateTime.UtcNow;

                    // Clip the string to the exact number of bytes read
                    if (AbstractSpectraCyberConstants.CLIP_BUFFER_RESPONSE && (length != AbstractSpectraCyberConstants.BUFFER_SIZE))
                    {
                        char[] actual = new char[length];

                        for (int i = 0; i < length; i++)
                        {
                            actual[i] = charInBuffer[i];
                        }

                        hexString = new string(actual);
                    }

                    // Leave the string how it is, with the possibility of trailing chararacters being "0"
                    else
                    {
                        hexString = new string(charInBuffer);
                    }

                    // Set the SerialIdentifier, as heard (but not necessarily expected)
                    response.SerialIdentifier = hexString[0];

                    // Check to see that replyString's first character is what was expected
                    if (response.SerialIdentifier != request.ResponseIdentifier)
                    {
                        throw new Exception();
                    }

                    // Convert the hex string into an int
                    response.DecimalData = HexStringToInt(hexString.Substring(1));
                }
                catch (Exception e)
                {
                    // Something went wrong, the response isn't valid
                    logger.Info("[SpectraCyberController] Failed to receive a response: " + e.ToString());
                    response.Valid = false;
                }
            }

            // Clear the input buffer
            ((SpectraCyber)SpectraCyber).SerialPort.DiscardInBuffer();
        }
Ejemplo n.º 15
0
 protected override void SendCommand(SpectraCyberRequest request, ref SpectraCyberResponse response)
 {
     // pass
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Method used to calibrate the Radio Telescope before each observation.
        ///
        /// The implementation of this functionality is on a "per-RT" basis, as
        /// in this may or may not work, it depends on if the derived
        /// AbstractRadioTelescope class has implemented it.
        /// </summary>
        public MovementResult ThermalCalibrateRadioTelescope(MovementPriority priority)
        {
            MovementResult moveResult = MovementResult.None;

            // Return if incoming priority is equal to or less than current movement
            if (priority <= RadioTelescope.PLCDriver.CurrentMovementPriority)
            {
                return(MovementResult.AlreadyMoving);
            }

            // We only want to do this if it is safe to do so. Return false if not
            if (!AllSensorsSafe)
            {
                return(MovementResult.SensorsNotSafe);
            }

            // If a lower-priority movement was running, safely interrupt it.
            RadioTelescope.PLCDriver.InterruptMovementAndWaitUntilStopped();

            // If the thread is locked (two moves coming in at the same time), return
            if (Monitor.TryEnter(MovementLock))
            {
                Orientation current = GetCurrentOrientation();

                moveResult = RadioTelescope.PLCDriver.MoveToOrientation(MiscellaneousConstants.THERMAL_CALIBRATION_ORIENTATION, current);

                if (moveResult != MovementResult.Success)
                {
                    if (RadioTelescope.PLCDriver.CurrentMovementPriority != MovementPriority.Critical)
                    {
                        RadioTelescope.PLCDriver.CurrentMovementPriority = MovementPriority.None;
                    }
                    Monitor.Exit(MovementLock);
                    return(moveResult);
                }

                // start a timer so we can have a time variable
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();

                // temporarily set spectracyber mode to continuum
                RadioTelescope.SpectraCyberController.SetSpectraCyberModeType(SpectraCyberModeTypeEnum.CONTINUUM);

                // read data
                SpectraCyberResponse response = RadioTelescope.SpectraCyberController.DoSpectraCyberScan();

                // end the timer
                stopWatch.Stop();
                double time = stopWatch.Elapsed.TotalSeconds;

                RFData rfResponse = RFData.GenerateFrom(response);

                // move back to previous location
                moveResult = RadioTelescope.PLCDriver.MoveToOrientation(current, MiscellaneousConstants.THERMAL_CALIBRATION_ORIENTATION);
                if (moveResult != MovementResult.Success)
                {
                    if (RadioTelescope.PLCDriver.CurrentMovementPriority != MovementPriority.Critical)
                    {
                        RadioTelescope.PLCDriver.CurrentMovementPriority = MovementPriority.None;
                    }
                    RadioTelescope.SpectraCyberController.StopScan();
                    Monitor.Exit(MovementLock);
                    return(moveResult);
                }

                // analyze data
                // temperature (Kelvin) = (intensity * time * wein's displacement constant) / (Planck's constant * speed of light)
                double weinConstant   = 2.8977729;
                double planckConstant = 6.62607004 * Math.Pow(10, -34);
                double speedConstant  = 299792458;
                double temperature    = (rfResponse.Intensity * time * weinConstant) / (planckConstant * speedConstant);

                // convert to fahrenheit
                temperature = temperature * (9 / 5) - 459.67;

                // check against weather station reading
                double weatherStationTemp = RadioTelescope.WeatherStation.GetOutsideTemp();

                // Set SpectraCyber mode back to UNKNOWN
                RadioTelescope.SpectraCyberController.SetSpectraCyberModeType(SpectraCyberModeTypeEnum.UNKNOWN);

                // return true if working correctly, false if not
                if (Math.Abs(weatherStationTemp - temperature) < MiscellaneousConstants.THERMAL_CALIBRATION_OFFSET)
                {
                    moveResult = RadioTelescope.PLCDriver.MoveToOrientation(MiscellaneousConstants.Stow, current);
                }

                if (RadioTelescope.PLCDriver.CurrentMovementPriority != MovementPriority.Critical)
                {
                    RadioTelescope.PLCDriver.CurrentMovementPriority = MovementPriority.None;
                }

                RadioTelescope.SpectraCyberController.StopScan();
                Monitor.Exit(MovementLock);
            }
            else
            {
                moveResult = MovementResult.AlreadyMoving;
            }

            return(moveResult);
        }
 protected abstract void SendCommand(SpectraCyberRequest request, ref SpectraCyberResponse response);
 public void Init()
 {
     SCResponse = new SpectraCyberResponse();
 }