chiark / gitweb /
refresh
[marlin.git] / patches / m206-always-use-homing-homeing
1 Bottom: 681cec9bc49d48ceecfc6d5e9eff1c4f7f623fc6
2 Top:    eaaf908848a4a0cd0e69f559ed75c700974fc20f
3 Author: Ian Jackson <ijackson@chiark.greenend.org.uk>
4 Date:   2012-08-01 21:12:14 +0100
5
6 M206: always use homing ("homeing") offsets
7
8 Previously the parameters set in M206 would only be used if a G82
9 command was sent with specific axis home values.  This limits its
10 usefulness.
11
12 Really, we should have a way to adjust the XYZ homing of a machine in
13 the eeprom.  So as the first stage of this, make M206 affect every
14 home command.  The values set using M206 are now added to the
15 configuration variables [XYZ]_HOME_POS.
16
17 This is achieved by replacing all uses of [XYZ]_HOME_POS in the code
18 by a new home_pos[] which includes the adjustment.  We also have to
19 adjust the uses of [XYZ]_{MIN,MAX}_POS similarly - see below.
20
21
22 To allow axis_is_at_home to be written as a function taking an axis
23 index rather than a macro taking an axis letter, we provide
24 constant arrays in program memory containing the values of
25 [XYZ]_{MIN,MAX,HOME}_POS from the compiled-in configuration.
26
27 This is done with some helper macros to deal with the declaration
28 (XYZ_CONSTS_FROM_CONFIG) and definition of the inline function which
29 does the program memory access.
30
31 We also introduce the overloaded function read_pgm_any, whose
32 instances are produced with DEFINE_PGM_READ_ANY, which allows the
33 access functions to automatically produce the correct type.
34
35 The type- and pointer-massaging code in the access function boils
36 down, when compiled, to a simple program memory access.
37
38
39 A question arises: if the M206 offset is set, should this adjustment
40 to the home position shift or change the possible range of movement
41 permitted by the software endstops ?
42
43 The documentation in Configuration.h describes these limits as:
44     // Travel limits after homing
45 Since this is a file containing physical limits, and actual suggested
46 values for these configuration parameters appear to include a certain
47 amount of slop, I've taken the view that these should be regarded as
48 nominal physical distances from the limit switches, and that the
49 permissible travel should be unaffected by M206.
50
51 So for example with the (rather unrealistic)
52   #define X_HOME_DIR -1
53   #define X_MIN_POS -20
54   #define X_HOME_POS 0
55   #define X_MAX_POS 100
56 no matter the setting of M206 X, the machine would be permitted
57 to move from 20mm "beyond" the limit switch trigger point in
58 the negative X direction and 100mm away from the limit switch in
59 the positive X direction, for a total travel of 120mm.
60
61 With M206 X-10 that would be considered to correspond to X coordinates
62 -30 to +90.  With M206 X+10 that would be considered to correspond to
63 X coordinates -10 to +110.
64
65
66 fixes #200 (in ErikZalm/Marlin).
67
68 Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
69
70
71 ---
72
73 diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h
74 index 75b57d0..b465d85 100644
75 --- a/Marlin/Marlin.h
76 +++ b/Marlin/Marlin.h
77 @@ -184,6 +184,8 @@ extern float homing_feedrate[];
78  extern bool axis_relative_modes[];
79  extern float current_position[NUM_AXIS] ;
80  extern float add_homeing[3];
81 +extern float min_pos[3];
82 +extern float max_pos[3];
83  extern unsigned char FanSpeed;
84  
85  // Handling multiple extruders pins
86 diff --git a/Marlin/Marlin.pde b/Marlin/Marlin.pde
87 index 5268a3d..c153298 100644
88 --- a/Marlin/Marlin.pde
89 +++ b/Marlin/Marlin.pde
90 @@ -143,6 +143,8 @@ volatile bool feedmultiplychanged=false;
91  volatile int extrudemultiply=100; //100->1 200->2
92  float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
93  float add_homeing[3]={0,0,0};
94 +float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
95 +float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
96  uint8_t active_extruder = 0;
97  unsigned char FanSpeed=0;
98  
99 @@ -543,6 +545,28 @@ bool code_seen(char code)
100    return (strchr_pointer != NULL);  //Return True if a character was found
101  }
102  
103 +#define DEFINE_PGM_READ_ANY(type, reader)              \
104 +    static inline float pgm_read_any(const type *p)    \
105 +       { return pgm_read_##reader##_near(p); }
106 +
107 +DEFINE_PGM_READ_ANY(float,       float);
108 +
109 +#define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG)    \
110 +static const PROGMEM type array##_P[3] =               \
111 +    { X_##CONFIG, Y_##CONFIG, Z_##CONFIG };            \
112 +static inline type array(int axis)                     \
113 +    { return pgm_read_any(&array##_P[axis]); }
114 +
115 +XYZ_CONSTS_FROM_CONFIG(float, base_min_pos,    MIN_POS);
116 +XYZ_CONSTS_FROM_CONFIG(float, base_max_pos,    MAX_POS);
117 +XYZ_CONSTS_FROM_CONFIG(float, base_home_pos,   HOME_POS);
118 +
119 +static void axis_is_at_home(int axis) {
120 +  current_position[axis] = base_home_pos(axis) + add_homeing[axis];
121 +  min_pos[axis] =          base_min_pos(axis) + add_homeing[axis];
122 +  max_pos[axis] =          base_max_pos(axis) + add_homeing[axis];
123 +}
124 +
125  #define HOMEAXIS(LETTER) \
126    if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
127      { \
128 @@ -564,8 +588,8 @@ bool code_seen(char code)
129      plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
130      st_synchronize();\
131      \
132 -    current_position[LETTER##_AXIS] = LETTER##_HOME_POS;\
133 -    destination[LETTER##_AXIS] = current_position[LETTER##_AXIS];\
134 +    axis_is_at_home(LETTER##_AXIS);                                    \
135 +    destination[LETTER##_AXIS] = current_position[LETTER##_AXIS]; \
136      feedrate = 0.0;\
137      endstops_hit_on_purpose();\
138    }
139 @@ -678,8 +702,8 @@ void process_commands()
140          plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
141          st_synchronize();
142      
143 -        current_position[X_AXIS] = X_HOME_POS;
144 -        current_position[Y_AXIS] = Y_HOME_POS;
145 +        axis_is_at_home(X_AXIS);
146 +        axis_is_at_home(Y_AXIS);
147          plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
148          destination[X_AXIS] = current_position[X_AXIS];
149          destination[Y_AXIS] = current_position[Y_AXIS];
150 @@ -1544,15 +1568,15 @@ void get_arc_coordinates()
151  void clamp_to_software_endstops(float target[3])
152  {
153    if (min_software_endstops) {
154 -    if (target[X_AXIS] < X_MIN_POS) target[X_AXIS] = X_MIN_POS;
155 -    if (target[Y_AXIS] < Y_MIN_POS) target[Y_AXIS] = Y_MIN_POS;
156 -    if (target[Z_AXIS] < Z_MIN_POS) target[Z_AXIS] = Z_MIN_POS;
157 +    if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
158 +    if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
159 +    if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
160    }
161  
162    if (max_software_endstops) {
163 -    if (target[X_AXIS] > X_MAX_POS) target[X_AXIS] = X_MAX_POS;
164 -    if (target[Y_AXIS] > Y_MAX_POS) target[Y_AXIS] = Y_MAX_POS;
165 -    if (target[Z_AXIS] > Z_MAX_POS) target[Z_AXIS] = Z_MAX_POS;
166 +    if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
167 +    if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
168 +    if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
169    }
170  }
171  
172 diff --git a/README.md b/README.md
173 index 86dd93d..fb2c189 100644
174 --- a/README.md
175 +++ b/README.md
176 @@ -152,6 +152,7 @@ Movement variables:
177  *   M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!\r
178  *   M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec\r
179  *   M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2  also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate\r
180 +*   M206 - set home offsets.  This sets the X,Y,Z coordinates of the endstops (and is added to the {X,Y,Z}_HOME_POS configuration options (and is also added to the coordinates, if any, provided to G82, as with earlier firmware)\r
181  *   M220 - set build speed mulitplying S:factor in percent ; aka "realtime tuneing in the gcode". So you can slow down if you have islands in one height-range, and speed up otherwise.\r
182  *   M221 - set the extrude multiplying S:factor in percent\r
183  *   M400 - Finish all buffered moves.