summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Arduino/library/RepStepper/RepStepper.h
blob: 26d9131cd425ba9a25092765dc09e62c90775a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
	RepStepper.h - RepRap Stepper library for Arduino

	This library interfaces with the RepRap Stepper Motor Driver and other standard stepper controllers
	that use the 2 wire Step/Direction interface.  Loosely based on the Stepper library by Tom Igoe & others: http://www.arduino.cc/en/Reference/Stepper
		
	More information on the stepper driver circuit here: http://make.rrrf.org/smd

	Memory Usage Estimate: 13 bytes

	History:
	
	* (0.1) Forked library by Zach Smith.
	* (0.2) Optimizations to reduce code overhead by Zach Smith
	* (0.3) Added delays for optocoupled driver boards as well as variables to record enable/direction status.
	* (0.4) Rewrote and refactored all code.  Fixed major interrupt bug by Zach Smith.
	
	License: GPL v2.0
*/

// ensure this library description is only included once
#ifndef RepStepper_h
#define RepStepper_h

#include "WConstants.h"

#define RS_FORWARD 1
#define RS_REVERSE 0

// library interface description
class RepStepper {
  public:
    // constructors:
    RepStepper(int number_of_steps, byte dir_pin, byte step_pin, byte enable_pin);

    // various setters methods
	void setRPM(int rpm);
    // Sets the speed in ticks per step
    void setSpeed(long speed) {
      step_delay = speed;
      
      if (step_delay > 0)
        rpm = 960000000UL / (step_delay * number_of_steps);
      else
        rpm = 0;
    }
    void setDirection(bool direction);
	void setSteps(int steps) {
          number_of_steps = steps;
          //recalculate our speed.
          this->setRPM(this->rpm);
        }

	
	int getMicros() {
          return step_delay / 16;
        }
	
    //various methods dealing with stepping.
	void pulse();
	void enable();
	void disable();
	
	//various internal variables: READ ONLY!  Do not set these directly.
	int rpm;					// Speed in RPMs
	long step_delay;  			// delay between steps, in processor ticks, based on speed
    int number_of_steps;		// total number of steps this motor can take
	bool enabled;				//are we enabled?
	bool direction;				//what is our direction?
	
	// motor pin numbers:
    byte step_pin;				//the step signal pin.

  private:


    byte direction_pin;			//the direction pin.
    byte enable_pin;			//the enable pin.
};

#endif