grblHAL core  20251016
nuts_bolts.h
Go to the documentation of this file.
1 /*
2  nuts_bolts.h - Header file for shared definitions, variables, and functions
3 
4  Part of grblHAL
5 
6  Copyright (c) 2017-2025 Terje Io
7  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
8  Copyright (c) 2009-2011 Simen Svale Skogsrud
9 
10  grblHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  grblHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #ifndef _NUTS_BOLTS_H_
25 #define _NUTS_BOLTS_H_
26 
27 #include "grbl.h"
28 #include "errors.h"
29 
30 #ifndef true
31 #define false 0
32 #define true 1
33 #endif
34 
35 #define Off 0
36 #define On 1
37 
38 #define SOME_LARGE_VALUE 1.0E+38f
39 #ifndef M_PI
40 #define M_PI 3.14159265358979323846f
41 #endif
42 
43 #define TOLERANCE_EQUAL 0.0001f
44 
45 #define RADDEG 0.01745329251994329577f // Radians per degree
46 #define DEGRAD 57.29577951308232087680f // Degrees per radians
47 #define SQRT3 1.73205080756887729353f
48 #define SIN120 0.86602540378443864676f
49 #define COS120 -0.5f
50 #define TAN60 1.73205080756887729353f
51 #define SIN30 0.5f
52 #define TAN30 0.57735026918962576451f
53 #define TAN30_2 0.28867513459481288225f
54 
55 #define ABORTED (sys.abort || sys.cancel)
56 
57 // Convert character to uppercase
58 #define CAPS(c) ((c >= 'a' && c <= 'z') ? (c & 0x5F) : c)
59 #define LCAPS(c) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
60 
61 #if !(defined(STM32F103xB) || defined(STM32F303xC))
62 #ifndef UNUSED
63 #define UNUSED(x) (void)(x)
64 #endif
65 #endif
66 
67 // Axis array index values. Must start with 0 and be continuous.
68 #define X_AXIS 0 // Axis indexing value.
69 #define Y_AXIS 1
70 #define Z_AXIS 2
71 #define X_AXIS_BIT bit(X_AXIS)
72 #define Y_AXIS_BIT bit(Y_AXIS)
73 #define Z_AXIS_BIT bit(Z_AXIS)
74 #if N_AXIS > 3
75 #define A_AXIS 3
76 #define A_AXIS_BIT bit(A_AXIS)
77 #endif
78 #if N_AXIS > 4
79 #define B_AXIS 4
80 #define B_AXIS_BIT bit(B_AXIS)
81 #endif
82 #if N_AXIS > 5
83 #define C_AXIS 5
84 #define C_AXIS_BIT bit(C_AXIS)
85 #endif
86 #if N_AXIS > 6
87 #define U_AXIS 6
88 #define U_AXIS_BIT bit(U_AXIS)
89 #endif
90 #if N_AXIS == 8
91 #define V_AXIS 7
92 #define V_AXIS_BIT bit(V_AXIS)
93 #endif
94 
95 #if N_AXIS == 3
96 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT)
97 #elif N_AXIS == 4
98 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT)
99 #elif N_AXIS == 5
100 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT)
101 #elif N_AXIS == 6
102 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT|C_AXIS_BIT)
103 #elif N_AXIS == 7
104 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT|C_AXIS_BIT|U_AXIS_BIT)
105 #else
106 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT|C_AXIS_BIT|U_AXIS_BIT|V_AXIS_BIT)
107 #endif
108 
109 #ifdef V_AXIS
110 #define N_ABC_AXIS 5
111 #elif defined(U_AXIS)
112 #define N_ABC_AXIS 4
113 #elif defined(C_AXIS)
114 #define N_ABC_AXIS 3
115 #elif defined(B_AXIS)
116 #define N_ABC_AXIS 2
117 #elif defined(A_AXIS)
118 #define N_ABC_AXIS 1
119 #else
120 #define N_ABC_AXIS 0
121 #endif
122 
123 typedef union {
124  uint8_t mask;
125  uint8_t bits;
126  uint8_t value;
127  struct {
128  uint8_t x :1,
129  y :1,
130  z :1,
131  a :1,
132  b :1,
133  c :1,
134  u :1,
135  v :1;
136  };
138 
140 typedef union {
141  float values[N_AXIS];
142  struct {
143  float x;
144  float y;
145  float z;
146 #ifdef A_AXIS
147  float a;
148 #endif
149 #ifdef B_AXIS
150  float b;
151 #endif
152 #ifdef C_AXIS
153  float c;
154 #endif
155 #ifdef U_AXIS
156  float u;
157 #endif
158 #ifdef V_AXIS
159  float v;
160 #endif
161  };
162  struct {
163  float m0;
164  float m1;
165  float m2;
166 #if N_AXIS > 3
167  float m3;
168 #endif
169 #if N_AXIS > 4
170  float m4;
171 #endif
172 #if N_AXIS > 5
173  float m5;
174 #endif
175 #if N_AXIS > 6
176  float m6;
177 #endif
178 #if N_AXIS == 8
179  float m7;
180 #endif
181  };
182 } coord_data_t;
183 
184 typedef union {
185  int32_t value[N_AXIS];
186  struct {
187  int32_t x;
188  int32_t y;
189  int32_t z;
190 #ifdef A_AXIS
191  int32_t a;
192 #endif
193 #ifdef B_AXIS
194  int32_t b;
195 #endif
196 #ifdef C_AXIS
197  int32_t c;
198 #endif
199 #ifdef U_AXIS
200  int32_t u;
201 #endif
202 #ifdef V_AXIS
203  int32_t v;
204 #endif
205  };
206 } mpos_t;
207 
208 typedef union {
209  float values[2];
210  struct {
211  float x;
212  float y;
213  };
214 } point_2d_t;
215 
216 #pragma pack(push, 1)
217 
219 typedef union {
220  uint32_t bits;
221  struct {
226  };
228 
230 typedef struct {
234 
236 typedef union {
237  uint16_t state;
238  home_signals_t details; // Stepper driver signals states.
240 
242 typedef struct {
246 
247 #pragma pack(pop)
248 
249 // NOTE: the pin_function_t enum must be kept in sync with any changes!
250 typedef union {
251  uint16_t bits;
252  uint16_t mask;
253  uint16_t value;
254  struct {
255  uint16_t reset :1,
261  e_stop :1,
271  };
273 
274 typedef enum {
278 
279 // Conversions
280 #define MM_PER_INCH (25.40f)
281 #define INCH_PER_MM (0.0393701f)
282 
283 #define MAX_INT_DIGITS 9 // Maximum number of digits in int32 (and float)
284 #define STRLEN_COORDVALUE (MAX_INT_DIGITS + N_DECIMAL_COORDVALUE_INCH + 1) // 8.4 format - excluding terminating null
285 
286 // Useful macros
287 #ifndef max
288 #define max(a,b) (((a) > (b)) ? (a) : (b))
289 #endif
290 #ifndef min
291 #define min(a,b) (((a) < (b)) ? (a) : (b))
292 #endif
293 #ifndef constrain
294 #define constrain(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
295 #endif
296 #define clear_vector(a) memset(a, 0, sizeof(a))
297 #define isequal_position_vector(a, b) !memcmp(a, b, sizeof(coord_data_t))
298 #define is0_position_vector(a) !memcmp(a, &((coord_data_t){0}), sizeof(coord_data_t))
299 
300 // Bit field and masking macros
301 #ifndef bit
302 #define bit(n) (1UL << (n))
303 #endif
304 #define bit_true(x, mask) (x) |= (mask)
305 #define bit_false(x, mask) (x) &= ~(mask)
306 #define BIT_SET(x, bit, v) { if (v) { x |= (bit); } else { x &= ~(bit); } }
307 
308 #define bit_istrue(x, mask) (((x) & (mask)) != 0)
309 #define bit_isfalse(x, mask) (((x) & (mask)) == 0)
310 
311 extern char const *const axis_letter[];
312 extern const coord_data_t null_vector;
313 
314 // Converts an uint32 variable to string.
315 char *uitoa (uint32_t n);
316 
317 // Converts a float variable to string with the specified number of decimal places.
318 char *ftoa (float n, uint8_t decimal_places);
319 
320 // Trim trailing zeros and possibly decimal point
321 char *trim_float (char *s);
322 
323 // Returns true if float value is a whole number (integer)
324 bool isintf (float value);
325 
326 status_code_t read_uint (const char *line, uint_fast8_t *char_counter, uint32_t *uint_ptr);
327 
328 // Read a floating point value from a string. Line points to the input buffer, char_counter
329 // is the indexer pointing to the current character of the line, while float_ptr is
330 // a pointer to the result variable. Returns true when it succeeds
331 bool read_float (const char *line, uint_fast8_t *char_counter, float *float_ptr);
332 
333 // Non-blocking delay function used for general operation and suspend features.
334 bool delay_sec (float seconds, delaymode_t mode);
335 
336 float convert_delta_vector_to_unit_vector(float *vector);
337 
338 // parse ISO8601 datetime
339 struct tm *get_datetime (const char *s);
340 
341 char *strcaps (char *s);
342 
343 uint_fast8_t bit_count (uint32_t bits);
344 
345 void dummy_handler (void);
346 
347 #ifdef _WIN32
348 
349 static int ffs (int i)
350 {
351  int idx = 0;
352 
353  while(i) {
354  idx++;
355  if(i & 1)
356  break;
357  i >>= 1;
358  }
359 
360  return idx;
361 }
362 
363 #endif // _WIN32
364 
365 #endif
#define N_AXIS
Defines number of axes supported - minimum 3, maximum 8. If more than 3 axes are configured a complia...
Definition: config.h:42
status_code_t
Definition: errors.h:30
float convert_delta_vector_to_unit_vector(float *vector)
Definition: nuts_bolts.c:334
struct tm * get_datetime(const char *s)
Definition: nuts_bolts.c:356
char * strcaps(char *s)
Definition: nuts_bolts.c:417
bool isintf(float value)
Definition: nuts_bolts.c:309
char const *const axis_letter[]
Definition: nuts_bolts.c:64
char * ftoa(float n, uint8_t decimal_places)
Definition: nuts_bolts.c:117
const coord_data_t null_vector
Definition: nuts_bolts.c:62
uint_fast8_t bit_count(uint32_t bits)
Definition: nuts_bolts.c:433
char * uitoa(uint32_t n)
Definition: nuts_bolts.c:98
delaymode_t
Definition: nuts_bolts.h:274
@ DelayMode_SysSuspend
Definition: nuts_bolts.h:276
@ DelayMode_Dwell
Definition: nuts_bolts.h:275
bool delay_sec(float seconds, delaymode_t mode)
Definition: nuts_bolts.c:315
bool read_float(const char *line, uint_fast8_t *char_counter, float *float_ptr)
Definition: nuts_bolts.c:244
char * trim_float(char *s)
Definition: nuts_bolts.c:172
status_code_t read_uint(const char *line, uint_fast8_t *char_counter, uint32_t *uint_ptr)
Definition: nuts_bolts.c:186
void dummy_handler(void)
Definition: nuts_bolts.c:445
Home switches struct, consists of two packed axes_signals_t structs.
Definition: nuts_bolts.h:230
axes_signals_t a
Primary home switches status, optional. Limit signals are used for homing if not available.
Definition: nuts_bolts.h:231
axes_signals_t b
Secondary home switch(es) status, required for auto squaring enabled axes if primary switches are ava...
Definition: nuts_bolts.h:232
// Stepper driver warning and fault signal states, consists of two packed stepper_state_t structs in ...
Definition: nuts_bolts.h:242
stepper_state_t warning
Stepper drivers warning states.
Definition: nuts_bolts.h:243
stepper_state_t fault
Stepper drivers fault states.
Definition: nuts_bolts.h:244
Definition: vfs.h:48
Definition: nuts_bolts.h:123
uint8_t u
Definition: nuts_bolts.h:134
uint8_t x
Definition: nuts_bolts.h:128
uint8_t y
Definition: nuts_bolts.h:129
uint8_t c
Definition: nuts_bolts.h:133
uint8_t b
Definition: nuts_bolts.h:132
uint8_t v
Definition: nuts_bolts.h:135
uint8_t bits
Definition: nuts_bolts.h:125
uint8_t value
Definition: nuts_bolts.h:126
uint8_t mask
Definition: nuts_bolts.h:124
uint8_t z
Definition: nuts_bolts.h:130
uint8_t a
Definition: nuts_bolts.h:131
Definition: nuts_bolts.h:250
uint16_t probe_overtravel
used for probe (toolsetter) protection
Definition: nuts_bolts.h:268
uint16_t bits
Definition: nuts_bolts.h:251
uint16_t stop_disable
Definition: nuts_bolts.h:260
uint16_t limits_override
Definition: nuts_bolts.h:265
uint16_t cycle_start
Definition: nuts_bolts.h:257
uint16_t feed_hold
Definition: nuts_bolts.h:256
uint16_t probe_disconnected
Definition: nuts_bolts.h:262
uint16_t reset
Definition: nuts_bolts.h:255
uint16_t safety_door_ajar
Definition: nuts_bolts.h:258
uint16_t motor_fault
Definition: nuts_bolts.h:263
uint16_t motor_warning
Definition: nuts_bolts.h:264
uint16_t deasserted
used for probe protection
Definition: nuts_bolts.h:270
uint16_t mask
Definition: nuts_bolts.h:252
uint16_t e_stop
M1.
Definition: nuts_bolts.h:261
uint16_t block_delete
Definition: nuts_bolts.h:259
uint16_t value
Definition: nuts_bolts.h:253
uint16_t probe_triggered
used for probe protection
Definition: nuts_bolts.h:269
uint16_t tls_overtravel
Definition: nuts_bolts.h:267
uint16_t single_block
Definition: nuts_bolts.h:266
Coordinate data.
Definition: nuts_bolts.h:140
float m0
Definition: nuts_bolts.h:163
float a
Definition: nuts_bolts.h:147
float m2
Definition: nuts_bolts.h:165
float y
Definition: nuts_bolts.h:144
float m1
Definition: nuts_bolts.h:164
float x
Definition: nuts_bolts.h:143
float z
Definition: nuts_bolts.h:145
float m3
Definition: nuts_bolts.h:167
Limit switches struct, consists of four packed axes_signals_t structs in 32 bits.
Definition: nuts_bolts.h:219
axes_signals_t min
Min limit switches status, required.
Definition: nuts_bolts.h:222
axes_signals_t min2
Secondary min limit switch(es) status, required for auto squaring enabled axes.
Definition: nuts_bolts.h:224
axes_signals_t max2
Secondary max limit switches status, optional (of no practical use?).
Definition: nuts_bolts.h:225
axes_signals_t max
Max limit switches status, optional.
Definition: nuts_bolts.h:223
uint32_t bits
Definition: nuts_bolts.h:220
Definition: nuts_bolts.h:184
int32_t a
Definition: nuts_bolts.h:191
int32_t z
Definition: nuts_bolts.h:189
int32_t y
Definition: nuts_bolts.h:188
int32_t x
Definition: nuts_bolts.h:187
Definition: nuts_bolts.h:208
float y
Definition: nuts_bolts.h:212
float x
Definition: nuts_bolts.h:211
Stepper driver states struct.
Definition: nuts_bolts.h:236
home_signals_t details
Definition: nuts_bolts.h:238
uint16_t state
Definition: nuts_bolts.h:237