public void TestSpeedException()
        {
            var pin1 = new Mock <IGpioPin>();
            var pin2 = new Mock <IGpioPin>();

            var stepper = new StepperMotor(200, pin1.Object, pin2.Object);

            Assert.Throws <Exception>(() => stepper.SetSpeed(-1));
            Assert.Throws <Exception>(() => stepper.SetSpeed(0));
        }
Exemple #2
0
        public async Task Init()
        {
            var gpio = GpioController.GetDefault();

            // Show an error if there is no GPIO controller
            if (gpio == null)
            {
                logger.WriteLn("There is no GPIO controller on this device.");
                return;
            }

            this.switchX = gpio.OpenPin(5);
            this.switchY = gpio.OpenPin(6);
            switchX.SetDriveMode(GpioPinDriveMode.Input);
            switchY.SetDriveMode(GpioPinDriveMode.Input);
            logger.WriteLn("GPIO initialized", LogType.Success);

            await servoDriver.Init();

            logger.WriteLn("Pen driver initialized", LogType.Success);
            await PenUp();

            await motorDriver.Init();

            motorX.SetSpeed(200);
            motorY.SetSpeed(200);
            logger.WriteLn("Plotter initialized", LogType.Success);
        }
        public void TestMovementTiming(int revsPerMinute)
        {
            var pin1 = new Mock <IGpioPin>();
            var pin2 = new Mock <IGpioPin>();

            var stepper = new StepperMotor(200, pin1.Object, pin2.Object);

            var timer = new Stopwatch();

            timer.Start();
            stepper.SetSpeed(revsPerMinute);
            var expectedDelay = revsPerMinute == 10 ? 30 : 15;

            stepper.Move(1, Direction.Clockwise);
            var elapsed1 = timer.ElapsedMilliseconds;

            Assert.InRange(elapsed1, 0, expectedDelay);

            stepper.Move(1, Direction.Clockwise);
            var elapsed2 = timer.ElapsedMilliseconds;

            Assert.InRange(elapsed2, elapsed1 + expectedDelay - 1, elapsed1 + 35);

            stepper.Move(10, Direction.Clockwise);
            var elapsed3 = timer.ElapsedMilliseconds;

            Assert.InRange(elapsed3, elapsed2 + expectedDelay * 10 - 1, elapsed2 + expectedDelay * 10 + 5);

            stepper.Move(5, Direction.Anticlockwise);
            var elapsed4 = timer.ElapsedMilliseconds;

            Assert.InRange(elapsed4, elapsed3 + 5 * expectedDelay - 1, elapsed3 + 5 * expectedDelay + 5);
        }
        private void StepperMotorTest(StepperMotorIndex motorIndex, SteppingMode steppingMode = SteppingMode.FullSteps)
        {
            if (stepperMotor.IsInitialized)
            {
                // Configure steps, and RPM
                const uint steps = 200;
                const byte rpm   = 20;

                // Set speed
                stepperMotor.SetSpeed(rpm);

                // Move motor forward
                stepperMotor.Move(motorIndex, MotorDirection.Forward, steps, steppingMode);
                //stepperMotor.MoveWithSpeedAdjustment(motorIndex, MotorDirection.Forward, steps, steppingMode);

                // ... and go back to initial position
                stepperMotor.Move(motorIndex, MotorDirection.Backward, steps, steppingMode);
                //stepperMotor.MoveWithSpeedAdjustment(motorIndex, MotorDirection.Backward, steps, steppingMode);
            }
        }