summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Arduino/library/LinearAxis/LinearAxis.h
blob: 4f5c64e8935156a877843f65dcc55efd7f936b5c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
	LinearAxis.h - RepRap Linear Axis library for Arduino

	The interface for controlling a linear axis: stepper motor + min/max sensors + optional encoder

	Memory Usage Estimate: 25 + repstepper usage.

	History:
	* (0.1) Created library by Zach Smith.
	* (0.2) Optimized for less memory usage and faster performance
	* (0.3) 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 LinearAxis_h
#define LinearAxis_h

#include <RepStepper.h>

// library interface description
class LinearAxis {
  public:
    
	// constructors:
    LinearAxis(char id, int steps, byte dir_pin, byte step_pin, byte min_pin, byte max_pin, byte enable_pin);

	//these are our other object variables.
	RepStepper stepper;

	//various guys to interface with class
	void readState();
	void doStep();
	bool atMin();
	bool atMax();
	
	//various position things.
	void setPosition(long position);
	void setTarget(long t);
	void forward1();
	void reverse1();
	
	//dda functions
	void initDDA(long max_delta);
	void ddaStep(long max_delta);

	char id;					//what is our id? x, y, z, etc.
	bool can_step;				//are we capable of taking a step yet?

	long delta;					//our delta for our DDA moves.
	long current;				//this is our current position.
	long target;				//this is our target position.
	long max;					//this is our max coordinate.
	long counter;				//this is our counter variable for dda.

  private:
	
	byte min_pin;
	byte max_pin;
};

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

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

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

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

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

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



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




#endif