summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Arduino/library/RepStepper/RepStepper.cpp
blob: 3963a2f6c88be78e221fa52d360d64a20b0cef40 (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

#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 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::setDirection(bool direction)
{
	digitalWrite(this->direction_pin, direction);
	delayMicroseconds(10); //make sure it stabilizes..
	this->direction = direction; //save our direction.
}

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