grblHAL core  20240704
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-2024 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 #if defined(__MSP430F5529__) || defined(__MSP432P401R__) || defined(__MSP432E401Y__) || defined(PART_TM4C123GH6PM) || defined(PART_TM4C1294NCPDT)
68 #define isnanf(x) __isnanf(x)
69 #define isinff(x) __isinff(x)
70 #endif
71 
72 // Axis array index values. Must start with 0 and be continuous.
73 #define X_AXIS 0 // Axis indexing value.
74 #define Y_AXIS 1
75 #define Z_AXIS 2
76 #define X_AXIS_BIT bit(X_AXIS)
77 #define Y_AXIS_BIT bit(Y_AXIS)
78 #define Z_AXIS_BIT bit(Z_AXIS)
79 #if N_AXIS > 3
80 #define A_AXIS 3
81 #define A_AXIS_BIT bit(A_AXIS)
82 #endif
83 #if N_AXIS > 4
84 #define B_AXIS 4
85 #define B_AXIS_BIT bit(B_AXIS)
86 #endif
87 #if N_AXIS > 5
88 #define C_AXIS 5
89 #define C_AXIS_BIT bit(C_AXIS)
90 #endif
91 #if N_AXIS > 6
92 #define U_AXIS 6
93 #define U_AXIS_BIT bit(U_AXIS)
94 #endif
95 #if N_AXIS == 8
96 #define V_AXIS 7
97 #define V_AXIS_BIT bit(V_AXIS)
98 #endif
99 
100 #if N_AXIS == 3
101 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT)
102 #elif N_AXIS == 4
103 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT)
104 #elif N_AXIS == 5
105 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT)
106 #elif N_AXIS == 6
107 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT|C_AXIS_BIT)
108 #elif N_AXIS == 7
109 #define AXES_BITMASK (X_AXIS_BIT|Y_AXIS_BIT|Z_AXIS_BIT|A_AXIS_BIT|B_AXIS_BIT|C_AXIS_BIT|U_AXIS_BIT)
110 #else
111 #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)
112 #endif
113 
114 #ifdef V_AXIS
115 #define N_ABC_AXIS 5
116 #elif defined(U_AXIS)
117 #define N_ABC_AXIS 4
118 #elif defined(C_AXIS)
119 #define N_ABC_AXIS 3
120 #elif defined(B_AXIS)
121 #define N_ABC_AXIS 2
122 #elif defined(A_AXIS)
123 #define N_ABC_AXIS 1
124 #else
125 #define N_ABC_AXIS 0
126 #endif
127 
128 extern char const *const axis_letter[];
129 
130 typedef union {
131  uint8_t mask;
132  uint8_t value;
133  struct {
134  uint8_t x :1,
135  y :1,
136  z :1,
137  a :1,
138  b :1,
139  c :1,
140  u :1,
141  v :1;
142  };
144 
145 typedef union {
146  float values[2];
147  struct {
148  float x;
149  float y;
150  };
151 } point_2d_t;
152 
153 #pragma pack(push, 1)
154 
156 typedef struct {
162 
164 typedef struct {
168 
169 #pragma pack(pop)
170 
171 typedef enum {
175 
176 // Conversions
177 #define MM_PER_INCH (25.40f)
178 #define INCH_PER_MM (0.0393701f)
179 
180 #define MAX_INT_DIGITS 9 // Maximum number of digits in int32 (and float)
181 #define STRLEN_COORDVALUE (MAX_INT_DIGITS + N_DECIMAL_COORDVALUE_INCH + 1) // 8.4 format - excluding terminating null
182 
183 // Useful macros
184 #ifndef max
185 #define max(a,b) (((a) > (b)) ? (a) : (b))
186 #endif
187 #ifndef min
188 #define min(a,b) (((a) < (b)) ? (a) : (b))
189 #endif
190 #ifndef constrain
191 #define constrain(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
192 #endif
193 #define clear_vector(a) memset(a, 0, sizeof(a))
194 #define isequal_position_vector(a, b) !memcmp(a, b, sizeof(coord_data_t))
195 #define is0_position_vector(a) !memcmp(a, &((coord_data_t){0}), sizeof(coord_data_t))
196 
197 // Bit field and masking macros
198 #ifndef bit
199 #define bit(n) (1UL << (n))
200 #endif
201 #define bit_true(x, mask) (x) |= (mask)
202 #define bit_false(x, mask) (x) &= ~(mask)
203 #define BIT_SET(x, bit, v) { if (v) { x |= (bit); } else { x &= ~(bit); } }
204 
205 #define bit_istrue(x, mask) ((x & (mask)) != 0)
206 #define bit_isfalse(x, mask) ((x & (mask)) == 0)
207 
208 // Converts an uint32 variable to string.
209 char *uitoa (uint32_t n);
210 
211 // Converts a float variable to string with the specified number of decimal places.
212 char *ftoa (float n, uint8_t decimal_places);
213 
214 // Trim trailing zeros and possibly decimal point
215 char *trim_float (char *s);
216 
217 // Returns true if float value is a whole number (integer)
218 bool isintf (float value);
219 
220 status_code_t read_uint (char *line, uint_fast8_t *char_counter, uint32_t *uint_ptr);
221 
222 // Read a floating point value from a string. Line points to the input buffer, char_counter
223 // is the indexer pointing to the current character of the line, while float_ptr is
224 // a pointer to the result variable. Returns true when it succeeds
225 bool read_float (char *line, uint_fast8_t *char_counter, float *float_ptr);
226 
227 // Non-blocking delay function used for general operation and suspend features.
228 bool delay_sec (float seconds, delaymode_t mode);
229 
230 float convert_delta_vector_to_unit_vector(float *vector);
231 
232 // parse ISO8601 datetime
233 struct tm *get_datetime (const char *s);
234 
235 // calculate checksum byte for data
236 uint8_t calc_checksum (uint8_t *data, uint32_t size);
237 
238 char *strcaps (char *s);
239 
240 void dummy_handler (void);
241 
242 #endif
float convert_delta_vector_to_unit_vector(float *vector)
Definition: nuts_bolts.c:332
struct tm * get_datetime(const char *s)
Definition: nuts_bolts.c:354
char * strcaps(char *s)
Definition: nuts_bolts.c:428
bool isintf(float value)
Definition: nuts_bolts.c:307
bool read_float(char *line, uint_fast8_t *char_counter, float *float_ptr)
Definition: nuts_bolts.c:242
char const *const axis_letter[]
Definition: nuts_bolts.c:62
uint8_t calc_checksum(uint8_t *data, uint32_t size)
Definition: nuts_bolts.c:415
status_code_t read_uint(char *line, uint_fast8_t *char_counter, uint32_t *uint_ptr)
Definition: nuts_bolts.c:184
char * ftoa(float n, uint8_t decimal_places)
Definition: nuts_bolts.c:115
char * uitoa(uint32_t n)
Definition: nuts_bolts.c:96
delaymode_t
Definition: nuts_bolts.h:171
@ DelayMode_SysSuspend
Definition: nuts_bolts.h:173
@ DelayMode_Dwell
Definition: nuts_bolts.h:172
bool delay_sec(float seconds, delaymode_t mode)
Definition: nuts_bolts.c:313
char * trim_float(char *s)
Definition: nuts_bolts.c:170
void dummy_handler(void)
Definition: nuts_bolts.c:444
Home switches struct, consists of two packed axes_signals_t structs.
Definition: nuts_bolts.h:164
axes_signals_t a
Primary home switches status, optional. Limit signals are used for homing if not available.
Definition: nuts_bolts.h:165
axes_signals_t b
Secondary home switch(es) status, required for auto squaring enabled axes if primary switches are ava...
Definition: nuts_bolts.h:166
Limit switches struct, consists of four packed axes_signals_t structs in 32 bits.
Definition: nuts_bolts.h:156
axes_signals_t min
Min limit switches status, required.
Definition: nuts_bolts.h:157
axes_signals_t min2
Secondary min limit switch(es) status, required for auto squaring enabled axes.
Definition: nuts_bolts.h:159
axes_signals_t max2
Secondary max limit switches status, optional (of no practical use?).
Definition: nuts_bolts.h:160
axes_signals_t max
Max limit switches status, optional.
Definition: nuts_bolts.h:158
Definition: vfs.h:47
Definition: nuts_bolts.h:130
uint8_t u
Definition: nuts_bolts.h:140
uint8_t x
Definition: nuts_bolts.h:134
uint8_t y
Definition: nuts_bolts.h:135
uint8_t c
Definition: nuts_bolts.h:139
uint8_t b
Definition: nuts_bolts.h:138
uint8_t v
Definition: nuts_bolts.h:141
uint8_t value
Definition: nuts_bolts.h:132
uint8_t mask
Definition: nuts_bolts.h:131
uint8_t z
Definition: nuts_bolts.h:136
uint8_t a
Definition: nuts_bolts.h:137
Definition: nuts_bolts.h:145
float y
Definition: nuts_bolts.h:149
float x
Definition: nuts_bolts.h:148