summaryrefslogtreecommitdiff
path: root/tags/firmware/Arduino/1.3/library/LinearAxis/LinearAxis.cpp
blob: 85fd14193219a0d0cae41520cf9a2a4406431fb3 (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
109
110
#include "LinearAxis.h"
#include "WConstants.h"

LinearAxis::LinearAxis(char id, int steps, byte dir_pin, byte step_pin, byte min_pin, byte max_pin, byte enable_pin) : stepper(steps, dir_pin, step_pin, enable_pin)
{
	this->id = id;
	this->current = 0;
	this->target = 0;
	this->max = 0;
	this->min_pin = min_pin;
	this->max_pin = max_pin;

	this->stepper.setDirection(RS_FORWARD);

	this->readState();
}

void LinearAxis::readState()
{
	//stop us if we're on target
	if (this->target == this->current)
		this->can_step = false;
	//stop us if we're at home and still going 
	else if (this->atMin() && (this->stepper.direction == RS_REVERSE))
		this->can_step = false;
	//stop us if we're at max and still going
	else if (this->atMax() && (this->stepper.direction == RS_FORWARD))
		this->can_step = false;
	//default to being able to step
	else
		this->can_step = true;
}

bool LinearAxis::atMin()
{
	return digitalRead(this->min_pin);
}

/*
 * NB!!!  Turned off by Adrian to free up pins
*/
bool LinearAxis::atMax()
{
	return 0;
	//return digitalRead(this->max_pin);
}

void LinearAxis::doStep()
{
	//gotta call readState() before you can step again!
	//this->can_step = false;
	
	//record our step
	if (this->stepper.direction == RS_FORWARD)
		this->forward1();
	else
		this->reverse1();
}

void LinearAxis::forward1()
{
	stepper.setDirection(RS_FORWARD);
	stepper.pulse();
	
	this->current++;
}

void LinearAxis::reverse1()
{
	stepper.setDirection(RS_REVERSE);
	stepper.pulse();
	
	this->current--;
}

void LinearAxis::setPosition(long p)
{
	this->current = p;
	
	//recalculate stuff.
	this->setTarget(this->target);
}

void LinearAxis::setTarget(long t)
{
	this->target = t;
	
	if (this->target >= this->current)
		stepper.setDirection(RS_FORWARD);
	else
		stepper.setDirection(RS_REVERSE);
		
	this->delta = abs(this->target - this->current);
}

void LinearAxis::initDDA(long max_delta)
{
	this->counter = -max_delta/2;
}

void LinearAxis::ddaStep(long max_delta)
{
	this->counter += this->delta;

	if (this->counter > 0)
	{
		this->doStep();
		this->counter -= max_delta;
	}
}