/// <summary>
        /// Create a handler for the given recording timepoints file.
        /// </summary>
        /// <param name="filename">Filename of the CSV file containing the recording timepoints</param>
        internal static void Read(String filename)
        {
            RecordingTimepoint timepoint;
            StreamReader       reader;
            String             line;

            logger.Debug("Enter: Read(String)");

            // Read in the file

            if (!File.Exists(filename))
            {
                logger.Debug("Recording file " + filename + " not found.");
                return;
            }

            logger.Debug("Reading recording file: " + filename);

            reader = new StreamReader(filename);

            while ((line = reader.ReadLine()) != null)
            {
                // Ignore comment lines
                if (line.StartsWith(";"))
                {
                    continue;
                }
                if (line.StartsWith("s"))
                {
                    continue;
                }

                // Parse the line into a set of recording timepoints
                timepoint = new RecordingTimepoint(line);
                if (!trialStartIndices.ContainsKey(timepoint.TrialNumber))
                {
                    trialStartIndices[timepoint.TrialNumber] = list.Count;
                }
                list.Add(timepoint);
            }
        }
        /// <summary>
        /// Returns the value of the recording at a given time.
        /// </summary>
        /// <param name="trialNumber">The trial number to search data for</param>
        /// <param name="secondsIntoTrial">The value is calculated for this number of seconds into the trial</param>
        internal static RecordingTimepoint GetRecording(int trialNumber, double secondsIntoTrial)
        {
            RecordingTimepoint lower;
            RecordingTimepoint upper;
            RecordingTimepoint result;
            int trialStartIndex;
            int trialEndIndex;

            // Get the starting index.
            if (trialStartIndices.ContainsKey(trialNumber))
            {
                trialStartIndex = trialStartIndices[trialNumber];
            }
            else
            {
                return(new RecordingTimepoint());
            }

            // Get the ending index for the trial.
            if (trialStartIndices.ContainsKey(trialNumber + 1))
            {
                trialEndIndex = trialStartIndices[trialNumber + 1] - 1;
            }
            else
            {
                trialEndIndex = list.Count - 1;
            }

            // If there is only one timepoint, return it
            if (trialStartIndex == trialEndIndex)
            {
                return(list[trialStartIndex]);
            }

            // If the recording data is requested for a time in the past, return the recording timepoint
            if (secondsIntoTrial <= list[trialStartIndex].Time)
            {
                return(list[trialStartIndex]);
            }

            // If the recording data is requested for a time in the future, return the last recording timepoint
            if (secondsIntoTrial >= list[trialEndIndex].Time)
            {
                return(list[trialEndIndex]);
            }

            // Find the two timepoints to interpolate between

            lower = null;
            upper = null;
            for (int i = trialStartIndex; i <= trialEndIndex; i++)
            {
                if ((list[i].Time > secondsIntoTrial) || (list[i + 1].Time < secondsIntoTrial))
                {
                    continue;
                }

                lower = list[i];
                upper = list[i + 1];
                break;
            }

            if ((lower == null) || (upper == null))
            {
                // Something went wrong. Return an empty timepoint.
                return(new RecordingTimepoint());
            }

            // Interpolate
            result = new RecordingTimepoint();
            result.Angles.pitch                   = Interpolate(lower.Angles.pitch, upper.Angles.pitch, lower.Time, upper.Time, secondsIntoTrial);
            result.Angles.roll                    = Interpolate(lower.Angles.roll, upper.Angles.roll, lower.Time, upper.Time, secondsIntoTrial);
            result.Angles.yaw                     = Interpolate(lower.Angles.yaw, upper.Angles.yaw, lower.Time, upper.Time, secondsIntoTrial);
            result.DirectionOfBalance.pitch       = lower.DirectionOfBalance.pitch;
            result.DirectionOfBalance.roll        = lower.DirectionOfBalance.roll;
            result.DirectionOfBalance.yaw         = lower.DirectionOfBalance.yaw;
            result.MovingDirectionOfBalance.pitch = lower.MovingDirectionOfBalance.pitch;
            result.MovingDirectionOfBalance.roll  = lower.MovingDirectionOfBalance.roll;
            result.MovingDirectionOfBalance.yaw   = lower.MovingDirectionOfBalance.yaw;
            result.TrialNumber                    = lower.TrialNumber;
            result.TrialPhase                     = lower.TrialPhase;
            result.Time = secondsIntoTrial;

            return(result);
        }
示例#3
0
        /// <summary>
        /// Logs the data from the round of calculations.
        /// </summary>
        /// <param name="time">Seconds since start of trial</param>
        /// <param name="currentMotion">Current motion</param>
        /// <param name="calculatedMotion">New motion, calculated by dynamics</param>
        /// <param name="recording">The c</param>
        /// <param name="controlInput">Current joystick position</param>
        internal void LogData(double time, MotionCommand currentMotion, MotionCommand calculatedMotion, RecordingTimepoint recording, ControlInput controlInput)
        {
            StringBuilder builder;

            builder = new StringBuilder();

            if (Hulk.InnerAxis == Hulk.Axis.Roll)
            {
                builder.AppendFormat(
                    "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18}\n",
                    time,
                    recording.TrialNumber,
                    recording.TrialPhase,
                    recording.DirectionOfBalance.roll,
                    recording.DirectionOfBalance.pitch,
                    recording.DirectionOfBalance.yaw,
                    recording.MovingDirectionOfBalance.roll,
                    recording.MovingDirectionOfBalance.pitch,
                    recording.MovingDirectionOfBalance.yaw,
                    currentMotion.innerPosition.ToString("F6"),
                    currentMotion.outerPosition.ToString("F6"),
                    0,
                    currentMotion.innerVelocity.ToString("F6"),
                    currentMotion.outerVelocity.ToString("F6"),
                    0,
                    calculatedMotion.innerPosition.ToString("F6"),
                    calculatedMotion.outerPosition.ToString("F6"),
                    0,
                    (controlInput.trigger ? "1" : "0")
                    );
            }
            else
            {
                builder.AppendFormat(
                    "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18}\n",
                    time,
                    recording.TrialNumber,
                    recording.TrialPhase,
                    recording.DirectionOfBalance.roll,
                    recording.DirectionOfBalance.pitch,
                    recording.DirectionOfBalance.yaw,
                    recording.MovingDirectionOfBalance.roll,
                    recording.MovingDirectionOfBalance.pitch,
                    recording.MovingDirectionOfBalance.yaw,
                    0,
                    currentMotion.outerPosition.ToString("F6"),
                    currentMotion.innerPosition.ToString("F6"),
                    0,
                    currentMotion.outerVelocity.ToString("F6"),
                    currentMotion.innerVelocity.ToString("F6"),
                    0,
                    calculatedMotion.outerPosition.ToString("F6"),
                    calculatedMotion.innerPosition.ToString("F6"),
                    (controlInput.trigger ? "1" : "0")
                    );
            }

            DataLogger.AppendData(builder.ToString());
        }