/// <summary> /// Initializes a new instance of the <see cref="StepperMotor" /> class. /// </summary> /// <param name="limitOfTravel">The limit of travel in steps.</param> /// <param name="stepper">The hardware driver that will perform the actual microstep.</param> /// <param name="performMicrostep">A method to write the current microstep value to the motor hardware.</param> /// <param name="microSteps">The number of micro steps per whole step.</param> protected StepperMotor(int limitOfTravel, IStepSequencer stepper, StepCallback performMicrostep = null) { //ToDo: this can be improved by deprecating the use of a delegate and instead providing // an implementation of IStepSequencer (defined in the Adafruit motor shield assembly). this.limitOfTravel = limitOfTravel; this.stepper = stepper; performStepCallback = performMicrostep ?? NullStepCallback; stepTimer = new Timer(StepTimerTick, null, Timeout.Infinite, Timeout.Infinite); // Stopped }
public AcceleratingStepperMotor(int limitOfTravel, IStepSequencer stepper) : base(limitOfTravel, stepper) { RampTime = 5.0; // Default to 5 second ramp, compute acceleration. }
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); }
public InstantaneousStepperMotor(int limitOfTravel, IStepSequencer stepper) : base(limitOfTravel, stepper) { }
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 adafruitMotorShieldV1 = new AdafruitV1MotorShield(); adafruitMotorShieldV1.InitializeShield(); StepperM1M2 = adafruitMotorShieldV1.GetMicrosteppingStepperMotor(MicrostepsPerStep, 1, 2); StepperM3M4 = adafruitMotorShieldV1.GetFullSteppingStepperMotor(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); #else throw new ApplicationException("Uncomment one of the shield #define statements"); #endif // Create the stepper motor axes and link them to the shield driver. axis1 = new InstantaneousStepperMotor(LimitOfTravel, StepperM1M2) { MaximumSpeed = MaxSpeed //RampTime = RampTime }; axis1.BeforeStep += UpdateDiagnosticLed; speed = 1.0; direction = +1; var fasterButton = new InterruptPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow); var slowerButton = new InterruptPort(Pins.GPIO_PIN_D2, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow); var reverseButton = new InterruptPort(Pins.GPIO_PIN_D13, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow); fasterButton.OnInterrupt += fasterButton_OnInterrupt; slowerButton.OnInterrupt += slowerButton_OnInterrupt; reverseButton.OnInterrupt += reverseButton_OnInterrupt; SetMotorVelocity(); Thread.Sleep(Timeout.Infinite); }