Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

EasyDriver Class for Stepper Motors


  • Please log in to reply
9 replies to this topic

#1 Pudgeball

Pudgeball

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationBurlington, Ontario, Canada

Posted 22 July 2011 - 08:24 PM

Hey all, I was searching around for something like this class, and I didn't actually find a class so I wrote one. If you have any comments on how to make it better, I'd love to hear them. :)

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);

...


#2 Glen

Glen

    Member

  • Members
  • PipPip
  • 26 posts

Posted 23 July 2011 - 03:22 AM

Edit: Corey's way below is the best way to do it.

#3 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 24 July 2011 - 01:13 AM

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.


All you need is a cast:

   int NumberOfSteps=(int)(Degrees/_CurrentResolution);


#4 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 24 July 2011 - 02:13 PM

All you need is a cast:

   int NumberOfSteps=(int)(Degrees/_CurrentResolution);


If you're an anal-retentive Aerospace Engineer (like me :blink: ) you might want to allow for the rounding instead of truncation and use one or the other of these...

    int NumberOfSteps = (int)(System.Math.Round(Degrees / _CurrentResolution));
    int NumberOfSteps = (int)(Degrees / _CurrentResolution + 0.5);

Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com

#5 Pudgeball

Pudgeball

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationBurlington, Ontario, Canada

Posted 24 July 2011 - 03:56 PM

If you're an anal-retentive Aerospace Engineer (like me :blink: ) you might want to allow for the rounding instead of truncation and use one or the other of these...

    int NumberOfSteps = (int)(System.Math.Round(Degrees / _CurrentResolution));
    int NumberOfSteps = (int)(Degrees / _CurrentResolution + 0.5);



Haha, yeah I'd probably go with the rounding idea. Thank you very much; Glen, Corey, and Inquisitor!

#6 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 02 February 2012 - 07:18 AM

I think this driver assumes that motor is 200 steps? Because my 400 step motor only does 180 degrees when I specify 360 but 200 step motor does the full 360. To keep this class black boxed... all public underline fields should be set to private. Also how to make it go faster/slower. I see a thread.sleep(1) but it is very slow when I can make my 400 step motor do over 3000 rpm with an arduino board and an easy driver. Other than that, good job. Very useful.

#7 NewNetDuinoUser

NewNetDuinoUser

    New Member

  • Members
  • Pip
  • 1 posts

Posted 04 February 2012 - 05:13 AM

Hi All,

I having some trouble using the above EasyDriver Class
Hardware: Netduino Plus, Big Easy Driver, 125 lb.in stepper motor, ATX power supply 12v+ to ground

I run the following code block, and the stepper activates but fails to rotate correctly, just jumps backwards and forward at random, only small increments(say 3-5 degrees at a time)

EasyDriver StepperMotor = new EasyDriver(Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D13);             while (true)             {                 StepperMotor.SetDirection(true);                 StepperMotor.RotateDegrees(360.0);                                  StepperMotor.SetDirection(false);                 StepperMotor.RotateDegrees(90.0);             }

Is it possible the stepper current is to low? or have i done something else

Any advise would be appreciated
Thanks

#8 inxtremo

inxtremo

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 24 February 2012 - 10:50 AM

EasyDriver StepperMotor = new EasyDriver(Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D13);             while (true)             {                 StepperMotor.SetDirection(true);                 StepperMotor.RotateDegrees(360.0);                                  StepperMotor.SetDirection(false);                 StepperMotor.RotateDegrees(90.0);             }



Hey, try it without
while(true){ blaa blaa}

I don´t know this code but i will try it this weekend. So the tip above may not work. But as far as i seen is in this code
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;
            }
        }

the logic to drive the stepper. So you won´t need an extra loop.

Hope this is right.

Greetings.

#9 inxtremo

inxtremo

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 02 March 2012 - 06:47 AM

Hay, working with the class for a couple of days and all working fine. I opened an other thread because of acceleration of the stepper motor. Any chance this this will included in the class in future? Thanks for the great work. Regards, Daniel

#10 zraju

zraju

    New Member

  • Members
  • Pip
  • 4 posts

Posted 29 October 2012 - 02:42 PM

Hi, I am having trouble with direction; I am using Easy driver and when set direction false or true it does go in one direction only. Any help will be appreciated. Thanks Raj




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.