summaryrefslogtreecommitdiff
path: root/tags/firmware/Arduino/1.3/library/RepStepper/RepStepper.cpp
blob: dc7059317600173d451c56f518557108ab12ecfa (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include "RepStepper.h"

/*
 * two-wire constructor.
 * Sets which wires should control the motor.
 */
RepStepper::RepStepper(int number_of_steps, byte dir_pin, byte step_pin, byte enable_pin)
{
	//init our variables.
	this->setSpeed(0);

	//get our parameters
	this->number_of_steps = number_of_steps;
	this->step_pin = step_pin;
	this->direction_pin = dir_pin;
	this->enable_pin = enable_pin;
	
	// setup the pins on the microcontroller:
	pinMode(this->step_pin, OUTPUT);
	pinMode(this->direction_pin, OUTPUT);
	this->enable();
	this->setDirection(RS_FORWARD);
}

/*
  Sets the speed in ticks per step
*/
void RepStepper::setSpeed(long speed)
{
	step_delay = speed;
	
	if (step_delay > 0)
		rpm = 960000000UL / (step_delay * number_of_steps);
	else
		rpm = 0;
}

/*
  Sets the speed in revs per minute
*/
void RepStepper::setRPM(int new_rpm)
{
	if (new_rpm == 0)
	{
		step_delay = 0;
		rpm = 0;
	}
	else
	{
		rpm = new_rpm;
		
		//lets use the highest precision possible... processor ticks.
		// 16MHZ = 16,000,000 ticks/sec * 60 seconds in a minute = 960,000,000 ticks / minute
		// take the total # of ticks / steps per rev / number of revolutions per minute = ticks per step
		step_delay = (960000000UL / number_of_steps) / rpm;
	}
}

void RepStepper::setSteps(int steps)
{
	number_of_steps = steps;
	
	//recalculate our speed.
	this->setRPM(this->rpm);
}

void RepStepper::setDirection(bool direction)
{
	digitalWrite(this->direction_pin, direction);
	delayMicroseconds(10); //make sure it stabilizes..
	this->direction = direction; //save our direction.
}

int RepStepper::getMicros()
{
	return step_delay / 16;
}

void RepStepper::enable()
{
	if (this->enable_pin != 255)
	{
		digitalWrite(enable_pin, HIGH);
		delayMicroseconds(10); //make sure it stabilizes
	}

	enabled = true;
}

void RepStepper::disable()
{
	if (this->enable_pin != 255)
	{
		digitalWrite(this->enable_pin, LOW);
		delayMicroseconds(10); //make sure it stabilizes
	}

	enabled = false;
}

//this sends a pulse to our stepper controller.
void RepStepper::pulse()
{
	digitalWrite(this->step_pin, HIGH);
	delayMicroseconds(5); //make sure it stabilizes... for opto isolated stepper drivers.
	digitalWrite(this->step_pin, LOW);
}