示例#1
0
        public static void Main()
        {
#if UseOnboardLedForDiagnostics
            Led = new OutputPort(Pins.ONBOARD_LED, false);
#endif
#if AdafruitV1Shield
            var latch  = new OutputPort(Pins.GPIO_PIN_D12, false);
            var enable = new OutputPort(Pins.GPIO_PIN_D7, true);
            var data   = new OutputPort(Pins.GPIO_PIN_D8, false);
            var clock  = new OutputPort(Pins.GPIO_PIN_D4, false);
            var adafruitMotorShieldV1 = new AdafruitV2MotorShield(latch, enable, data, clock);
            adafruitMotorShieldV1.InitializeShield();
            StepperM1M2 = adafruitMotorShieldV1.GetHalfSteppingStepperMotor(1, 2);
            StepperM3M4 = adafruitMotorShieldV1.GetMicrosteppingStepperMotor(64, 3, 4);
#elif AdafruitV2Shield
            var adafruitMotorShieldV2 = new AdafruitV2MotorShield();  // use shield at default I2C address.
            adafruitMotorShieldV2.InitializeShield();
            StepperM1M2 = adafruitMotorShieldV2.GetMicrosteppingStepperMotor(MicrostepsPerStep, 1, 2);
            StepperM3M4 = adafruitMotorShieldV2.GetMicrosteppingStepperMotor(MicrostepsPerStep, 3, 4);
#elif SparkfunArduMotoShield
            var shield = new ArdumotoShield();
            shield.InitializeShield();
            var phase1 = shield.GetHBridge(Connector.A, TargetDevice.Netduino);
            var phase2 = shield.GetHBridge(Connector.B, TargetDevice.Netduino);
            StepperM1M2 = shield.GetMicrosteppingStepperMotor(MicrostepsPerStep, phase1, phase2);
#elif LedSimulatorShield
            var StepperM1M2 = LedMotorSimulator.GetSimulatedStepperMotor(Pins.GPIO_PIN_D0,
                                                                         PWMChannels.PWM_ONBOARD_LED,
                                                                         Pins.GPIO_PIN_D1,
                                                                         PWMChannels.PWM_PIN_D6);
            var StepperM3M4 = LedMotorSimulator.GetSimulatedStepperMotor(Pins.GPIO_PIN_D2,
                                                                         PWMChannels.PWM_PIN_D9,
                                                                         Pins.GPIO_PIN_D3,
                                                                         PWMChannels.PWM_PIN_D10);
#else
            throw new ApplicationException("Uncomment one of the shield #define statements");
#endif

            var axis1 = new AcceleratingStepperMotor(LimitOfTravel, StepperM1M2)
            {
                MaximumSpeed = StepperMotor.MaximumPossibleSpeed,
                RampTime     = 2.0
            };
            var axis2 = new AcceleratingStepperMotor(LimitOfTravel, StepperM3M4)
            {
                MaximumSpeed = StepperMotor.MaximumPossibleSpeed,
                RampTime     = 2.0
            };
            var sequencer       = new DualAxisSequencer(axis1, axis2);
            var randomGenerator = new Random();

            while (true)
            {
                var target = randomGenerator.Next(LimitOfTravel);
                sequencer.RunInSequence(target, target);
                sequencer.BlockUntilSequenceComplete();
                Thread.Sleep(5000); // A pause here just makes it easier to observe what is going on.
            }
        }
示例#2
0
        public static void Main()
        {
            // shield-specific setup. Uncomment one of the shield #define lines above.
#if UseOnboardLedForDiagnostics
            Led = new OutputPort(Pins.ONBOARD_LED, false);
#endif
#if AdafruitV1Shield
            var latch  = new OutputPort(Pins.GPIO_PIN_D12, false);
            var enable = new OutputPort(Pins.GPIO_PIN_D7, true);
            var data   = new OutputPort(Pins.GPIO_PIN_D8, false);
            var clock  = new OutputPort(Pins.GPIO_PIN_D4, false);
            var adafruitMotorShieldV1 = new AdafruitV1MotorShield(latch, enable, data, clock);
            adafruitMotorShieldV1.InitializeShield();
            StepperM1M2 = adafruitMotorShieldV1.GetMicrosteppingStepperMotor(MicrostepsPerStep, 1, 2);
            //StepperM3M4 = adafruitMotorShieldV1.GetMicrosteppingStepperMotor(MicrostepsPerStep, 3, 4);
#elif AdafruitV2Shield
            var adafruitMotorShieldV2 = new SparkfunArdumoto();  // use shield at default I2C address.
            adafruitMotorShieldV2.InitializeShield();
            StepperM1M2 = adafruitMotorShieldV2.GetMicrosteppingStepperMotor(MicrostepsPerStep, 1, 2);
            StepperM3M4 = adafruitMotorShieldV2.GetMicrosteppingStepperMotor(MicrostepsPerStep, 3, 4);
#elif SparkfunArduMotoShield
            var shield = new SparkfunArdumoto();
            shield.InitializeShield();
            var phase1 = shield.GetHBridge(Connector.A, TargetDevice.Netduino);
            var phase2 = shield.GetHBridge(Connector.B, TargetDevice.Netduino);
            StepperM1M2 = shield.GetMicrosteppingStepperMotor(MicrostepsPerStep, phase1, phase2);
#elif LedSimulatorShield
            var StepperM1M2 = LedMotorSimulator.GetSimulatedStepperMotor(Pins.GPIO_PIN_D0,
                                                                         PWMChannels.PWM_ONBOARD_LED,
                                                                         Pins.GPIO_PIN_D1,
                                                                         PWMChannels.PWM_PIN_D6);
            var StepperM3M4 = LedMotorSimulator.GetSimulatedStepperMotor(Pins.GPIO_PIN_D2,
                                                                         PWMChannels.PWM_PIN_D9,
                                                                         Pins.GPIO_PIN_D3,
                                                                         PWMChannels.PWM_PIN_D10);
#else
            throw new ApplicationException("Uncomment one of the shield #define statements");
#endif

            // Create the stepper motor axes and link them to the Adafruit driver.
            var axis1 = new AcceleratingStepperMotor(LimitOfTravel, StepperM1M2)
            {
                MaximumSpeed = MaxSpeed,
                RampTime     = RampTime
            };
            // Now we subscribe to the MotorStopped event on each axis. When the event fires,
            // we start the axis going again with a new random target position.
            axis1.MotorStopped += HandleAxisStoppedEvent;
            // We need to call our event handler once manually to get things going.
            // After that it is fully automatic.
            HandleAxisStoppedEvent(axis1);

            // Repeat for the second axis, if it's supported.
#if UseSecondAxis
            //var axis2 = new StepperMotor(LimitOfTravel, StepperM3M4)
            //    {
            //    MaximumSpeed = MaxSpeed,
            //    RampTime = RampTime
            //    };
            //axis2.MotorStopped += HandleAxisStoppedEvent;
            //HandleAxisStoppedEvent(axis2);
#endif

            // Finally, we sleep forever as there is nothing else to do in the main thread.
            // The motors continue to work in the background all by themselves.
            Thread.Sleep(Timeout.Infinite);
        }