├── coolant_control.h ├── eeprom.h ├── examples └── grblUpload │ ├── license.txt │ └── grblUpload.ino ├── spindle_control.h ├── limits.h ├── probe.h ├── coolant_control.c ├── cpu_map.h ├── grbl.h ├── stepper.h ├── print.h ├── serial.h ├── protocol.h ├── README.md ├── probe.c ├── motion_control.h ├── defaults ├── defaults_generic.h ├── defaults_oxcnc.h ├── defaults_simulator.h ├── defaults_shapeoko3.h ├── defaults_sherline.h ├── defaults_shapeoko2.h ├── defaults_x_carve_1000mm.h ├── defaults_x_carve_500mm.h ├── defaults_zen_toolworks_7x7.h └── defaults_shapeoko.h ├── nuts_bolts.h ├── main.c ├── defaults.h ├── report.h ├── planner.h ├── nuts_bolts.c ├── spindle_control.c ├── system.h ├── cpu_map ├── cpu_map_atmega2560.h └── cpu_map_atmega328p.h ├── eeprom.c ├── settings.h ├── print.c ├── serial.c ├── gcode.h ├── settings.c ├── system.c ├── limits.c └── motion_control.c /coolant_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | coolant_control.h - spindle control methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #ifndef coolant_control_h 22 | #define coolant_control_h 23 | 24 | 25 | void coolant_init(); 26 | void coolant_stop(); 27 | void coolant_set_state(uint8_t mode); 28 | void coolant_run(uint8_t mode); 29 | 30 | #endif -------------------------------------------------------------------------------- /eeprom.h: -------------------------------------------------------------------------------- 1 | /* 2 | eeprom.h - EEPROM methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2009-2011 Simen Svale Skogsrud 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #ifndef eeprom_h 22 | #define eeprom_h 23 | 24 | unsigned char eeprom_get_char(unsigned int addr); 25 | void eeprom_put_char(unsigned int addr, unsigned char new_value); 26 | void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size); 27 | int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /examples/grblUpload/license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sungeun K. Jeon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /spindle_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | spindle_control.h - spindle control methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef spindle_control_h 23 | #define spindle_control_h 24 | 25 | 26 | // Initializes spindle pins and hardware PWM, if enabled. 27 | void spindle_init(); 28 | 29 | // Sets spindle direction and spindle rpm via PWM, if enabled. 30 | void spindle_run(uint8_t direction, float rpm); 31 | 32 | void spindle_set_state(uint8_t state, float rpm); 33 | 34 | // Kills spindle. 35 | void spindle_stop(); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /examples/grblUpload/grblUpload.ino: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | This sketch compiles and uploads Grbl to your 328p-based Arduino! 3 | 4 | To use: 5 | - First make sure you have imported Grbl source code into your Arduino 6 | IDE. There are details on our Github website on how to do this. 7 | 8 | - Select your Arduino Board and Serial Port in the Tools drop-down menu. 9 | NOTE: Grbl only officially supports 328p-based Arduinos, like the Uno. 10 | Using other boards will likely not work! 11 | 12 | - Then just click 'Upload'. That's it! 13 | 14 | For advanced users: 15 | If you'd like to see what else Grbl can do, there are some additional 16 | options for customization and features you can enable or disable. 17 | Navigate your file system to where the Arduino IDE has stored the Grbl 18 | source code files, open the 'config.h' file in your favorite text 19 | editor. Inside are dozens of feature descriptions and #defines. Simply 20 | comment or uncomment the #defines or alter their assigned values, save 21 | your changes, and then click 'Upload' here. 22 | 23 | Copyright (c) 2015 Sungeun K. Jeon 24 | Released under the MIT-license. See license.txt for details. 25 | ***********************************************************************/ 26 | 27 | #include 28 | 29 | // Do not alter this file! 30 | -------------------------------------------------------------------------------- /limits.h: -------------------------------------------------------------------------------- 1 | /* 2 | limits.h - code pertaining to limit-switches and performing the homing cycle 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef limits_h 23 | #define limits_h 24 | 25 | 26 | // Initialize the limits module 27 | void limits_init(); 28 | 29 | // Disables hard limits. 30 | void limits_disable(); 31 | 32 | // Returns limit state as a bit-wise uint8 variable. 33 | uint8_t limits_get_state(); 34 | 35 | // Perform one portion of the homing cycle based on the input settings. 36 | void limits_go_home(uint8_t cycle_mask); 37 | 38 | // Check for soft limit violations 39 | void limits_soft_check(float *target); 40 | 41 | #endif -------------------------------------------------------------------------------- /probe.h: -------------------------------------------------------------------------------- 1 | /* 2 | probe.h - code pertaining to probing methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2014-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #ifndef probe_h 22 | #define probe_h 23 | 24 | // Values that define the probing state machine. 25 | #define PROBE_OFF 0 // Probing disabled or not in use. (Must be zero.) 26 | #define PROBE_ACTIVE 1 // Actively watching the input pin. 27 | 28 | // Probe pin initialization routine. 29 | void probe_init(); 30 | 31 | // Called by probe_init() and the mc_probe() routines. Sets up the probe pin invert mask to 32 | // appropriately set the pin logic according to setting for normal-high/normal-low operation 33 | // and the probing cycle modes for toward-workpiece/away-from-workpiece. 34 | void probe_configure_invert_mask(uint8_t is_probe_away); 35 | 36 | // Returns probe pin state. Triggered = true. Called by gcode parser and probe state monitor. 37 | uint8_t probe_get_state(); 38 | 39 | // Monitors probe pin state and records the system position when detected. Called by the 40 | // stepper ISR per ISR tick. 41 | void probe_state_monitor(); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /coolant_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | coolant_control.c - coolant control methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #include "grbl.h" 22 | 23 | 24 | void coolant_init() 25 | { 26 | COOLANT_FLOOD_DDR |= (1 << COOLANT_FLOOD_BIT); 27 | #ifdef ENABLE_M7 28 | COOLANT_MIST_DDR |= (1 << COOLANT_MIST_BIT); 29 | #endif 30 | coolant_stop(); 31 | } 32 | 33 | 34 | void coolant_stop() 35 | { 36 | COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT); 37 | #ifdef ENABLE_M7 38 | COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT); 39 | #endif 40 | } 41 | 42 | 43 | void coolant_set_state(uint8_t mode) 44 | { 45 | if (mode == COOLANT_FLOOD_ENABLE) { 46 | COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT); 47 | 48 | #ifdef ENABLE_M7 49 | } else if (mode == COOLANT_MIST_ENABLE) { 50 | COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT); 51 | #endif 52 | 53 | } else { 54 | coolant_stop(); 55 | } 56 | } 57 | 58 | 59 | void coolant_run(uint8_t mode) 60 | { 61 | if (sys.state == STATE_CHECK_MODE) { return; } 62 | protocol_buffer_synchronize(); // Ensure coolant turns on when specified in program. 63 | coolant_set_state(mode); 64 | } 65 | -------------------------------------------------------------------------------- /cpu_map.h: -------------------------------------------------------------------------------- 1 | /* 2 | cpu_map.h - CPU and pin mapping configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* The cpu_map.h files serve as a central pin mapping selection file for different processor 22 | types, i.e. AVR 328p or AVR Mega 2560. Each processor has its own pin mapping file. 23 | (i.e. cpu_map_atmega328p.h) Grbl officially supports the Arduino Uno, but the 24 | other supplied pin mappings are supplied by users, so your results may vary. */ 25 | 26 | // NOTE: With new processors, only add the define name and filename to use. 27 | 28 | #ifndef cpu_map_h 29 | #define cpu_map_h 30 | 31 | 32 | #ifdef CPU_MAP_ATMEGA328P // (Arduino Uno) Officially supported by Grbl. 33 | #include "cpu_map/cpu_map_atmega328p.h" 34 | #endif 35 | 36 | #ifdef CPU_MAP_ATMEGA2560 // (Arduino Mega 2560) Working @EliteEng 37 | #include "cpu_map/cpu_map_atmega2560.h" 38 | #endif 39 | 40 | /* 41 | #ifdef CPU_MAP_CUSTOM_PROC 42 | // For a custom pin map or different processor, copy and edit one of the available cpu 43 | // map files and modify it to your needs. Make sure the defined name is also changed in 44 | // the config.h file. 45 | #endif 46 | */ 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /grbl.h: -------------------------------------------------------------------------------- 1 | /* 2 | grbl.h - main Grbl include file 3 | Part of Grbl 4 | 5 | Copyright (c) 2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #ifndef grbl_h 22 | #define grbl_h 23 | 24 | // Grbl versioning system 25 | #define GRBL_VERSION "0.9i" 26 | #define GRBL_VERSION_BUILD "20150620" 27 | 28 | // Define standard libraries used by Grbl. 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | // Define the Grbl system include files. NOTE: Do not alter organization. 42 | #include "config.h" 43 | #include "nuts_bolts.h" 44 | #include "settings.h" 45 | #include "system.h" 46 | #include "defaults.h" 47 | #include "cpu_map.h" 48 | #include "coolant_control.h" 49 | #include "eeprom.h" 50 | #include "gcode.h" 51 | #include "limits.h" 52 | #include "motion_control.h" 53 | #include "planner.h" 54 | #include "print.h" 55 | #include "probe.h" 56 | #include "protocol.h" 57 | #include "report.h" 58 | #include "serial.h" 59 | #include "spindle_control.h" 60 | #include "stepper.h" 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /stepper.h: -------------------------------------------------------------------------------- 1 | /* 2 | stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef stepper_h 23 | #define stepper_h 24 | 25 | #ifndef SEGMENT_BUFFER_SIZE 26 | #define SEGMENT_BUFFER_SIZE 6 27 | #endif 28 | 29 | // Initialize and setup the stepper motor subsystem 30 | void stepper_init(); 31 | 32 | // Enable steppers, but cycle does not start unless called by motion control or realtime command. 33 | void st_wake_up(); 34 | 35 | // Immediately disables steppers 36 | void st_go_idle(); 37 | 38 | // Generate the step and direction port invert masks. 39 | void st_generate_step_dir_invert_masks(); 40 | 41 | // Reset the stepper subsystem variables 42 | void st_reset(); 43 | 44 | // Reloads step segment buffer. Called continuously by realtime execution system. 45 | void st_prep_buffer(); 46 | 47 | // Called by planner_recalculate() when the executing block is updated by the new plan. 48 | void st_update_plan_block_parameters(); 49 | 50 | // Called by realtime status reporting if realtime rate reporting is enabled in config.h. 51 | #ifdef REPORT_REALTIME_RATE 52 | float st_get_realtime_rate(); 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /print.h: -------------------------------------------------------------------------------- 1 | /* 2 | print.h - Functions for formatting output strings 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef print_h 23 | #define print_h 24 | 25 | 26 | void printString(const char *s); 27 | 28 | void printPgmString(const char *s); 29 | 30 | void printInteger(long n); 31 | 32 | void print_uint32_base10(uint32_t n); 33 | 34 | // Prints uint8 variable with base and number of desired digits. 35 | void print_unsigned_int8(uint8_t n, uint8_t base, uint8_t digits); 36 | 37 | // Prints an uint8 variable in base 2. 38 | void print_uint8_base2(uint8_t n); 39 | 40 | // Prints an uint8 variable in base 10. 41 | void print_uint8_base10(uint8_t n); 42 | 43 | void printFloat(float n, uint8_t decimal_places); 44 | 45 | // Floating value printing handlers for special variables types used in Grbl. 46 | // - CoordValue: Handles all position or coordinate values in inches or mm reporting. 47 | // - RateValue: Handles feed rate and current velocity in inches or mm reporting. 48 | // - SettingValue: Handles all floating point settings values (always in mm.) 49 | void printFloat_CoordValue(float n); 50 | 51 | void printFloat_RateValue(float n); 52 | 53 | void printFloat_SettingValue(float n); 54 | 55 | // Debug tool to print free memory in bytes at the called point. Not used otherwise. 56 | void printFreeMemory(); 57 | 58 | #endif -------------------------------------------------------------------------------- /serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | serial.c - Low level functions for sending and recieving bytes via the serial port 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef serial_h 23 | #define serial_h 24 | 25 | 26 | #ifndef RX_BUFFER_SIZE 27 | #define RX_BUFFER_SIZE 128 28 | #endif 29 | #ifndef TX_BUFFER_SIZE 30 | #define TX_BUFFER_SIZE 64 31 | #endif 32 | 33 | #define SERIAL_NO_DATA 0xff 34 | 35 | #ifdef ENABLE_XONXOFF 36 | #define RX_BUFFER_FULL 96 // XOFF high watermark 37 | #define RX_BUFFER_LOW 64 // XON low watermark 38 | #define SEND_XOFF 1 39 | #define SEND_XON 2 40 | #define XOFF_SENT 3 41 | #define XON_SENT 4 42 | #define XOFF_CHAR 0x13 43 | #define XON_CHAR 0x11 44 | #endif 45 | 46 | void serial_init(); 47 | 48 | // Writes one byte to the TX serial buffer. Called by main program. 49 | void serial_write(uint8_t data); 50 | 51 | // Fetches the first byte in the serial read buffer. Called by main program. 52 | uint8_t serial_read(); 53 | 54 | // Reset and empty data in read buffer. Used by e-stop and reset. 55 | void serial_reset_read_buffer(); 56 | 57 | // Returns the number of bytes used in the RX serial buffer. 58 | uint8_t serial_get_rx_buffer_count(); 59 | 60 | // Returns the number of bytes used in the TX serial buffer. 61 | // NOTE: Not used except for debugging and ensuring no TX bottlenecks. 62 | uint8_t serial_get_tx_buffer_count(); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /protocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | protocol.h - controls Grbl execution protocol and procedures 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef protocol_h 23 | #define protocol_h 24 | 25 | // Line buffer size from the serial input stream to be executed. 26 | // NOTE: Not a problem except for extreme cases, but the line buffer size can be too small 27 | // and g-code blocks can get truncated. Officially, the g-code standards support up to 256 28 | // characters. In future versions, this will be increased, when we know how much extra 29 | // memory space we can invest into here or we re-write the g-code parser not to have this 30 | // buffer. 31 | #ifndef LINE_BUFFER_SIZE 32 | #define LINE_BUFFER_SIZE 80 33 | #endif 34 | 35 | // Starts Grbl main loop. It handles all incoming characters from the serial port and executes 36 | // them as they complete. It is also responsible for finishing the initialization procedures. 37 | void protocol_main_loop(); 38 | 39 | // Checks and executes a realtime command at various stop points in main program 40 | void protocol_execute_realtime(); 41 | 42 | // Notify the stepper subsystem to start executing the g-code program in buffer. 43 | // void protocol_cycle_start(); 44 | 45 | // Reinitializes the buffer after a feed hold for a resume. 46 | // void protocol_cycle_reinitialize(); 47 | 48 | // Initiates a feed hold of the running program 49 | // void protocol_feed_hold(); 50 | 51 | // Executes the auto cycle feature, if enabled. 52 | void protocol_auto_cycle_start(); 53 | 54 | // Block until all buffered steps are executed 55 | void protocol_buffer_synchronize(); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grbl-servo 2 | grbl 0.9i with Servo motor support 3 | 4 | GRBL 0.9i with servo motor support. 5 | Use the PIN D11 to drive the servo. 6 | Use the commands M03 Sxxx (xxx between 0 and 255) to rotate the servo between 0-180. 7 | The command M05 turn the servo to zero degrees. 8 | 9 | you can change the pulse duration in the file spindle_control.c: 10 | 11 | define RC_SERVO_SHORT 15 // Timer ticks for 0.6ms pulse duration (9 for 0.6ms) 12 | 13 | define RC_SERVO_LONG 32 // Timer ticks for 2.5 ms pulse duration (39 for 2.5ms) 14 | 15 | define RC_SERVO_INVERT 1 // Uncomment to invert servo direction 16 | 17 | If you want to have the servo working from 0 --> 180 degrees change RC_SERVO_SHORT and put 9, RC_SERVO_LONG and put 39 18 | If you want invert the servo direction uncomment the line above. 19 | 20 | I tested the code very well with 328p (Arduino Uno, Duemilanove etv), not with 2560 (Arduino Mega), but I think it would work well also with the Mega. 21 | 22 | ------------------------------------------------------------------- 23 | 24 | The link for GRBL vanilla is: http://github.com/grbl/grbl 25 | 26 | Grbl is a no-compromise, high performance, low cost alternative to parallel-port-based motion control for CNC milling. It will run on a vanilla Arduino (Duemillanove/Uno) as long as it sports an Atmega 328. 27 | 28 | The controller is written in highly optimized C utilizing every clever feature of the AVR-chips to achieve precise timing and asynchronous operation. It is able to maintain up to 30kHz of stable, jitter free control pulses. 29 | 30 | It accepts standards-compliant g-code and has been tested with the output of several CAM tools with no problems. Arcs, circles and helical motion are fully supported, as well as, all other primary g-code commands. Macro functions, variables, and most canned cycles are not supported, but we think GUIs can do a much better job at translating them into straight g-code anyhow. 31 | 32 | Grbl includes full acceleration management with look ahead. That means the controller will look up to 18 motions into the future and plan its velocities ahead to deliver smooth acceleration and jerk-free cornering. 33 | 34 | Licensing: Grbl is free software, released under the GPLv3 license. 35 | 36 | For more information and help, check out our Wiki pages! If you find that the information is out-dated, please to help us keep it updated by editing it or notifying our community! Thanks! 37 | 38 | Lead Developer [2011 - Current]: Sungeun(Sonny) K. Jeon, Ph.D. (USA) aka @chamnit 39 | 40 | Lead Developer [2009 - 2011]: Simen Svale Skogsrud (Norway). aka The Originator/Creator/Pioneer/Father of Grbl. 41 | 42 | The link for GRBL vanilla is: http://github.com/grbl/grbl 43 | -------------------------------------------------------------------------------- /probe.c: -------------------------------------------------------------------------------- 1 | /* 2 | probe.c - code pertaining to probing methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2014-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #include "grbl.h" 22 | 23 | 24 | // Inverts the probe pin state depending on user settings and probing cycle mode. 25 | uint8_t probe_invert_mask; 26 | 27 | 28 | // Probe pin initialization routine. 29 | void probe_init() 30 | { 31 | PROBE_DDR &= ~(PROBE_MASK); // Configure as input pins 32 | #ifdef DISABLE_PROBE_PIN_PULL_UP 33 | PROBE_PORT &= ~(PROBE_MASK); // Normal low operation. Requires external pull-down. 34 | #else 35 | PROBE_PORT |= PROBE_MASK; // Enable internal pull-up resistors. Normal high operation. 36 | #endif 37 | // probe_configure_invert_mask(false); // Initialize invert mask. Not required. Updated when in-use. 38 | } 39 | 40 | 41 | // Called by probe_init() and the mc_probe() routines. Sets up the probe pin invert mask to 42 | // appropriately set the pin logic according to setting for normal-high/normal-low operation 43 | // and the probing cycle modes for toward-workpiece/away-from-workpiece. 44 | void probe_configure_invert_mask(uint8_t is_probe_away) 45 | { 46 | probe_invert_mask = 0; // Initialize as zero. 47 | if (bit_isfalse(settings.flags,BITFLAG_INVERT_PROBE_PIN)) { probe_invert_mask ^= PROBE_MASK; } 48 | if (is_probe_away) { probe_invert_mask ^= PROBE_MASK; } 49 | } 50 | 51 | 52 | // Returns the probe pin state. Triggered = true. Called by gcode parser and probe state monitor. 53 | uint8_t probe_get_state() { return((PROBE_PIN & PROBE_MASK) ^ probe_invert_mask); } 54 | 55 | 56 | // Monitors probe pin state and records the system position when detected. Called by the 57 | // stepper ISR per ISR tick. 58 | // NOTE: This function must be extremely efficient as to not bog down the stepper ISR. 59 | void probe_state_monitor() 60 | { 61 | if (sys.probe_state == PROBE_ACTIVE) { 62 | if (probe_get_state()) { 63 | sys.probe_state = PROBE_OFF; 64 | memcpy(sys.probe_position, sys.position, sizeof(float)*N_AXIS); 65 | bit_true(sys.rt_exec_state, EXEC_MOTION_CANCEL); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /motion_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | motion_control.h - high level interface for issuing motion commands 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef motion_control_h 23 | #define motion_control_h 24 | 25 | 26 | #define HOMING_CYCLE_LINE_NUMBER -1 27 | 28 | // Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second 29 | // unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in 30 | // (1 minute)/feed_rate time. 31 | #ifdef USE_LINE_NUMBERS 32 | void mc_line(float *target, float feed_rate, uint8_t invert_feed_rate, int32_t line_number); 33 | #else 34 | void mc_line(float *target, float feed_rate, uint8_t invert_feed_rate); 35 | #endif 36 | 37 | // Execute an arc in offset mode format. position == current xyz, target == target xyz, 38 | // offset == offset from current xyz, axis_XXX defines circle plane in tool space, axis_linear is 39 | // the direction of helical travel, radius == circle radius, is_clockwise_arc boolean. Used 40 | // for vector transformation direction. 41 | #ifdef USE_LINE_NUMBERS 42 | void mc_arc(float *position, float *target, float *offset, float radius, float feed_rate, 43 | uint8_t invert_feed_rate, uint8_t axis_0, uint8_t axis_1, uint8_t axis_linear, uint8_t is_clockwise_arc, int32_t line_number); 44 | #else 45 | void mc_arc(float *position, float *target, float *offset, float radius, float feed_rate, 46 | uint8_t invert_feed_rate, uint8_t axis_0, uint8_t axis_1, uint8_t axis_linear, uint8_t is_clockwise_arc); 47 | #endif 48 | 49 | // Dwell for a specific number of seconds 50 | void mc_dwell(float seconds); 51 | 52 | // Perform homing cycle to locate machine zero. Requires limit switches. 53 | void mc_homing_cycle(); 54 | 55 | // Perform tool length probe cycle. Requires probe switch. 56 | #ifdef USE_LINE_NUMBERS 57 | void mc_probe_cycle(float *target, float feed_rate, uint8_t invert_feed_rate, uint8_t is_probe_away, 58 | uint8_t is_no_error, int32_t line_number); 59 | #else 60 | void mc_probe_cycle(float *target, float feed_rate, uint8_t invert_feed_rate, uint8_t is_probe_away, 61 | uint8_t is_no_error); 62 | #endif 63 | 64 | // Performs system reset. If in motion state, kills all motion and sets system alarm. 65 | void mc_reset(); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /defaults/defaults_generic.h: -------------------------------------------------------------------------------- 1 | /* 2 | defaults_generic.h - defaults settings configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Grbl generic default settings. Should work across different machines. 31 | #define DEFAULT_X_STEPS_PER_MM 250.0 32 | #define DEFAULT_Y_STEPS_PER_MM 250.0 33 | #define DEFAULT_Z_STEPS_PER_MM 250.0 34 | #define DEFAULT_X_MAX_RATE 500.0 // mm/min 35 | #define DEFAULT_Y_MAX_RATE 500.0 // mm/min 36 | #define DEFAULT_Z_MAX_RATE 500.0 // mm/min 37 | #define DEFAULT_X_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 38 | #define DEFAULT_Y_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 39 | #define DEFAULT_Z_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 40 | #define DEFAULT_X_MAX_TRAVEL 200.0 // mm 41 | #define DEFAULT_Y_MAX_TRAVEL 200.0 // mm 42 | #define DEFAULT_Z_MAX_TRAVEL 200.0 // mm 43 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 44 | #define DEFAULT_STEPPING_INVERT_MASK 0 45 | #define DEFAULT_DIRECTION_INVERT_MASK 0 46 | #define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled) 47 | #define DEFAULT_STATUS_REPORT_MASK ((BITFLAG_RT_STATUS_MACHINE_POSITION)|(BITFLAG_RT_STATUS_WORK_POSITION)) 48 | #define DEFAULT_JUNCTION_DEVIATION 0.01 // mm 49 | #define DEFAULT_ARC_TOLERANCE 0.002 // mm 50 | #define DEFAULT_REPORT_INCHES 0 // false 51 | #define DEFAULT_INVERT_ST_ENABLE 0 // false 52 | #define DEFAULT_INVERT_LIMIT_PINS 0 // false 53 | #define DEFAULT_SOFT_LIMIT_ENABLE 0 // false 54 | #define DEFAULT_HARD_LIMIT_ENABLE 0 // false 55 | #define DEFAULT_HOMING_ENABLE 0 // false 56 | #define DEFAULT_HOMING_DIR_MASK 0 // move positive dir 57 | #define DEFAULT_HOMING_FEED_RATE 25.0 // mm/min 58 | #define DEFAULT_HOMING_SEEK_RATE 500.0 // mm/min 59 | #define DEFAULT_HOMING_DEBOUNCE_DELAY 250 // msec (0-65k) 60 | #define DEFAULT_HOMING_PULLOFF 1.0 // mm 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /defaults/defaults_oxcnc.h: -------------------------------------------------------------------------------- 1 | /* 2 | defaults_oxcnc.h - defaults settings configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Grbl settings for OpenBuilds OX CNC Machine 31 | // http://www.openbuilds.com/builds/openbuilds-ox-cnc-machine.341/ 32 | #define DEFAULT_X_STEPS_PER_MM 26.670 33 | #define DEFAULT_Y_STEPS_PER_MM 26.670 34 | #define DEFAULT_Z_STEPS_PER_MM 50 35 | #define DEFAULT_X_MAX_RATE 500.0 // mm/min 36 | #define DEFAULT_Y_MAX_RATE 500.0 // mm/min 37 | #define DEFAULT_Z_MAX_RATE 500.0 // mm/min 38 | #define DEFAULT_X_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 39 | #define DEFAULT_Y_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 40 | #define DEFAULT_Z_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 41 | #define DEFAULT_X_MAX_TRAVEL 500.0 // mm 42 | #define DEFAULT_Y_MAX_TRAVEL 750.0 // mm 43 | #define DEFAULT_Z_MAX_TRAVEL 80.0 // mm 44 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 45 | #define DEFAULT_STEPPING_INVERT_MASK 0 46 | #define DEFAULT_DIRECTION_INVERT_MASK 0 47 | #define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled) 48 | #define DEFAULT_STATUS_REPORT_MASK ((BITFLAG_RT_STATUS_MACHINE_POSITION)|(BITFLAG_RT_STATUS_WORK_POSITION)) 49 | #define DEFAULT_JUNCTION_DEVIATION 0.02 // mm 50 | #define DEFAULT_ARC_TOLERANCE 0.002 // mm 51 | #define DEFAULT_REPORT_INCHES 0 // false 52 | #define DEFAULT_INVERT_ST_ENABLE 0 // false 53 | #define DEFAULT_INVERT_LIMIT_PINS 0 // false 54 | #define DEFAULT_SOFT_LIMIT_ENABLE 0 // false 55 | #define DEFAULT_HARD_LIMIT_ENABLE 0 // false 56 | #define DEFAULT_HOMING_ENABLE 0 // false 57 | #define DEFAULT_HOMING_DIR_MASK 0 // move positive dir 58 | #define DEFAULT_HOMING_FEED_RATE 25.0 // mm/min 59 | #define DEFAULT_HOMING_SEEK_RATE 500.0 // mm/min 60 | #define DEFAULT_HOMING_DEBOUNCE_DELAY 250 // msec (0-65k) 61 | #define DEFAULT_HOMING_PULLOFF 1.0 // mm 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /defaults/defaults_simulator.h: -------------------------------------------------------------------------------- 1 | /* 2 | defaults_simulator.h - defaults settings configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Settings only for Grbl Simulator (www.github.com/grbl/grbl-sim) 31 | // Grbl generic default settings. Should work across different machines. 32 | #define DEFAULT_X_STEPS_PER_MM 1000.0 33 | #define DEFAULT_Y_STEPS_PER_MM 1000.0 34 | #define DEFAULT_Z_STEPS_PER_MM 1000.0 35 | #define DEFAULT_X_MAX_RATE 1000.0 // mm/min 36 | #define DEFAULT_Y_MAX_RATE 1000.0 // mm/min 37 | #define DEFAULT_Z_MAX_RATE 1000.0 // mm/min 38 | #define DEFAULT_X_ACCELERATION (100.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 39 | #define DEFAULT_Y_ACCELERATION (100.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 40 | #define DEFAULT_Z_ACCELERATION (100.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 41 | #define DEFAULT_X_MAX_TRAVEL 1000.0 // mm 42 | #define DEFAULT_Y_MAX_TRAVEL 1000.0 // mm 43 | #define DEFAULT_Z_MAX_TRAVEL 1000.0 // mm 44 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 45 | #define DEFAULT_STEPPING_INVERT_MASK 0 46 | #define DEFAULT_DIRECTION_INVERT_MASK 0 47 | #define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled) 48 | #define DEFAULT_STATUS_REPORT_MASK ((BITFLAG_RT_STATUS_MACHINE_POSITION)|(BITFLAG_RT_STATUS_WORK_POSITION)) 49 | #define DEFAULT_JUNCTION_DEVIATION 0.01 // mm 50 | #define DEFAULT_ARC_TOLERANCE 0.002 // mm 51 | #define DEFAULT_REPORT_INCHES 0 // false 52 | #define DEFAULT_INVERT_ST_ENABLE 0 // false 53 | #define DEFAULT_INVERT_LIMIT_PINS 0 // false 54 | #define DEFAULT_SOFT_LIMIT_ENABLE 0 // false 55 | #define DEFAULT_HARD_LIMIT_ENABLE 0 // false 56 | #define DEFAULT_HOMING_ENABLE 0 // false 57 | #define DEFAULT_HOMING_DIR_MASK 0 // move positive dir 58 | #define DEFAULT_HOMING_FEED_RATE 25.0 // mm/min 59 | #define DEFAULT_HOMING_SEEK_RATE 500.0 // mm/min 60 | #define DEFAULT_HOMING_DEBOUNCE_DELAY 250 // msec (0-65k) 61 | #define DEFAULT_HOMING_PULLOFF 1.0 // mm 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /nuts_bolts.h: -------------------------------------------------------------------------------- 1 | /* 2 | nuts_bolts.h - Header file for shared definitions, variables, and functions 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef nuts_bolts_h 23 | #define nuts_bolts_h 24 | 25 | #define false 0 26 | #define true 1 27 | 28 | // Axis array index values. Must start with 0 and be continuous. 29 | #define N_AXIS 3 // Number of axes 30 | #define X_AXIS 0 // Axis indexing value. 31 | #define Y_AXIS 1 32 | #define Z_AXIS 2 33 | // #define A_AXIS 3 34 | 35 | // CoreXY motor assignments. DO NOT ALTER. 36 | // NOTE: If the A and B motor axis bindings are changed, this effects the CoreXY equations. 37 | #ifdef COREXY 38 | #define A_MOTOR X_AXIS // Must be X_AXIS 39 | #define B_MOTOR Y_AXIS // Must be Y_AXIS 40 | #endif 41 | 42 | // Conversions 43 | #define MM_PER_INCH (25.40) 44 | #define INCH_PER_MM (0.0393701) 45 | #define TICKS_PER_MICROSECOND (F_CPU/1000000) 46 | 47 | // Useful macros 48 | #define clear_vector(a) memset(a, 0, sizeof(a)) 49 | #define clear_vector_float(a) memset(a, 0.0, sizeof(float)*N_AXIS) 50 | // #define clear_vector_long(a) memset(a, 0.0, sizeof(long)*N_AXIS) 51 | #define max(a,b) (((a) > (b)) ? (a) : (b)) 52 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 53 | 54 | // Bit field and masking macros 55 | #define bit(n) (1 << n) 56 | #define bit_true_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) |= (mask); SREG = sreg; } 57 | #define bit_false_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) &= ~(mask); SREG = sreg; } 58 | #define bit_toggle_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) ^= (mask); SREG = sreg; } 59 | #define bit_true(x,mask) (x) |= (mask) 60 | #define bit_false(x,mask) (x) &= ~(mask) 61 | #define bit_istrue(x,mask) ((x & mask) != 0) 62 | #define bit_isfalse(x,mask) ((x & mask) == 0) 63 | 64 | // Read a floating point value from a string. Line points to the input buffer, char_counter 65 | // is the indexer pointing to the current character of the line, while float_ptr is 66 | // a pointer to the result variable. Returns true when it succeeds 67 | uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr); 68 | 69 | // Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms(). 70 | void delay_ms(uint16_t ms); 71 | 72 | // Delays variable-defined microseconds. Compiler compatibility fix for _delay_us(). 73 | void delay_us(uint32_t us); 74 | 75 | // Computes hypotenuse, avoiding avr-gcc's bloated version and the extra error checking. 76 | float hypot_f(float x, float y); 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /defaults/defaults_shapeoko3.h: -------------------------------------------------------------------------------- 1 | /* 2 | defaults_shapeoko3.h - defaults settings configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Description: Shapeoko CNC mill with three NEMA 23 stepper motors, driven by CarbideMotion 31 | #define MICROSTEPS_XY 8 32 | #define STEP_REVS_XY 200 33 | #define MM_PER_REV_XY (2.0*20) // 2mm belt pitch, 20 pulley teeth 34 | #define MICROSTEPS_Z 8 35 | #define STEP_REVS_Z 200 36 | #define MM_PER_REV_Z (2.0*20) // 2mm belt pitch, 20 pulley teeth 37 | #define DEFAULT_X_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 38 | #define DEFAULT_Y_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 39 | #define DEFAULT_Z_STEPS_PER_MM (MICROSTEPS_Z*STEP_REVS_Z/MM_PER_REV_Z) 40 | #define DEFAULT_X_MAX_RATE 5000.0 // mm/min 41 | #define DEFAULT_Y_MAX_RATE 5000.0 // mm/min 42 | #define DEFAULT_Z_MAX_RATE 5000.0 // mm/min 43 | #define DEFAULT_X_ACCELERATION (400.0*60*60) // 400*60*60 mm/min^2 = 400 mm/sec^2 44 | #define DEFAULT_Y_ACCELERATION (400.0*60*60) // 400*60*60 mm/min^2 = 400 mm/sec^2 45 | #define DEFAULT_Z_ACCELERATION (400.0*60*60) // 400*60*60 mm/min^2 = 400 mm/sec^2 46 | #define DEFAULT_X_MAX_TRAVEL 425.0 // mm 47 | #define DEFAULT_Y_MAX_TRAVEL 465.0 // mm 48 | #define DEFAULT_Z_MAX_TRAVEL 80.0 // mm 49 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 50 | #define DEFAULT_STEPPING_INVERT_MASK 0 51 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Description: Sherline 5400 mill with three NEMA 23 Keling KL23H256-21-8B 185 oz-in stepper motors, 31 | // driven by three Pololu A4988 stepper drivers with a 30V, 6A power supply at 1.5A per winding. 32 | #define MICROSTEPS 2 33 | #define STEPS_PER_REV 200.0 34 | #define MM_PER_REV (0.050*MM_PER_INCH) // 0.050 inch/rev leadscrew 35 | #define DEFAULT_X_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/MM_PER_REV) 36 | #define DEFAULT_Y_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/MM_PER_REV) 37 | #define DEFAULT_Z_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/MM_PER_REV) 38 | #define DEFAULT_X_MAX_RATE 635.0 // mm/min (25 ipm) 39 | #define DEFAULT_Y_MAX_RATE 635.0 // mm/min 40 | #define DEFAULT_Z_MAX_RATE 635.0 // mm/min 41 | #define DEFAULT_X_ACCELERATION (50.0*60*60) // 50*60*60 mm/min^2 = 50 mm/sec^2 42 | #define DEFAULT_Y_ACCELERATION (50.0*60*60) // 50*60*60 mm/min^2 = 50 mm/sec^2 43 | #define DEFAULT_Z_ACCELERATION (50.0*60*60) // 50*60*60 mm/min^2 = 50 mm/sec^2 44 | #define DEFAULT_X_MAX_TRAVEL 225.0 // mm 45 | #define DEFAULT_Y_MAX_TRAVEL 125.0 // mm 46 | #define DEFAULT_Z_MAX_TRAVEL 170.0 // mm 47 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 48 | #define DEFAULT_STEPPING_INVERT_MASK 0 49 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Description: Shapeoko CNC mill with three NEMA 17 stepper motors, driven by Synthetos 31 | // grblShield at 28V. 32 | #define MICROSTEPS_XY 8 33 | #define STEP_REVS_XY 200 34 | #define MM_PER_REV_XY (2.0*20) // 2mm belt pitch, 20 pulley teeth 35 | #define MICROSTEPS_Z 2 36 | #define STEP_REVS_Z 200 37 | #define MM_PER_REV_Z 1.250 // 1.25 mm/rev leadscrew 38 | #define DEFAULT_X_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 39 | #define DEFAULT_Y_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 40 | #define DEFAULT_Z_STEPS_PER_MM (MICROSTEPS_Z*STEP_REVS_Z/MM_PER_REV_Z) 41 | #define DEFAULT_X_MAX_RATE 5000.0 // mm/min 42 | #define DEFAULT_Y_MAX_RATE 5000.0 // mm/min 43 | #define DEFAULT_Z_MAX_RATE 500.0 // mm/min 44 | #define DEFAULT_X_ACCELERATION (250.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 45 | #define DEFAULT_Y_ACCELERATION (250.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 46 | #define DEFAULT_Z_ACCELERATION (50.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 47 | #define DEFAULT_X_MAX_TRAVEL 290.0 // mm 48 | #define DEFAULT_Y_MAX_TRAVEL 290.0 // mm 49 | #define DEFAULT_Z_MAX_TRAVEL 100.0 // mm 50 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 51 | #define DEFAULT_STEPPING_INVERT_MASK 0 52 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Description: X-Carve 3D Carver CNC mill with three 200 step/rev motors driven by Synthetos 31 | // grblShield at 24V. 32 | #define MICROSTEPS_XY 8 33 | #define STEP_REVS_XY 200 34 | #define MM_PER_REV_XY (2.0*20) // 2mm belt pitch, 20 pulley teeth 35 | #define MICROSTEPS_Z 2 36 | #define STEP_REVS_Z 200 37 | #define MM_PER_REV_Z 2.117 // ACME 3/8-12 Leadscrew 38 | #define DEFAULT_X_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 39 | #define DEFAULT_Y_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 40 | #define DEFAULT_Z_STEPS_PER_MM (MICROSTEPS_Z*STEP_REVS_Z/MM_PER_REV_Z) 41 | #define DEFAULT_X_MAX_RATE 8000.0 // mm/min 42 | #define DEFAULT_Y_MAX_RATE 8000.0 // mm/min 43 | #define DEFAULT_Z_MAX_RATE 500.0 // mm/min 44 | #define DEFAULT_X_ACCELERATION (500.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 45 | #define DEFAULT_Y_ACCELERATION (500.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 46 | #define DEFAULT_Z_ACCELERATION (50.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 47 | #define DEFAULT_X_MAX_TRAVEL 740.0 // mm 48 | #define DEFAULT_Y_MAX_TRAVEL 790.0 // mm 49 | #define DEFAULT_Z_MAX_TRAVEL 100.0 // mm 50 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 51 | #define DEFAULT_STEPPING_INVERT_MASK 0 52 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Description: X-Carve 3D Carver CNC mill with three 200 step/rev motors driven by Synthetos 31 | // grblShield at 24V. 32 | #define MICROSTEPS_XY 8 33 | #define STEP_REVS_XY 200 34 | #define MM_PER_REV_XY (2.0*20) // 2mm belt pitch, 20 pulley teeth 35 | #define MICROSTEPS_Z 2 36 | #define STEP_REVS_Z 200 37 | #define MM_PER_REV_Z 2.117 // ACME 3/8-12 Leadscrew 38 | #define DEFAULT_X_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 39 | #define DEFAULT_Y_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 40 | #define DEFAULT_Z_STEPS_PER_MM (MICROSTEPS_Z*STEP_REVS_Z/MM_PER_REV_Z) 41 | #define DEFAULT_X_MAX_RATE 8000.0 // mm/min 42 | #define DEFAULT_Y_MAX_RATE 8000.0 // mm/min 43 | #define DEFAULT_Z_MAX_RATE 500.0 // mm/min 44 | #define DEFAULT_X_ACCELERATION (500.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 45 | #define DEFAULT_Y_ACCELERATION (500.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 46 | #define DEFAULT_Z_ACCELERATION (50.0*60*60) // 25*60*60 mm/min^2 = 25 mm/sec^2 47 | #define DEFAULT_X_MAX_TRAVEL 290.0 // mm 48 | #define DEFAULT_Y_MAX_TRAVEL 290.0 // mm 49 | #define DEFAULT_Z_MAX_TRAVEL 100.0 // mm 50 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 51 | #define DEFAULT_STEPPING_INVERT_MASK 0 52 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | // Description: Zen Toolworks 7x7 mill with three Shinano SST43D2121 65oz-in NEMA 17 stepper motors. 31 | // Leadscrew is different from some ZTW kits, where most are 1.25mm/rev rather than 8.0mm/rev here. 32 | // Driven by 30V, 6A power supply and TI DRV8811 stepper motor drivers. 33 | #define MICROSTEPS 8 34 | #define STEPS_PER_REV 200.0 35 | #define MM_PER_REV 8.0 // 8 mm/rev leadscrew 36 | #define DEFAULT_X_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/MM_PER_REV) 37 | #define DEFAULT_Y_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/MM_PER_REV) 38 | #define DEFAULT_Z_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/MM_PER_REV) 39 | #define DEFAULT_X_MAX_RATE 6000.0 // mm/min 40 | #define DEFAULT_Y_MAX_RATE 6000.0 // mm/min 41 | #define DEFAULT_Z_MAX_RATE 6000.0 // mm/min 42 | #define DEFAULT_X_ACCELERATION (600.0*60*60) // 600*60*60 mm/min^2 = 600 mm/sec^2 43 | #define DEFAULT_Y_ACCELERATION (600.0*60*60) // 600*60*60 mm/min^2 = 600 mm/sec^2 44 | #define DEFAULT_Z_ACCELERATION (600.0*60*60) // 600*60*60 mm/min^2 = 600 mm/sec^2 45 | #define DEFAULT_X_MAX_TRAVEL 190.0 // mm 46 | #define DEFAULT_Y_MAX_TRAVEL 180.0 // mm 47 | #define DEFAULT_Z_MAX_TRAVEL 150.0 // mm 48 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 49 | #define DEFAULT_STEPPING_INVERT_MASK 0 50 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings file for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | here are supplied by users, so your results may vary. However, this should give you 24 | a good starting point as you get to know your machine and tweak the settings for your 25 | nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | 31 | // Description: Shapeoko CNC mill with three NEMA 17 stepper motors, driven by Synthetos 32 | // grblShield with a 24V, 4.2A power supply. 33 | #define MICROSTEPS_XY 8 34 | #define STEP_REVS_XY 400 35 | #define MM_PER_REV_XY (0.08*18*MM_PER_INCH) // 0.08 in belt pitch, 18 pulley teeth 36 | #define MICROSTEPS_Z 2 37 | #define STEP_REVS_Z 400 38 | #define MM_PER_REV_Z 1.250 // 1.25 mm/rev leadscrew 39 | #define DEFAULT_X_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 40 | #define DEFAULT_Y_STEPS_PER_MM (MICROSTEPS_XY*STEP_REVS_XY/MM_PER_REV_XY) 41 | #define DEFAULT_Z_STEPS_PER_MM (MICROSTEPS_Z*STEP_REVS_Z/MM_PER_REV_Z) 42 | #define DEFAULT_X_MAX_RATE 1000.0 // mm/min 43 | #define DEFAULT_Y_MAX_RATE 1000.0 // mm/min 44 | #define DEFAULT_Z_MAX_RATE 1000.0 // mm/min 45 | #define DEFAULT_X_ACCELERATION (15.0*60*60) // 15*60*60 mm/min^2 = 15 mm/sec^2 46 | #define DEFAULT_Y_ACCELERATION (15.0*60*60) // 15*60*60 mm/min^2 = 15 mm/sec^2 47 | #define DEFAULT_Z_ACCELERATION (15.0*60*60) // 15*60*60 mm/min^2 = 15 mm/sec^2 48 | #define DEFAULT_X_MAX_TRAVEL 200.0 // mm 49 | #define DEFAULT_Y_MAX_TRAVEL 200.0 // mm 50 | #define DEFAULT_Z_MAX_TRAVEL 200.0 // mm 51 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 52 | #define DEFAULT_STEPPING_INVERT_MASK 0 53 | #define DEFAULT_DIRECTION_INVERT_MASK ((1<. 20 | */ 21 | 22 | #include "grbl.h" 23 | 24 | 25 | // Declare system global variable structure 26 | system_t sys; 27 | 28 | 29 | int main(void) 30 | { 31 | // Initialize system upon power-up. 32 | serial_init(); // Setup serial baud rate and interrupts 33 | settings_init(); // Load Grbl settings from EEPROM 34 | stepper_init(); // Configure stepper pins and interrupt timers 35 | system_init(); // Configure pinout pins and pin-change interrupt 36 | 37 | memset(&sys, 0, sizeof(sys)); // Clear all system variables 38 | sys.abort = true; // Set abort to complete initialization 39 | sei(); // Enable interrupts 40 | 41 | // Check for power-up and set system alarm if homing is enabled to force homing cycle 42 | // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the 43 | // startup scripts, but allows access to settings and internal commands. Only a homing 44 | // cycle '$H' or kill alarm locks '$X' will disable the alarm. 45 | // NOTE: The startup script will run after successful completion of the homing cycle, but 46 | // not after disabling the alarm locks. Prevents motion startup blocks from crashing into 47 | // things uncontrollably. Very bad. 48 | #ifdef HOMING_INIT_LOCK 49 | if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; } 50 | #endif 51 | 52 | // Force Grbl into an ALARM state upon a power-cycle or hard reset. 53 | #ifdef FORCE_INITIALIZATION_ALARM 54 | sys.state = STATE_ALARM; 55 | #endif 56 | 57 | // Grbl initialization loop upon power-up or a system abort. For the latter, all processes 58 | // will return to this loop to be cleanly re-initialized. 59 | for(;;) { 60 | 61 | // TODO: Separate configure task that require interrupts to be disabled, especially upon 62 | // a system abort and ensuring any active interrupts are cleanly reset. 63 | 64 | // Reset Grbl primary systems. 65 | serial_reset_read_buffer(); // Clear serial read buffer 66 | gc_init(); // Set g-code parser to default state 67 | spindle_init(); 68 | coolant_init(); 69 | limits_init(); 70 | probe_init(); 71 | plan_reset(); // Clear block buffer and planner variables 72 | st_reset(); // Clear stepper subsystem variables. 73 | 74 | // Sync cleared gcode and planner positions to current system position. 75 | plan_sync_position(); 76 | gc_sync_position(); 77 | 78 | // Reset system variables. 79 | sys.abort = false; 80 | sys.rt_exec_state = 0; 81 | sys.rt_exec_alarm = 0; 82 | sys.suspend = false; 83 | 84 | // Start Grbl main loop. Processes program inputs and executes them. 85 | protocol_main_loop(); 86 | 87 | } 88 | return 0; /* Never reached */ 89 | } 90 | -------------------------------------------------------------------------------- /defaults.h: -------------------------------------------------------------------------------- 1 | /* 2 | defaults.h - defaults settings configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* The defaults.h file serves as a central default settings selector for different machine 22 | types, from DIY CNC mills to CNC conversions of off-the-shelf machines. The settings 23 | files listed here are supplied by users, so your results may vary. However, this should 24 | give you a good starting point as you get to know your machine and tweak the settings for 25 | your nefarious needs. 26 | Ensure one and only one of these DEFAULTS_XXX values is defined in config.h */ 27 | 28 | #ifndef defaults_h 29 | 30 | // Only define the DEFAULT_XXX with where to find the corresponding default_XXX.h file. 31 | // Don't #define defaults_h here, let the selected file do it. Prevents including more than one. 32 | 33 | #ifdef DEFAULTS_GENERIC 34 | // Grbl generic default settings. Should work across different machines. 35 | #include "defaults/defaults_generic.h" 36 | #endif 37 | 38 | #ifdef DEFAULTS_SHERLINE_5400 39 | // Description: Sherline 5400 mill with three NEMA 23 Keling KL23H256-21-8B 185 oz-in stepper motors, 40 | // driven by three Pololu A4988 stepper drivers with a 30V, 6A power supply at 1.5A per winding. 41 | #include "defaults/defaults_sherline.h" 42 | #endif 43 | 44 | #ifdef DEFAULTS_SHAPEOKO 45 | // Description: Shapeoko CNC mill with three NEMA 17 stepper motors, driven by Synthetos 46 | // grblShield with a 24V, 4.2A power supply. 47 | #include "defaults/defaults_shapeoko.h" 48 | #endif 49 | 50 | #ifdef DEFAULTS_SHAPEOKO_2 51 | // Description: Shapeoko CNC mill with three NEMA 17 stepper motors, driven by Synthetos 52 | // grblShield at 28V. 53 | #include "defaults/defaults_shapeoko2.h" 54 | #endif 55 | 56 | #ifdef DEFAULTS_SHAPEOKO_3 57 | // Description: Shapeoko CNC mill with three NEMA 23 stepper motors, driven by CarbideMotion 58 | #include "defaults/defaults_shapeoko3.h" 59 | #endif 60 | 61 | #ifdef DEFAULTS_X_CARVE_500MM 62 | // Description: X-Carve 3D Carver CNC mill with three 200 step/rev motors driven by Synthetos 63 | // grblShield at 24V. 64 | #include "defaults/defaults_x_carve_500mm.h" 65 | #endif 66 | 67 | #ifdef DEFAULTS_X_CARVE_1000MM 68 | // Description: X-Carve 3D Carver CNC mill with three 200 step/rev motors driven by Synthetos 69 | // grblShield at 24V. 70 | #include "defaults/defaults_x_carve_1000mm.h" 71 | #endif 72 | 73 | #ifdef DEFAULTS_ZEN_TOOLWORKS_7x7 74 | // Description: Zen Toolworks 7x7 mill with three Shinano SST43D2121 65oz-in NEMA 17 stepper motors. 75 | // Leadscrew is different from some ZTW kits, where most are 1.25mm/rev rather than 8.0mm/rev here. 76 | // Driven by 30V, 6A power supply and TI DRV8811 stepper motor drivers. 77 | #include "defaults/defaults_zen_toolworks_7x7.h" 78 | #endif 79 | 80 | #ifdef DEFAULTS_OXCNC 81 | // Grbl settings for OpenBuilds OX CNC Machine 82 | // http://www.openbuilds.com/builds/openbuilds-ox-cnc-machine.341/ 83 | #include "defaults/defaults_oxcnc.h" 84 | #endif 85 | 86 | #ifdef DEFAULTS_SIMULATOR 87 | // Settings only for Grbl Simulator (www.github.com/grbl/grbl-sim) 88 | #include "defaults/defaults_simulator.h" 89 | #endif 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /report.h: -------------------------------------------------------------------------------- 1 | /* 2 | report.h - reporting and messaging methods 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | #ifndef report_h 21 | #define report_h 22 | 23 | // Define Grbl status codes. 24 | #define STATUS_OK 0 25 | #define STATUS_EXPECTED_COMMAND_LETTER 1 26 | #define STATUS_BAD_NUMBER_FORMAT 2 27 | #define STATUS_INVALID_STATEMENT 3 28 | #define STATUS_NEGATIVE_VALUE 4 29 | #define STATUS_SETTING_DISABLED 5 30 | #define STATUS_SETTING_STEP_PULSE_MIN 6 31 | #define STATUS_SETTING_READ_FAIL 7 32 | #define STATUS_IDLE_ERROR 8 33 | #define STATUS_ALARM_LOCK 9 34 | #define STATUS_SOFT_LIMIT_ERROR 10 35 | #define STATUS_OVERFLOW 11 36 | #define STATUS_MAX_STEP_RATE_EXCEEDED 12 37 | 38 | #define STATUS_GCODE_UNSUPPORTED_COMMAND 20 39 | #define STATUS_GCODE_MODAL_GROUP_VIOLATION 21 40 | #define STATUS_GCODE_UNDEFINED_FEED_RATE 22 41 | #define STATUS_GCODE_COMMAND_VALUE_NOT_INTEGER 23 42 | #define STATUS_GCODE_AXIS_COMMAND_CONFLICT 24 43 | #define STATUS_GCODE_WORD_REPEATED 25 44 | #define STATUS_GCODE_NO_AXIS_WORDS 26 45 | #define STATUS_GCODE_INVALID_LINE_NUMBER 27 46 | #define STATUS_GCODE_VALUE_WORD_MISSING 28 47 | #define STATUS_GCODE_UNSUPPORTED_COORD_SYS 29 48 | #define STATUS_GCODE_G53_INVALID_MOTION_MODE 30 49 | #define STATUS_GCODE_AXIS_WORDS_EXIST 31 50 | #define STATUS_GCODE_NO_AXIS_WORDS_IN_PLANE 32 51 | #define STATUS_GCODE_INVALID_TARGET 33 52 | #define STATUS_GCODE_ARC_RADIUS_ERROR 34 53 | #define STATUS_GCODE_NO_OFFSETS_IN_PLANE 35 54 | #define STATUS_GCODE_UNUSED_WORDS 36 55 | #define STATUS_GCODE_G43_DYNAMIC_AXIS_ERROR 37 56 | 57 | // Define Grbl alarm codes. 58 | #define ALARM_HARD_LIMIT_ERROR 1 59 | #define ALARM_SOFT_LIMIT_ERROR 2 60 | #define ALARM_ABORT_CYCLE 3 61 | #define ALARM_PROBE_FAIL 4 62 | #define ALARM_HOMING_FAIL 5 63 | 64 | // Define Grbl feedback message codes. 65 | #define MESSAGE_CRITICAL_EVENT 1 66 | #define MESSAGE_ALARM_LOCK 2 67 | #define MESSAGE_ALARM_UNLOCK 3 68 | #define MESSAGE_ENABLED 4 69 | #define MESSAGE_DISABLED 5 70 | #define MESSAGE_SAFETY_DOOR_AJAR 6 71 | #define MESSAGE_PROGRAM_END 7 72 | #define MESSAGE_RESTORE_DEFAULTS 8 73 | 74 | // Prints system status messages. 75 | void report_status_message(uint8_t status_code); 76 | 77 | // Prints system alarm messages. 78 | void report_alarm_message(int8_t alarm_code); 79 | 80 | // Prints miscellaneous feedback messages. 81 | void report_feedback_message(uint8_t message_code); 82 | 83 | // Prints welcome message 84 | void report_init_message(); 85 | 86 | // Prints Grbl help and current global settings 87 | void report_grbl_help(); 88 | 89 | // Prints Grbl global settings 90 | void report_grbl_settings(); 91 | 92 | // Prints an echo of the pre-parsed line received right before execution. 93 | void report_echo_line_received(char *line); 94 | 95 | // Prints realtime status report 96 | void report_realtime_status(); 97 | 98 | // Prints recorded probe position 99 | void report_probe_parameters(); 100 | 101 | // Prints Grbl NGC parameters (coordinate offsets, probe) 102 | void report_ngc_parameters(); 103 | 104 | // Prints current g-code parser mode state 105 | void report_gcode_modes(); 106 | 107 | // Prints startup line 108 | void report_startup_line(uint8_t n, char *line); 109 | 110 | // Prints build info and user info 111 | void report_build_info(char *line); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /planner.h: -------------------------------------------------------------------------------- 1 | /* 2 | planner.h - buffers movement commands and manages the acceleration profile plan 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef planner_h 23 | #define planner_h 24 | 25 | 26 | // The number of linear motions that can be in the plan at any give time 27 | #ifndef BLOCK_BUFFER_SIZE 28 | #ifdef USE_LINE_NUMBERS 29 | #define BLOCK_BUFFER_SIZE 16 30 | #else 31 | #define BLOCK_BUFFER_SIZE 18 32 | #endif 33 | #endif 34 | 35 | // This struct stores a linear movement of a g-code block motion with its critical "nominal" values 36 | // are as specified in the source g-code. 37 | typedef struct { 38 | // Fields used by the bresenham algorithm for tracing the line 39 | // NOTE: Used by stepper algorithm to execute the block correctly. Do not alter these values. 40 | uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h) 41 | uint32_t steps[N_AXIS]; // Step count along each axis 42 | uint32_t step_event_count; // The maximum step axis count and number of steps required to complete this block. 43 | 44 | // Fields used by the motion planner to manage acceleration 45 | float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2 46 | float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and 47 | // neighboring nominal speeds with overrides in (mm/min)^2 48 | float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2 49 | float nominal_speed_sqr; // Axis-limit adjusted nominal speed for this block in (mm/min)^2 50 | float acceleration; // Axis-limit adjusted line acceleration in (mm/min^2) 51 | float millimeters; // The remaining distance for this block to be executed in (mm) 52 | // uint8_t max_override; // Maximum override value based on axis speed limits 53 | 54 | #ifdef USE_LINE_NUMBERS 55 | int32_t line_number; 56 | #endif 57 | } plan_block_t; 58 | 59 | 60 | // Initialize and reset the motion plan subsystem 61 | void plan_reset(); 62 | 63 | // Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position 64 | // in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed 65 | // rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. 66 | #ifdef USE_LINE_NUMBERS 67 | void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate, int32_t line_number); 68 | #else 69 | void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate); 70 | #endif 71 | 72 | // Called when the current block is no longer needed. Discards the block and makes the memory 73 | // availible for new blocks. 74 | void plan_discard_current_block(); 75 | 76 | // Gets the current block. Returns NULL if buffer empty 77 | plan_block_t *plan_get_current_block(); 78 | 79 | // Called periodically by step segment buffer. Mostly used internally by planner. 80 | uint8_t plan_next_block_index(uint8_t block_index); 81 | 82 | // Called by step segment buffer when computing executing block velocity profile. 83 | float plan_get_exec_block_exit_speed(); 84 | 85 | // Reset the planner position vector (in steps) 86 | void plan_sync_position(); 87 | 88 | // Reinitialize plan with a partially completed block 89 | void plan_cycle_reinitialize(); 90 | 91 | // Returns the number of active blocks are in the planner buffer. 92 | uint8_t plan_get_block_buffer_count(); 93 | 94 | // Returns the status of the block ring buffer. True, if buffer is full. 95 | uint8_t plan_check_full_buffer(); 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /nuts_bolts.c: -------------------------------------------------------------------------------- 1 | /* 2 | nuts_bolts.c - Shared functions 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #include "grbl.h" 23 | 24 | 25 | #define MAX_INT_DIGITS 8 // Maximum number of digits in int32 (and float) 26 | 27 | 28 | // Extracts a floating point value from a string. The following code is based loosely on 29 | // the avr-libc strtod() function by Michael Stumpf and Dmitry Xmelkov and many freely 30 | // available conversion method examples, but has been highly optimized for Grbl. For known 31 | // CNC applications, the typical decimal value is expected to be in the range of E0 to E-4. 32 | // Scientific notation is officially not supported by g-code, and the 'E' character may 33 | // be a g-code word on some CNC systems. So, 'E' notation will not be recognized. 34 | // NOTE: Thanks to Radu-Eosif Mihailescu for identifying the issues with using strtod(). 35 | uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr) 36 | { 37 | char *ptr = line + *char_counter; 38 | unsigned char c; 39 | 40 | // Grab first character and increment pointer. No spaces assumed in line. 41 | c = *ptr++; 42 | 43 | // Capture initial positive/minus character 44 | bool isnegative = false; 45 | if (c == '-') { 46 | isnegative = true; 47 | c = *ptr++; 48 | } else if (c == '+') { 49 | c = *ptr++; 50 | } 51 | 52 | // Extract number into fast integer. Track decimal in terms of exponent value. 53 | uint32_t intval = 0; 54 | int8_t exp = 0; 55 | uint8_t ndigit = 0; 56 | bool isdecimal = false; 57 | while(1) { 58 | c -= '0'; 59 | if (c <= 9) { 60 | ndigit++; 61 | if (ndigit <= MAX_INT_DIGITS) { 62 | if (isdecimal) { exp--; } 63 | intval = (((intval << 2) + intval) << 1) + c; // intval*10 + c 64 | } else { 65 | if (!(isdecimal)) { exp++; } // Drop overflow digits 66 | } 67 | } else if (c == (('.'-'0') & 0xff) && !(isdecimal)) { 68 | isdecimal = true; 69 | } else { 70 | break; 71 | } 72 | c = *ptr++; 73 | } 74 | 75 | // Return if no digits have been read. 76 | if (!ndigit) { return(false); }; 77 | 78 | // Convert integer into floating point. 79 | float fval; 80 | fval = (float)intval; 81 | 82 | // Apply decimal. Should perform no more than two floating point multiplications for the 83 | // expected range of E0 to E-4. 84 | if (fval != 0) { 85 | while (exp <= -2) { 86 | fval *= 0.01; 87 | exp += 2; 88 | } 89 | if (exp < 0) { 90 | fval *= 0.1; 91 | } else if (exp > 0) { 92 | do { 93 | fval *= 10.0; 94 | } while (--exp > 0); 95 | } 96 | } 97 | 98 | // Assign floating point value with correct sign. 99 | if (isnegative) { 100 | *float_ptr = -fval; 101 | } else { 102 | *float_ptr = fval; 103 | } 104 | 105 | *char_counter = ptr - line - 1; // Set char_counter to next statement 106 | 107 | return(true); 108 | } 109 | 110 | 111 | // Delays variable defined milliseconds. Compiler compatibility fix for _delay_ms(), 112 | // which only accepts constants in future compiler releases. 113 | void delay_ms(uint16_t ms) 114 | { 115 | while ( ms-- ) { _delay_ms(1); } 116 | } 117 | 118 | 119 | // Delays variable defined microseconds. Compiler compatibility fix for _delay_us(), 120 | // which only accepts constants in future compiler releases. Written to perform more 121 | // efficiently with larger delays, as the counter adds parasitic time in each iteration. 122 | void delay_us(uint32_t us) 123 | { 124 | while (us) { 125 | if (us < 10) { 126 | _delay_us(1); 127 | us--; 128 | } else if (us < 100) { 129 | _delay_us(10); 130 | us -= 10; 131 | } else if (us < 1000) { 132 | _delay_us(100); 133 | us -= 100; 134 | } else { 135 | _delay_ms(1); 136 | us -= 1000; 137 | } 138 | } 139 | } 140 | 141 | 142 | // Simple hypotenuse computation function. 143 | float hypot_f(float x, float y) { return(sqrt(x*x + y*y)); } 144 | -------------------------------------------------------------------------------- /spindle_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | spindle_control.c - spindle control methods 3 | Part of Grbl 4 | Copyright (c) 2012-2015 Sungeun K. Jeon 5 | Copyright (c) 2009-2011 Simen Svale Skogsrud 6 | Grbl is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | Grbl is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | You should have received a copy of the GNU General Public License 15 | along with Grbl. If not, see . 16 | */ 17 | 18 | 19 | /* RC-Servo PWM modification: switch between 0.6ms and 2.5ms pulse-width at 61Hz 20 | Prescaler 1024 = 15625Hz / 256Steps = 61Hz 64µs/step -> Values 15 / 32 for 1ms / 2ms 21 | Reload value = 0x07 22 | Replace this file in C:\Program Files (x86)\Arduino\libraries\GRBL 23 | */ 24 | 25 | 26 | #include "grbl.h" 27 | 28 | #define RC_SERVO_SHORT 15 // Timer ticks for 0.6ms pulse duration (9 for 0.6ms) 29 | #define RC_SERVO_LONG 32 // Timer ticks for 2.5 ms pulse duration (39 for 2.5ms) 30 | //#define RC_SERVO_INVERT 1 // Uncomment to invert servo direction 31 | 32 | 33 | void spindle_init() 34 | { 35 | // Configure variable spindle PWM and enable pin, if requried. On the Uno, PWM and enable are 36 | // combined unless configured otherwise. 37 | #ifdef VARIABLE_SPINDLE 38 | SPINDLE_PWM_DDR |= (1< SPINDLE_RPM_RANGE ) { rpm = SPINDLE_RPM_RANGE; } // Prevent integer overflow 107 | } 108 | 109 | #ifdef RC_SERVO_INVERT 110 | current_pwm = floor( RC_SERVO_LONG - rpm*(RC_SERVO_RANGE/SPINDLE_RPM_RANGE)); 111 | OCR_REGISTER = current_pwm; 112 | #else 113 | current_pwm = floor( rpm*(RC_SERVO_RANGE/SPINDLE_RPM_RANGE) + RC_SERVO_SHORT); 114 | OCR_REGISTER = current_pwm; 115 | #endif 116 | #ifdef MINIMUM_SPINDLE_PWM 117 | if (current_pwm < MINIMUM_SPINDLE_PWM) { current_pwm = MINIMUM_SPINDLE_PWM; } 118 | OCR_REGISTER = current_pwm; 119 | #endif 120 | #endif 121 | } 122 | } 123 | spindle_set_state(uint8_t state, float rpm){ 124 | } -------------------------------------------------------------------------------- /system.h: -------------------------------------------------------------------------------- 1 | /* 2 | system.h - Header for system level commands and real-time processes 3 | Part of Grbl 4 | 5 | Copyright (c) 2014-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | #ifndef system_h 22 | #define system_h 23 | 24 | #include "grbl.h" 25 | 26 | // Define system executor bit map. Used internally by realtime protocol as realtime command flags, 27 | // which notifies the main program to execute the specified realtime command asynchronously. 28 | // NOTE: The system executor uses an unsigned 8-bit volatile variable (8 flag limit.) The default 29 | // flags are always false, so the realtime protocol only needs to check for a non-zero value to 30 | // know when there is a realtime command to execute. 31 | #define EXEC_STATUS_REPORT bit(0) // bitmask 00000001 32 | #define EXEC_CYCLE_START bit(1) // bitmask 00000010 33 | #define EXEC_CYCLE_STOP bit(2) // bitmask 00000100 34 | #define EXEC_FEED_HOLD bit(3) // bitmask 00001000 35 | #define EXEC_RESET bit(4) // bitmask 00010000 36 | #define EXEC_SAFETY_DOOR bit(5) // bitmask 00100000 37 | #define EXEC_MOTION_CANCEL bit(6) // bitmask 01000000 38 | 39 | // Alarm executor bit map. 40 | // NOTE: EXEC_CRITICAL_EVENT is an optional flag that must be set with an alarm flag. When enabled, 41 | // this halts Grbl into an infinite loop until the user aknowledges the problem and issues a soft- 42 | // reset command. For example, a hard limit event needs this type of halt and aknowledgement. 43 | #define EXEC_CRITICAL_EVENT bit(0) // bitmask 00000001 (SPECIAL FLAG. See NOTE:) 44 | #define EXEC_ALARM_HARD_LIMIT bit(1) // bitmask 00000010 45 | #define EXEC_ALARM_SOFT_LIMIT bit(2) // bitmask 00000100 46 | #define EXEC_ALARM_ABORT_CYCLE bit(3) // bitmask 00001000 47 | #define EXEC_ALARM_PROBE_FAIL bit(4) // bitmask 00010000 48 | #define EXEC_ALARM_HOMING_FAIL bit(5) // bitmask 00100000 49 | 50 | // Define system state bit map. The state variable primarily tracks the individual functions 51 | // of Grbl to manage each without overlapping. It is also used as a messaging flag for 52 | // critical events. 53 | #define STATE_IDLE 0 // Must be zero. No flags. 54 | #define STATE_ALARM bit(0) // In alarm state. Locks out all g-code processes. Allows settings access. 55 | #define STATE_CHECK_MODE bit(1) // G-code check mode. Locks out planner and motion only. 56 | #define STATE_HOMING bit(2) // Performing homing cycle 57 | #define STATE_CYCLE bit(3) // Cycle is running or motions are being executed. 58 | #define STATE_HOLD bit(4) // Active feed hold 59 | #define STATE_SAFETY_DOOR bit(5) // Safety door is ajar. Feed holds and de-energizes system. 60 | #define STATE_MOTION_CANCEL bit(6) // Motion cancel by feed hold and return to idle. 61 | 62 | // Define system suspend states. 63 | #define SUSPEND_DISABLE 0 // Must be zero. 64 | #define SUSPEND_ENABLE_HOLD bit(0) // Enabled. Indicates the cycle is active and currently undergoing a hold. 65 | #define SUSPEND_ENABLE_READY bit(1) // Ready to resume with a cycle start command. 66 | #define SUSPEND_ENERGIZE bit(2) // Re-energizes output before resume. 67 | #define SUSPEND_MOTION_CANCEL bit(3) // Cancels resume motion. Used by probing routine. 68 | 69 | 70 | // Define global system variables 71 | typedef struct { 72 | uint8_t abort; // System abort flag. Forces exit back to main loop for reset. 73 | uint8_t state; // Tracks the current state of Grbl. 74 | uint8_t suspend; // System suspend bitflag variable that manages holds, cancels, and safety door. 75 | 76 | volatile uint8_t rt_exec_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks. 77 | volatile uint8_t rt_exec_alarm; // Global realtime executor bitflag variable for setting various alarms. 78 | 79 | int32_t position[N_AXIS]; // Real-time machine (aka home) position vector in steps. 80 | // NOTE: This may need to be a volatile variable, if problems arise. 81 | 82 | uint8_t homing_axis_lock; // Locks axes when limits engage. Used as an axis motion mask in the stepper ISR. 83 | volatile uint8_t probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR. 84 | int32_t probe_position[N_AXIS]; // Last probe position in machine coordinates and steps. 85 | uint8_t probe_succeeded; // Tracks if last probing cycle was successful. 86 | } system_t; 87 | extern system_t sys; 88 | 89 | 90 | // Initialize the serial protocol 91 | void system_init(); 92 | 93 | // Returns if safety door is open or closed, based on pin state. 94 | uint8_t system_check_safety_door_ajar(); 95 | 96 | // Executes an internal system command, defined as a string starting with a '$' 97 | uint8_t system_execute_line(char *line); 98 | 99 | // Execute the startup script lines stored in EEPROM upon initialization 100 | void system_execute_startup(char *line); 101 | 102 | // Returns machine position of axis 'idx'. Must be sent a 'step' array. 103 | float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx); 104 | 105 | // Updates a machine 'position' array based on the 'step' array sent. 106 | void system_convert_array_steps_to_mpos(float *position, int32_t *steps); 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /cpu_map/cpu_map_atmega2560.h: -------------------------------------------------------------------------------- 1 | /* 2 | cpu_map_atmega2560.h - CPU and pin mapping configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* This cpu_map file serves as a central pin mapping settings file for AVR Mega 2560 */ 22 | 23 | 24 | #ifdef GRBL_PLATFORM 25 | #error "cpu_map already defined: GRBL_PLATFORM=" GRBL_PLATFORM 26 | #endif 27 | 28 | 29 | #define GRBL_PLATFORM "Atmega2560" 30 | 31 | // Serial port pins 32 | #define SERIAL_RX USART0_RX_vect 33 | #define SERIAL_UDRE USART0_UDRE_vect 34 | 35 | // Increase Buffers to make use of extra SRAM 36 | //#define RX_BUFFER_SIZE 256 37 | //#define TX_BUFFER_SIZE 128 38 | //#define BLOCK_BUFFER_SIZE 36 39 | //#define LINE_BUFFER_SIZE 100 40 | 41 | // Define step pulse output pins. NOTE: All step bit pins must be on the same port. 42 | #define STEP_DDR DDRA 43 | #define STEP_PORT PORTA 44 | #define STEP_PIN PINA 45 | #define X_STEP_BIT 2 // MEGA2560 Digital Pin 24 46 | #define Y_STEP_BIT 3 // MEGA2560 Digital Pin 25 47 | #define Z_STEP_BIT 4 // MEGA2560 Digital Pin 26 48 | #define STEP_MASK ((1< 25 | #include 26 | 27 | /* These EEPROM bits have different names on different devices. */ 28 | #ifndef EEPE 29 | #define EEPE EEWE //!< EEPROM program/write enable. 30 | #define EEMPE EEMWE //!< EEPROM master program/write enable. 31 | #endif 32 | 33 | /* These two are unfortunately not defined in the device include files. */ 34 | #define EEPM1 5 //!< EEPROM Programming Mode Bit 1. 35 | #define EEPM0 4 //!< EEPROM Programming Mode Bit 0. 36 | 37 | /* Define to reduce code size. */ 38 | #define EEPROM_IGNORE_SELFPROG //!< Remove SPM flag polling. 39 | 40 | /*! \brief Read byte from EEPROM. 41 | * 42 | * This function reads one byte from a given EEPROM address. 43 | * 44 | * \note The CPU is halted for 4 clock cycles during EEPROM read. 45 | * 46 | * \param addr EEPROM address to read from. 47 | * \return The byte read from the EEPROM address. 48 | */ 49 | unsigned char eeprom_get_char( unsigned int addr ) 50 | { 51 | do {} while( EECR & (1< 0; size--) { 133 | checksum = (checksum << 1) || (checksum >> 7); 134 | checksum += *source; 135 | eeprom_put_char(destination++, *(source++)); 136 | } 137 | eeprom_put_char(destination, checksum); 138 | } 139 | 140 | int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) { 141 | unsigned char data, checksum = 0; 142 | for(; size > 0; size--) { 143 | data = eeprom_get_char(source++); 144 | checksum = (checksum << 1) || (checksum >> 7); 145 | checksum += data; 146 | *(destination++) = data; 147 | } 148 | return(checksum == eeprom_get_char(source)); 149 | } 150 | 151 | // end of file 152 | -------------------------------------------------------------------------------- /settings.h: -------------------------------------------------------------------------------- 1 | /* 2 | settings.h - eeprom configuration handling 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef settings_h 23 | #define settings_h 24 | 25 | #include "grbl.h" 26 | 27 | 28 | // Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl 29 | // when firmware is upgraded. Always stored in byte 0 of eeprom 30 | #define SETTINGS_VERSION 9 // NOTE: Check settings_reset() when moving to next version. 31 | 32 | // Define bit flag masks for the boolean settings in settings.flag. 33 | #define BITFLAG_REPORT_INCHES bit(0) 34 | // #define BITFLAG_AUTO_START bit(1) // Obsolete. Don't alter to keep back compatibility. 35 | #define BITFLAG_INVERT_ST_ENABLE bit(2) 36 | #define BITFLAG_HARD_LIMIT_ENABLE bit(3) 37 | #define BITFLAG_HOMING_ENABLE bit(4) 38 | #define BITFLAG_SOFT_LIMIT_ENABLE bit(5) 39 | #define BITFLAG_INVERT_LIMIT_PINS bit(6) 40 | #define BITFLAG_INVERT_PROBE_PIN bit(7) 41 | 42 | // Define status reporting boolean enable bit flags in settings.status_report_mask 43 | #define BITFLAG_RT_STATUS_MACHINE_POSITION bit(0) 44 | #define BITFLAG_RT_STATUS_WORK_POSITION bit(1) 45 | #define BITFLAG_RT_STATUS_PLANNER_BUFFER bit(2) 46 | #define BITFLAG_RT_STATUS_SERIAL_RX bit(3) 47 | #define BITFLAG_RT_STATUS_LIMIT_PINS bit(4) 48 | 49 | // Define settings restore bitflags. 50 | #define SETTINGS_RESTORE_ALL 0xFF // All bitflags 51 | #define SETTINGS_RESTORE_DEFAULTS bit(0) 52 | #define SETTINGS_RESTORE_PARAMETERS bit(1) 53 | #define SETTINGS_RESTORE_STARTUP_LINES bit(2) 54 | #define SETTINGS_RESTORE_BUILD_INFO bit(3) 55 | 56 | // Define EEPROM memory address location values for Grbl settings and parameters 57 | // NOTE: The Atmega328p has 1KB EEPROM. The upper half is reserved for parameters and 58 | // the startup script. The lower half contains the global settings and space for future 59 | // developments. 60 | #define EEPROM_ADDR_GLOBAL 1U 61 | #define EEPROM_ADDR_PARAMETERS 512U 62 | #define EEPROM_ADDR_STARTUP_BLOCK 768U 63 | #define EEPROM_ADDR_BUILD_INFO 942U 64 | 65 | // Define EEPROM address indexing for coordinate parameters 66 | #define N_COORDINATE_SYSTEM 6 // Number of supported work coordinate systems (from index 1) 67 | #define SETTING_INDEX_NCOORD N_COORDINATE_SYSTEM+1 // Total number of system stored (from index 0) 68 | // NOTE: Work coordinate indices are (0=G54, 1=G55, ... , 6=G59) 69 | #define SETTING_INDEX_G28 N_COORDINATE_SYSTEM // Home position 1 70 | #define SETTING_INDEX_G30 N_COORDINATE_SYSTEM+1 // Home position 2 71 | // #define SETTING_INDEX_G92 N_COORDINATE_SYSTEM+2 // Coordinate offset (G92.2,G92.3 not supported) 72 | 73 | // Define Grbl axis settings numbering scheme. Starts at START_VAL, every INCREMENT, over N_SETTINGS. 74 | #define AXIS_N_SETTINGS 4 75 | #define AXIS_SETTINGS_START_VAL 100 // NOTE: Reserving settings values >= 100 for axis settings. Up to 255. 76 | #define AXIS_SETTINGS_INCREMENT 10 // Must be greater than the number of axis settings 77 | 78 | // Global persistent settings (Stored from byte EEPROM_ADDR_GLOBAL onwards) 79 | typedef struct { 80 | // Axis settings 81 | float steps_per_mm[N_AXIS]; 82 | float max_rate[N_AXIS]; 83 | float acceleration[N_AXIS]; 84 | float max_travel[N_AXIS]; 85 | 86 | // Remaining Grbl settings 87 | uint8_t pulse_microseconds; 88 | uint8_t step_invert_mask; 89 | uint8_t dir_invert_mask; 90 | uint8_t stepper_idle_lock_time; // If max value 255, steppers do not disable. 91 | uint8_t status_report_mask; // Mask to indicate desired report data. 92 | float junction_deviation; 93 | float arc_tolerance; 94 | 95 | uint8_t flags; // Contains default boolean settings 96 | 97 | uint8_t homing_dir_mask; 98 | float homing_feed_rate; 99 | float homing_seek_rate; 100 | uint16_t homing_debounce_delay; 101 | float homing_pulloff; 102 | } settings_t; 103 | extern settings_t settings; 104 | 105 | // Initialize the configuration subsystem (load settings from EEPROM) 106 | void settings_init(); 107 | 108 | // Helper function to clear and restore EEPROM defaults 109 | void settings_restore(uint8_t restore_flag); 110 | 111 | // A helper method to set new settings from command line 112 | uint8_t settings_store_global_setting(uint8_t parameter, float value); 113 | 114 | // Stores the protocol line variable as a startup line in EEPROM 115 | void settings_store_startup_line(uint8_t n, char *line); 116 | 117 | // Reads an EEPROM startup line to the protocol line variable 118 | uint8_t settings_read_startup_line(uint8_t n, char *line); 119 | 120 | // Stores build info user-defined string 121 | void settings_store_build_info(char *line); 122 | 123 | // Reads build info user-defined string 124 | uint8_t settings_read_build_info(char *line); 125 | 126 | // Writes selected coordinate data to EEPROM 127 | void settings_write_coord_data(uint8_t coord_select, float *coord_data); 128 | 129 | // Reads selected coordinate data from EEPROM 130 | uint8_t settings_read_coord_data(uint8_t coord_select, float *coord_data); 131 | 132 | // Returns the step pin mask according to Grbl's internal axis numbering 133 | uint8_t get_step_pin_mask(uint8_t i); 134 | 135 | // Returns the direction pin mask according to Grbl's internal axis numbering 136 | uint8_t get_direction_pin_mask(uint8_t i); 137 | 138 | // Returns the limit pin mask according to Grbl's internal axis numbering 139 | uint8_t get_limit_pin_mask(uint8_t i); 140 | 141 | 142 | #endif 143 | -------------------------------------------------------------------------------- /print.c: -------------------------------------------------------------------------------- 1 | /* 2 | print.c - Functions for formatting output strings 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #include "grbl.h" 23 | 24 | 25 | void printString(const char *s) 26 | { 27 | while (*s) 28 | serial_write(*s++); 29 | } 30 | 31 | 32 | // Print a string stored in PGM-memory 33 | void printPgmString(const char *s) 34 | { 35 | char c; 36 | while ((c = pgm_read_byte_near(s++))) 37 | serial_write(c); 38 | } 39 | 40 | 41 | // void printIntegerInBase(unsigned long n, unsigned long base) 42 | // { 43 | // unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. 44 | // unsigned long i = 0; 45 | // 46 | // if (n == 0) { 47 | // serial_write('0'); 48 | // return; 49 | // } 50 | // 51 | // while (n > 0) { 52 | // buf[i++] = n % base; 53 | // n /= base; 54 | // } 55 | // 56 | // for (; i > 0; i--) 57 | // serial_write(buf[i - 1] < 10 ? 58 | // '0' + buf[i - 1] : 59 | // 'A' + buf[i - 1] - 10); 60 | // } 61 | 62 | 63 | // Prints an uint8 variable with base and number of desired digits. 64 | void print_unsigned_int8(uint8_t n, uint8_t base, uint8_t digits) 65 | { 66 | unsigned char buf[digits]; 67 | uint8_t i = 0; 68 | 69 | for (; i < digits; i++) { 70 | buf[i] = n % base ; 71 | n /= base; 72 | } 73 | 74 | for (; i > 0; i--) 75 | serial_write('0' + buf[i - 1]); 76 | } 77 | 78 | 79 | // Prints an uint8 variable in base 2. 80 | void print_uint8_base2(uint8_t n) { 81 | print_unsigned_int8(n,2,8); 82 | } 83 | 84 | 85 | // Prints an uint8 variable in base 10. 86 | void print_uint8_base10(uint8_t n) 87 | { 88 | uint8_t digits; 89 | if (n < 10) { digits = 1; } 90 | else if (n < 100) { digits = 2; } 91 | else { digits = 3; } 92 | print_unsigned_int8(n,10,digits); 93 | } 94 | 95 | 96 | void print_uint32_base10(uint32_t n) 97 | { 98 | if (n == 0) { 99 | serial_write('0'); 100 | return; 101 | } 102 | 103 | unsigned char buf[10]; 104 | uint8_t i = 0; 105 | 106 | while (n > 0) { 107 | buf[i++] = n % 10; 108 | n /= 10; 109 | } 110 | 111 | for (; i > 0; i--) 112 | serial_write('0' + buf[i-1]); 113 | } 114 | 115 | 116 | void printInteger(long n) 117 | { 118 | if (n < 0) { 119 | serial_write('-'); 120 | print_uint32_base10(-n); 121 | } else { 122 | print_uint32_base10(n); 123 | } 124 | } 125 | 126 | 127 | // Convert float to string by immediately converting to a long integer, which contains 128 | // more digits than a float. Number of decimal places, which are tracked by a counter, 129 | // may be set by the user. The integer is then efficiently converted to a string. 130 | // NOTE: AVR '%' and '/' integer operations are very efficient. Bitshifting speed-up 131 | // techniques are actually just slightly slower. Found this out the hard way. 132 | void printFloat(float n, uint8_t decimal_places) 133 | { 134 | if (n < 0) { 135 | serial_write('-'); 136 | n = -n; 137 | } 138 | 139 | uint8_t decimals = decimal_places; 140 | while (decimals >= 2) { // Quickly convert values expected to be E0 to E-4. 141 | n *= 100; 142 | decimals -= 2; 143 | } 144 | if (decimals) { n *= 10; } 145 | n += 0.5; // Add rounding factor. Ensures carryover through entire value. 146 | 147 | // Generate digits backwards and store in string. 148 | unsigned char buf[10]; 149 | uint8_t i = 0; 150 | uint32_t a = (long)n; 151 | buf[decimal_places] = '.'; // Place decimal point, even if decimal places are zero. 152 | while(a > 0) { 153 | if (i == decimal_places) { i++; } // Skip decimal point location 154 | buf[i++] = (a % 10) + '0'; // Get digit 155 | a /= 10; 156 | } 157 | while (i < decimal_places) { 158 | buf[i++] = '0'; // Fill in zeros to decimal point for (n < 1) 159 | } 160 | if (i == decimal_places) { // Fill in leading zero, if needed. 161 | i++; 162 | buf[i++] = '0'; 163 | } 164 | 165 | // Print the generated string. 166 | for (; i > 0; i--) 167 | serial_write(buf[i-1]); 168 | } 169 | 170 | 171 | // Floating value printing handlers for special variables types used in Grbl and are defined 172 | // in the config.h. 173 | // - CoordValue: Handles all position or coordinate values in inches or mm reporting. 174 | // - RateValue: Handles feed rate and current velocity in inches or mm reporting. 175 | // - SettingValue: Handles all floating point settings values (always in mm.) 176 | void printFloat_CoordValue(float n) { 177 | if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { 178 | printFloat(n*INCH_PER_MM,N_DECIMAL_COORDVALUE_INCH); 179 | } else { 180 | printFloat(n,N_DECIMAL_COORDVALUE_MM); 181 | } 182 | } 183 | 184 | void printFloat_RateValue(float n) { 185 | if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { 186 | printFloat(n*INCH_PER_MM,N_DECIMAL_RATEVALUE_INCH); 187 | } else { 188 | printFloat(n,N_DECIMAL_RATEVALUE_MM); 189 | } 190 | } 191 | 192 | void printFloat_SettingValue(float n) { printFloat(n,N_DECIMAL_SETTINGVALUE); } 193 | 194 | 195 | // Debug tool to print free memory in bytes at the called point. 196 | // NOTE: Keep commented unless using. Part of this function always gets compiled in. 197 | // void printFreeMemory() 198 | // { 199 | // extern int __heap_start, *__brkval; 200 | // uint16_t free; // Up to 64k values. 201 | // free = (int) &free - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 202 | // printInteger((int32_t)free); 203 | // printString(" "); 204 | // } 205 | -------------------------------------------------------------------------------- /cpu_map/cpu_map_atmega328p.h: -------------------------------------------------------------------------------- 1 | /* 2 | cpu_map_atmega328p.h - CPU and pin mapping configuration file 3 | Part of Grbl 4 | 5 | Copyright (c) 2012-2015 Sungeun K. Jeon 6 | 7 | Grbl is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | Grbl is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with Grbl. If not, see . 19 | */ 20 | 21 | /* Grbl officially supports the Arduino Uno, but the other supplied pin mappings are 22 | supplied by users, so your results may vary. This cpu_map file serves as a central 23 | pin mapping settings file for AVR 328p used on the Arduino Uno. */ 24 | 25 | #ifdef GRBL_PLATFORM 26 | #error "cpu_map already defined: GRBL_PLATFORM=" GRBL_PLATFORM 27 | #endif 28 | 29 | 30 | #define GRBL_PLATFORM "Atmega328p" 31 | 32 | // Define serial port pins and interrupt vectors. 33 | #define SERIAL_RX USART_RX_vect 34 | #define SERIAL_UDRE USART_UDRE_vect 35 | 36 | // Define step pulse output pins. NOTE: All step bit pins must be on the same port. 37 | #define STEP_DDR DDRD 38 | #define STEP_PORT PORTD 39 | #define X_STEP_BIT 2 // Uno Digital Pin 2 40 | #define Y_STEP_BIT 3 // Uno Digital Pin 3 41 | #define Z_STEP_BIT 4 // Uno Digital Pin 4 42 | #define STEP_MASK ((1<. 20 | */ 21 | 22 | #include "grbl.h" 23 | 24 | 25 | uint8_t serial_rx_buffer[RX_BUFFER_SIZE]; 26 | uint8_t serial_rx_buffer_head = 0; 27 | volatile uint8_t serial_rx_buffer_tail = 0; 28 | 29 | uint8_t serial_tx_buffer[TX_BUFFER_SIZE]; 30 | uint8_t serial_tx_buffer_head = 0; 31 | volatile uint8_t serial_tx_buffer_tail = 0; 32 | 33 | 34 | #ifdef ENABLE_XONXOFF 35 | volatile uint8_t flow_ctrl = XON_SENT; // Flow control state variable 36 | #endif 37 | 38 | 39 | // Returns the number of bytes used in the RX serial buffer. 40 | uint8_t serial_get_rx_buffer_count() 41 | { 42 | uint8_t rtail = serial_rx_buffer_tail; // Copy to limit multiple calls to volatile 43 | if (serial_rx_buffer_head >= rtail) { return(serial_rx_buffer_head-rtail); } 44 | return (RX_BUFFER_SIZE - (rtail-serial_rx_buffer_head)); 45 | } 46 | 47 | 48 | // Returns the number of bytes used in the TX serial buffer. 49 | // NOTE: Not used except for debugging and ensuring no TX bottlenecks. 50 | uint8_t serial_get_tx_buffer_count() 51 | { 52 | uint8_t ttail = serial_tx_buffer_tail; // Copy to limit multiple calls to volatile 53 | if (serial_tx_buffer_head >= ttail) { return(serial_tx_buffer_head-ttail); } 54 | return (TX_BUFFER_SIZE - (ttail-serial_tx_buffer_head)); 55 | } 56 | 57 | 58 | void serial_init() 59 | { 60 | // Set baud rate 61 | #if BAUD_RATE < 57600 62 | uint16_t UBRR0_value = ((F_CPU / (8L * BAUD_RATE)) - 1)/2 ; 63 | UCSR0A &= ~(1 << U2X0); // baud doubler off - Only needed on Uno XXX 64 | #else 65 | uint16_t UBRR0_value = ((F_CPU / (4L * BAUD_RATE)) - 1)/2; 66 | UCSR0A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200 67 | #endif 68 | UBRR0H = UBRR0_value >> 8; 69 | UBRR0L = UBRR0_value; 70 | 71 | // enable rx and tx 72 | UCSR0B |= 1<= RX_BUFFER_FULL) && flow_ctrl == XON_SENT) { 183 | flow_ctrl = SEND_XOFF; 184 | UCSR0B |= (1 << UDRIE0); // Force TX 185 | } 186 | #endif 187 | 188 | } 189 | //TODO: else alarm on overflow? 190 | } 191 | } 192 | 193 | 194 | void serial_reset_read_buffer() 195 | { 196 | serial_rx_buffer_tail = serial_rx_buffer_head; 197 | 198 | #ifdef ENABLE_XONXOFF 199 | flow_ctrl = XON_SENT; 200 | #endif 201 | } 202 | -------------------------------------------------------------------------------- /gcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | gcode.h - rs274/ngc parser. 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #ifndef gcode_h 23 | #define gcode_h 24 | 25 | 26 | // Define modal group internal numbers for checking multiple command violations and tracking the 27 | // type of command that is called in the block. A modal group is a group of g-code commands that are 28 | // mutually exclusive, or cannot exist on the same line, because they each toggle a state or execute 29 | // a unique motion. These are defined in the NIST RS274-NGC v3 g-code standard, available online, 30 | // and are similar/identical to other g-code interpreters by manufacturers (Haas,Fanuc,Mazak,etc). 31 | // NOTE: Modal group define values must be sequential and starting from zero. 32 | #define MODAL_GROUP_G0 0 // [G4,G10,G28,G28.1,G30,G30.1,G53,G92,G92.1] Non-modal 33 | #define MODAL_GROUP_G1 1 // [G0,G1,G2,G3,G38.2,G38.3,G38.4,G38.5,G80] Motion 34 | #define MODAL_GROUP_G2 2 // [G17,G18,G19] Plane selection 35 | #define MODAL_GROUP_G3 3 // [G90,G91] Distance mode 36 | #define MODAL_GROUP_G4 4 // [G91.1] Arc IJK distance mode 37 | #define MODAL_GROUP_G5 5 // [G93,G94] Feed rate mode 38 | #define MODAL_GROUP_G6 6 // [G20,G21] Units 39 | #define MODAL_GROUP_G7 7 // [G40] Cutter radius compensation mode. G41/42 NOT SUPPORTED. 40 | #define MODAL_GROUP_G8 8 // [G43.1,G49] Tool length offset 41 | #define MODAL_GROUP_G12 9 // [G54,G55,G56,G57,G58,G59] Coordinate system selection 42 | #define MODAL_GROUP_G13 10 // [G61] Control mode 43 | 44 | #define MODAL_GROUP_M4 11 // [M0,M1,M2,M30] Stopping 45 | #define MODAL_GROUP_M7 12 // [M3,M4,M5] Spindle turning 46 | #define MODAL_GROUP_M8 13 // [M7,M8,M9] Coolant control 47 | 48 | // #define OTHER_INPUT_F 14 49 | // #define OTHER_INPUT_S 15 50 | // #define OTHER_INPUT_T 16 51 | 52 | // Define command actions for within execution-type modal groups (motion, stopping, non-modal). Used 53 | // internally by the parser to know which command to execute. 54 | 55 | // Modal Group G0: Non-modal actions 56 | #define NON_MODAL_NO_ACTION 0 // (Default: Must be zero) 57 | #define NON_MODAL_DWELL 1 // G4 58 | #define NON_MODAL_SET_COORDINATE_DATA 2 // G10 59 | #define NON_MODAL_GO_HOME_0 3 // G28 60 | #define NON_MODAL_SET_HOME_0 4 // G28.1 61 | #define NON_MODAL_GO_HOME_1 5 // G30 62 | #define NON_MODAL_SET_HOME_1 6 // G30.1 63 | #define NON_MODAL_ABSOLUTE_OVERRIDE 7 // G53 64 | #define NON_MODAL_SET_COORDINATE_OFFSET 8 // G92 65 | #define NON_MODAL_RESET_COORDINATE_OFFSET 9 //G92.1 66 | 67 | // Modal Group G1: Motion modes 68 | #define MOTION_MODE_SEEK 0 // G0 (Default: Must be zero) 69 | #define MOTION_MODE_LINEAR 1 // G1 70 | #define MOTION_MODE_CW_ARC 2 // G2 71 | #define MOTION_MODE_CCW_ARC 3 // G3 72 | #define MOTION_MODE_PROBE_TOWARD 4 // G38.2 NOTE: G38.2, G38.3, G38.4, G38.5 must be sequential. See report_gcode_modes(). 73 | #define MOTION_MODE_PROBE_TOWARD_NO_ERROR 5 // G38.3 74 | #define MOTION_MODE_PROBE_AWAY 6 // G38.4 75 | #define MOTION_MODE_PROBE_AWAY_NO_ERROR 7 // G38.5 76 | #define MOTION_MODE_NONE 8 // G80 77 | 78 | // Modal Group G2: Plane select 79 | #define PLANE_SELECT_XY 0 // G17 (Default: Must be zero) 80 | #define PLANE_SELECT_ZX 1 // G18 81 | #define PLANE_SELECT_YZ 2 // G19 82 | 83 | // Modal Group G3: Distance mode 84 | #define DISTANCE_MODE_ABSOLUTE 0 // G90 (Default: Must be zero) 85 | #define DISTANCE_MODE_INCREMENTAL 1 // G91 86 | 87 | // Modal Group G4: Arc IJK distance mode 88 | #define DISTANCE_ARC_MODE_INCREMENTAL 0 // G91.1 (Default: Must be zero) 89 | 90 | // Modal Group M4: Program flow 91 | #define PROGRAM_FLOW_RUNNING 0 // (Default: Must be zero) 92 | #define PROGRAM_FLOW_PAUSED 1 // M0, M1 93 | #define PROGRAM_FLOW_COMPLETED 2 // M2, M30 94 | 95 | // Modal Group G5: Feed rate mode 96 | #define FEED_RATE_MODE_UNITS_PER_MIN 0 // G94 (Default: Must be zero) 97 | #define FEED_RATE_MODE_INVERSE_TIME 1 // G93 98 | 99 | // Modal Group G6: Units mode 100 | #define UNITS_MODE_MM 0 // G21 (Default: Must be zero) 101 | #define UNITS_MODE_INCHES 1 // G20 102 | 103 | // Modal Group G7: Cutter radius compensation mode 104 | #define CUTTER_COMP_DISABLE 0 // G40 (Default: Must be zero) 105 | 106 | // Modal Group G13: Control mode 107 | #define CONTROL_MODE_EXACT_PATH 0 // G61 (Default: Must be zero) 108 | 109 | // Modal Group M7: Spindle control 110 | #define SPINDLE_DISABLE 0 // M5 (Default: Must be zero) 111 | #define SPINDLE_ENABLE_CW 1 // M3 112 | #define SPINDLE_ENABLE_CCW 2 // M4 113 | 114 | // Modal Group M8: Coolant control 115 | #define COOLANT_DISABLE 0 // M9 (Default: Must be zero) 116 | #define COOLANT_MIST_ENABLE 1 // M7 117 | #define COOLANT_FLOOD_ENABLE 2 // M8 118 | 119 | // Modal Group G8: Tool length offset 120 | #define TOOL_LENGTH_OFFSET_CANCEL 0 // G49 (Default: Must be zero) 121 | #define TOOL_LENGTH_OFFSET_ENABLE_DYNAMIC 1 // G43.1 122 | 123 | // Modal Group G12: Active work coordinate system 124 | // N/A: Stores coordinate system value (54-59) to change to. 125 | 126 | 127 | // Define parameter word mapping. 128 | #define WORD_F 0 129 | #define WORD_I 1 130 | #define WORD_J 2 131 | #define WORD_K 3 132 | #define WORD_L 4 133 | #define WORD_N 5 134 | #define WORD_P 6 135 | #define WORD_R 7 136 | #define WORD_S 8 137 | #define WORD_T 9 138 | #define WORD_X 10 139 | #define WORD_Y 11 140 | #define WORD_Z 12 141 | 142 | 143 | // NOTE: When this struct is zeroed, the above defines set the defaults for the system. 144 | typedef struct { 145 | uint8_t motion; // {G0,G1,G2,G3,G38.2,G80} 146 | uint8_t feed_rate; // {G93,G94} 147 | uint8_t units; // {G20,G21} 148 | uint8_t distance; // {G90,G91} 149 | // uint8_t distance_arc; // {G91.1} NOTE: Don't track. Only default supported. 150 | uint8_t plane_select; // {G17,G18,G19} 151 | // uint8_t cutter_comp; // {G40} NOTE: Don't track. Only default supported. 152 | uint8_t tool_length; // {G43.1,G49} 153 | uint8_t coord_select; // {G54,G55,G56,G57,G58,G59} 154 | // uint8_t control; // {G61} NOTE: Don't track. Only default supported. 155 | uint8_t program_flow; // {M0,M1,M2,M30} 156 | uint8_t coolant; // {M7,M8,M9} 157 | uint8_t spindle; // {M3,M4,M5} 158 | } gc_modal_t; 159 | 160 | typedef struct { 161 | float f; // Feed 162 | float ijk[3]; // I,J,K Axis arc offsets 163 | uint8_t l; // G10 or canned cycles parameters 164 | int32_t n; // Line number 165 | float p; // G10 or dwell parameters 166 | // float q; // G82 peck drilling 167 | float r; // Arc radius 168 | float s; // Spindle speed 169 | uint8_t t; // Tool selection 170 | float xyz[3]; // X,Y,Z Translational axes 171 | } gc_values_t; 172 | 173 | 174 | typedef struct { 175 | gc_modal_t modal; 176 | 177 | float spindle_speed; // RPM 178 | float feed_rate; // Millimeters/min 179 | uint8_t tool; // Tracks tool number. NOT USED. 180 | int32_t line_number; // Last line number sent 181 | 182 | float position[N_AXIS]; // Where the interpreter considers the tool to be at this point in the code 183 | 184 | float coord_system[N_AXIS]; // Current work coordinate system (G54+). Stores offset from absolute machine 185 | // position in mm. Loaded from EEPROM when called. 186 | float coord_offset[N_AXIS]; // Retains the G92 coordinate offset (work coordinates) relative to 187 | // machine zero in mm. Non-persistent. Cleared upon reset and boot. 188 | float tool_length_offset; // Tracks tool length offset value when enabled. 189 | } parser_state_t; 190 | extern parser_state_t gc_state; 191 | 192 | typedef struct { 193 | // uint16_t command_words; // NOTE: If this bitflag variable fills, G and M words can be separated. 194 | // uint16_t value_words; 195 | 196 | uint8_t non_modal_command; 197 | gc_modal_t modal; 198 | gc_values_t values; 199 | 200 | } parser_block_t; 201 | extern parser_block_t gc_block; 202 | 203 | // Initialize the parser 204 | void gc_init(); 205 | 206 | // Execute one block of rs275/ngc/g-code 207 | uint8_t gc_execute_line(char *line); 208 | 209 | // Set g-code parser position. Input in steps. 210 | void gc_sync_position(); 211 | 212 | #endif 213 | -------------------------------------------------------------------------------- /settings.c: -------------------------------------------------------------------------------- 1 | /* 2 | settings.c - eeprom configuration handling 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | 8 | Grbl is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Grbl is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Grbl. If not, see . 20 | */ 21 | 22 | #include "grbl.h" 23 | 24 | settings_t settings; 25 | 26 | 27 | // Method to store startup lines into EEPROM 28 | void settings_store_startup_line(uint8_t n, char *line) 29 | { 30 | uint32_t addr = n*(LINE_BUFFER_SIZE+1)+EEPROM_ADDR_STARTUP_BLOCK; 31 | memcpy_to_eeprom_with_checksum(addr,(char*)line, LINE_BUFFER_SIZE); 32 | } 33 | 34 | 35 | // Method to store build info into EEPROM 36 | void settings_store_build_info(char *line) 37 | { 38 | memcpy_to_eeprom_with_checksum(EEPROM_ADDR_BUILD_INFO,(char*)line, LINE_BUFFER_SIZE); 39 | } 40 | 41 | 42 | // Method to store coord data parameters into EEPROM 43 | void settings_write_coord_data(uint8_t coord_select, float *coord_data) 44 | { 45 | uint32_t addr = coord_select*(sizeof(float)*N_AXIS+1) + EEPROM_ADDR_PARAMETERS; 46 | memcpy_to_eeprom_with_checksum(addr,(char*)coord_data, sizeof(float)*N_AXIS); 47 | } 48 | 49 | 50 | // Method to store Grbl global settings struct and version number into EEPROM 51 | void write_global_settings() 52 | { 53 | eeprom_put_char(0, SETTINGS_VERSION); 54 | memcpy_to_eeprom_with_checksum(EEPROM_ADDR_GLOBAL, (char*)&settings, sizeof(settings_t)); 55 | } 56 | 57 | 58 | // Method to restore EEPROM-saved Grbl global settings back to defaults. 59 | void settings_restore(uint8_t restore_flag) { 60 | if (restore_flag & SETTINGS_RESTORE_DEFAULTS) { 61 | settings.pulse_microseconds = DEFAULT_STEP_PULSE_MICROSECONDS; 62 | settings.stepper_idle_lock_time = DEFAULT_STEPPER_IDLE_LOCK_TIME; 63 | settings.step_invert_mask = DEFAULT_STEPPING_INVERT_MASK; 64 | settings.dir_invert_mask = DEFAULT_DIRECTION_INVERT_MASK; 65 | settings.status_report_mask = DEFAULT_STATUS_REPORT_MASK; 66 | settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION; 67 | settings.arc_tolerance = DEFAULT_ARC_TOLERANCE; 68 | settings.homing_dir_mask = DEFAULT_HOMING_DIR_MASK; 69 | settings.homing_feed_rate = DEFAULT_HOMING_FEED_RATE; 70 | settings.homing_seek_rate = DEFAULT_HOMING_SEEK_RATE; 71 | settings.homing_debounce_delay = DEFAULT_HOMING_DEBOUNCE_DELAY; 72 | settings.homing_pulloff = DEFAULT_HOMING_PULLOFF; 73 | 74 | settings.flags = 0; 75 | if (DEFAULT_REPORT_INCHES) { settings.flags |= BITFLAG_REPORT_INCHES; } 76 | if (DEFAULT_INVERT_ST_ENABLE) { settings.flags |= BITFLAG_INVERT_ST_ENABLE; } 77 | if (DEFAULT_INVERT_LIMIT_PINS) { settings.flags |= BITFLAG_INVERT_LIMIT_PINS; } 78 | if (DEFAULT_SOFT_LIMIT_ENABLE) { settings.flags |= BITFLAG_SOFT_LIMIT_ENABLE; } 79 | if (DEFAULT_HARD_LIMIT_ENABLE) { settings.flags |= BITFLAG_HARD_LIMIT_ENABLE; } 80 | if (DEFAULT_HOMING_ENABLE) { settings.flags |= BITFLAG_HOMING_ENABLE; } 81 | 82 | settings.steps_per_mm[X_AXIS] = DEFAULT_X_STEPS_PER_MM; 83 | settings.steps_per_mm[Y_AXIS] = DEFAULT_Y_STEPS_PER_MM; 84 | settings.steps_per_mm[Z_AXIS] = DEFAULT_Z_STEPS_PER_MM; 85 | settings.max_rate[X_AXIS] = DEFAULT_X_MAX_RATE; 86 | settings.max_rate[Y_AXIS] = DEFAULT_Y_MAX_RATE; 87 | settings.max_rate[Z_AXIS] = DEFAULT_Z_MAX_RATE; 88 | settings.acceleration[X_AXIS] = DEFAULT_X_ACCELERATION; 89 | settings.acceleration[Y_AXIS] = DEFAULT_Y_ACCELERATION; 90 | settings.acceleration[Z_AXIS] = DEFAULT_Z_ACCELERATION; 91 | settings.max_travel[X_AXIS] = (-DEFAULT_X_MAX_TRAVEL); 92 | settings.max_travel[Y_AXIS] = (-DEFAULT_Y_MAX_TRAVEL); 93 | settings.max_travel[Z_AXIS] = (-DEFAULT_Z_MAX_TRAVEL); 94 | 95 | write_global_settings(); 96 | } 97 | 98 | if (restore_flag & SETTINGS_RESTORE_PARAMETERS) { 99 | uint8_t idx; 100 | float coord_data[N_AXIS]; 101 | memset(&coord_data, 0, sizeof(coord_data)); 102 | for (idx=0; idx <= SETTING_INDEX_NCOORD; idx++) { settings_write_coord_data(idx, coord_data); } 103 | } 104 | 105 | if (restore_flag & SETTINGS_RESTORE_STARTUP_LINES) { 106 | #if N_STARTUP_LINE > 0 107 | eeprom_put_char(EEPROM_ADDR_STARTUP_BLOCK, 0); 108 | #endif 109 | #if N_STARTUP_LINE > 1 110 | eeprom_put_char(EEPROM_ADDR_STARTUP_BLOCK+(LINE_BUFFER_SIZE+1), 0); 111 | #endif 112 | } 113 | 114 | if (restore_flag & SETTINGS_RESTORE_BUILD_INFO) { eeprom_put_char(EEPROM_ADDR_BUILD_INFO , 0); } 115 | } 116 | 117 | 118 | // Reads startup line from EEPROM. Updated pointed line string data. 119 | uint8_t settings_read_startup_line(uint8_t n, char *line) 120 | { 121 | uint32_t addr = n*(LINE_BUFFER_SIZE+1)+EEPROM_ADDR_STARTUP_BLOCK; 122 | if (!(memcpy_from_eeprom_with_checksum((char*)line, addr, LINE_BUFFER_SIZE))) { 123 | // Reset line with default value 124 | line[0] = 0; // Empty line 125 | settings_store_startup_line(n, line); 126 | return(false); 127 | } 128 | return(true); 129 | } 130 | 131 | 132 | // Reads startup line from EEPROM. Updated pointed line string data. 133 | uint8_t settings_read_build_info(char *line) 134 | { 135 | if (!(memcpy_from_eeprom_with_checksum((char*)line, EEPROM_ADDR_BUILD_INFO, LINE_BUFFER_SIZE))) { 136 | // Reset line with default value 137 | line[0] = 0; // Empty line 138 | settings_store_build_info(line); 139 | return(false); 140 | } 141 | return(true); 142 | } 143 | 144 | 145 | // Read selected coordinate data from EEPROM. Updates pointed coord_data value. 146 | uint8_t settings_read_coord_data(uint8_t coord_select, float *coord_data) 147 | { 148 | uint32_t addr = coord_select*(sizeof(float)*N_AXIS+1) + EEPROM_ADDR_PARAMETERS; 149 | if (!(memcpy_from_eeprom_with_checksum((char*)coord_data, addr, sizeof(float)*N_AXIS))) { 150 | // Reset with default zero vector 151 | clear_vector_float(coord_data); 152 | settings_write_coord_data(coord_select,coord_data); 153 | return(false); 154 | } 155 | return(true); 156 | } 157 | 158 | 159 | // Reads Grbl global settings struct from EEPROM. 160 | uint8_t read_global_settings() { 161 | // Check version-byte of eeprom 162 | uint8_t version = eeprom_get_char(0); 163 | if (version == SETTINGS_VERSION) { 164 | // Read settings-record and check checksum 165 | if (!(memcpy_from_eeprom_with_checksum((char*)&settings, EEPROM_ADDR_GLOBAL, sizeof(settings_t)))) { 166 | return(false); 167 | } 168 | } else { 169 | return(false); 170 | } 171 | return(true); 172 | } 173 | 174 | 175 | // A helper method to set settings from command line 176 | uint8_t settings_store_global_setting(uint8_t parameter, float value) { 177 | if (value < 0.0) { return(STATUS_NEGATIVE_VALUE); } 178 | if (parameter >= AXIS_SETTINGS_START_VAL) { 179 | // Store axis configuration. Axis numbering sequence set by AXIS_SETTING defines. 180 | // NOTE: Ensure the setting index corresponds to the report.c settings printout. 181 | parameter -= AXIS_SETTINGS_START_VAL; 182 | uint8_t set_idx = 0; 183 | while (set_idx < AXIS_N_SETTINGS) { 184 | if (parameter < N_AXIS) { 185 | // Valid axis setting found. 186 | switch (set_idx) { 187 | case 0: 188 | #ifdef MAX_STEP_RATE_HZ 189 | if (value*settings.max_rate[parameter] > (MAX_STEP_RATE_HZ*60.0)) { return(STATUS_MAX_STEP_RATE_EXCEEDED); } 190 | #endif 191 | settings.steps_per_mm[parameter] = value; 192 | break; 193 | case 1: 194 | #ifdef MAX_STEP_RATE_HZ 195 | if (value*settings.steps_per_mm[parameter] > (MAX_STEP_RATE_HZ*60.0)) { return(STATUS_MAX_STEP_RATE_EXCEEDED); } 196 | #endif 197 | settings.max_rate[parameter] = value; 198 | break; 199 | case 2: settings.acceleration[parameter] = value*60*60; break; // Convert to mm/min^2 for grbl internal use. 200 | case 3: settings.max_travel[parameter] = -value; break; // Store as negative for grbl internal use. 201 | } 202 | break; // Exit while-loop after setting has been configured and proceed to the EEPROM write call. 203 | } else { 204 | set_idx++; 205 | // If axis index greater than N_AXIS or setting index greater than number of axis settings, error out. 206 | if ((parameter < AXIS_SETTINGS_INCREMENT) || (set_idx == AXIS_N_SETTINGS)) { return(STATUS_INVALID_STATEMENT); } 207 | parameter -= AXIS_SETTINGS_INCREMENT; 208 | } 209 | } 210 | } else { 211 | // Store non-axis Grbl settings 212 | uint8_t int_value = trunc(value); 213 | switch(parameter) { 214 | case 0: 215 | if (int_value < 3) { return(STATUS_SETTING_STEP_PULSE_MIN); } 216 | settings.pulse_microseconds = int_value; break; 217 | case 1: settings.stepper_idle_lock_time = int_value; break; 218 | case 2: 219 | settings.step_invert_mask = int_value; 220 | st_generate_step_dir_invert_masks(); // Regenerate step and direction port invert masks. 221 | break; 222 | case 3: 223 | settings.dir_invert_mask = int_value; 224 | st_generate_step_dir_invert_masks(); // Regenerate step and direction port invert masks. 225 | break; 226 | case 4: // Reset to ensure change. Immediate re-init may cause problems. 227 | if (int_value) { settings.flags |= BITFLAG_INVERT_ST_ENABLE; } 228 | else { settings.flags &= ~BITFLAG_INVERT_ST_ENABLE; } 229 | break; 230 | case 5: // Reset to ensure change. Immediate re-init may cause problems. 231 | if (int_value) { settings.flags |= BITFLAG_INVERT_LIMIT_PINS; } 232 | else { settings.flags &= ~BITFLAG_INVERT_LIMIT_PINS; } 233 | break; 234 | case 6: // Reset to ensure change. Immediate re-init may cause problems. 235 | if (int_value) { settings.flags |= BITFLAG_INVERT_PROBE_PIN; } 236 | else { settings.flags &= ~BITFLAG_INVERT_PROBE_PIN; } 237 | break; 238 | case 10: settings.status_report_mask = int_value; break; 239 | case 11: settings.junction_deviation = value; break; 240 | case 12: settings.arc_tolerance = value; break; 241 | case 13: 242 | if (int_value) { settings.flags |= BITFLAG_REPORT_INCHES; } 243 | else { settings.flags &= ~BITFLAG_REPORT_INCHES; } 244 | break; 245 | case 20: 246 | if (int_value) { 247 | if (bit_isfalse(settings.flags, BITFLAG_HOMING_ENABLE)) { return(STATUS_SOFT_LIMIT_ERROR); } 248 | settings.flags |= BITFLAG_SOFT_LIMIT_ENABLE; 249 | } else { settings.flags &= ~BITFLAG_SOFT_LIMIT_ENABLE; } 250 | break; 251 | case 21: 252 | if (int_value) { settings.flags |= BITFLAG_HARD_LIMIT_ENABLE; } 253 | else { settings.flags &= ~BITFLAG_HARD_LIMIT_ENABLE; } 254 | limits_init(); // Re-init to immediately change. NOTE: Nice to have but could be problematic later. 255 | break; 256 | case 22: 257 | if (int_value) { settings.flags |= BITFLAG_HOMING_ENABLE; } 258 | else { 259 | settings.flags &= ~BITFLAG_HOMING_ENABLE; 260 | settings.flags &= ~BITFLAG_SOFT_LIMIT_ENABLE; // Force disable soft-limits. 261 | } 262 | break; 263 | case 23: settings.homing_dir_mask = int_value; break; 264 | case 24: settings.homing_feed_rate = value; break; 265 | case 25: settings.homing_seek_rate = value; break; 266 | case 26: settings.homing_debounce_delay = int_value; break; 267 | case 27: settings.homing_pulloff = value; break; 268 | default: 269 | return(STATUS_INVALID_STATEMENT); 270 | } 271 | } 272 | write_global_settings(); 273 | return(STATUS_OK); 274 | } 275 | 276 | 277 | // Initialize the config subsystem 278 | void settings_init() { 279 | if(!read_global_settings()) { 280 | report_status_message(STATUS_SETTING_READ_FAIL); 281 | settings_restore(SETTINGS_RESTORE_ALL); // Force restore all EEPROM data. 282 | report_grbl_settings(); 283 | } 284 | 285 | // NOTE: Checking paramater data, startup lines, and build info string should be done here, 286 | // but it seems fairly redundant. Each of these can be manually checked and reset or restored. 287 | // Check all parameter data into a dummy variable. If error, reset to zero, otherwise do nothing. 288 | // float coord_data[N_AXIS]; 289 | // uint8_t i; 290 | // for (i=0; i<=SETTING_INDEX_NCOORD; i++) { 291 | // if (!settings_read_coord_data(i, coord_data)) { 292 | // report_status_message(STATUS_SETTING_READ_FAIL); 293 | // } 294 | // } 295 | // NOTE: Startup lines are checked and executed by protocol_main_loop at the end of initialization. 296 | } 297 | 298 | 299 | // Returns step pin mask according to Grbl internal axis indexing. 300 | uint8_t get_step_pin_mask(uint8_t axis_idx) 301 | { 302 | if ( axis_idx == X_AXIS ) { return((1<. 19 | */ 20 | 21 | #include "grbl.h" 22 | 23 | 24 | void system_init() 25 | { 26 | CONTROL_DDR &= ~(CONTROL_MASK); // Configure as input pins 27 | #ifdef DISABLE_CONTROL_PIN_PULL_UP 28 | CONTROL_PORT &= ~(CONTROL_MASK); // Normal low operation. Requires external pull-down. 29 | #else 30 | CONTROL_PORT |= CONTROL_MASK; // Enable internal pull-up resistors. Normal high operation. 31 | #endif 32 | CONTROL_PCMSK |= CONTROL_MASK; // Enable specific pins of the Pin Change Interrupt 33 | PCICR |= (1 << CONTROL_INT); // Enable Pin Change Interrupt 34 | } 35 | 36 | 37 | // Pin change interrupt for pin-out commands, i.e. cycle start, feed hold, and reset. Sets 38 | // only the realtime command execute variable to have the main program execute these when 39 | // its ready. This works exactly like the character-based realtime commands when picked off 40 | // directly from the incoming serial data stream. 41 | ISR(CONTROL_INT_vect) 42 | { 43 | uint8_t pin = (CONTROL_PIN & CONTROL_MASK); 44 | #ifndef INVERT_CONTROL_PIN 45 | pin ^= CONTROL_MASK; 46 | #endif 47 | // Enter only if any CONTROL pin is detected as active. 48 | if (pin) { 49 | if (bit_istrue(pin,bit(RESET_BIT))) { 50 | mc_reset(); 51 | } else if (bit_istrue(pin,bit(CYCLE_START_BIT))) { 52 | bit_true(sys.rt_exec_state, EXEC_CYCLE_START); 53 | #ifndef ENABLE_SAFETY_DOOR_INPUT_PIN 54 | } else if (bit_istrue(pin,bit(FEED_HOLD_BIT))) { 55 | bit_true(sys.rt_exec_state, EXEC_FEED_HOLD); 56 | #else 57 | } else if (bit_istrue(pin,bit(SAFETY_DOOR_BIT))) { 58 | bit_true(sys.rt_exec_state, EXEC_SAFETY_DOOR); 59 | #endif 60 | } 61 | } 62 | } 63 | 64 | 65 | // Returns if safety door is ajar(T) or closed(F), based on pin state. 66 | uint8_t system_check_safety_door_ajar() 67 | { 68 | #ifdef ENABLE_SAFETY_DOOR_INPUT_PIN 69 | #ifdef INVERT_CONTROL_PIN 70 | return(bit_istrue(CONTROL_PIN,bit(SAFETY_DOOR_BIT))); 71 | #else 72 | return(bit_isfalse(CONTROL_PIN,bit(SAFETY_DOOR_BIT))); 73 | #endif 74 | #else 75 | return(false); // Input pin not enabled, so just return that it's closed. 76 | #endif 77 | } 78 | 79 | 80 | // Executes user startup script, if stored. 81 | void system_execute_startup(char *line) 82 | { 83 | uint8_t n; 84 | for (n=0; n < N_STARTUP_LINE; n++) { 85 | if (!(settings_read_startup_line(n, line))) { 86 | report_status_message(STATUS_SETTING_READ_FAIL); 87 | } else { 88 | if (line[0] != 0) { 89 | printString(line); // Echo startup line to indicate execution. 90 | report_status_message(gc_execute_line(line)); 91 | } 92 | } 93 | } 94 | } 95 | 96 | 97 | // Directs and executes one line of formatted input from protocol_process. While mostly 98 | // incoming streaming g-code blocks, this also executes Grbl internal commands, such as 99 | // settings, initiating the homing cycle, and toggling switch states. This differs from 100 | // the realtime command module by being susceptible to when Grbl is ready to execute the 101 | // next line during a cycle, so for switches like block delete, the switch only effects 102 | // the lines that are processed afterward, not necessarily real-time during a cycle, 103 | // since there are motions already stored in the buffer. However, this 'lag' should not 104 | // be an issue, since these commands are not typically used during a cycle. 105 | uint8_t system_execute_line(char *line) 106 | { 107 | uint8_t char_counter = 1; 108 | uint8_t helper_var = 0; // Helper variable 109 | float parameter, value; 110 | switch( line[char_counter] ) { 111 | case 0 : report_grbl_help(); break; 112 | case '$': case 'G': case 'C': case 'X': 113 | if ( line[(char_counter+1)] != 0 ) { return(STATUS_INVALID_STATEMENT); } 114 | switch( line[char_counter] ) { 115 | case '$' : // Prints Grbl settings 116 | if ( sys.state & (STATE_CYCLE | STATE_HOLD) ) { return(STATUS_IDLE_ERROR); } // Block during cycle. Takes too long to print. 117 | else { report_grbl_settings(); } 118 | break; 119 | case 'G' : // Prints gcode parser state 120 | // TODO: Move this to realtime commands for GUIs to request this data during suspend-state. 121 | report_gcode_modes(); 122 | break; 123 | case 'C' : // Set check g-code mode [IDLE/CHECK] 124 | // Perform reset when toggling off. Check g-code mode should only work if Grbl 125 | // is idle and ready, regardless of alarm locks. This is mainly to keep things 126 | // simple and consistent. 127 | if ( sys.state == STATE_CHECK_MODE ) { 128 | mc_reset(); 129 | report_feedback_message(MESSAGE_DISABLED); 130 | } else { 131 | if (sys.state) { return(STATUS_IDLE_ERROR); } // Requires no alarm mode. 132 | sys.state = STATE_CHECK_MODE; 133 | report_feedback_message(MESSAGE_ENABLED); 134 | } 135 | break; 136 | case 'X' : // Disable alarm lock [ALARM] 137 | if (sys.state == STATE_ALARM) { 138 | report_feedback_message(MESSAGE_ALARM_UNLOCK); 139 | sys.state = STATE_IDLE; 140 | // Don't run startup script. Prevents stored moves in startup from causing accidents. 141 | if (system_check_safety_door_ajar()) { // Check safety door switch before returning. 142 | bit_true(sys.rt_exec_state, EXEC_SAFETY_DOOR); 143 | protocol_execute_realtime(); // Enter safety door mode. 144 | } 145 | } // Otherwise, no effect. 146 | break; 147 | // case 'J' : break; // Jogging methods 148 | // TODO: Here jogging can be placed for execution as a seperate subprogram. It does not need to be 149 | // susceptible to other realtime commands except for e-stop. The jogging function is intended to 150 | // be a basic toggle on/off with controlled acceleration and deceleration to prevent skipped 151 | // steps. The user would supply the desired feedrate, axis to move, and direction. Toggle on would 152 | // start motion and toggle off would initiate a deceleration to stop. One could 'feather' the 153 | // motion by repeatedly toggling to slow the motion to the desired location. Location data would 154 | // need to be updated real-time and supplied to the user through status queries. 155 | // More controlled exact motions can be taken care of by inputting G0 or G1 commands, which are 156 | // handled by the planner. It would be possible for the jog subprogram to insert blocks into the 157 | // block buffer without having the planner plan them. It would need to manage de/ac-celerations 158 | // on its own carefully. This approach could be effective and possibly size/memory efficient. 159 | // } 160 | // break; 161 | } 162 | break; 163 | default : 164 | // Block any system command that requires the state as IDLE/ALARM. (i.e. EEPROM, homing) 165 | if ( !(sys.state == STATE_IDLE || sys.state == STATE_ALARM) ) { return(STATUS_IDLE_ERROR); } 166 | switch( line[char_counter] ) { 167 | case '#' : // Print Grbl NGC parameters 168 | if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } 169 | else { report_ngc_parameters(); } 170 | break; 171 | case 'H' : // Perform homing cycle [IDLE/ALARM] 172 | if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { 173 | sys.state = STATE_HOMING; // Set system state variable 174 | // Only perform homing if Grbl is idle or lost. 175 | 176 | // TODO: Likely not required. 177 | if (system_check_safety_door_ajar()) { // Check safety door switch before homing. 178 | bit_true(sys.rt_exec_state, EXEC_SAFETY_DOOR); 179 | protocol_execute_realtime(); // Enter safety door mode. 180 | } 181 | 182 | 183 | mc_homing_cycle(); 184 | if (!sys.abort) { // Execute startup scripts after successful homing. 185 | sys.state = STATE_IDLE; // Set to IDLE when complete. 186 | st_go_idle(); // Set steppers to the settings idle state before returning. 187 | system_execute_startup(line); 188 | } 189 | } else { return(STATUS_SETTING_DISABLED); } 190 | break; 191 | case 'I' : // Print or store build info. [IDLE/ALARM] 192 | if ( line[++char_counter] == 0 ) { 193 | settings_read_build_info(line); 194 | report_build_info(line); 195 | } else { // Store startup line [IDLE/ALARM] 196 | if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); } 197 | helper_var = char_counter; // Set helper variable as counter to start of user info line. 198 | do { 199 | line[char_counter-helper_var] = line[char_counter]; 200 | } while (line[char_counter++] != 0); 201 | settings_store_build_info(line); 202 | } 203 | break; 204 | case 'R' : // Restore defaults [IDLE/ALARM] 205 | if (line[++char_counter] != 'S') { return(STATUS_INVALID_STATEMENT); } 206 | if (line[++char_counter] != 'T') { return(STATUS_INVALID_STATEMENT); } 207 | if (line[++char_counter] != '=') { return(STATUS_INVALID_STATEMENT); } 208 | if (line[char_counter+2] != 0) { return(STATUS_INVALID_STATEMENT); } 209 | switch (line[++char_counter]) { 210 | case '$': settings_restore(SETTINGS_RESTORE_DEFAULTS); break; 211 | case '#': settings_restore(SETTINGS_RESTORE_PARAMETERS); break; 212 | case '*': settings_restore(SETTINGS_RESTORE_ALL); break; 213 | default: return(STATUS_INVALID_STATEMENT); 214 | } 215 | report_feedback_message(MESSAGE_RESTORE_DEFAULTS); 216 | mc_reset(); // Force reset to ensure settings are initialized correctly. 217 | break; 218 | case 'N' : // Startup lines. [IDLE/ALARM] 219 | if ( line[++char_counter] == 0 ) { // Print startup lines 220 | for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) { 221 | if (!(settings_read_startup_line(helper_var, line))) { 222 | report_status_message(STATUS_SETTING_READ_FAIL); 223 | } else { 224 | report_startup_line(helper_var,line); 225 | } 226 | } 227 | break; 228 | } else { // Store startup line [IDLE Only] Prevents motion during ALARM. 229 | if (sys.state != STATE_IDLE) { return(STATUS_IDLE_ERROR); } // Store only when idle. 230 | helper_var = true; // Set helper_var to flag storing method. 231 | // No break. Continues into default: to read remaining command characters. 232 | } 233 | default : // Storing setting methods [IDLE/ALARM] 234 | if(!read_float(line, &char_counter, ¶meter)) { return(STATUS_BAD_NUMBER_FORMAT); } 235 | if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); } 236 | if (helper_var) { // Store startup line 237 | // Prepare sending gcode block to gcode parser by shifting all characters 238 | helper_var = char_counter; // Set helper variable as counter to start of gcode block 239 | do { 240 | line[char_counter-helper_var] = line[char_counter]; 241 | } while (line[char_counter++] != 0); 242 | // Execute gcode block to ensure block is valid. 243 | helper_var = gc_execute_line(line); // Set helper_var to returned status code. 244 | if (helper_var) { return(helper_var); } 245 | else { 246 | helper_var = trunc(parameter); // Set helper_var to int value of parameter 247 | settings_store_startup_line(helper_var,line); 248 | } 249 | } else { // Store global setting. 250 | if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); } 251 | if((line[char_counter] != 0) || (parameter > 255)) { return(STATUS_INVALID_STATEMENT); } 252 | return(settings_store_global_setting((uint8_t)parameter, value)); 253 | } 254 | } 255 | } 256 | return(STATUS_OK); // If '$' command makes it to here, then everything's ok. 257 | } 258 | 259 | 260 | // Returns machine position of axis 'idx'. Must be sent a 'step' array. 261 | // NOTE: If motor steps and machine position are not in the same coordinate frame, this function 262 | // serves as a central place to compute the transformation. 263 | float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx) 264 | { 265 | float pos; 266 | #ifdef COREXY 267 | if (idx==A_MOTOR) { 268 | pos = 0.5*((steps[A_MOTOR] + steps[B_MOTOR])/settings.steps_per_mm[idx]); 269 | } else if (idx==B_MOTOR) { 270 | pos = 0.5*((steps[A_MOTOR] - steps[B_MOTOR])/settings.steps_per_mm[idx]); 271 | } else { 272 | pos = steps[idx]/settings.steps_per_mm[idx]; 273 | } 274 | #else 275 | pos = steps[idx]/settings.steps_per_mm[idx]; 276 | #endif 277 | return(pos); 278 | } 279 | 280 | 281 | void system_convert_array_steps_to_mpos(float *position, int32_t *steps) 282 | { 283 | uint8_t idx; 284 | for (idx=0; idx. 20 | */ 21 | 22 | #include "grbl.h" 23 | 24 | 25 | // Homing axis search distance multiplier. Computed by this value times the cycle travel. 26 | #ifndef HOMING_AXIS_SEARCH_SCALAR 27 | #define HOMING_AXIS_SEARCH_SCALAR 1.5 // Must be > 1 to ensure limit switch will be engaged. 28 | #endif 29 | #ifndef HOMING_AXIS_LOCATE_SCALAR 30 | #define HOMING_AXIS_LOCATE_SCALAR 5.0 // Must be > 1 to ensure limit switch is cleared. 31 | #endif 32 | 33 | void limits_init() 34 | { 35 | LIMIT_DDR &= ~(LIMIT_MASK); // Set as input pins 36 | 37 | #ifdef DISABLE_LIMIT_PIN_PULL_UP 38 | LIMIT_PORT &= ~(LIMIT_MASK); // Normal low operation. Requires external pull-down. 39 | #else 40 | LIMIT_PORT |= (LIMIT_MASK); // Enable internal pull-up resistors. Normal high operation. 41 | #endif 42 | 43 | if (bit_istrue(settings.flags,BITFLAG_HARD_LIMIT_ENABLE)) { 44 | LIMIT_PCMSK |= LIMIT_MASK; // Enable specific pins of the Pin Change Interrupt 45 | PCICR |= (1 << LIMIT_INT); // Enable Pin Change Interrupt 46 | } else { 47 | limits_disable(); 48 | } 49 | 50 | #ifdef ENABLE_SOFTWARE_DEBOUNCE 51 | MCUSR &= ~(1< 0); 263 | 264 | // The active cycle axes should now be homed and machine limits have been located. By 265 | // default, Grbl defines machine space as all negative, as do most CNCs. Since limit switches 266 | // can be on either side of an axes, check and set axes machine zero appropriately. Also, 267 | // set up pull-off maneuver from axes limit switches that have been homed. This provides 268 | // some initial clearance off the switches and should also help prevent them from falsely 269 | // triggering when hard limits are enabled or when more than one axes shares a limit pin. 270 | #ifdef COREXY 271 | int32_t off_axis_position = 0; 272 | #endif 273 | int32_t set_axis_position; 274 | // Set machine positions for homed limit switches. Don't update non-homed axes. 275 | for (idx=0; idx -settings.max_travel[idx]) { soft_limit_error = true; } 325 | } else { 326 | if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { soft_limit_error = true; } 327 | } 328 | #else 329 | // NOTE: max_travel is stored as negative 330 | if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { soft_limit_error = true; } 331 | #endif 332 | 333 | if (soft_limit_error) { 334 | // Force feed hold if cycle is active. All buffered blocks are guaranteed to be within 335 | // workspace volume so just come to a controlled stop so position is not lost. When complete 336 | // enter alarm mode. 337 | if (sys.state == STATE_CYCLE) { 338 | bit_true_atomic(sys.rt_exec_state, EXEC_FEED_HOLD); 339 | do { 340 | protocol_execute_realtime(); 341 | if (sys.abort) { return; } 342 | } while ( sys.state != STATE_IDLE ); 343 | } 344 | 345 | mc_reset(); // Issue system reset and ensure spindle and coolant are shutdown. 346 | bit_true_atomic(sys.rt_exec_alarm, (EXEC_ALARM_SOFT_LIMIT|EXEC_CRITICAL_EVENT)); // Indicate soft limit critical event 347 | protocol_execute_realtime(); // Execute to enter critical event loop and system abort 348 | return; 349 | } 350 | } 351 | } 352 | -------------------------------------------------------------------------------- /motion_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | motion_control.c - high level interface for issuing motion commands 3 | Part of Grbl 4 | 5 | Copyright (c) 2011-2015 Sungeun K. Jeon 6 | Copyright (c) 2009-2011 Simen Svale Skogsrud 7 | Copyright (c) 2011 Simen Svale Skogsrud 8 | 9 | Grbl is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Grbl is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with Grbl. If not, see . 21 | */ 22 | 23 | #include "grbl.h" 24 | 25 | 26 | // Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second 27 | // unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in 28 | // (1 minute)/feed_rate time. 29 | // NOTE: This is the primary gateway to the grbl planner. All line motions, including arc line 30 | // segments, must pass through this routine before being passed to the planner. The seperation of 31 | // mc_line and plan_buffer_line is done primarily to place non-planner-type functions from being 32 | // in the planner and to let backlash compensation or canned cycle integration simple and direct. 33 | #ifdef USE_LINE_NUMBERS 34 | void mc_line(float *target, float feed_rate, uint8_t invert_feed_rate, int32_t line_number) 35 | #else 36 | void mc_line(float *target, float feed_rate, uint8_t invert_feed_rate) 37 | #endif 38 | { 39 | // If enabled, check for soft limit violations. Placed here all line motions are picked up 40 | // from everywhere in Grbl. 41 | if (bit_istrue(settings.flags,BITFLAG_SOFT_LIMIT_ENABLE)) { limits_soft_check(target); } 42 | 43 | // If in check gcode mode, prevent motion by blocking planner. Soft limits still work. 44 | if (sys.state == STATE_CHECK_MODE) { return; } 45 | 46 | // NOTE: Backlash compensation may be installed here. It will need direction info to track when 47 | // to insert a backlash line motion(s) before the intended line motion and will require its own 48 | // plan_check_full_buffer() and check for system abort loop. Also for position reporting 49 | // backlash steps will need to be also tracked, which will need to be kept at a system level. 50 | // There are likely some other things that will need to be tracked as well. However, we feel 51 | // that backlash compensation should NOT be handled by Grbl itself, because there are a myriad 52 | // of ways to implement it and can be effective or ineffective for different CNC machines. This 53 | // would be better handled by the interface as a post-processor task, where the original g-code 54 | // is translated and inserts backlash motions that best suits the machine. 55 | // NOTE: Perhaps as a middle-ground, all that needs to be sent is a flag or special command that 56 | // indicates to Grbl what is a backlash compensation motion, so that Grbl executes the move but 57 | // doesn't update the machine position values. Since the position values used by the g-code 58 | // parser and planner are separate from the system machine positions, this is doable. 59 | 60 | // If the buffer is full: good! That means we are well ahead of the robot. 61 | // Remain in this loop until there is room in the buffer. 62 | do { 63 | protocol_execute_realtime(); // Check for any run-time commands 64 | if (sys.abort) { return; } // Bail, if system abort. 65 | if ( plan_check_full_buffer() ) { protocol_auto_cycle_start(); } // Auto-cycle start when buffer is full. 66 | else { break; } 67 | } while (1); 68 | 69 | // Plan and queue motion into planner buffer 70 | #ifdef USE_LINE_NUMBERS 71 | plan_buffer_line(target, feed_rate, invert_feed_rate, line_number); 72 | #else 73 | plan_buffer_line(target, feed_rate, invert_feed_rate); 74 | #endif 75 | } 76 | 77 | 78 | // Execute an arc in offset mode format. position == current xyz, target == target xyz, 79 | // offset == offset from current xyz, axis_X defines circle plane in tool space, axis_linear is 80 | // the direction of helical travel, radius == circle radius, isclockwise boolean. Used 81 | // for vector transformation direction. 82 | // The arc is approximated by generating a huge number of tiny, linear segments. The chordal tolerance 83 | // of each segment is configured in settings.arc_tolerance, which is defined to be the maximum normal 84 | // distance from segment to the circle when the end points both lie on the circle. 85 | #ifdef USE_LINE_NUMBERS 86 | void mc_arc(float *position, float *target, float *offset, float radius, float feed_rate, 87 | uint8_t invert_feed_rate, uint8_t axis_0, uint8_t axis_1, uint8_t axis_linear, uint8_t is_clockwise_arc, int32_t line_number) 88 | #else 89 | void mc_arc(float *position, float *target, float *offset, float radius, float feed_rate, 90 | uint8_t invert_feed_rate, uint8_t axis_0, uint8_t axis_1, uint8_t axis_linear, uint8_t is_clockwise_arc) 91 | #endif 92 | { 93 | float center_axis0 = position[axis_0] + offset[axis_0]; 94 | float center_axis1 = position[axis_1] + offset[axis_1]; 95 | float r_axis0 = -offset[axis_0]; // Radius vector from center to current location 96 | float r_axis1 = -offset[axis_1]; 97 | float rt_axis0 = target[axis_0] - center_axis0; 98 | float rt_axis1 = target[axis_1] - center_axis1; 99 | 100 | // CCW angle between position and target from circle center. Only one atan2() trig computation required. 101 | float angular_travel = atan2(r_axis0*rt_axis1-r_axis1*rt_axis0, r_axis0*rt_axis0+r_axis1*rt_axis1); 102 | if (is_clockwise_arc) { // Correct atan2 output per direction 103 | if (angular_travel >= -ARC_ANGULAR_TRAVEL_EPSILON) { angular_travel -= 2*M_PI; } 104 | } else { 105 | if (angular_travel <= ARC_ANGULAR_TRAVEL_EPSILON) { angular_travel += 2*M_PI; } 106 | } 107 | 108 | // NOTE: Segment end points are on the arc, which can lead to the arc diameter being smaller by up to 109 | // (2x) settings.arc_tolerance. For 99% of users, this is just fine. If a different arc segment fit 110 | // is desired, i.e. least-squares, midpoint on arc, just change the mm_per_arc_segment calculation. 111 | // For the intended uses of Grbl, this value shouldn't exceed 2000 for the strictest of cases. 112 | uint16_t segments = floor(fabs(0.5*angular_travel*radius)/ 113 | sqrt(settings.arc_tolerance*(2*radius - settings.arc_tolerance)) ); 114 | 115 | if (segments) { 116 | // Multiply inverse feed_rate to compensate for the fact that this movement is approximated 117 | // by a number of discrete segments. The inverse feed_rate should be correct for the sum of 118 | // all segments. 119 | if (invert_feed_rate) { feed_rate *= segments; } 120 | 121 | float theta_per_segment = angular_travel/segments; 122 | float linear_per_segment = (target[axis_linear] - position[axis_linear])/segments; 123 | 124 | /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector, 125 | and phi is the angle of rotation. Solution approach by Jens Geisler. 126 | r_T = [cos(phi) -sin(phi); 127 | sin(phi) cos(phi] * r ; 128 | 129 | For arc generation, the center of the circle is the axis of rotation and the radius vector is 130 | defined from the circle center to the initial position. Each line segment is formed by successive 131 | vector rotations. Single precision values can accumulate error greater than tool precision in rare 132 | cases. So, exact arc path correction is implemented. This approach avoids the problem of too many very 133 | expensive trig operations [sin(),cos(),tan()] which can take 100-200 usec each to compute. 134 | 135 | Small angle approximation may be used to reduce computation overhead further. A third-order approximation 136 | (second order sin() has too much error) holds for most, if not, all CNC applications. Note that this 137 | approximation will begin to accumulate a numerical drift error when theta_per_segment is greater than 138 | ~0.25 rad(14 deg) AND the approximation is successively used without correction several dozen times. This 139 | scenario is extremely unlikely, since segment lengths and theta_per_segment are automatically generated 140 | and scaled by the arc tolerance setting. Only a very large arc tolerance setting, unrealistic for CNC 141 | applications, would cause this numerical drift error. However, it is best to set N_ARC_CORRECTION from a 142 | low of ~4 to a high of ~20 or so to avoid trig operations while keeping arc generation accurate. 143 | 144 | This approximation also allows mc_arc to immediately insert a line segment into the planner 145 | without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied 146 | a correction, the planner should have caught up to the lag caused by the initial mc_arc overhead. 147 | This is important when there are successive arc motions. 148 | */ 149 | // Computes: cos_T = 1 - theta_per_segment^2/2, sin_T = theta_per_segment - theta_per_segment^3/6) in ~52usec 150 | float cos_T = 2.0 - theta_per_segment*theta_per_segment; 151 | float sin_T = theta_per_segment*0.16666667*(cos_T + 4.0); 152 | cos_T *= 0.5; 153 | 154 | float sin_Ti; 155 | float cos_Ti; 156 | float r_axisi; 157 | uint16_t i; 158 | uint8_t count = 0; 159 | 160 | for (i = 1; i 0) { 211 | // NOTE: Check and execute realtime commands during dwell every <= DWELL_TIME_STEP milliseconds. 212 | protocol_execute_realtime(); 213 | if (sys.abort) { return; } 214 | _delay_ms(DWELL_TIME_STEP); // Delay DWELL_TIME_STEP increment 215 | } 216 | } 217 | 218 | 219 | // Perform homing cycle to locate and set machine zero. Only '$H' executes this command. 220 | // NOTE: There should be no motions in the buffer and Grbl must be in an idle state before 221 | // executing the homing cycle. This prevents incorrect buffered plans after homing. 222 | void mc_homing_cycle() 223 | { 224 | // Check and abort homing cycle, if hard limits are already enabled. Helps prevent problems 225 | // with machines with limits wired on both ends of travel to one limit pin. 226 | // TODO: Move the pin-specific LIMIT_PIN call to limits.c as a function. 227 | #ifdef LIMITS_TWO_SWITCHES_ON_AXES 228 | if (limits_get_state()) { 229 | mc_reset(); // Issue system reset and ensure spindle and coolant are shutdown. 230 | bit_true_atomic(sys.rt_exec_alarm, (EXEC_ALARM_HARD_LIMIT|EXEC_CRITICAL_EVENT)); 231 | return; 232 | } 233 | #endif 234 | 235 | limits_disable(); // Disable hard limits pin change register for cycle duration 236 | 237 | // ------------------------------------------------------------------------------------- 238 | // Perform homing routine. NOTE: Special motion case. Only system reset works. 239 | 240 | // Search to engage all axes limit switches at faster homing seek rate. 241 | limits_go_home(HOMING_CYCLE_0); // Homing cycle 0 242 | #ifdef HOMING_CYCLE_1 243 | limits_go_home(HOMING_CYCLE_1); // Homing cycle 1 244 | #endif 245 | #ifdef HOMING_CYCLE_2 246 | limits_go_home(HOMING_CYCLE_2); // Homing cycle 2 247 | #endif 248 | 249 | protocol_execute_realtime(); // Check for reset and set system abort. 250 | if (sys.abort) { return; } // Did not complete. Alarm state set by mc_alarm. 251 | 252 | // Homing cycle complete! Setup system for normal operation. 253 | // ------------------------------------------------------------------------------------- 254 | 255 | // Gcode parser position was circumvented by the limits_go_home() routine, so sync position now. 256 | gc_sync_position(); 257 | 258 | // If hard limits feature enabled, re-enable hard limits pin change register after homing cycle. 259 | limits_init(); 260 | } 261 | 262 | 263 | // Perform tool length probe cycle. Requires probe switch. 264 | // NOTE: Upon probe failure, the program will be stopped and placed into ALARM state. 265 | #ifdef USE_LINE_NUMBERS 266 | void mc_probe_cycle(float *target, float feed_rate, uint8_t invert_feed_rate, uint8_t is_probe_away, 267 | uint8_t is_no_error, int32_t line_number) 268 | #else 269 | void mc_probe_cycle(float *target, float feed_rate, uint8_t invert_feed_rate, uint8_t is_probe_away, 270 | uint8_t is_no_error) 271 | #endif 272 | { 273 | // TODO: Need to update this cycle so it obeys a non-auto cycle start. 274 | if (sys.state == STATE_CHECK_MODE) { return; } 275 | 276 | // Finish all queued commands and empty planner buffer before starting probe cycle. 277 | protocol_buffer_synchronize(); 278 | 279 | // Initialize probing control variables 280 | sys.probe_succeeded = false; // Re-initialize probe history before beginning cycle. 281 | probe_configure_invert_mask(is_probe_away); 282 | 283 | // After syncing, check if probe is already triggered. If so, halt and issue alarm. 284 | // NOTE: This probe initialization error applies to all probing cycles. 285 | if ( probe_get_state() ) { // Check probe pin state. 286 | bit_true_atomic(sys.rt_exec_alarm, EXEC_ALARM_PROBE_FAIL); 287 | protocol_execute_realtime(); 288 | } 289 | if (sys.abort) { return; } // Return if system reset has been issued. 290 | 291 | // Setup and queue probing motion. Auto cycle-start should not start the cycle. 292 | #ifdef USE_LINE_NUMBERS 293 | mc_line(target, feed_rate, invert_feed_rate, line_number); 294 | #else 295 | mc_line(target, feed_rate, invert_feed_rate); 296 | #endif 297 | 298 | // Activate the probing state monitor in the stepper module. 299 | sys.probe_state = PROBE_ACTIVE; 300 | 301 | // Perform probing cycle. Wait here until probe is triggered or motion completes. 302 | bit_true_atomic(sys.rt_exec_state, EXEC_CYCLE_START); 303 | do { 304 | protocol_execute_realtime(); 305 | if (sys.abort) { return; } // Check for system abort 306 | } while (sys.state != STATE_IDLE); 307 | 308 | // Probing cycle complete! 309 | 310 | // Set state variables and error out, if the probe failed and cycle with error is enabled. 311 | if (sys.probe_state == PROBE_ACTIVE) { 312 | if (is_no_error) { memcpy(sys.probe_position, sys.position, sizeof(float)*N_AXIS); } 313 | else { bit_true_atomic(sys.rt_exec_alarm, EXEC_ALARM_PROBE_FAIL); } 314 | } else { 315 | sys.probe_succeeded = true; // Indicate to system the probing cycle completed successfully. 316 | } 317 | sys.probe_state = PROBE_OFF; // Ensure probe state monitor is disabled. 318 | protocol_execute_realtime(); // Check and execute run-time commands 319 | if (sys.abort) { return; } // Check for system abort 320 | 321 | // Reset the stepper and planner buffers to remove the remainder of the probe motion. 322 | st_reset(); // Reest step segment buffer. 323 | plan_reset(); // Reset planner buffer. Zero planner positions. Ensure probing motion is cleared. 324 | plan_sync_position(); // Sync planner position to current machine position. 325 | 326 | // TODO: Update the g-code parser code to not require this target calculation but uses a gc_sync_position() call. 327 | // NOTE: The target[] variable updated here will be sent back and synced with the g-code parser. 328 | system_convert_array_steps_to_mpos(target, sys.position); 329 | 330 | #ifdef MESSAGE_PROBE_COORDINATES 331 | // All done! Output the probe position as message. 332 | report_probe_parameters(); 333 | #endif 334 | } 335 | 336 | 337 | // Method to ready the system to reset by setting the realtime reset command and killing any 338 | // active processes in the system. This also checks if a system reset is issued while Grbl 339 | // is in a motion state. If so, kills the steppers and sets the system alarm to flag position 340 | // lost, since there was an abrupt uncontrolled deceleration. Called at an interrupt level by 341 | // realtime abort command and hard limits. So, keep to a minimum. 342 | void mc_reset() 343 | { 344 | // Only this function can set the system reset. Helps prevent multiple kill calls. 345 | if (bit_isfalse(sys.rt_exec_state, EXEC_RESET)) { 346 | bit_true_atomic(sys.rt_exec_state, EXEC_RESET); 347 | 348 | // Kill spindle and coolant. 349 | spindle_stop(); 350 | coolant_stop(); 351 | 352 | // Kill steppers only if in any motion state, i.e. cycle, actively holding, or homing. 353 | // NOTE: If steppers are kept enabled via the step idle delay setting, this also keeps 354 | // the steppers enabled by avoiding the go_idle call altogether, unless the motion state is 355 | // violated, by which, all bets are off. 356 | if ((sys.state & (STATE_CYCLE | STATE_HOMING)) || (sys.suspend == SUSPEND_ENABLE_HOLD)) { 357 | if (sys.state == STATE_HOMING) { bit_true_atomic(sys.rt_exec_alarm, EXEC_ALARM_HOMING_FAIL); } 358 | else { bit_true_atomic(sys.rt_exec_alarm, EXEC_ALARM_ABORT_CYCLE); } 359 | st_go_idle(); // Force kill steppers. Position has likely been lost. 360 | } 361 | } 362 | } 363 | --------------------------------------------------------------------------------