summaryrefslogtreecommitdiff
path: root/trunk/software/firmware/FiveD_GCode/FiveD_GCode_Interpreter/cartesian_dda.h
blob: 2095601abcb9f106d5d9f4dbabd9beeab2dea85e (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * This class controls the movement of the RepRap machine.
 * It implements a DDA in five dimensions, so the length of extruded 
 * filament is treated as a variable, just like X, Y, and Z.  Speed
 * is also a variable, making accelleration and deceleration automatic.
 *
 * Adrian Bowyer 9 May 2009
 */

#ifndef CARTESIAN_DDA_H
#define CARTESIAN_DDA_H

// Main class for moving the RepRap machine about

class cartesian_dda
{
private:


  bool using_mm;
  FloatPoint units;            // Factors for converting either mm or inches to steps

  FloatPoint target_position;  // Where it's going
  FloatPoint delta_position;   // The difference between the two
  float distance;              // How long the path is
  
  LongPoint current_steps;     // Similar information as above in steps rather than units
  LongPoint target_steps;
  LongPoint delta_steps;
  LongPoint dda_counter;       // DDA error-accumulation variables
  long t_scale;                // When doing lots of t steps, scale them so the DDA doesn't spend for ever on them
  
  volatile bool x_direction;            // Am I going in the + or - direction?
  volatile bool y_direction;
  volatile bool z_direction;
  volatile bool e_direction;
  volatile bool f_direction;

  volatile bool x_can_step;             // Am I not at an endstop?  Have I not reached the target? etc.
  volatile bool y_can_step;
  volatile bool z_can_step;
  volatile bool e_can_step;
  volatile bool f_can_step;

// Variables for acceleration calculations

  volatile long total_steps;            // The number of steps to take along the longest movement axis
  
  long timestep;               // microseconds
  bool nullmove;               // this move is zero length
  volatile bool real_move;     // Flag to know if we've changed something physical
  volatile bool feed_change;   // Flag to know if feedrate has changed
  volatile bool live;          // Flag for when we're plotting a line

// Internal functions that need not concern the user

  // Take a single step

  void do_x_step();               
  void do_y_step();
  void do_z_step();
  void do_e_step();
  
  // Can this axis step?
  
  bool xCanStep(long current, long target, bool dir);
  bool yCanStep(long current, long target, bool dir);
  bool zCanStep(long current, long target, bool dir);
  bool eCanStep(long current, long target, bool dir);
  bool fCanStep(long current, long target, bool dir);
  
  // Read a limit switch
  
  //bool read_switch(byte pin, bool inv);
  
  // Work out the number of microseconds between steps
  
  long calculate_feedrate_delay(const float& feedrate);
  
  // Switch the steppers on and off
  
  void enable_steppers();
  void disable_steppers();
  
  
public:

  cartesian_dda();
  
  // Set where I'm going
  
  void set_target(const FloatPoint& p);
  
  // Start the DDA
  
  void dda_start();
  
  // Do one step of the DDA
  
  void dda_step();
  
  // Are we running at the moment?
  
  bool active();
  
  // Are we extruding at the moment?
  
  bool extruding();
  
  // True for mm; false for inches
  
  void set_units(bool using_mm);
  void set_units();
  bool get_units();
  
  FloatPoint returnUnits();
  
  // Kill - stop all activity and turn off steppers
  
  void shutdown();
  
};

// Short functions inline to save memory; particularly useful in the Arduino

inline FloatPoint cartesian_dda::returnUnits()
{
  return units;
}

inline bool cartesian_dda::get_units()
{
  return using_mm;
}

inline bool cartesian_dda::active()
{
  return live;
}

inline bool cartesian_dda::extruding()
{
  return live && (current_steps.e != target_steps.e);
}


inline void cartesian_dda::do_x_step()
{
	digitalWrite(X_STEP_PIN, HIGH);
	digitalWrite(X_STEP_PIN, LOW);
}

inline void cartesian_dda::do_y_step()
{
	digitalWrite(Y_STEP_PIN, HIGH);
	digitalWrite(Y_STEP_PIN, LOW);
}

inline void cartesian_dda::do_z_step()
{
	digitalWrite(Z_STEP_PIN, HIGH);
	digitalWrite(Z_STEP_PIN, LOW);
}

inline void cartesian_dda::do_e_step()
{
        ex[extruder_in_use]->sStep();
}

inline long cartesian_dda::calculate_feedrate_delay(const float& feedrate)
{  
        
	// Calculate delay between steps in microseconds.  Here it is in English:
        // (feedrate is in mm/minute, distance is in mm)
	// 60000000.0*distance/feedrate  = move duration in microseconds
	// move duration/total_steps = time between steps for master axis.

	return round( (distance*60000000.0) / (feedrate*(float)total_steps) );	
}


inline bool cartesian_dda::xCanStep(long current, long target, bool dir)
{
//stop us if we're on target

	if (target == current)
		return false;

//stop us if we're home and still going lower

#if ENDSTOPS_MIN_ENABLED == 1
#if X_ENDSTOP_INVERTING
	if(!dir && !digitalRead(X_MIN_PIN) )
        {
                zeroHit.x = current;
		return false;
        }
#else
	if(!dir && digitalRead(X_MIN_PIN) )
        {
                zeroHit.x = current;
		return false;
        }
#endif
#endif

//stop us if we're at max and still going higher

#if ENDSTOPS_MAX_ENABLED == 1
#if X_ENDSTOP_INVERTING
	if(dir && !digitalRead(X_MAX_PIN) )
		return false;
#else
	if(!dir && digitalRead(X_MAX_PIN) )
		return false;
#endif
#endif

// All OK - we can step
  
	return true;
}

inline bool cartesian_dda::yCanStep(long current, long target, bool dir)
{
//stop us if we're on target

	if (target == current)
		return false;

//stop us if we're home and still going lower

#if ENDSTOPS_MIN_ENABLED == 1
#if Y_ENDSTOP_INVERTING
	if(!dir && !digitalRead(Y_MIN_PIN) )
        {
                zeroHit.y = current;
		return false;
        }
#else
	if(!dir && digitalRead(Y_MIN_PIN) )
        {
                zeroHit.y = current;
		return false;
        }
#endif
#endif

//stop us if we're at max and still going higher

#if ENDSTOPS_MAX_ENABLED == 1
#if Y_ENDSTOP_INVERTING
	if(dir && !digitalRead(Y_MAX_PIN) )
		return false;
#else
	if(!dir && digitalRead(Y_MAX_PIN) )
		return false;
#endif
#endif

// All OK - we can step
  
	return true;
}

inline bool cartesian_dda::zCanStep(long current, long target, bool dir)
{
//stop us if we're on target

	if (target == current)
		return false;

//stop us if we're home and still going lower

#if ENDSTOPS_MIN_ENABLED == 1
#if Z_ENDSTOP_INVERTING
	if(!dir && !digitalRead(Z_MIN_PIN) )
        {
                zeroHit.z = current;
		return false;
        }
#else
	if(!dir && digitalRead(Z_MIN_PIN) )
        {
                zeroHit.z = current;
		return false;
        }
#endif
#endif

//stop us if we're at max and still going higher

#if ENDSTOPS_MAX_ENABLED == 1
#if Z_ENDSTOP_INVERTING
	if(dir && !digitalRead(Z_MAX_PIN) )
		return false;
#else
	if(!dir && digitalRead(Z_MAX_PIN) )
		return false;
#endif
#endif

// All OK - we can step
  
	return true;
}

inline bool cartesian_dda::eCanStep(long current, long target, bool dir)
{
//stop us if we're on target

	return !(target == current);
}

inline bool cartesian_dda::fCanStep(long current, long target, bool dir)
{
//stop us if we're on target

	return !(target == current);
}
#endif