예제 #1
0
        private static void RunUsingSleep(
            IStepDrive stepper, int stepsPerMinute, int pauseBetweenStepsInMs)
        {
            // Save "now":
            DateTime lastTime = DateTime.UtcNow;

            while (true)
            {
                // Compute the next time at which the stepper must work:
                // For demo purposes, you can use one step per second, for example:
                // DateTime nextTime = lastTime.AddSeconds(1.0);
                DateTime nextTime = lastTime.AddMinutes(1.0);

                // Wait the correct time, avoiding cumulative errors:
                Thread.Sleep((int)((nextTime - DateTime.UtcNow).Ticks / TimeSpan.TicksPerMillisecond));

                // Let the stepper do its job to turn the clock:
                stepper.MoveSteps(stepsPerMinute, pauseBetweenStepsInMs);

                // Set this time as the base for the next period:
                lastTime = nextTime;

                // Let the stepper turn freely, save power, and make not even a silent noise caused by PWM:
                stepper.ReleaseHoldingTorque();
            }
        }
예제 #2
0
        /// <summary>
        /// Runs a mechanical clock by turning a stepper motor periodically.
        /// </summary>
        /// <param name="stepper">The stepper to drive.</param>
        /// <param name="stepsPerMinute">The number of steps that the stepper shall perform each minute. This may be any
        /// positive or negative non-null number and depends on the mechanical implementation of the actual clock.</param>
        /// <param name="pauseBetweenStepsInMs">The number of milliseconds to wait between stepper steps. Set this
        /// parameter depending on the mechanical implementation.</param>
        public static void Run(IStepDrive stepper, int stepsPerMinute, int pauseBetweenStepsInMs)
        {
            if (stepper == null)
            {
                throw new ArgumentNullException(nameof(stepper));
            }
            if (stepsPerMinute == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(stepsPerMinute));
            }

            // Variant 1: Sleep a minute, turn the stepper, sleep again:
            // RunUsingSleep(stepper, stepsPerMinute, pauseBetweenStepsInMs);

            // Variant 2: Use a timer fireing each minute:
            RunUsingTimer(stepper, stepsPerMinute, pauseBetweenStepsInMs);
        }
예제 #3
0
        /// <summary>
        /// Let a stepper turn randomly.
        /// </summary>
        /// <param name="stepper"></param>
        public static void Run(IStepDrive stepper)
        {
            if (stepper == null)
            {
                throw new ArgumentNullException(nameof(stepper));
            }

            var random = new Random();

            const int steps = 200;

            while (true)
            {
                stepper.MoveSteps(random.Next(steps * 2 + 1) - steps, 0);
                stepper.ReleaseHoldingTorque();
                Thread.Sleep(1000);
            }
        }
예제 #4
0
        private static void RunUsingTimer(
            IStepDrive stepper, int stepsPerMinute, int pauseBetweenStepsInMs)
        {
            // Do steps each minute, that is, every 60000 milliseconds:
            var timer = new Timer(
                (state) =>
            {
                stepper.MoveSteps(stepsPerMinute, pauseBetweenStepsInMs);
                stepper.ReleaseHoldingTorque();
            },
                null,
                60000,
                60000);

            // Sleep and let the timer do its job:
            for (; ;)
            {
                Thread.Sleep(1000);
            }
        }