One thing I'm not crazy about is my RotateDegrees() function but I couldn't find a better method to convert a double to a int because of the limitations of .Net MicroFramework.
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace PBRobot.Motion { /* Version 1.0.1 * * This class controls the EasyDriver which controls the stepper motors */ public class EasyDriver { #region Enums public enum StepResolution { FullStep, HalfStep, QuarterStep, EighthStep } #endregion #region Properties public OutputPort _DirectionPin { private get; set; } public OutputPort _StepPin { private get; set; } public OutputPort _MS1 { private get; set; } public OutputPort _MS2 { private get; set; } public bool CanSetOutput { get; private set; } public bool Working { get; private set; } public int Steps { get; private set; } public double _CurrentResolution { get; private set; } #endregion #region Initialization public EasyDriver(Cpu.Pin DirectionPin, Cpu.Pin StepPin) :this(DirectionPin, StepPin, Pins.GPIO_NONE, Pins.GPIO_NONE) { CanSetOutput = false; } public EasyDriver(Cpu.Pin DirectionPin, Cpu.Pin StepPin, Cpu.Pin MS1, Cpu.Pin MS2) { _DirectionPin = new OutputPort(DirectionPin, false); _StepPin = new OutputPort(StepPin, false); if (MS1 != Pins.GPIO_NONE) _MS1 = new OutputPort(MS1, false); if (MS2 != Pins.GPIO_NONE) _MS2 = new OutputPort(MS2, false); CanSetOutput = true; SetCurrentResolution(StepResolution.EighthStep); } #endregion #region Methods public void SetDirection(bool Direction) { _DirectionPin.Write(Direction); } public void RotateSteps(int NumberOfSteps) { Activate(NumberOfSteps); } public void RotateDegrees(double Degrees) { string temp = (Degrees/_CurrentResolution).ToString(); int NumberOfSteps = Convert.ToInt32(temp); Activate(NumberOfSteps); } private void Activate(int NumberOfSteps) { if (!Working) { Working = true; Steps = NumberOfSteps; for (int x = 0; x < Steps; x++) { _StepPin.Write(true); Thread.Sleep(1); _StepPin.Write(false); } Working = false; } } public void SetMS1(bool Output) { if (CanSetOutput) _MS1.Write(Output); } public void SetMS2(bool Output) { if (CanSetOutput) _MS2.Write(Output); } public void SetCurrentResolution(StepResolution NewResolution) { if (CanSetOutput) { switch (NewResolution) { case StepResolution.FullStep: _CurrentResolution = 1.8; break; case StepResolution.HalfStep: _CurrentResolution = 0.9; break; case StepResolution.QuarterStep: _CurrentResolution = 0.45; break; case StepResolution.EighthStep: _CurrentResolution = 0.225; break; } } } public void SetStepResolution(StepResolution NewResolution) { bool tempMS1 = true; bool tempMS2 = true; switch(NewResolution) { case StepResolution.FullStep: tempMS1 = false; tempMS2 = false; break; case StepResolution.HalfStep: tempMS1 = true; tempMS2 = false; break; case StepResolution.QuarterStep: tempMS1 = false; tempMS2 = true; break; case StepResolution.EighthStep: tempMS1 = true; tempMS2 = true; break; } SetMS1(tempMS1); SetMS2(tempMS2); } public void Stop() { Steps = 0; } #endregion } }
And to use it:
... EasyDriver StepperMotor = new EasyDriver(Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2); StepperMotor.SetDirection(true); StepperMotor.RotateDegrees(360.0); ...