コード例 #1
0
        /// <summary>
        ///     Wait for the given <see cref="SaciaBoard"/> motor
        ///     to reach its stopped position.
        /// </summary>
        /// <param name="board">The <see cref="SaciaBoard"/></param>
        /// <param name="distance">The distance to run the motor</param>
        /// <param name="timeout">The time to wait. If time reach <see cref="TimeoutException"/> will be thrown</param>
        /// <returns></returns>
        /// <exception cref="System.TimeoutException">The operation has timed out waiting for an output to be set.</exception>
        public static async Task RunUntilStopped(this SaciaBoard board, int distance, TimeSpan timeout)
        {
            var expectedPos = board.Position + distance;

            var waitForStopped = board.WaitForStopped(CancellationToken.None, timeout);

            if (board.UseSoftwarePosition)
            {
                board.Run(distance);

                var task = await Task.WhenAny(waitForStopped)
                           .ConfigureAwait(false);

                // re-await so any cancellations or exceptions can be rethrown
                await task;
            }
            else
            {
                var waitForPos = board.WaitForPositionReached(expectedPos, timeout);

                board.Run(distance);

                var task = await Task.WhenAny(waitForStopped, waitForPos).ConfigureAwait(false);

                // re-await so any cancellations or exceptions can be rethrown
                await task;
            }
        }
コード例 #2
0
        /// <summary>
        ///     Wait for the given <see cref="SaciaBoard"/> motor
        ///     to reach its stopped position.
        /// </summary>
        /// <param name="board">The <see cref="SaciaBoard"/></param>
        /// <param name="maxDistance">The maximum distance to run the motor.</param>
        /// <param name="input">The input to wait for.</param>
        /// <param name="value">The expected value of the input.</param>
        /// <param name="timeout">The time to wait. If time reach <see cref="TimeoutException"/> will be thrown</param>
        /// <param name="errorIfMaxDistanceReached">
        ///     If true, throw <see cref="InvalidOperationException"/>
        ///     if max distance reached before input detected
        /// </param>
        /// <returns></returns>
        /// <exception cref="System.TimeoutException">The operation has timed out waiting for an output to be set.</exception>
        public static async Task RunUntilStopped(this SaciaBoard board, int maxDistance, int input, bool value, TimeSpan timeout, bool errorIfMaxDistanceReached = true)
        {
            var maxPos = board.Position + maxDistance;

            var waitForInput = board.WaitForInput(input, value, CancellationToken.None, timeout);
            var motorStopped = board.WaitForStopped(CancellationToken.None, timeout);

            if (board.UseSoftwarePosition)
            {
                board.Run(maxDistance, input, value);

                var task = await Task.WhenAny(waitForInput, motorStopped).ConfigureAwait(false);

                //if (board.Inputs[input] != value)
                //{
                //    if (board.Position == maxPos && errorIfMaxDistanceReached)
                //    {
                //        throw new InvalidOperationException("Max distance reached before input detected.");
                //    }
                //}

                await task;
            }
            else
            {
                var waitForPos = board.WaitForPositionReached(maxPos, timeout);

                board.Run(maxDistance, input, value);

                var task = await Task.WhenAny(waitForInput, motorStopped, waitForPos);

                if (board.Inputs[input] != value)
                {
                    if (board.Position == maxPos && errorIfMaxDistanceReached)
                    {
                        throw new InvalidOperationException("Max distance reached before input detected.");
                    }
                }

                await task;
            }
        }