├── ArduinoIDE_addonForSanguinololu ├── README.md ├── Sanguino │ └── avr │ │ ├── README.md │ │ ├── boards.txt │ │ └── variants │ │ └── sanguino │ │ └── pins_arduino.h └── sanguinololu_ArduinoIDE.png ├── COPYING ├── Makefile ├── README.md ├── config.h ├── coolant_control.c ├── coolant_control.h ├── cpu_map.h ├── defaults.h ├── eeprom.c ├── eeprom.h ├── fastio.h ├── gcode.c ├── gcode.h ├── grblForCyclone.ino ├── limits.c ├── limits.h ├── main.c ├── motion_control.c ├── motion_control.h ├── nuts_bolts.c ├── nuts_bolts.h ├── planner.c ├── planner.h ├── print.c ├── print.h ├── probe.c ├── probe.h ├── protocol.c ├── protocol.h ├── ramps.h ├── report.c ├── report.h ├── serial.c ├── serial.h ├── settings.c ├── settings.h ├── spindle_control.c ├── spindle_control.h ├── stepper.c ├── stepper.h ├── system.c └── system.h /ArduinoIDE_addonForSanguinololu/README.md: -------------------------------------------------------------------------------- 1 | # If you are using Sanguinololu 2 | 3 | It is necessary to install the Sanguinololu board into the Arduino IDE before being able to compile and flash the GRBL firmware. 4 | 5 | For this, copy the Sanguinololu folder into the hardware/ folder of the Arduino IDE (often located in the Sketchbook library) 6 | 7 | **Note:** If you have problems programming the Sanguinololu, you may need to change the baud rate (upload.speed=XXXXX) in "Sanguino/avr/boards.txt". Try values: 38400, 57600, and 115200. 8 | If problems persist, refer to: 9 | 10 | 11 | Result 12 | -- 13 | 14 | Then running the Arduino IDE, you'll see new options in the "Tools" menu: 15 | 16 | ![](sanguinololu_ArduinoIDE.png) 17 | 18 | **ATmega644P** is probably the one on board your Sanguinololu. 19 | 20 | 21 | Developed by Kristian Sloth Lauszus, 2012 22 | -- 23 | Obtained from: 24 | The code is released under the GNU General Public License. 25 | 26 | -------------------------------------------------------------------------------- /ArduinoIDE_addonForSanguinololu/Sanguino/avr/README.md: -------------------------------------------------------------------------------- 1 | #### Developed by Kristian Sloth Lauszus, 2012 2 | 3 | The code is released under the GNU General Public License. 4 | _________ 5 | 6 | This content is needed so it's compatible with the Arduino IDE 1.5.x. 7 | 8 | For more information see the following sites: [http://www.arduino.cc/en/Guide/Environment#thirdpartyhardware](http://www.arduino.cc/en/Guide/Environment#thirdpartyhardware) and [http://code.google.com/p/arduino/wiki/Platforms1](http://code.google.com/p/arduino/wiki/Platforms1) 9 | or send me an email at lauszus@gmail.com. -------------------------------------------------------------------------------- /ArduinoIDE_addonForSanguinololu/Sanguino/avr/boards.txt: -------------------------------------------------------------------------------- 1 | menu.cpu=Processor 2 | 3 | ############################################################## 4 | 5 | sanguino.name=Sanguino 6 | 7 | sanguino.upload.tool=arduino:avrdude 8 | sanguino.upload.protocol=arduino 9 | 10 | sanguino.bootloader.tool=arduino:avrdude 11 | sanguino.bootloader.low_fuses=0xFF 12 | sanguino.bootloader.high_fuses=0xDC 13 | sanguino.bootloader.extended_fuses=0xFD 14 | sanguino.bootloader.unlock_bits=0x3F 15 | sanguino.bootloader.lock_bits=0x0F 16 | 17 | sanguino.build.board=AVR_SANGUINO 18 | sanguino.build.core=arduino:arduino 19 | sanguino.build.variant=sanguino 20 | 21 | ###### ATmega644x 22 | 23 | 24 | ## Sanguino W/ ATmega644P or ATmega644PA (16MHz) 25 | sanguino.menu.cpu.atmega644p=ATmega644P or ATmega644PA (16 MHz) 26 | 27 | sanguino.menu.cpu.atmega644p.upload.maximum_size=63488 28 | sanguino.menu.cpu.atmega644p.upload.maximum_data_size=4096 29 | sanguino.menu.cpu.atmega644p.upload.speed=38400 30 | 31 | sanguino.menu.cpu.atmega644p.bootloader.file=optiboot/optiboot_atmega644p.hex 32 | 33 | sanguino.menu.cpu.atmega644p.build.mcu=atmega644p 34 | sanguino.menu.cpu.atmega644p.build.f_cpu=16000000L 35 | 36 | 37 | 38 | ## Sanguino W/ ATmega644 or ATmega644A (16MHz) 39 | sanguino.menu.cpu.atmega644=ATmega644 or ATmega644A (16 MHz) 40 | 41 | sanguino.menu.cpu.atmega644.upload.maximum_size=64512 42 | sanguino.menu.cpu.atmega644.upload.maximum_data_size=4096 43 | sanguino.menu.cpu.atmega644.upload.speed=115200 44 | 45 | sanguino.menu.cpu.atmega644.bootloader.file=optiboot/optiboot_atmega644.hex 46 | 47 | sanguino.menu.cpu.atmega644.build.mcu=atmega644 48 | sanguino.menu.cpu.atmega644.build.f_cpu=16000000L 49 | 50 | 51 | 52 | ###### ATmega1284x 53 | 54 | ## Sanguino W/ ATmega1284 or ATmega1284P 16MHz 55 | sanguino.menu.cpu.atmega1284p=ATmega1284 or ATmega1284P (16 MHz) 56 | 57 | sanguino.menu.cpu.atmega1284p.upload.maximum_size=130048 58 | sanguino.menu.cpu.atmega1284p.upload.maximum_data_size=16384 59 | sanguino.menu.cpu.atmega1284p.upload.speed=115200 60 | 61 | sanguino.menu.cpu.atmega1284p.bootloader.file=optiboot/optiboot_atmega1284p.hex 62 | 63 | sanguino.menu.cpu.atmega1284p.build.mcu=atmega1284p 64 | sanguino.menu.cpu.atmega1284p.build.f_cpu=16000000L 65 | 66 | ############################################################ 67 | -------------------------------------------------------------------------------- /ArduinoIDE_addonForSanguinololu/Sanguino/avr/variants/sanguino/pins_arduino.h: -------------------------------------------------------------------------------- 1 | /* 2 | pins_arduino.h - Pin definition functions for Arduino 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2007 David A. Mellis 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library 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 GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General 18 | Public License along with this library; if not, write to the 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20 | Boston, MA 02111-1307 USA 21 | 22 | $Id: wiring.h 249 2007-02-03 16:52:51Z mellis $ 23 | 24 | Changelog 25 | ----------- 26 | 11/25/11 - ryan@ryanmsutton.com - Add pins for Sanguino 644P and 1284P 27 | 07/15/12 - ryan@ryanmsutton.com - Updated for arduino0101 28 | 29 | Improvements by Kristian Sloth Lauszus, lauszus@gmail.com 30 | */ 31 | 32 | #ifndef Pins_Arduino_h 33 | #define Pins_Arduino_h 34 | 35 | #include 36 | 37 | static const uint8_t SS = 4; 38 | static const uint8_t MOSI = 5; 39 | static const uint8_t MISO = 6; 40 | static const uint8_t SCK = 7; 41 | 42 | static const uint8_t SDA = 17; 43 | static const uint8_t SCL = 16; 44 | 45 | #define LED_BUILTIN 0 46 | 47 | static const uint8_t A0 = 31; 48 | static const uint8_t A1 = 30; 49 | static const uint8_t A2 = 29; 50 | static const uint8_t A3 = 28; 51 | static const uint8_t A4 = 27; 52 | static const uint8_t A5 = 26; 53 | static const uint8_t A6 = 25; 54 | static const uint8_t A7 = 24; 55 | 56 | // ATMEL ATMEGA644/ATMEGA1284 / SANGUINO 57 | // 58 | // +---\/---+ 59 | // (D 0) PB0 1| |40 PA0 (AI 0 / D31) 60 | // (D 1) PB1 2| |39 PA1 (AI 1 / D30) 61 | // INT2 (D 2) PB2 3| |38 PA2 (AI 2 / D29) 62 | // PWM (D 3) PB3 4| |37 PA3 (AI 3 / D28) 63 | // SS PWM (D 4) PB4 5| |36 PA4 (AI 4 / D27) 64 | // MOSI (D 5) PB5 6| |35 PA5 (AI 5 / D26) 65 | // MISO (D 6) PB6 7| |34 PA6 (AI 6 / D25) 66 | // SCK (D 7) PB7 8| |33 PA7 (AI 7 / D24) 67 | // RST 9| |32 AREF 68 | // VCC 10| |31 GND 69 | // GND 11| |30 AVCC 70 | // XTAL2 12| |29 PC7 (D 23) 71 | // XTAL1 13| |28 PC6 (D 22) 72 | // RX0 (D 8) PD0 14| |27 PC5 (D 21) TDI 73 | // TX0 (D 9) PD1 15| |26 PC4 (D 20) TDO 74 | // INT0 RX1 (D 10) PD2 16| |25 PC3 (D 19) TMS 75 | // INT1 TX1 (D 11) PD3 17| |24 PC2 (D 18) TCK 76 | // PWM (D 12) PD4 18| |23 PC1 (D 17) SDA 77 | // PWM (D 13) PD5 19| |22 PC0 (D 16) SCL 78 | // PWM (D 14) PD6 20| |21 PD7 (D 15) PWM 79 | // +--------+ 80 | // 81 | #define NUM_DIGITAL_PINS 24 82 | #define NUM_ANALOG_INPUTS 8 83 | 84 | #define analogInputToDigitalPin(p) ((p < 8) ? 31 - (p): -1) 85 | #define analogPinToChannel(p) ((p < 8) ? (p) : 31 - (p)) 86 | 87 | #define digitalPinHasPWM(p) ((p) == 3 || (p) == 4 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 ) 88 | 89 | #define digitalPinToPCICR(p) ( (((p) >= 0) && ((p) <= 31)) ? (&PCICR) : ((uint8_t *)0) ) 90 | 91 | #define digitalPinToPCICRbit(p) ( (((p) >= 24) && ((p) <= 31)) ? 0 : \ 92 | ( (((p) >= 0) && ((p) <= 7)) ? 1 : \ 93 | ( (((p) >= 16) && ((p) <= 23)) ? 2 : \ 94 | ( (((p) >= 8) && ((p) <= 15)) ? 3 : \ 95 | 0 ) ) ) ) 96 | 97 | #define digitalPinToPCMSK(p) ( (((p) >= 24) && ((p) <= 31)) ? (&PCMSK0) : \ 98 | ( (((p) >= 0) && ((p) <= 7)) ? (&PCMSK1) : \ 99 | ( (((p) >= 16) && ((p) <= 23)) ? (&PCMSK2) : \ 100 | ( (((p) >= 8) && ((p) <= 15)) ? (&PCMSK3) : \ 101 | ((uint8_t *)0) ) ) ) ) 102 | 103 | 104 | #define digitalPinToPCMSKbit(p) ( (((p) >= 24) && ((p) <= 31)) ? (31 - (p)) : \ 105 | ( (((p) >= 0) && ((p) <= 7)) ? (p) : \ 106 | ( (((p) >= 16) && ((p) <= 23)) ? ((p) - 16) : \ 107 | ( (((p) >= 8) && ((p) <= 15)) ? ((p) - 8) : \ 108 | 0 ) ) ) ) 109 | 110 | #define digitalPinToInterrupt(p) ((p) == 10 ? 0 : ((p) == 11 ? 1 : ((p) == 2 ? 2 : NOT_AN_INTERRUPT))) 111 | 112 | #ifdef ARDUINO_MAIN 113 | // these arrays map port names (e.g. port B) to the 114 | // appropriate addresses for various functions (e.g. reading 115 | // and writing) 116 | const uint16_t PROGMEM port_to_mode_PGM[] = 117 | { 118 | NOT_A_PORT, 119 | (uint16_t) &DDRA, 120 | (uint16_t) &DDRB, 121 | (uint16_t) &DDRC, 122 | (uint16_t) &DDRD, 123 | }; 124 | 125 | const uint16_t PROGMEM port_to_output_PGM[] = 126 | { 127 | NOT_A_PORT, 128 | (uint16_t) &PORTA, 129 | (uint16_t) &PORTB, 130 | (uint16_t) &PORTC, 131 | (uint16_t) &PORTD, 132 | }; 133 | const uint16_t PROGMEM port_to_input_PGM[] = 134 | { 135 | NOT_A_PORT, 136 | (uint16_t) &PINA, 137 | (uint16_t) &PINB, 138 | (uint16_t) &PINC, 139 | (uint16_t) &PIND, 140 | }; 141 | const uint8_t PROGMEM digital_pin_to_port_PGM[] = 142 | { 143 | PB, /* 0 */ 144 | PB, 145 | PB, 146 | PB, 147 | PB, 148 | PB, 149 | PB, 150 | PB, 151 | PD, /* 8 */ 152 | PD, 153 | PD, 154 | PD, 155 | PD, 156 | PD, 157 | PD, 158 | PD, 159 | PC, /* 16 */ 160 | PC, 161 | PC, 162 | PC, 163 | PC, 164 | PC, 165 | PC, 166 | PC, 167 | PA, /* 24 */ 168 | PA, 169 | PA, 170 | PA, 171 | PA, 172 | PA, 173 | PA, 174 | PA /* 31 */ 175 | }; 176 | const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = 177 | { 178 | _BV(0), /* 0, port B */ 179 | _BV(1), 180 | _BV(2), 181 | _BV(3), 182 | _BV(4), 183 | _BV(5), 184 | _BV(6), 185 | _BV(7), 186 | _BV(0), /* 8, port D */ 187 | _BV(1), 188 | _BV(2), 189 | _BV(3), 190 | _BV(4), 191 | _BV(5), 192 | _BV(6), 193 | _BV(7), 194 | _BV(0), /* 16, port C */ 195 | _BV(1), 196 | _BV(2), 197 | _BV(3), 198 | _BV(4), 199 | _BV(5), 200 | _BV(6), 201 | _BV(7), 202 | _BV(7), /* 24, port A */ 203 | _BV(6), 204 | _BV(5), 205 | _BV(4), 206 | _BV(3), 207 | _BV(2), 208 | _BV(1), 209 | _BV(0) 210 | }; 211 | const uint8_t PROGMEM digital_pin_to_timer_PGM[] = 212 | { 213 | NOT_ON_TIMER, /* 0 - PB0 */ 214 | NOT_ON_TIMER, /* 1 - PB1 */ 215 | NOT_ON_TIMER, /* 2 - PB2 */ 216 | TIMER0A, /* 3 - PB3 */ 217 | TIMER0B, /* 4 - PB4 */ 218 | NOT_ON_TIMER, /* 5 - PB5 */ 219 | NOT_ON_TIMER, /* 6 - PB6 */ 220 | NOT_ON_TIMER, /* 7 - PB7 */ 221 | NOT_ON_TIMER, /* 8 - PD0 */ 222 | NOT_ON_TIMER, /* 9 - PD1 */ 223 | NOT_ON_TIMER, /* 10 - PD2 */ 224 | NOT_ON_TIMER, /* 11 - PD3 */ 225 | TIMER1B, /* 12 - PD4 */ 226 | TIMER1A, /* 13 - PD5 */ 227 | TIMER2B, /* 14 - PD6 */ 228 | TIMER2A, /* 15 - PD7 */ 229 | NOT_ON_TIMER, /* 16 - PC0 */ 230 | NOT_ON_TIMER, /* 17 - PC1 */ 231 | NOT_ON_TIMER, /* 18 - PC2 */ 232 | NOT_ON_TIMER, /* 19 - PC3 */ 233 | NOT_ON_TIMER, /* 20 - PC4 */ 234 | NOT_ON_TIMER, /* 21 - PC5 */ 235 | NOT_ON_TIMER, /* 22 - PC6 */ 236 | NOT_ON_TIMER, /* 23 - PC7 */ 237 | NOT_ON_TIMER, /* 24 - PA0 */ 238 | NOT_ON_TIMER, /* 25 - PA1 */ 239 | NOT_ON_TIMER, /* 26 - PA2 */ 240 | NOT_ON_TIMER, /* 27 - PA3 */ 241 | NOT_ON_TIMER, /* 28 - PA4 */ 242 | NOT_ON_TIMER, /* 29 - PA5 */ 243 | NOT_ON_TIMER, /* 30 - PA6 */ 244 | NOT_ON_TIMER /* 31 - PA7 */ 245 | }; 246 | #endif 247 | 248 | // These serial port names are intended to allow libraries and architecture-neutral 249 | // sketches to automatically default to the correct port name for a particular type 250 | // of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN, 251 | // the first hardware serial port whose RX/TX pins are not dedicated to another use. 252 | // 253 | // SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor 254 | // 255 | // SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial 256 | // 257 | // SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library 258 | // 259 | // SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins. 260 | // 261 | // SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX 262 | // pins are NOT connected to anything by default. 263 | 264 | #define SERIAL_PORT_MONITOR Serial 265 | #define SERIAL_PORT_HARDWARE Serial 266 | 267 | #if defined(UBRR1H) 268 | #define SERIAL_PORT_HARDWARE1 Serial1 269 | #define SERIAL_PORT_HARDWARE_OPEN Serial1 270 | #endif 271 | 272 | #endif 273 | -------------------------------------------------------------------------------- /ArduinoIDE_addonForSanguinololu/sanguinololu_ArduinoIDE.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CarlosGS/grblForCyclone/68e7ff3b655574d5e36c083b1ef6594606c20a05/ArduinoIDE_addonForSanguinololu/sanguinololu_ArduinoIDE.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # There exist several targets which are by default empty and which can be 3 | # used for execution of your targets. These targets are usually executed 4 | # before and after some main targets. They are: 5 | # 6 | # .build-pre: called before 'build' target 7 | # .build-post: called after 'build' target 8 | # .clean-pre: called before 'clean' target 9 | # .clean-post: called after 'clean' target 10 | # .clobber-pre: called before 'clobber' target 11 | # .clobber-post: called after 'clobber' target 12 | # .all-pre: called before 'all' target 13 | # .all-post: called after 'all' target 14 | # .help-pre: called before 'help' target 15 | # .help-post: called after 'help' target 16 | # 17 | # Targets beginning with '.' are not intended to be called on their own. 18 | # 19 | # Main targets can be executed directly, and they are: 20 | # 21 | # build build a specific configuration 22 | # clean remove built files from a configuration 23 | # clobber remove all built files 24 | # all build all configurations 25 | # help print help mesage 26 | # 27 | # Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and 28 | # .help-impl are implemented in nbproject/makefile-impl.mk. 29 | # 30 | # Available make variables: 31 | # 32 | # CND_BASEDIR base directory for relative paths 33 | # CND_DISTDIR default top distribution directory (build artifacts) 34 | # CND_BUILDDIR default top build directory (object files, ...) 35 | # CONF name of current configuration 36 | # CND_PLATFORM_${CONF} platform name (current configuration) 37 | # CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) 38 | # CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) 39 | # CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) 40 | # CND_PACKAGE_DIR_${CONF} directory of package (current configuration) 41 | # CND_PACKAGE_NAME_${CONF} name of package (current configuration) 42 | # CND_PACKAGE_PATH_${CONF} path to package (current configuration) 43 | # 44 | # NOCDDL 45 | 46 | 47 | # Environment 48 | MKDIR=mkdir 49 | CP=cp 50 | CCADMIN=CCadmin 51 | 52 | 53 | # build 54 | build: .build-post 55 | 56 | .build-pre: 57 | # Add your pre 'build' code here... 58 | 59 | .build-post: .build-impl 60 | # Add your post 'build' code here... 61 | 62 | 63 | # clean 64 | clean: .clean-post 65 | 66 | .clean-pre: 67 | # Add your pre 'clean' code here... 68 | 69 | .clean-post: .clean-impl 70 | # Add your post 'clean' code here... 71 | 72 | 73 | # clobber 74 | clobber: .clobber-post 75 | 76 | .clobber-pre: 77 | # Add your pre 'clobber' code here... 78 | 79 | .clobber-post: .clobber-impl 80 | # Add your post 'clobber' code here... 81 | 82 | 83 | # all 84 | all: .all-post 85 | 86 | .all-pre: 87 | # Add your pre 'all' code here... 88 | 89 | .all-post: .all-impl 90 | # Add your post 'all' code here... 91 | 92 | 93 | # build tests 94 | build-tests: .build-tests-post 95 | 96 | .build-tests-pre: 97 | # Add your pre 'build-tests' code here... 98 | 99 | .build-tests-post: .build-tests-impl 100 | # Add your post 'build-tests' code here... 101 | 102 | 103 | # run tests 104 | test: .test-post 105 | 106 | .test-pre: build-tests 107 | # Add your pre 'test' code here... 108 | 109 | .test-post: .test-impl 110 | # Add your post 'test' code here... 111 | 112 | 113 | # help 114 | help: .help-post 115 | 116 | .help-pre: 117 | # Add your pre 'help' code here... 118 | 119 | .help-post: .help-impl 120 | # Add your post 'help' code here... 121 | 122 | 123 | 124 | # include project implementation makefile 125 | include nbproject/Makefile-impl.mk 126 | 127 | # include project make variables 128 | include nbproject/Makefile-variables.mk 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GRBL RAMPS 1.4 port 2 | By ArSi arsi@arsi.sk 3 | 4 | Modified again by Carlosgs to **support RAMPS and Sanguinololu** without any re-wiring (i.e. if [MarlinForCyclone](https://github.com/carlosgs/MarlinForCyclone) worked straight away, this one will). 5 | Pre-configured for [Cyclone PCB Factory v2.X](http://reprap.org/wiki/Cyclone_PCB_Factory). 6 | 7 | How to flash the GRBL firmware 8 | -- 9 | 0) Before every installation/update of GRBL, make sure to [**clear the EEPROM memory**](https://www.arduino.cc/en/Tutorial/EEPROMClear). This will reset the calibration values to zero, avoiding further problems (thanks to Jungle and others in [the mail list](https://groups.google.com/forum/#!forum/cyclone-pcb-factory) for submitting this issue) 10 | 11 | 1) Download this repository ([link](https://github.com/carlosgs/grblForCyclone/archive/grblForCyclone.zip)), extract the zip file, and open the file **grblForCyclone.ino** from the [**Arduino IDE**](https://www.arduino.cc/en/Main/Software). 12 | 13 | 2) **Select the board you are using from the "Tools->Board" menu**: 14 | * If you are using RAMPS, select **"Arduino/Genuino Mega or Mega 2560"**. 15 | * If you are using Sanguinololu, [click here](ArduinoIDE_addonForSanguinololu). 16 | * If you are using CNCshield, [click here](https://github.com/carlosgs/grblForCyclone/tree/grblForCyclone_CNCShield) for a modification of the latest GRBL firmware. Ignore the rest of this page; instead follow the [official installation instructions](https://github.com/grbl/grbl/wiki/Flashing-Grbl-to-an-Arduino). 17 | 18 | 3) Next, **if you want to modify any default parameters, do so in "[defaults.h](defaults.h)"** (end of the file, below DEFAULTS\_CYCLONE\_2\_1). 19 | 20 | 4) Finally, once you have selected the correct board and USB port, you can **click "Upload" to flash the GRBL firmware**. Success!! :-) **[Click here](https://github.com/carlosgs/grblForCyclone/wiki) for a GRBL/bCNC usage tutorial** 21 | 22 | (Tested with version 1.6.5 of the Arduino IDE, 29/Sep/2015) 23 | 24 | How to connect the XYZ motors, XY endstops and Z probe 25 | -- 26 | 27 | ``` 28 | OPTION A: USING RAMPS SHIELD 29 | 30 | Ramps 1.4 | GRBL | Test 31 | X axis | X axis | OK working 32 | Y axis | Y axis | OK working 33 | Z axis | Z axis | OK working 34 | digital pin 3 | min X | OK working 35 | digital pin 14 | min Y | OK working 36 | (min Z disabled) | min Z | DISABLED 37 | digital pin 18 | Z probe | OK working 38 | 39 | - pin 3 is labeled as min X endstop in RAMPS 40 | - pin 14 is labeled as min Y 41 | - pin 18 is labeled as min Z (connect the probe wires here) 42 | 43 | UNTESTED: 44 | - Spindle ON: digital pin 10 (one of the transistor outputs) 45 | - Spindle direction: digital pin 9 (one of the transistor outputs) 46 | - Coolant ON: digital pin 8 (one of the transistor outputs) 47 | ``` 48 | 49 | ``` 50 | OPTION B: USING SANGUINOLOLU BOARD 51 | 52 | Sanguinololu 1.3a| GRBL | Test 53 | X axis | X axis | OK working 54 | Y axis | Y axis | OK working 55 | Z axis | Z axis | OK working 56 | digital pin 18 | min X | OK working 57 | digital pin 19 | min Y | OK working 58 | (min Z disabled) | min Z | DISABLED 59 | digital pin 20 | Z probe | OK working 60 | 61 | - pin 18 is labeled as X-STOP (endstop) in Sanguinololu 62 | - pin 19 is labeled as Y-STOP 63 | - pin 20 is labeled as Z-STOP (connect the probe wires here) 64 | 65 | UNTESTED: 66 | - Spindle ON: Hotend power output 67 | - Spindle direction: Extruder direction pin 68 | - Coolant ON: Heat-bed power output 69 | 70 | ``` 71 | 72 | 73 | 74 | 75 | ************************************************************************************** 76 | Steppers redirected to fastio.h from marlin firmware 77 | min switches remaped to the same position in RAMPS 78 | command buttons currently disabled 79 | Ramps pins config moved to ramps.h 80 | cpu_map.h - min switches 81 | added GRBL_RAMPS1_4.ino for arduino IDE 82 | 83 | ************************************************************************************** 84 | 85 | 86 | Sanguinololu support 87 | -- 88 | Check [this folder](ArduinoIDE_addonForSanguinololu) for additional documentation regarding Sanguinololu boards. 89 | 90 | 91 | *************************************************************************************** 92 | 93 | #Grbl - An embedded g-code interpreter and motion-controller for the Arduino/AVR328 microcontroller 94 | 95 | *** 96 | 97 | 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. 98 | 99 | 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. 100 | 101 | 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. 102 | 103 | 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. 104 | 105 | * [Licensing](https://github.com/grbl/grbl/wiki/Licensing): Grbl v0.9 is free software, released under the GPLv3 license. Obsolete versions of Grbl, v0.8 and prior, are released under the permissive MIT-license. This will ensure Grbl will always be an open-source project while making the code permissive for others. 106 | 107 | * For more information and help, check out our **[Wiki pages!](https://github.com/grbl/grbl/wiki)** If you find that the information is out-dated, please to help us keep it updated by editing it or notifying our community! Thanks! 108 | 109 | * Lead Developer [_2011 - Current_]: Sonny Jeon, Ph.D. (USA) 110 | 111 | * Lead Developer [_2009 - 2011_]: Simen Svale Skogsrud (Norway). aka The Originator/Creator/Pioneer/Father of Grbl. 112 | 113 | *** 114 | 115 | _**Master Branch:**_ 116 | * [Grbl v0.9g Atmega328p 16mhz 115200baud with generic defaults](http://bit.ly/1m8E1Qa) _(2014-08-22)_ 117 | * [Grbl v0.9g Atmega328p 16mhz 115200baud with ShapeOko2 defaults](http://bit.ly/1kOAzig) _(2014-08-22)_ 118 | - **IMPORTANT INFO WHEN UPGRADING TO GRBL v0.9g:** 119 | - Baudrate is now **115200** (Up from 9600). 120 | - Settings WILL be overwritten. Please make sure you have a backup. Also, settings have been renumbered and some have changed how they work. See our [Configuring v0.9 Wiki page](https://github.com/grbl/grbl/wiki/Configuring-Grbl-v0.9) for details. 121 | 122 | _**Archives:**_ 123 | * [Grbl v0.8c Atmega328p 16mhz 9600baud](http://bit.ly/SSdCJE) 124 | * [Grbl v0.7d Atmega328p 16mhz 9600baud](http://bit.ly/ZhL15G) 125 | * [Grbl v0.6b Atmega328p 16mhz 9600baud](http://bit.ly/VD04A5) 126 | * [Grbl v0.51 Atmega328p 16mhz 9600baud](http://bit.ly/W75BS1) 127 | * [Grbl v0.6b Atmega168 16mhz 9600baud](http://bit.ly/SScWnE) 128 | * [Grbl v0.51 Atmega168 16mhz 9600baud](http://bit.ly/VXyrYu) 129 | 130 | *** 131 | 132 | ##Update Summary for v0.9 from v0.8 133 | - **IMPORTANT: Default serial baudrate is now 115200! (Up from 9600). And your settings will be over-written! Make sure to have a backup.** 134 | - **_NEW_ Super Smooth Stepper Algorithm:** Complete overhaul of the handling of the stepper driver to simplify and reduce task time per ISR tick. Much smoother operation with the new Adaptive Multi-Axis Step Smoothing (AMASS) algorithm which does what its name implies (see stepper.c source for details). Users should immediately see significant improvements in how their machines move and overall performance! 135 | - **Stability and Robustness Updates:** Grbl's overall stability has been focused on for this version. The planner and step-execution interface has been completely re-written for robustness and incorruptibility by the introduction of an intermediate step segment buffer that "checks-out" steps from the planner buffer in real-time. This means we can now fearlessly drive Grbl to it's highest limits. Combined with the new stepper algorithm and planner optimizations, this translated to **5x to 10x** overall performance increases in our testing! Also, stability and robustness tests have been reported to easily take 1.4 million (yes, **million**) line g-code programs like a champ! 136 | - **(x4)+ Faster Planner:** Planning computations improved four-fold or more by optimizing end-to-end operations, which included streamlining the computations and introducing a planner pointer to locate un-improvable portions of the buffer and not waste cycles recomputing them. 137 | - **Compile-able via Arduino IDE!:** Grbl's source code may be now download and altered, and then be compiled and flashed directly through the Arduino IDE, which should work on all platforms. See the Wiki for details on how to do it. 138 | - **G-Code Parser Overhaul:** Completely re-written from the ground-up for 100%-compliance* to the g-code standard. (* Parts of the NIST standard are a bit out-dated and arbitrary, so we altered some minor things to make more sense. Differences are outlined in the source code.) We also took steps to allow us to break up the g-code parser into distinct separate tasks, which is key for some future development ideas and improvements. 139 | - **Independent Acceleration and Velocity Settings:** Each axes may be defined with unique acceleration and velocity parameters and Grbl will automagically calculate the maximum acceleration and velocity through a path depending on the direction traveled. This is very useful for machines that have very different axes properties, like the ShapeOko's z-axis. 140 | - **Soft Limits:** Checks if any motion command exceeds workspace limits before executing it, and alarms out, if detected. Another safety feature, but, unlike hard limits, position does not get lost, as it forces a feed hold before erroring out. NOTE: This still requires limit switches for homing so Grbl knows where the machine origin is, and the new max axis travel settings configured correctly for the machine. 141 | - **Probing:** The G38.2 straight probe and G43.1/49 tool offset g-code commands are now supported. A simple probe switch must be connected to the Uno analog pin 5 (normally-open to ground). Grbl will report the probe position back to the user when the probing cycle detects a pin state change. 142 | - **Tool Length Offsets:** Probing doesn't make sense without tool length offsets(TLO), so we added it! The G43.1 dynamic TLO (described by linuxcnc.org) and G49 TLO cancel commands are now supported. G43.1 dynamic TLO works like the normal G43 TLO(NOT SUPPORTED) but requires an additional axis word with the offset value attached. We did this so Grbl does not have to track and maintain a tool offset database in its memory. Perhaps in the future, we will support a tool database, but not for this version. 143 | - **Improved Arc Performance:** The larger the arc radius, the faster Grbl will trace it! We are now defining arcs in terms of arc chordal tolerance, rather than a fixed segment length. This automatically scales the arc segment length such that maximum radial error of the segment from the true arc is never more than the chordal tolerance value of a super-accurate default of 0.002 mm. 144 | - **CPU Pin Mapping:** In an effort for Grbl to be compatible with other AVR architectures, such as the 1280 or 2560, a new cpu_map.h pin configuration file has been created to allow Grbl to be compiled for them. This is currently user supported, so your mileage may vary. If you run across a bug, please let us know or better send us a fix! Thanks in advance! 145 | - **New Grbl SIMULATOR! (by @jgeisler and @ashelly):** A completely independent wrapper of the Grbl main source code that may be compiled as an executable on a computer. No Arduino required. Simply simulates the responses of Grbl as if it was on an Arduino. May be used for many things: checking out how Grbl works, pre-process moves for GUI graphics, debugging of new features, etc. Much left to do, but potentially very powerful, as the dummy AVR variables can be written to output anything you need. 146 | - **Configurable Real-time Status Reporting:** Users can now customize the type of real-time data Grbl reports back when they issue a '?' status report. This includes data such as: machine position, work position, planner buffer usage, serial RX buffer usage. 147 | - **Updated Homing Routine:** Sets workspace volume in all negative space regardless of limit switch position. Common on pro CNCs. But, the behavior may be changed by a compile-time option though. Now tied directly into the main planner and stepper modules to reduce flash space and allow maximum speeds during seeking. 148 | - **Optional Limit Pin Sharing:** Limit switches can be combined to share the same pins to free up precious I/O pins for other purposes. When combined, users must adjust the homing cycle mask in config.h to not home the axes on a shared pin at the same time. Don't worry; hard limits and the homing cycle still work just like they did before. 149 | - **Optional Variable Spindle Speed Output:** Available only as a compile-time option through the config.h file. Enables PWM output for 'S' g-code commands. Enabling this feature will swap the Z-limit D11 pin and spindle enable D12 pin to access the hardware PWM on pin D12. The Z-limit pin, now on D12, should work just as it did before. 150 | - **Additional Compile-Time Feature Options:** Line number tracking, real-time feed rate reporting. 151 | - **SLATED FOR v1.0 DEVELOPMENT** Jogging controls and feedrate/spindle/coolant overrides. (In v0.9, the framework for feedrate overrides are in-place, only the minor details to complete it have yet to be installed.) 152 | 153 | - 154 | ``` 155 | List of Supported G-Codes in Grbl v0.9 156 | - Non-Modal Commands: G4, G10 L2, G10 L20, G28, G30, G28.1, G30.1, G53, G92, G92.1 157 | - Motion Modes: G0, G1, G2, G3, G38.1, G80 158 | - Feed Rate Modes: G93, G94 159 | - Unit Modes: G20, G21 160 | - Distance Modes: G90, G91 161 | - Plane Select Modes: G17, G18, G19 162 | - Tool Length Offset Modes: G43.1, G49 163 | - Coordinate System Modes: G54, G55, G56, G57, G58, G59 164 | - Program Flow: M0, M1, M2, M30* 165 | - Coolant Control: M7*, M8, M9 166 | - Spindle Control: M3, M4, M5 167 | ``` 168 | 169 | ------------- 170 | Grbl is an open-source project and fueled by the free-time of our intrepid administrators and altruistic users. If you'd like to donate, all proceeds will be used to help fund supporting hardware and testing equipment. Thank you! 171 | 172 | [![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=EBQWAWQAAT878) 173 | 174 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* 2 | config.h - compile time configuration 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2013-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2013 Sungeun K. Jeon 25 | */ 26 | 27 | // This file contains compile-time configurations for Grbl's internal system. For the most part, 28 | // users will not need to directly modify these, but they are here for specific needs, i.e. 29 | // performance tuning or adjusting to non-typical machines. 30 | 31 | // IMPORTANT: Any changes here requires a full re-compiling of the source code to propagate them. 32 | 33 | #ifndef config_h 34 | #define config_h 35 | #include "system.h" 36 | 37 | 38 | 39 | // Default settings. Used when resetting EEPROM. Change to desired name in defaults.h 40 | #define DEFAULTS_CYCLONE_2_1 41 | 42 | // Serial baud rate 43 | #define BAUD_RATE 115200 44 | 45 | // Default cpu mappings. Grbl officially supports the Arduino Uno only. Other processor types 46 | // may exist from user-supplied templates or directly user-defined in cpu_map.h 47 | 48 | // Automatic detection of Sanguinololu/MEGA 49 | #ifdef __AVR_ATmega2560__ 50 | #define CPU_MAP_ATMEGA2560_RAMPS_1_4 // Ramps 51 | #else 52 | #define CPU_MAP_ATmega644P_Sanguinololu // Sanguinololu 53 | #endif 54 | 55 | // Define runtime command special characters. These characters are 'picked-off' directly from the 56 | // serial read data stream and are not passed to the grbl line execution parser. Select characters 57 | // that do not and must not exist in the streamed g-code program. ASCII control characters may be 58 | // used, if they are available per user setup. Also, extended ASCII codes (>127), which are never in 59 | // g-code programs, maybe selected for interface programs. 60 | // NOTE: If changed, manually update help message in report.c. 61 | #define CMD_STATUS_REPORT '?' 62 | #define CMD_FEED_HOLD '!' 63 | #define CMD_CYCLE_START '~' 64 | #define CMD_RESET 0x18 // ctrl-x. 65 | 66 | // If homing is enabled, homing init lock sets Grbl into an alarm state upon power up. This forces 67 | // the user to perform the homing cycle (or override the locks) before doing anything else. This is 68 | // mainly a safety feature to remind the user to home, since position is unknown to Grbl. 69 | #define HOMING_INIT_LOCK // Comment to disable 70 | 71 | // Define the homing cycle patterns with bitmasks. The homing cycle first performs a search mode 72 | // to quickly engage the limit switches, followed by a slower locate mode, and finished by a short 73 | // pull-off motion to disengage the limit switches. The following HOMING_CYCLE_x defines are executed 74 | // in order starting with suffix 0 and completes the homing routine for the specified-axes only. If 75 | // an axis is omitted from the defines, it will not home, nor will the system update its position. 76 | // Meaning that this allows for users with non-standard cartesian machines, such as a lathe (x then z, 77 | // with no y), to configure the homing cycle behavior to their needs. 78 | // NOTE: The homing cycle is designed to allow sharing of limit pins, if the axes are not in the same 79 | // cycle, but this requires some pin settings changes in cpu_map.h file. For example, the default homing 80 | // cycle can share the Z limit pin with either X or Y limit pins, since they are on different cycles. 81 | // By sharing a pin, this frees up a precious IO pin for other purposes. In theory, all axes limit pins 82 | // may be reduced to one pin, if all axes are homed with seperate cycles, or vice versa, all three axes 83 | // on separate pin, but homed in one cycle. Also, it should be noted that the function of hard limits 84 | // will not be affected by pin sharing. 85 | // NOTE: Defaults are set for a traditional 3-axis CNC machine. Z-axis first to clear, followed by X & Y. 86 | #define HOMING_CYCLE_0 (1< 3us, and, when added with the 207 | // user-supplied step pulse time, the total time must not exceed 127us. Reported successful 208 | // values for certain setups have ranged from 5 to 20us. 209 | // #define STEP_PULSE_DELAY 10 // Step pulse delay in microseconds. Default disabled. 210 | 211 | // The number of linear motions in the planner buffer to be planned at any give time. The vast 212 | // majority of RAM that Grbl uses is based on this buffer size. Only increase if there is extra 213 | // available RAM, like when re-compiling for a Mega or Sanguino. Or decrease if the Arduino 214 | // begins to crash due to the lack of available RAM or if the CPU is having trouble keeping 215 | // up with planning new incoming motions as they are executed. 216 | // #define BLOCK_BUFFER_SIZE 18 // Uncomment to override default in planner.h. 217 | 218 | // Governs the size of the intermediary step segment buffer between the step execution algorithm 219 | // and the planner blocks. Each segment is set of steps executed at a constant velocity over a 220 | // fixed time defined by ACCELERATION_TICKS_PER_SECOND. They are computed such that the planner 221 | // block velocity profile is traced exactly. The size of this buffer governs how much step 222 | // execution lead time there is for other Grbl processes have to compute and do their thing 223 | // before having to come back and refill this buffer, currently at ~50msec of step moves. 224 | // #define SEGMENT_BUFFER_SIZE 6 // Uncomment to override default in stepper.h. 225 | 226 | // Line buffer size from the serial input stream to be executed. Also, governs the size of 227 | // each of the startup blocks, as they are each stored as a string of this size. Make sure 228 | // to account for the available EEPROM at the defined memory address in settings.h and for 229 | // the number of desired startup blocks. 230 | // NOTE: 80 characters is not a problem except for extreme cases, but the line buffer size 231 | // can be too small and g-code blocks can get truncated. Officially, the g-code standards 232 | // support up to 256 characters. In future versions, this default will be increased, when 233 | // we know how much extra memory space we can re-invest into this. 234 | // #define LINE_BUFFER_SIZE 80 // Uncomment to override default in protocol.h 235 | 236 | // Serial send and receive buffer size. The receive buffer is often used as another streaming 237 | // buffer to store incoming blocks to be processed by Grbl when its ready. Most streaming 238 | // interfaces will character count and track each block send to each block response. So, 239 | // increase the receive buffer if a deeper receive buffer is needed for streaming and avaiable 240 | // memory allows. The send buffer primarily handles messages in Grbl. Only increase if large 241 | // messages are sent and Grbl begins to stall, waiting to send the rest of the message. 242 | // NOTE: Buffer size values must be greater than zero and less than 256. 243 | // #define RX_BUFFER_SIZE 128 // Uncomment to override defaults in serial.h 244 | // #define TX_BUFFER_SIZE 64 245 | 246 | // Toggles XON/XOFF software flow control for serial communications. Not officially supported 247 | // due to problems involving the Atmega8U2 USB-to-serial chips on current Arduinos. The firmware 248 | // on these chips do not support XON/XOFF flow control characters and the intermediate buffer 249 | // in the chips cause latency and overflow problems with standard terminal programs. However, 250 | // using specifically-programmed UI's to manage this latency problem has been confirmed to work. 251 | // As well as, older FTDI FT232RL-based Arduinos(Duemilanove) are known to work with standard 252 | // terminal programs since their firmware correctly manage these XON/XOFF characters. In any 253 | // case, please report any successes to grbl administrators! 254 | // #define ENABLE_XONXOFF // Default disabled. Uncomment to enable. 255 | 256 | // A simple software debouncing feature for hard limit switches. When enabled, the interrupt 257 | // monitoring the hard limit switch pins will enable the Arduino's watchdog timer to re-check 258 | // the limit pin state after a delay of about 32msec. This can help with CNC machines with 259 | // problematic false triggering of their hard limit switches, but it WILL NOT fix issues with 260 | // electrical interference on the signal cables from external sources. It's recommended to first 261 | // use shielded signal cables with their shielding connected to ground (old USB/computer cables 262 | // work well and are cheap to find) and wire in a low-pass circuit into each limit pin. 263 | // #define ENABLE_SOFTWARE_DEBOUNCE // Default disabled. Uncomment to enable. 264 | 265 | // --------------------------------------------------------------------------------------- 266 | 267 | // TODO: Install compile-time option to send numeric status codes rather than strings. 268 | 269 | // --------------------------------------------------------------------------------------- 270 | // COMPILE-TIME ERROR CHECKING OF DEFINE VALUES: 271 | 272 | // #if (ISR_TICKS_PER_ACCELERATION_TICK > 255) 273 | // #error Parameters ACCELERATION_TICKS / ISR_TICKS must be < 256 to prevent integer overflow. 274 | // #endif 275 | 276 | // --------------------------------------------------------------------------------------- 277 | 278 | 279 | #endif 280 | -------------------------------------------------------------------------------- /coolant_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | coolant_control.c - coolant control methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 "system.h" 22 | #include "coolant_control.h" 23 | #include "protocol.h" 24 | #include "gcode.h" 25 | extern void rampsInitCoolant(); 26 | extern void rampsInitSpindle(); 27 | extern void rampsInitSteppers(); 28 | extern void rampsWriteDisable(uint8_t value); 29 | extern void rampsWriteSteps(uint8_t value); 30 | extern void rampsWriteDirections(uint8_t value); 31 | extern void rampsStartSpindle(); 32 | extern void rampsStopSpindle(); 33 | extern void rampsLeftSpindle(); 34 | extern void rampsRightSpindle(); 35 | extern void rampsCoolantOn(); 36 | extern void rampsCoolantOff(); 37 | 38 | void coolant_init() { 39 | // COOLANT_FLOOD_DDR |= (1 << COOLANT_FLOOD_BIT); 40 | rampsInitCoolant(); 41 | #ifdef ENABLE_M7 42 | COOLANT_MIST_DDR |= (1 << COOLANT_MIST_BIT); 43 | #endif 44 | coolant_stop(); 45 | } 46 | 47 | 48 | void coolant_stop() { 49 | // COOLANT_FLOOD_PORT &= ~(1 << COOLANT_FLOOD_BIT); 50 | rampsCoolantOff(); 51 | #ifdef ENABLE_M7 52 | COOLANT_MIST_PORT &= ~(1 << COOLANT_MIST_BIT); 53 | #endif 54 | } 55 | 56 | 57 | void coolant_run(uint8_t mode) 58 | { 59 | if (sys.state == STATE_CHECK_MODE) { return; } 60 | 61 | protocol_auto_cycle_start(); //temp fix for M8 lockup 62 | protocol_buffer_synchronize(); // Ensure coolant turns on when specified in program. 63 | if (mode == COOLANT_FLOOD_ENABLE) { 64 | //COOLANT_FLOOD_PORT |= (1 << COOLANT_FLOOD_BIT); 65 | rampsCoolantOn(); 66 | 67 | #ifdef ENABLE_M7 68 | } else if (mode == COOLANT_MIST_ENABLE) { 69 | COOLANT_MIST_PORT |= (1 << COOLANT_MIST_BIT); 70 | #endif 71 | 72 | } else { 73 | coolant_stop(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /coolant_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | coolant_control.h - spindle control methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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_run(uint8_t mode); 28 | 29 | #endif -------------------------------------------------------------------------------- /defaults.h: -------------------------------------------------------------------------------- 1 | /* 2 | defaults.h - defaults settings configuration file 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 | our nefarious needs. */ 26 | 27 | #ifndef defaults_h 28 | #define defaults_h 29 | 30 | #ifdef DEFAULTS_GENERIC 31 | // Grbl generic default settings. Should work across different machines. 32 | #define DEFAULT_X_STEPS_PER_MM 33.3 33 | #define DEFAULT_Y_STEPS_PER_MM 33.3 34 | #define DEFAULT_Z_STEPS_PER_MM 320 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 200.0 // mm 42 | #define DEFAULT_Y_MAX_TRAVEL 200.0 // mm 43 | #define DEFAULT_Z_MAX_TRAVEL 200.0 // mm 44 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 45 | #define DEFAULT_STEPPING_INVERT_MASK 0 46 | #define DEFAULT_DIRECTION_INVERT_MASK ((1< all three jumpers installed 271 | #define STEPS_PER_REV 200.0 272 | #define MM_PER_REV 1.25 // 1.25 mm/rev leadscrew 273 | #define Cyclone_XY_Gear_Ratio 21.0/21.0 // Number of gear teeth (motor/rod) 274 | #define Cyclone_Z_Gear_Ratio 8.0/15.0 // Number of gear teeth (motor/rod) 275 | #define DEFAULT_X_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/(Cyclone_XY_Gear_Ratio*MM_PER_REV)) 276 | #define DEFAULT_Y_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/(Cyclone_XY_Gear_Ratio*MM_PER_REV)) 277 | #define DEFAULT_Z_STEPS_PER_MM (STEPS_PER_REV*MICROSTEPS/(Cyclone_Z_Gear_Ratio*MM_PER_REV)) 278 | #define DEFAULT_X_MAX_RATE 5*60.0 // mm/min 279 | #define DEFAULT_Y_MAX_RATE 5*60.0 // mm/min 280 | #define DEFAULT_Z_MAX_RATE 2.5*60.0 // mm/min 281 | #define DEFAULT_X_ACCELERATION (16.0*60*60) // 50*60*60 mm/min^2 = 50 mm/sec^2 282 | #define DEFAULT_Y_ACCELERATION (16.0*60*60) // 50*60*60 mm/min^2 = 50 mm/sec^2 283 | #define DEFAULT_Z_ACCELERATION (16.0*60*60) // 50*60*60 mm/min^2 = 50 mm/sec^2 284 | #define DEFAULT_X_MAX_TRAVEL 168.0 // mm 285 | #define DEFAULT_Y_MAX_TRAVEL 101.0 // mm 286 | #define DEFAULT_Z_MAX_TRAVEL 50.0 // mm 287 | #define DEFAULT_STEP_PULSE_MICROSECONDS 10 288 | #define DEFAULT_STEPPING_INVERT_MASK 0 289 | #define DEFAULT_DIRECTION_INVERT_MASK ((0< 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 | -------------------------------------------------------------------------------- /eeprom.h: -------------------------------------------------------------------------------- 1 | /* 2 | eeprom.h - EEPROM methods 3 | Part of Grbl 4 | 5 | The MIT License (MIT) 6 | 7 | GRBL(tm) - Embedded CNC g-code interpreter and motion-controller 8 | Copyright (c) 2009-2011 Simen Svale Skogsrud 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in 18 | all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | THE SOFTWARE. 27 | */ 28 | 29 | #ifndef eeprom_h 30 | #define eeprom_h 31 | 32 | unsigned char eeprom_get_char(unsigned int addr); 33 | void eeprom_put_char(unsigned int addr, unsigned char new_value); 34 | void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size); 35 | int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /gcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | gcode.h - rs274/ngc parser. 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef gcode_h 28 | #define gcode_h 29 | 30 | 31 | // Define modal group internal numbers for checking multiple command violations and tracking the 32 | // type of command that is called in the block. A modal group is a group of g-code commands that are 33 | // mutually exclusive, or cannot exist on the same line, because they each toggle a state or execute 34 | // a unique motion. These are defined in the NIST RS274-NGC v3 g-code standard, available online, 35 | // and are similar/identical to other g-code interpreters by manufacturers (Haas,Fanuc,Mazak,etc). 36 | // NOTE: Modal group define values must be sequential and starting from zero. 37 | #define MODAL_GROUP_G0 0 // [G4,G10,G28,G28.1,G30,G30.1,G53,G92,G92.1] Non-modal 38 | #define MODAL_GROUP_G1 1 // [G0,G1,G2,G3,G38.2,G80] Motion 39 | #define MODAL_GROUP_G2 2 // [G17,G18,G19] Plane selection 40 | #define MODAL_GROUP_G3 3 // [G90,G91] Distance mode 41 | #define MODAL_GROUP_G5 4 // [G93,G94] Feed rate mode 42 | #define MODAL_GROUP_G6 5 // [G20,G21] Units 43 | #define MODAL_GROUP_G8 6 // [G43,G43.1,G49] Tool length offset 44 | #define MODAL_GROUP_G12 7 // [G54,G55,G56,G57,G58,G59] Coordinate system selection 45 | 46 | #define MODAL_GROUP_M4 8 // [M0,M1,M2,M30] Stopping 47 | #define MODAL_GROUP_M7 9 // [M3,M4,M5] Spindle turning 48 | #define MODAL_GROUP_M8 10 // [M7,M8,M9] Coolant control 49 | 50 | #define OTHER_INPUT_F 11 51 | #define OTHER_INPUT_S 12 52 | #define OTHER_INPUT_T 13 53 | 54 | // Define command actions for within execution-type modal groups (motion, stopping, non-modal). Used 55 | // internally by the parser to know which command to execute. 56 | 57 | // Modal Group G0: Non-modal actions 58 | #define NON_MODAL_NO_ACTION 0 // (Default: Must be zero) 59 | #define NON_MODAL_DWELL 1 // G4 60 | #define NON_MODAL_SET_COORDINATE_DATA 2 // G10 61 | #define NON_MODAL_GO_HOME_0 3 // G28 62 | #define NON_MODAL_SET_HOME_0 4 // G28.1 63 | #define NON_MODAL_GO_HOME_1 5 // G30 64 | #define NON_MODAL_SET_HOME_1 6 // G30.1 65 | #define NON_MODAL_ABSOLUTE_OVERRIDE 7 // G53 66 | #define NON_MODAL_SET_COORDINATE_OFFSET 8 // G92 67 | #define NON_MODAL_RESET_COORDINATE_OFFSET 9 //G92.1 68 | 69 | // Modal Group G1: Motion modes 70 | #define MOTION_MODE_SEEK 0 // G0 (Default: Must be zero) 71 | #define MOTION_MODE_LINEAR 1 // G1 72 | #define MOTION_MODE_CW_ARC 2 // G2 73 | #define MOTION_MODE_CCW_ARC 3 // G3 74 | #define MOTION_MODE_PROBE 4 // G38.2 75 | #define MOTION_MODE_NONE 5 // G80 76 | 77 | // Modal Group G2: Plane select 78 | #define PLANE_SELECT_XY 0 // G17 (Default: Must be zero) 79 | #define PLANE_SELECT_ZX 1 // G18 80 | #define PLANE_SELECT_YZ 2 // G19 81 | 82 | // Modal Group G3: Distance mode 83 | #define DISTANCE_MODE_ABSOLUTE 0 // G90 (Default: Must be zero) 84 | #define DISTANCE_MODE_INCREMENTAL 1 // G91 85 | 86 | // Modal Group M4: Program flow 87 | #define PROGRAM_FLOW_RUNNING 0 // (Default: Must be zero) 88 | #define PROGRAM_FLOW_PAUSED 1 // M0, M1 89 | #define PROGRAM_FLOW_COMPLETED 2 // M2, M30 90 | 91 | // Modal Group G5: Feed rate mode 92 | #define FEED_RATE_MODE_UNITS_PER_MIN 0 // G94 (Default: Must be zero) 93 | #define FEED_RATE_MODE_INVERSE_TIME 1 // G93 94 | 95 | // Modal Group G6: Units mode 96 | #define UNITS_MODE_MM 0 // G21 (Default: Must be zero) 97 | #define UNITS_MODE_INCHES 1 // G20 98 | 99 | // Modal Group M7: Spindle control 100 | #define SPINDLE_DISABLE 0 // M5 (Default: Must be zero) 101 | #define SPINDLE_ENABLE_CW 1 // M3 102 | #define SPINDLE_ENABLE_CCW 2 // M4 103 | 104 | // Modal Group M8: Coolant control 105 | #define COOLANT_DISABLE 0 // M9 (Default: Must be zero) 106 | #define COOLANT_MIST_ENABLE 1 // M7 107 | #define COOLANT_FLOOD_ENABLE 2 // M8 108 | 109 | // Modal Group G8: Tool length offset 110 | #define TOOL_LENGTH_OFFSET_CANCEL 0 // G49 (Default: Must be zero) 111 | #define TOOL_LENGTH_OFFSET_ENABLE_DYNAMIC 1 // G43.1 112 | 113 | // Modal Group G12: Active work coordinate system 114 | // N/A: Stores coordinate system value (54-59) to change to. 115 | 116 | #define WORD_F 0 117 | #define WORD_I 1 118 | #define WORD_J 2 119 | #define WORD_K 3 120 | #define WORD_L 4 121 | #define WORD_N 5 122 | #define WORD_P 6 123 | #define WORD_R 7 124 | #define WORD_S 8 125 | #define WORD_T 9 126 | #define WORD_X 10 127 | #define WORD_Y 11 128 | #define WORD_Z 12 129 | 130 | 131 | 132 | 133 | // NOTE: When this struct is zeroed, the above defines set the defaults for the system. 134 | typedef struct { 135 | uint8_t motion; // {G0,G1,G2,G3,G38.2,G80} 136 | uint8_t feed_rate; // {G93,G94} 137 | uint8_t units; // {G20,G21} 138 | uint8_t distance; // {G90,G91} 139 | uint8_t plane_select; // {G17,G18,G19} 140 | uint8_t tool_length; // {G43.1,G49} 141 | uint8_t coord_select; // {G54,G55,G56,G57,G58,G59} 142 | uint8_t program_flow; // {M0,M1,M2,M30} 143 | uint8_t coolant; // {M7,M8,M9} 144 | uint8_t spindle; // {M3,M4,M5} 145 | } gc_modal_t; 146 | 147 | typedef struct { 148 | float f; // Feed 149 | float ijk[3]; // I,J,K Axis arc offsets 150 | uint8_t l; // G10 or canned cycles parameters 151 | int32_t n; // Line number 152 | float p; // G10 or dwell parameters 153 | // float q; // G82 peck drilling 154 | float r; // Arc radius 155 | float s; // Spindle speed 156 | uint8_t t; // Tool selection 157 | float xyz[3]; // X,Y,Z Translational axes 158 | } gc_values_t; 159 | 160 | 161 | typedef struct { 162 | gc_modal_t modal; 163 | 164 | float spindle_speed; // RPM 165 | float feed_rate; // Millimeters/min 166 | uint8_t tool; // Tracks tool number. NOT USED. 167 | 168 | float position[N_AXIS]; // Where the interpreter considers the tool to be at this point in the code 169 | 170 | float coord_system[N_AXIS]; // Current work coordinate system (G54+). Stores offset from absolute machine 171 | // position in mm. Loaded from EEPROM when called. 172 | float coord_offset[N_AXIS]; // Retains the G92 coordinate offset (work coordinates) relative to 173 | // machine zero in mm. Non-persistent. Cleared upon reset and boot. 174 | float tool_length_offset; // Tracks tool length offset value when enabled. 175 | } parser_state_t; 176 | extern parser_state_t gc_state; 177 | 178 | typedef struct { 179 | // uint16_t command_words; // NOTE: If this bitflag variable fills, G and M words can be separated. 180 | // uint16_t value_words; 181 | 182 | uint8_t non_modal_command; 183 | gc_modal_t modal; 184 | gc_values_t values; 185 | 186 | } parser_block_t; 187 | extern parser_block_t gc_block; 188 | 189 | // Initialize the parser 190 | void gc_init(); 191 | 192 | // Execute one block of rs275/ngc/g-code 193 | uint8_t gc_execute_line(char *line); 194 | 195 | // Set g-code parser position. Input in steps. 196 | void gc_sync_position(); 197 | 198 | #endif 199 | -------------------------------------------------------------------------------- /grblForCyclone.ino: -------------------------------------------------------------------------------- 1 | // GRBL RAMPS 1.4 port 2 | // By ArSi arsi@arsi.sk 3 | //************************************************************************************** 4 | // Modifications for Cyclone PCB Factory by carlosgs 5 | // Check README.md for details 6 | //*************************************************************************************** 7 | 8 | 9 | 10 | //#Grbl - An embedded g-code interpreter and motion-controller for the Arduino/AVR328 microcontroller 11 | // 12 | //*** 13 | 14 | //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. 15 | 16 | //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. 17 | 18 | //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. 19 | 20 | //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. 21 | 22 | // [Licensing](https://github.com/grbl/grbl/wiki/Licensing): Grbl v0.9 is free software, released under the GPLv3 license. Obsolete versions of Grbl, v0.8 and prior, //are released under the permissive MIT-license. This will ensure Grbl will always be an open-source project while making the code permissive for others. 23 | 24 | //For more information and help, check out our **[Wiki pages!](https://github.com/grbl/grbl/wiki)** If you find that the information is out-dated, please to help us //keep it updated by editing it or notifying our community! Thanks! 25 | 26 | //Lead Developer [_2011 - Current_]: Sonny Jeon, Ph.D. (USA) 27 | 28 | //Lead Developer [_2009 - 2011_]: Simen Svale Skogsrud (Norway). aka The Originator/Creator/Pioneer/Father of Grbl. 29 | 30 | 31 | 32 | 33 | 34 | 35 | #include "system.h" 36 | #include "nuts_bolts.h" 37 | #include "eeprom.h" 38 | #include "gcode.h" 39 | 40 | #include "config.h" 41 | #include "defaults.h" 42 | #include "settings.h" 43 | #include "fastio.h" 44 | 45 | #include "stepper.h" 46 | #include "planner.h" 47 | #include "report.h" 48 | #include "serial.h" 49 | #include "cpu_map.h" 50 | #include "limits.h" 51 | #include "motion_control.h" 52 | #include "spindle_control.h" 53 | #include "protocol.h" 54 | #include "probe.h" 55 | #include "print.h" 56 | #include "coolant_control.h" 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /limits.c: -------------------------------------------------------------------------------- 1 | /* 2 | limits.c - code pertaining to limit-switches and performing the homing cycle 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "settings.h" 29 | #include "protocol.h" 30 | #include "planner.h" 31 | #include "stepper.h" 32 | #include "motion_control.h" 33 | #include "limits.h" 34 | #include "report.h" 35 | #include 36 | 37 | // Homing axis search distance multiplier. Computed by this value times the axis max travel. 38 | #define HOMING_AXIS_SEARCH_SCALAR 1.5 // Must be > 1 to ensure limit switch will be engaged. 39 | 40 | 41 | void limits_init() 42 | { 43 | /*LIMIT_DDR &= ~(LIMIT_MASK); // Set as input pins 44 | 45 | if (bit_istrue(settings.flags,BITFLAG_INVERT_LIMIT_PINS)) { 46 | LIMIT_PORT &= ~(LIMIT_MASK); // Normal low operation. Requires external pull-down. 47 | } else { 48 | LIMIT_PORT |= (LIMIT_MASK); // Enable internal pull-up resistors. Normal high operation. 49 | } 50 | 51 | if (bit_istrue(settings.flags,BITFLAG_HARD_LIMIT_ENABLE)) { 52 | LIMIT_PCMSK |= LIMIT_MASK; // Enable specific pins of the Pin Change Interrupt 53 | PCICR |= (1 << LIMIT_INT); // Enable Pin Change Interrupt 54 | } else { 55 | limits_disable(); 56 | } 57 | 58 | #ifdef ENABLE_SOFTWARE_DEBOUNCE 59 | MCUSR &= ~(1< settings.max_travel[idx]) { max_travel = settings.max_travel[idx]; } 153 | } 154 | max_travel *= -HOMING_AXIS_SEARCH_SCALAR; // Ensure homing switches engaged by over-estimating max travel. 155 | 156 | plan_reset(); // Reset planner buffer to zero planner current position and to clear previous motions. 157 | 158 | do { 159 | // Initialize invert_pin boolean based on approach and invert pin user setting. 160 | if (bit_isfalse(settings.flags,BITFLAG_INVERT_LIMIT_PINS)) { invert_pin = approach; } 161 | else { invert_pin = !approach; } 162 | 163 | // Initialize and declare variables needed for homing routine. 164 | uint8_t n_active_axis = 0; 165 | uint8_t axislock = 0; 166 | 167 | for (idx=0; idx 0); 222 | 223 | // The active cycle axes should now be homed and machine limits have been located. By 224 | // default, grbl defines machine space as all negative, as do most CNCs. Since limit switches 225 | // can be on either side of an axes, check and set axes machine zero appropriately. Also, 226 | // set up pull-off maneuver from axes limit switches that have been homed. This provides 227 | // some initial clearance off the switches and should also help prevent them from falsely 228 | // triggering when hard limits are enabled or when more than one axes shares a limit pin. 229 | for (idx=0; idx -settings.max_travel[idx]) { soft_limit_error = true; } 287 | } else { 288 | if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { soft_limit_error = true; } 289 | } 290 | #else 291 | // NOTE: max_travel is stored as negative 292 | if (target[idx] > 0 || target[idx] < settings.max_travel[idx]) { soft_limit_error = true; } 293 | #endif 294 | 295 | if (soft_limit_error) { 296 | // Force feed hold if cycle is active. All buffered blocks are guaranteed to be within 297 | // workspace volume so just come to a controlled stop so position is not lost. When complete 298 | // enter alarm mode. 299 | if (sys.state == STATE_CYCLE) { 300 | bit_true_atomic(sys.execute, EXEC_FEED_HOLD); 301 | do { 302 | protocol_execute_runtime(); 303 | if (sys.abort) { return; } 304 | } while ( sys.state != STATE_IDLE || sys.state != STATE_QUEUED); 305 | } 306 | 307 | mc_reset(); // Issue system reset and ensure spindle and coolant are shutdown. 308 | bit_true_atomic(sys.execute, (EXEC_ALARM | EXEC_CRIT_EVENT)); // Indicate soft limit critical event 309 | protocol_execute_runtime(); // Execute to enter critical event loop and system abort 310 | return; 311 | } 312 | } 313 | } 314 | -------------------------------------------------------------------------------- /limits.h: -------------------------------------------------------------------------------- 1 | /* 2 | limits.h - code pertaining to limit-switches and performing the homing cycle 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | */ 25 | 26 | #ifndef limits_h 27 | #define limits_h 28 | 29 | 30 | // Initialize the limits module 31 | void limits_init(); 32 | 33 | void limits_disable(); 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 -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | main.c - An embedded CNC Controller with rs274/ngc (g-code) support 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "serial.h" 29 | #include "settings.h" 30 | #include "protocol.h" 31 | #include "gcode.h" 32 | #include "planner.h" 33 | #include "stepper.h" 34 | #include "spindle_control.h" 35 | #include "coolant_control.h" 36 | #include "motion_control.h" 37 | #include "limits.h" 38 | #include "probe.h" 39 | #include "report.h" 40 | 41 | 42 | // Declare system global variable structure 43 | system_t sys; 44 | 45 | 46 | int main(void) 47 | { 48 | // Initialize system upon power-up. 49 | serial_init(); // Setup serial baud rate and interrupts 50 | settings_init(); // Load grbl settings from EEPROM 51 | stepper_init(); // Configure stepper pins and interrupt timers 52 | system_init(); // Configure pinout pins and pin-change interrupt 53 | 54 | memset(&sys, 0, sizeof(sys)); // Clear all system variables 55 | sys.abort = true; // Set abort to complete initialization 56 | sei(); // Enable interrupts 57 | 58 | // Check for power-up and set system alarm if homing is enabled to force homing cycle 59 | // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the 60 | // startup scripts, but allows access to settings and internal commands. Only a homing 61 | // cycle '$H' or kill alarm locks '$X' will disable the alarm. 62 | // NOTE: The startup script will run after successful completion of the homing cycle, but 63 | // not after disabling the alarm locks. Prevents motion startup blocks from crashing into 64 | // things uncontrollably. Very bad. 65 | #ifdef HOMING_INIT_LOCK 66 | if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; } 67 | #endif 68 | 69 | // Grbl initialization loop upon power-up or a system abort. For the latter, all processes 70 | // will return to this loop to be cleanly re-initialized. 71 | for(;;) { 72 | 73 | // TODO: Separate configure task that require interrupts to be disabled, especially upon 74 | // a system abort and ensuring any active interrupts are cleanly reset. 75 | 76 | // Reset Grbl primary systems. 77 | serial_reset_read_buffer(); // Clear serial read buffer 78 | gc_init(); // Set g-code parser to default state 79 | spindle_init(); 80 | coolant_init(); 81 | limits_init(); 82 | probe_init(); 83 | plan_reset(); // Clear block buffer and planner variables 84 | st_reset(); // Clear stepper subsystem variables. 85 | 86 | // Sync cleared gcode and planner positions to current system position. 87 | plan_sync_position(); 88 | gc_sync_position(); 89 | 90 | // Reset system variables. 91 | sys.abort = false; 92 | sys.execute = 0; 93 | if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { sys.auto_start = true; } 94 | else { sys.auto_start = false; } 95 | 96 | // Start Grbl main loop. Processes program inputs and executes them. 97 | protocol_main_loop(); 98 | 99 | } 100 | return 0; /* Never reached */ 101 | } 102 | -------------------------------------------------------------------------------- /motion_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | motion_control.h - high level interface for issuing motion commands 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef motion_control_h 28 | #define motion_control_h 29 | 30 | #define HOMING_CYCLE_LINE_NUMBER -1 31 | 32 | // Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second 33 | // unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in 34 | // (1 minute)/feed_rate time. 35 | #ifdef USE_LINE_NUMBERS 36 | void mc_line(float *target, float feed_rate, uint8_t invert_feed_rate, int32_t line_number); 37 | #else 38 | void mc_line(float *target, float feed_rate, uint8_t invert_feed_rate); 39 | #endif 40 | 41 | // Execute an arc in offset mode format. position == current xyz, target == target xyz, 42 | // offset == offset from current xyz, axis_XXX defines circle plane in tool space, axis_linear is 43 | // the direction of helical travel, radius == circle radius, isclockwise boolean. Used 44 | // for vector transformation direction. 45 | #ifdef USE_LINE_NUMBERS 46 | void mc_arc(float *position, float *target, float *offset, float radius, float feed_rate, 47 | uint8_t invert_feed_rate, uint8_t axis_0, uint8_t axis_1, uint8_t axis_linear, int32_t line_number); 48 | #else 49 | void mc_arc(float *position, float *target, float *offset, float radius, float feed_rate, 50 | uint8_t invert_feed_rate, uint8_t axis_0, uint8_t axis_1, uint8_t axis_linear); 51 | #endif 52 | 53 | // Dwell for a specific number of seconds 54 | void mc_dwell(float seconds); 55 | 56 | // Perform homing cycle to locate machine zero. Requires limit switches. 57 | void mc_homing_cycle(); 58 | 59 | // Perform tool length probe cycle. Requires probe switch. 60 | #ifdef USE_LINE_NUMBERS 61 | void mc_probe_cycle(float *target, float feed_rate, uint8_t invert_feed_rate, int32_t line_number); 62 | #else 63 | void mc_probe_cycle(float *target, float feed_rate, uint8_t invert_feed_rate); 64 | #endif 65 | 66 | // Performs system reset. If in motion state, kills all motion and sets system alarm. 67 | void mc_reset(); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /nuts_bolts.c: -------------------------------------------------------------------------------- 1 | /* 2 | nuts_bolts.c - Shared functions 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "print.h" 29 | 30 | 31 | #define MAX_INT_DIGITS 8 // Maximum number of digits in int32 (and float) 32 | 33 | 34 | // Extracts a floating point value from a string. The following code is based loosely on 35 | // the avr-libc strtod() function by Michael Stumpf and Dmitry Xmelkov and many freely 36 | // available conversion method examples, but has been highly optimized for Grbl. For known 37 | // CNC applications, the typical decimal value is expected to be in the range of E0 to E-4. 38 | // Scientific notation is officially not supported by g-code, and the 'E' character may 39 | // be a g-code word on some CNC systems. So, 'E' notation will not be recognized. 40 | // NOTE: Thanks to Radu-Eosif Mihailescu for identifying the issues with using strtod(). 41 | uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr) 42 | { 43 | char *ptr = line + *char_counter; 44 | unsigned char c; 45 | 46 | // Grab first character and increment pointer. No spaces assumed in line. 47 | c = *ptr++; 48 | 49 | // Capture initial positive/minus character 50 | bool isnegative = false; 51 | if (c == '-') { 52 | isnegative = true; 53 | c = *ptr++; 54 | } else if (c == '+') { 55 | c = *ptr++; 56 | } 57 | 58 | // Extract number into fast integer. Track decimal in terms of exponent value. 59 | uint32_t intval = 0; 60 | int8_t exp = 0; 61 | uint8_t ndigit = 0; 62 | bool isdecimal = false; 63 | while(1) { 64 | c -= '0'; 65 | if (c <= 9) { 66 | ndigit++; 67 | if (ndigit <= MAX_INT_DIGITS) { 68 | if (isdecimal) { exp--; } 69 | intval = (((intval << 2) + intval) << 1) + c; // intval*10 + c 70 | } else { 71 | if (!(isdecimal)) { exp++; } // Drop overflow digits 72 | } 73 | } else if (c == (('.'-'0') & 0xff) && !(isdecimal)) { 74 | isdecimal = true; 75 | } else { 76 | break; 77 | } 78 | c = *ptr++; 79 | } 80 | 81 | // Return if no digits have been read. 82 | if (!ndigit) { return(false); }; 83 | 84 | // Convert integer into floating point. 85 | float fval; 86 | fval = (float)intval; 87 | 88 | // Apply decimal. Should perform no more than two floating point multiplications for the 89 | // expected range of E0 to E-4. 90 | if (fval != 0) { 91 | while (exp <= -2) { 92 | fval *= 0.01; 93 | exp += 2; 94 | } 95 | if (exp < 0) { 96 | fval *= 0.1; 97 | } else if (exp > 0) { 98 | do { 99 | fval *= 10.0; 100 | } while (--exp > 0); 101 | } 102 | } 103 | 104 | // Assign floating point value with correct sign. 105 | if (isnegative) { 106 | *float_ptr = -fval; 107 | } else { 108 | *float_ptr = fval; 109 | } 110 | 111 | *char_counter = ptr - line - 1; // Set char_counter to next statement 112 | 113 | return(true); 114 | } 115 | 116 | 117 | // Delays variable defined milliseconds. Compiler compatibility fix for _delay_ms(), 118 | // which only accepts constants in future compiler releases. 119 | void delay_ms(uint16_t ms) 120 | { 121 | while ( ms-- ) { _delay_ms(1); } 122 | } 123 | 124 | 125 | // Delays variable defined microseconds. Compiler compatibility fix for _delay_us(), 126 | // which only accepts constants in future compiler releases. Written to perform more 127 | // efficiently with larger delays, as the counter adds parasitic time in each iteration. 128 | void delay_us(uint32_t us) 129 | { 130 | while (us) { 131 | if (us < 10) { 132 | _delay_us(1); 133 | us--; 134 | } else if (us < 100) { 135 | _delay_us(10); 136 | us -= 10; 137 | } else if (us < 1000) { 138 | _delay_us(100); 139 | us -= 100; 140 | } else { 141 | _delay_ms(1); 142 | us -= 1000; 143 | } 144 | } 145 | } 146 | 147 | 148 | // Simple hypotenuse computation function. 149 | float hypot_f(float x, float y) { return(sqrt(x*x + y*y)); } 150 | -------------------------------------------------------------------------------- /nuts_bolts.h: -------------------------------------------------------------------------------- 1 | /* 2 | nuts_bolts.h - Header file for shared definitions, variables, and functions 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef nuts_bolts_h 28 | #define nuts_bolts_h 29 | 30 | #define false 0 31 | #define true 1 32 | 33 | #define N_AXIS 3 // Number of axes 34 | #define X_AXIS 0 // Axis indexing value. Must start with 0 and be continuous. 35 | #define Y_AXIS 1 36 | #define Z_AXIS 2 37 | // #define A_AXIS 3 38 | 39 | #define MM_PER_INCH (25.40) 40 | #define INCH_PER_MM (0.0393701) 41 | 42 | #define TICKS_PER_MICROSECOND (F_CPU/1000000) 43 | 44 | // Useful macros 45 | #define clear_vector(a) memset(a, 0, sizeof(a)) 46 | #define clear_vector_float(a) memset(a, 0.0, sizeof(float)*N_AXIS) 47 | // #define clear_vector_long(a) memset(a, 0.0, sizeof(long)*N_AXIS) 48 | #define max(a,b) (((a) > (b)) ? (a) : (b)) 49 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 50 | 51 | // Bit field and masking macros 52 | #define bit(n) (1 << n) 53 | #define bit_true_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) |= (mask); SREG = sreg; } 54 | #define bit_false_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) &= ~(mask); SREG = sreg; } 55 | #define bit_toggle_atomic(x,mask) {uint8_t sreg = SREG; cli(); (x) ^= (mask); SREG = sreg; } 56 | #define bit_true(x,mask) (x) |= (mask) 57 | #define bit_false(x,mask) (x) &= ~(mask) 58 | #define bit_istrue(x,mask) ((x & mask) != 0) 59 | #define bit_isfalse(x,mask) ((x & mask) == 0) 60 | 61 | // Read a floating point value from a string. Line points to the input buffer, char_counter 62 | // is the indexer pointing to the current character of the line, while float_ptr is 63 | // a pointer to the result variable. Returns true when it succeeds 64 | uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr); 65 | 66 | // Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms(). 67 | void delay_ms(uint16_t ms); 68 | 69 | // Delays variable-defined microseconds. Compiler compatibility fix for _delay_us(). 70 | void delay_us(uint32_t us); 71 | 72 | // Computes hypotenuse, avoiding avr-gcc's bloated version and the extra error checking. 73 | float hypot_f(float x, float y); 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /planner.h: -------------------------------------------------------------------------------- 1 | /* 2 | planner.h - buffers movement commands and manages the acceleration profile plan 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef planner_h 28 | #define planner_h 29 | 30 | 31 | // The number of linear motions that can be in the plan at any give time 32 | #ifndef BLOCK_BUFFER_SIZE 33 | #ifdef USE_LINE_NUMBERS 34 | #define BLOCK_BUFFER_SIZE 16 35 | #else 36 | #define BLOCK_BUFFER_SIZE 18 37 | #endif 38 | #endif 39 | 40 | // This struct stores a linear movement of a g-code block motion with its critical "nominal" values 41 | // are as specified in the source g-code. 42 | typedef struct { 43 | // Fields used by the bresenham algorithm for tracing the line 44 | // NOTE: Used by stepper algorithm to execute the block correctly. Do not alter these values. 45 | uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h) 46 | uint32_t steps[N_AXIS]; // Step count along each axis 47 | uint32_t step_event_count; // The maximum step axis count and number of steps required to complete this block. 48 | 49 | // Fields used by the motion planner to manage acceleration 50 | float entry_speed_sqr; // The current planned entry speed at block junction in (mm/min)^2 51 | float max_entry_speed_sqr; // Maximum allowable entry speed based on the minimum of junction limit and 52 | // neighboring nominal speeds with overrides in (mm/min)^2 53 | float max_junction_speed_sqr; // Junction entry speed limit based on direction vectors in (mm/min)^2 54 | float nominal_speed_sqr; // Axis-limit adjusted nominal speed for this block in (mm/min)^2 55 | float acceleration; // Axis-limit adjusted line acceleration in (mm/min^2) 56 | float millimeters; // The remaining distance for this block to be executed in (mm) 57 | // uint8_t max_override; // Maximum override value based on axis speed limits 58 | 59 | #ifdef USE_LINE_NUMBERS 60 | int32_t line_number; 61 | #endif 62 | } plan_block_t; 63 | 64 | 65 | // Initialize and reset the motion plan subsystem 66 | void plan_reset(); 67 | 68 | // Add a new linear movement to the buffer. target[N_AXIS] is the signed, absolute target position 69 | // in millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed 70 | // rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes. 71 | #ifdef USE_LINE_NUMBERS 72 | void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate, int32_t line_number); 73 | #else 74 | void plan_buffer_line(float *target, float feed_rate, uint8_t invert_feed_rate); 75 | #endif 76 | 77 | // Called when the current block is no longer needed. Discards the block and makes the memory 78 | // availible for new blocks. 79 | void plan_discard_current_block(); 80 | 81 | // Gets the current block. Returns NULL if buffer empty 82 | plan_block_t *plan_get_current_block(); 83 | 84 | // Called periodically by step segment buffer. Mostly used internally by planner. 85 | uint8_t plan_next_block_index(uint8_t block_index); 86 | 87 | // Called by step segment buffer when computing executing block velocity profile. 88 | float plan_get_exec_block_exit_speed(); 89 | 90 | // Reset the planner position vector (in steps) 91 | void plan_sync_position(); 92 | 93 | // Reinitialize plan with a partially completed block 94 | void plan_cycle_reinitialize(); 95 | 96 | // Returns the number of active blocks are in the planner buffer. 97 | uint8_t plan_get_block_buffer_count(); 98 | 99 | // Returns the status of the block ring buffer. True, if buffer is full. 100 | uint8_t plan_check_full_buffer(); 101 | 102 | #endif 103 | -------------------------------------------------------------------------------- /print.c: -------------------------------------------------------------------------------- 1 | /* 2 | print.c - Functions for formatting output strings 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "serial.h" 29 | #include "settings.h" 30 | 31 | 32 | void printString(const char *s) 33 | { 34 | while (*s) 35 | serial_write(*s++); 36 | } 37 | 38 | 39 | // Print a string stored in PGM-memory 40 | void printPgmString(const char *s) 41 | { 42 | char c; 43 | while ((c = pgm_read_byte_near(s++))) 44 | serial_write(c); 45 | } 46 | 47 | 48 | // void printIntegerInBase(unsigned long n, unsigned long base) 49 | // { 50 | // unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. 51 | // unsigned long i = 0; 52 | // 53 | // if (n == 0) { 54 | // serial_write('0'); 55 | // return; 56 | // } 57 | // 58 | // while (n > 0) { 59 | // buf[i++] = n % base; 60 | // n /= base; 61 | // } 62 | // 63 | // for (; i > 0; i--) 64 | // serial_write(buf[i - 1] < 10 ? 65 | // '0' + buf[i - 1] : 66 | // 'A' + buf[i - 1] - 10); 67 | // } 68 | 69 | 70 | void print_uint8_base2(uint8_t n) 71 | { 72 | unsigned char buf[8]; 73 | uint8_t i = 0; 74 | 75 | for (; i < 8; i++) { 76 | buf[i] = n & 1; 77 | n >>= 1; 78 | } 79 | 80 | for (; i > 0; i--) 81 | serial_write('0' + buf[i - 1]); 82 | } 83 | 84 | 85 | void print_uint8_base10(uint8_t n) 86 | { 87 | if (n == 0) { 88 | serial_write('0'); 89 | return; 90 | } 91 | 92 | unsigned char buf[3]; 93 | uint8_t i = 0; 94 | 95 | while (n > 0) { 96 | buf[i++] = n % 10 + '0'; 97 | n /= 10; 98 | } 99 | 100 | for (; i > 0; i--) 101 | serial_write(buf[i - 1]); 102 | } 103 | 104 | 105 | void print_uint32_base10(unsigned long n) 106 | { 107 | if (n == 0) { 108 | serial_write('0'); 109 | return; 110 | } 111 | 112 | unsigned char buf[10]; 113 | uint8_t i = 0; 114 | 115 | while (n > 0) { 116 | buf[i++] = n % 10; 117 | n /= 10; 118 | } 119 | 120 | for (; i > 0; i--) 121 | serial_write('0' + buf[i-1]); 122 | } 123 | 124 | 125 | void printInteger(long n) 126 | { 127 | if (n < 0) { 128 | serial_write('-'); 129 | print_uint32_base10((-n)); 130 | } else { 131 | print_uint32_base10(n); 132 | } 133 | } 134 | 135 | 136 | // Convert float to string by immediately converting to a long integer, which contains 137 | // more digits than a float. Number of decimal places, which are tracked by a counter, 138 | // may be set by the user. The integer is then efficiently converted to a string. 139 | // NOTE: AVR '%' and '/' integer operations are very efficient. Bitshifting speed-up 140 | // techniques are actually just slightly slower. Found this out the hard way. 141 | void printFloat(float n, uint8_t decimal_places) 142 | { 143 | if (n < 0) { 144 | serial_write('-'); 145 | n = -n; 146 | } 147 | 148 | uint8_t decimals = decimal_places; 149 | while (decimals >= 2) { // Quickly convert values expected to be E0 to E-4. 150 | n *= 100; 151 | decimals -= 2; 152 | } 153 | if (decimals) { n *= 10; } 154 | n += 0.5; // Add rounding factor. Ensures carryover through entire value. 155 | 156 | // Generate digits backwards and store in string. 157 | unsigned char buf[10]; 158 | uint8_t i = 0; 159 | uint32_t a = (long)n; 160 | buf[decimal_places] = '.'; // Place decimal point, even if decimal places are zero. 161 | while(a > 0) { 162 | if (i == decimal_places) { i++; } // Skip decimal point location 163 | buf[i++] = (a % 10) + '0'; // Get digit 164 | a /= 10; 165 | } 166 | while (i < decimal_places) { 167 | buf[i++] = '0'; // Fill in zeros to decimal point for (n < 1) 168 | } 169 | if (i == decimal_places) { // Fill in leading zero, if needed. 170 | i++; 171 | buf[i++] = '0'; 172 | } 173 | 174 | // Print the generated string. 175 | for (; i > 0; i--) 176 | serial_write(buf[i-1]); 177 | } 178 | 179 | 180 | // Floating value printing handlers for special variables types used in Grbl and are defined 181 | // in the config.h. 182 | // - CoordValue: Handles all position or coordinate values in inches or mm reporting. 183 | // - RateValue: Handles feed rate and current velocity in inches or mm reporting. 184 | // - SettingValue: Handles all floating point settings values (always in mm.) 185 | void printFloat_CoordValue(float n) { 186 | if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { 187 | printFloat(n*INCH_PER_MM,N_DECIMAL_COORDVALUE_INCH); 188 | } else { 189 | printFloat(n,N_DECIMAL_COORDVALUE_MM); 190 | } 191 | } 192 | 193 | void printFloat_RateValue(float n) { 194 | if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { 195 | printFloat(n*INCH_PER_MM,N_DECIMAL_RATEVALUE_INCH); 196 | } else { 197 | printFloat(n,N_DECIMAL_RATEVALUE_MM); 198 | } 199 | } 200 | 201 | void printFloat_SettingValue(float n) { printFloat(n,N_DECIMAL_SETTINGVALUE); } 202 | 203 | 204 | // Debug tool to print free memory in bytes at the called point. Not used otherwise. 205 | void printFreeMemory() 206 | { 207 | extern int __heap_start, *__brkval; 208 | uint16_t free; // Up to 64k values. 209 | free = (int) &free - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 210 | printInteger((int32_t)free); 211 | printString(" "); 212 | } 213 | -------------------------------------------------------------------------------- /print.h: -------------------------------------------------------------------------------- 1 | /* 2 | print.h - Functions for formatting output strings 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef print_h 28 | #define print_h 29 | 30 | 31 | void printString(const char *s); 32 | 33 | void printPgmString(const char *s); 34 | 35 | void printInteger(long n); 36 | 37 | void print_uint32_base10(uint32_t n); 38 | 39 | void print_uint8_base2(uint8_t n); 40 | 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 -------------------------------------------------------------------------------- /probe.c: -------------------------------------------------------------------------------- 1 | /* 2 | probe.c - code pertaining to probing methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2014 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 22 | #include "system.h" 23 | #include "settings.h" 24 | #include "probe.h" 25 | 26 | // Inverts the probe pin state depending on user settings. 27 | uint8_t probe_invert_mask; 28 | 29 | 30 | // Probe pin initialization routine. 31 | void probe_init() 32 | { 33 | /*PROBE_DDR &= ~(PROBE_MASK); // Configure as input pins 34 | if (bit_istrue(settings.flags,BITFLAG_INVERT_PROBE_PIN)) { 35 | PROBE_PORT &= ~(PROBE_MASK); // Normal low operation. Requires external pull-down. 36 | probe_invert_mask = 0; 37 | } else { 38 | PROBE_PORT |= PROBE_MASK; // Enable internal pull-up resistors. Normal high operation. 39 | probe_invert_mask = PROBE_MASK; 40 | }*/ 41 | pinMode(PROBE_PIN,INPUT); 42 | digitalWrite(PROBE_PIN,HIGH); 43 | } 44 | 45 | 46 | // Returns the probe pin state. Triggered = true. Called by gcode parser and probe state monitor. 47 | uint8_t probe_get_state() { return !digitalRead(PROBE_PIN); }//return((PROBE_PIN & PROBE_MASK) ^ probe_invert_mask); } 48 | 49 | 50 | // Monitors probe pin state and records the system position when detected. Called by the 51 | // stepper ISR per ISR tick. 52 | // NOTE: This function must be extremely efficient as to not bog down the stepper ISR. 53 | void probe_state_monitor() 54 | { 55 | if (sys.probe_state == PROBE_ACTIVE) { 56 | if (probe_get_state()) { 57 | sys.probe_state = PROBE_OFF; 58 | memcpy(sys.probe_position, sys.position, sizeof(float)*N_AXIS); 59 | bit_true(sys.execute, EXEC_FEED_HOLD); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /probe.h: -------------------------------------------------------------------------------- 1 | /* 2 | probe.h - code pertaining to probing methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2014 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 // No probing. (Must be zero.) 26 | #define PROBE_ACTIVE 1 // Actively watching the input pin. 27 | 28 | 29 | // Probe pin initialization routine. 30 | void probe_init(); 31 | 32 | // Returns probe pin state. 33 | uint8_t probe_get_state(); 34 | 35 | // Monitors probe pin state and records the system position when detected. Called by the 36 | // stepper ISR per ISR tick. 37 | void probe_state_monitor(); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /protocol.c: -------------------------------------------------------------------------------- 1 | /* 2 | protocol.c - controls Grbl execution protocol and procedures 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "serial.h" 29 | #include "settings.h" 30 | #include "protocol.h" 31 | #include "gcode.h" 32 | #include "planner.h" 33 | #include "stepper.h" 34 | #include "motion_control.h" 35 | #include "report.h" 36 | 37 | 38 | static char line[LINE_BUFFER_SIZE]; // Line to be executed. Zero-terminated. 39 | 40 | 41 | // Directs and executes one line of formatted input from protocol_process. While mostly 42 | // incoming streaming g-code blocks, this also directs and executes Grbl internal commands, 43 | // such as settings, initiating the homing cycle, and toggling switch states. 44 | static void protocol_execute_line(char *line) 45 | { 46 | protocol_execute_runtime(); // Runtime command check point. 47 | if (sys.abort) { return; } // Bail to calling function upon system abort 48 | 49 | if (line[0] == 0) { 50 | // Empty or comment line. Send status message for syncing purposes. 51 | report_status_message(STATUS_OK); 52 | 53 | } else if (line[0] == '$') { 54 | // Grbl '$' system command 55 | report_status_message(system_execute_line(line)); 56 | 57 | } else if (sys.state == STATE_ALARM) { 58 | // Everything else is gcode. Block if in alarm mode. 59 | report_status_message(STATUS_ALARM_LOCK); 60 | 61 | } else { 62 | // Parse and execute g-code block! 63 | report_status_message(gc_execute_line(line)); 64 | } 65 | } 66 | 67 | 68 | /* 69 | GRBL PRIMARY LOOP: 70 | */ 71 | void protocol_main_loop() 72 | { 73 | // ------------------------------------------------------------ 74 | // Complete initialization procedures upon a power-up or reset. 75 | // ------------------------------------------------------------ 76 | 77 | // Print welcome message 78 | report_init_message(); 79 | 80 | // Check for and report alarm state after a reset, error, or an initial power up. 81 | if (sys.state == STATE_ALARM) { 82 | report_feedback_message(MESSAGE_ALARM_LOCK); 83 | } else { 84 | // All systems go! 85 | sys.state = STATE_IDLE; // Set system to ready. Clear all state flags. 86 | system_execute_startup(line); // Execute startup script. 87 | } 88 | 89 | // --------------------------------------------------------------------------------- 90 | // Primary loop! Upon a system abort, this exits back to main() to reset the system. 91 | // --------------------------------------------------------------------------------- 92 | 93 | uint8_t iscomment = false; 94 | uint8_t char_counter = 0; 95 | uint8_t c; 96 | for (;;) { 97 | 98 | // Process one line of incoming serial data, as the data becomes available. Performs an 99 | // initial filtering by removing spaces and comments and capitalizing all letters. 100 | 101 | // NOTE: While comment, spaces, and block delete(if supported) handling should technically 102 | // be done in the g-code parser, doing it here helps compress the incoming data into Grbl's 103 | // line buffer, which is limited in size. The g-code standard actually states a line can't 104 | // exceed 256 characters, but the Arduino Uno does not have the memory space for this. 105 | // With a better processor, it would be very easy to pull this initial parsing out as a 106 | // seperate task to be shared by the g-code parser and Grbl's system commands. 107 | 108 | while((c = serial_read()) != SERIAL_NO_DATA) { 109 | if ((c == '\n') || (c == '\r')) { // End of line reached 110 | line[char_counter] = 0; // Set string termination character. 111 | protocol_execute_line(line); // Line is complete. Execute it! 112 | iscomment = false; 113 | char_counter = 0; 114 | } else { 115 | if (iscomment) { 116 | // Throw away all comment characters 117 | if (c == ')') { 118 | // End of comment. Resume line. 119 | iscomment = false; 120 | } 121 | } else { 122 | if (c <= ' ') { 123 | // Throw away whitepace and control characters 124 | } else if (c == '/') { 125 | // Block delete NOT SUPPORTED. Ignore character. 126 | // NOTE: If supported, would simply need to check the system if block delete is enabled. 127 | } else if (c == '(') { 128 | // Enable comments flag and ignore all characters until ')' or EOL. 129 | // NOTE: This doesn't follow the NIST definition exactly, but is good enough for now. 130 | // In the future, we could simply remove the items within the comments, but retain the 131 | // comment control characters, so that the g-code parser can error-check it. 132 | iscomment = true; 133 | // } else if (c == ';') { 134 | // Comment character to EOL NOT SUPPORTED. LinuxCNC definition. Not NIST. 135 | 136 | // TODO: Install '%' feature 137 | // } else if (c == '%') { 138 | // Program start-end percent sign NOT SUPPORTED. 139 | // NOTE: This maybe installed to tell Grbl when a program is running vs manual input, 140 | // where, during a program, the system auto-cycle start will continue to execute 141 | // everything until the next '%' sign. This will help fix resuming issues with certain 142 | // functions that empty the planner buffer to execute its task on-time. 143 | 144 | } else if (char_counter >= (LINE_BUFFER_SIZE-1)) { 145 | // Detect line buffer overflow. Report error and reset line buffer. 146 | report_status_message(STATUS_OVERFLOW); 147 | iscomment = false; 148 | char_counter = 0; 149 | } else if (c >= 'a' && c <= 'z') { // Upcase lowercase 150 | line[char_counter++] = c-'a'+'A'; 151 | } else { 152 | line[char_counter++] = c; 153 | } 154 | } 155 | } 156 | } 157 | 158 | // If there are no more characters in the serial read buffer to be processed and executed, 159 | // this indicates that g-code streaming has either filled the planner buffer or has 160 | // completed. In either case, auto-cycle start, if enabled, any queued moves. 161 | protocol_auto_cycle_start(); 162 | 163 | protocol_execute_runtime(); // Runtime command check point. 164 | if (sys.abort) { return; } // Bail to main() program loop to reset system. 165 | 166 | } 167 | 168 | return; /* Never reached */ 169 | } 170 | 171 | 172 | // Executes run-time commands, when required. This is called from various check points in the main 173 | // program, primarily where there may be a while loop waiting for a buffer to clear space or any 174 | // point where the execution time from the last check point may be more than a fraction of a second. 175 | // This is a way to execute runtime commands asynchronously (aka multitasking) with grbl's g-code 176 | // parsing and planning functions. This function also serves as an interface for the interrupts to 177 | // set the system runtime flags, where only the main program handles them, removing the need to 178 | // define more computationally-expensive volatile variables. This also provides a controlled way to 179 | // execute certain tasks without having two or more instances of the same task, such as the planner 180 | // recalculating the buffer upon a feedhold or override. 181 | // NOTE: The sys.execute variable flags are set by any process, step or serial interrupts, pinouts, 182 | // limit switches, or the main program. 183 | void protocol_execute_runtime() 184 | { 185 | uint8_t rt_exec = sys.execute; // Copy to avoid calling volatile multiple times 186 | if (rt_exec) { // Enter only if any bit flag is true 187 | 188 | // System alarm. Everything has shutdown by something that has gone severely wrong. Report 189 | // the source of the error to the user. If critical, Grbl disables by entering an infinite 190 | // loop until system reset/abort. 191 | if (rt_exec & (EXEC_ALARM | EXEC_CRIT_EVENT)) { 192 | sys.state = STATE_ALARM; // Set system alarm state 193 | 194 | // Critical events. Hard/soft limit events identified by both critical event and alarm exec 195 | // flags. Probe fail is identified by the critical event exec flag only. 196 | if (rt_exec & EXEC_CRIT_EVENT) { 197 | if (rt_exec & EXEC_ALARM) { report_alarm_message(ALARM_LIMIT_ERROR); } 198 | else { report_alarm_message(ALARM_PROBE_FAIL); } 199 | report_feedback_message(MESSAGE_CRITICAL_EVENT); 200 | bit_false_atomic(sys.execute,EXEC_RESET); // Disable any existing reset 201 | do { 202 | // Nothing. Block EVERYTHING until user issues reset or power cycles. Hard limits 203 | // typically occur while unattended or not paying attention. Gives the user time 204 | // to do what is needed before resetting, like killing the incoming stream. The 205 | // same could be said about soft limits. While the position is not lost, the incoming 206 | // stream could be still engaged and cause a serious crash if it continues afterwards. 207 | } while (bit_isfalse(sys.execute,EXEC_RESET)); 208 | 209 | // Standard alarm event. Only abort during motion qualifies. 210 | } else { 211 | // Runtime abort command issued during a cycle, feed hold, or homing cycle. Message the 212 | // user that position may have been lost and set alarm state to enable the alarm lockout 213 | // to indicate the possible severity of the problem. 214 | report_alarm_message(ALARM_ABORT_CYCLE); 215 | } 216 | bit_false_atomic(sys.execute,(EXEC_ALARM | EXEC_CRIT_EVENT)); 217 | } 218 | 219 | // Execute system abort. 220 | if (rt_exec & EXEC_RESET) { 221 | sys.abort = true; // Only place this is set true. 222 | return; // Nothing else to do but exit. 223 | } 224 | 225 | // Execute and serial print status 226 | if (rt_exec & EXEC_STATUS_REPORT) { 227 | report_realtime_status(); 228 | bit_false_atomic(sys.execute,EXEC_STATUS_REPORT); 229 | } 230 | 231 | // Execute a feed hold with deceleration, only during cycle. 232 | if (rt_exec & EXEC_FEED_HOLD) { 233 | // !!! During a cycle, the segment buffer has just been reloaded and full. So the math involved 234 | // with the feed hold should be fine for most, if not all, operational scenarios. 235 | if (sys.state == STATE_CYCLE) { 236 | sys.state = STATE_HOLD; 237 | st_update_plan_block_parameters(); 238 | st_prep_buffer(); 239 | sys.auto_start = false; // Disable planner auto start upon feed hold. 240 | } 241 | bit_false_atomic(sys.execute,EXEC_FEED_HOLD); 242 | } 243 | 244 | // Execute a cycle start by starting the stepper interrupt begin executing the blocks in queue. 245 | if (rt_exec & EXEC_CYCLE_START) { 246 | if (sys.state == STATE_QUEUED) { 247 | sys.state = STATE_CYCLE; 248 | st_prep_buffer(); // Initialize step segment buffer before beginning cycle. 249 | st_wake_up(); 250 | if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { 251 | sys.auto_start = true; // Re-enable auto start after feed hold. 252 | } else { 253 | sys.auto_start = false; // Reset auto start per settings. 254 | } 255 | } 256 | bit_false_atomic(sys.execute,EXEC_CYCLE_START); 257 | } 258 | 259 | // Reinitializes the cycle plan and stepper system after a feed hold for a resume. Called by 260 | // runtime command execution in the main program, ensuring that the planner re-plans safely. 261 | // NOTE: Bresenham algorithm variables are still maintained through both the planner and stepper 262 | // cycle reinitializations. The stepper path should continue exactly as if nothing has happened. 263 | // NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes. 264 | if (rt_exec & EXEC_CYCLE_STOP) { 265 | if ( plan_get_current_block() ) { sys.state = STATE_QUEUED; } 266 | else { sys.state = STATE_IDLE; } 267 | bit_false_atomic(sys.execute,EXEC_CYCLE_STOP); 268 | } 269 | 270 | } 271 | 272 | // Overrides flag byte (sys.override) and execution should be installed here, since they 273 | // are runtime and require a direct and controlled interface to the main stepper program. 274 | 275 | // Reload step segment buffer 276 | if (sys.state & (STATE_CYCLE | STATE_HOLD | STATE_HOMING)) { st_prep_buffer(); } 277 | 278 | } 279 | 280 | 281 | // Block until all buffered steps are executed or in a cycle state. Works with feed hold 282 | // during a synchronize call, if it should happen. Also, waits for clean cycle end. 283 | void protocol_buffer_synchronize() 284 | { 285 | // If system is queued, ensure cycle resumes if the auto start flag is present. 286 | protocol_auto_cycle_start(); 287 | // Check and set auto start to resume cycle after synchronize and caller completes. 288 | if (sys.state == STATE_CYCLE) { sys.auto_start = true; } 289 | while (plan_get_current_block() || (sys.state == STATE_CYCLE)) { 290 | protocol_execute_runtime(); // Check and execute run-time commands 291 | if (sys.abort) { return; } // Check for system abort 292 | } 293 | } 294 | 295 | 296 | // Auto-cycle start has two purposes: 1. Resumes a plan_synchronize() call from a function that 297 | // requires the planner buffer to empty (spindle enable, dwell, etc.) 2. As a user setting that 298 | // automatically begins the cycle when a user enters a valid motion command manually. This is 299 | // intended as a beginners feature to help new users to understand g-code. It can be disabled 300 | // as a beginner tool, but (1.) still operates. If disabled, the operation of cycle start is 301 | // manually issuing a cycle start command whenever the user is ready and there is a valid motion 302 | // command in the planner queue. 303 | // NOTE: This function is called from the main loop and mc_line() only and executes when one of 304 | // two conditions exist respectively: There are no more blocks sent (i.e. streaming is finished, 305 | // single commands), or the planner buffer is full and ready to go. 306 | void protocol_auto_cycle_start() { if (sys.auto_start) { bit_true_atomic(sys.execute, EXEC_CYCLE_START); } } 307 | -------------------------------------------------------------------------------- /protocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | protocol.h - controls Grbl execution protocol and procedures 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef protocol_h 28 | #define protocol_h 29 | 30 | // Line buffer size from the serial input stream to be executed. 31 | // NOTE: Not a problem except for extreme cases, but the line buffer size can be too small 32 | // and g-code blocks can get truncated. Officially, the g-code standards support up to 256 33 | // characters. In future versions, this will be increased, when we know how much extra 34 | // memory space we can invest into here or we re-write the g-code parser not to have this 35 | // buffer. 36 | #ifndef LINE_BUFFER_SIZE 37 | #define LINE_BUFFER_SIZE 80 38 | #endif 39 | 40 | // Starts Grbl main loop. It handles all incoming characters from the serial port and executes 41 | // them as they complete. It is also responsible for finishing the initialization procedures. 42 | void protocol_main_loop(); 43 | 44 | // Checks and executes a runtime command at various stop points in main program 45 | void protocol_execute_runtime(); 46 | 47 | // Notify the stepper subsystem to start executing the g-code program in buffer. 48 | // void protocol_cycle_start(); 49 | 50 | // Reinitializes the buffer after a feed hold for a resume. 51 | // void protocol_cycle_reinitialize(); 52 | 53 | // Initiates a feed hold of the running program 54 | // void protocol_feed_hold(); 55 | 56 | // Executes the auto cycle feature, if enabled. 57 | void protocol_auto_cycle_start(); 58 | 59 | // Block until all buffered steps are executed 60 | void protocol_buffer_synchronize(); 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /ramps.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: ramps.h 3 | * Author: arsi 4 | * 5 | * Created on September 1, 2014, 4:44 PM 6 | */ 7 | #ifndef RAMPS_H 8 | #define RAMPS_H 9 | 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include"cpu_map.h" 16 | #include 17 | #include"fastio.h" 18 | 19 | #define CHECK(var,pos) ((var) & (1<<(pos))) 20 | 21 | extern void rampsInitCoolant(); 22 | extern void rampsInitSpindle(); 23 | extern void rampsInitSteppers(); 24 | extern void rampsWriteDisable(uint8_t value); 25 | extern void rampsWriteSteps(uint8_t value); 26 | extern void rampsWriteDirections(uint8_t value); 27 | extern void rampsStartSpindle(); 28 | extern void rampsStopSpindle(); 29 | extern void rampsLeftSpindle(); 30 | extern void rampsRightSpindle(); 31 | extern void rampsCoolantOn(); 32 | extern void rampsCoolantOff(); 33 | 34 | inline void rampsCoolantOn() { 35 | WRITE(COOLANT_FLOOD_PIN, 1); 36 | } 37 | 38 | inline void rampsCoolantOff() { 39 | WRITE(COOLANT_FLOOD_PIN, 0); 40 | } 41 | 42 | inline void rampsLeftSpindle() { 43 | WRITE(SPINDLE_DIRECTION_PIN, 1); 44 | } 45 | 46 | inline void rampsRightSpindle() { 47 | WRITE(SPINDLE_DIRECTION_PIN, 0); 48 | } 49 | 50 | inline void rampsStartSpindle() { 51 | WRITE(SPINDLE_ENABLE_PIN, 1); 52 | } 53 | 54 | inline void rampsStopSpindle() { 55 | WRITE(SPINDLE_ENABLE_PIN, 0); 56 | } 57 | 58 | inline void rampsInitCoolant() { 59 | SET_OUTPUT(COOLANT_FLOOD_PIN); 60 | } 61 | 62 | inline void rampsInitSpindle() { 63 | SET_OUTPUT(SPINDLE_ENABLE_PIN); 64 | SET_OUTPUT(SPINDLE_DIRECTION_PIN); 65 | } 66 | 67 | /** 68 | * Perform port direction init for ramps for steppers 69 | */ 70 | inline void rampsInitSteppers() { 71 | SET_OUTPUT(X_STEP_PIN); 72 | SET_OUTPUT(Y_STEP_PIN); 73 | SET_OUTPUT(Z_STEP_PIN); 74 | SET_OUTPUT(X_DIR_PIN); 75 | SET_OUTPUT(Y_DIR_PIN); 76 | SET_OUTPUT(Z_DIR_PIN); 77 | SET_OUTPUT(X_ENABLE_PIN); 78 | SET_OUTPUT(Y_ENABLE_PIN); 79 | SET_OUTPUT(Z_ENABLE_PIN); 80 | } 81 | 82 | /** 83 | * Perform set of EN driver signal 84 | * @param value 85 | */ 86 | inline void rampsWriteDisable(uint8_t value) { 87 | if (CHECK(value, STEPPERS_DISABLE_BIT)) { 88 | WRITE(X_ENABLE_PIN, 1); 89 | WRITE(Y_ENABLE_PIN, 1); 90 | WRITE(Z_ENABLE_PIN, 1); 91 | } else { 92 | WRITE(X_ENABLE_PIN, 0); 93 | WRITE(Y_ENABLE_PIN, 0); 94 | WRITE(Z_ENABLE_PIN, 0); 95 | } 96 | } 97 | 98 | /** 99 | * write stepper pulse 100 | * @param value 101 | */ 102 | inline void rampsWriteSteps(uint8_t value) { 103 | if (CHECK(value, X_STEP_BIT)) { 104 | WRITE(X_STEP_PIN, 1); 105 | } else { 106 | WRITE(X_STEP_PIN, 0); 107 | } 108 | if (CHECK(value, Y_STEP_BIT)) { 109 | WRITE(Y_STEP_PIN, 1); 110 | } else { 111 | WRITE(Y_STEP_PIN, 0); 112 | } 113 | if (CHECK(value, Z_STEP_BIT)) { 114 | WRITE(Z_STEP_PIN, 1); 115 | } else { 116 | WRITE(Z_STEP_PIN, 0); 117 | } 118 | 119 | } 120 | 121 | /** 122 | * set stepper direction 123 | * @param value 124 | */ 125 | inline void rampsWriteDirections(uint8_t value) { 126 | if (CHECK(value, X_DIRECTION_BIT)) { 127 | WRITE(X_DIR_PIN, 1); 128 | } else { 129 | WRITE(X_DIR_PIN, 0); 130 | } 131 | if (CHECK(value, Y_DIRECTION_BIT)) { 132 | WRITE(Y_DIR_PIN, 1); 133 | } else { 134 | WRITE(Y_DIR_PIN, 0); 135 | } 136 | if (CHECK(value, Z_DIRECTION_BIT)) { 137 | WRITE(Z_DIR_PIN, 1); 138 | } else { 139 | WRITE(Z_DIR_PIN, 0); 140 | } 141 | } 142 | 143 | 144 | #ifdef __cplusplus 145 | } 146 | #endif 147 | 148 | #endif /* RAMPS_H */ 149 | 150 | -------------------------------------------------------------------------------- /report.h: -------------------------------------------------------------------------------- 1 | /* 2 | report.h - reporting and messaging methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 | 37 | #define STATUS_GCODE_UNSUPPORTED_COMMAND 20 38 | #define STATUS_GCODE_MODAL_GROUP_VIOLATION 21 39 | #define STATUS_GCODE_UNDEFINED_FEED_RATE 22 40 | #define STATUS_GCODE_COMMAND_VALUE_NOT_INTEGER 23 41 | #define STATUS_GCODE_AXIS_COMMAND_CONFLICT 24 42 | #define STATUS_GCODE_WORD_REPEATED 25 43 | #define STATUS_GCODE_NO_AXIS_WORDS 26 44 | #define STATUS_GCODE_INVALID_LINE_NUMBER 27 45 | #define STATUS_GCODE_VALUE_WORD_MISSING 28 46 | #define STATUS_GCODE_UNSUPPORTED_COORD_SYS 29 47 | #define STATUS_GCODE_G53_INVALID_MOTION_MODE 30 48 | #define STATUS_GCODE_AXIS_WORDS_EXIST 31 49 | #define STATUS_GCODE_NO_AXIS_WORDS_IN_PLANE 32 50 | #define STATUS_GCODE_INVALID_TARGET 33 51 | #define STATUS_GCODE_ARC_RADIUS_ERROR 34 52 | #define STATUS_GCODE_NO_OFFSETS_IN_PLANE 35 53 | #define STATUS_GCODE_UNUSED_WORDS 36 54 | #define STATUS_GCODE_G43_DYNAMIC_AXIS_ERROR 37 55 | 56 | // Define Grbl alarm codes. Less than zero to distinguish alarm error from status error. 57 | #define ALARM_LIMIT_ERROR -1 58 | #define ALARM_ABORT_CYCLE -2 59 | #define ALARM_PROBE_FAIL -3 60 | 61 | // Define Grbl feedback message codes. 62 | #define MESSAGE_CRITICAL_EVENT 1 63 | #define MESSAGE_ALARM_LOCK 2 64 | #define MESSAGE_ALARM_UNLOCK 3 65 | #define MESSAGE_ENABLED 4 66 | #define MESSAGE_DISABLED 5 67 | 68 | // Prints system status messages. 69 | void report_status_message(uint8_t status_code); 70 | 71 | // Prints system alarm messages. 72 | void report_alarm_message(int8_t alarm_code); 73 | 74 | // Prints miscellaneous feedback messages. 75 | void report_feedback_message(uint8_t message_code); 76 | 77 | // Prints welcome message 78 | void report_init_message(); 79 | 80 | // Prints Grbl help and current global settings 81 | void report_grbl_help(); 82 | 83 | // Prints Grbl global settings 84 | void report_grbl_settings(); 85 | 86 | // Prints realtime status report 87 | void report_realtime_status(); 88 | 89 | // Prints recorded probe position 90 | void report_probe_parameters(); 91 | 92 | // Prints Grbl NGC parameters (coordinate offsets, probe) 93 | void report_ngc_parameters(); 94 | 95 | // Prints current g-code parser mode state 96 | void report_gcode_modes(); 97 | 98 | // Prints startup line 99 | void report_startup_line(uint8_t n, char *line); 100 | 101 | // Prints build info and user info 102 | void report_build_info(char *line); 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /serial.c: -------------------------------------------------------------------------------- 1 | /* 2 | serial.c - Low level functions for sending and recieving bytes via the serial port 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include 28 | #include "system.h" 29 | #include "serial.h" 30 | #include "motion_control.h" 31 | #include "protocol.h" 32 | 33 | 34 | uint8_t serial_rx_buffer[RX_BUFFER_SIZE]; 35 | uint8_t serial_rx_buffer_head = 0; 36 | volatile uint8_t serial_rx_buffer_tail = 0; 37 | 38 | uint8_t serial_tx_buffer[TX_BUFFER_SIZE]; 39 | uint8_t serial_tx_buffer_head = 0; 40 | volatile uint8_t serial_tx_buffer_tail = 0; 41 | 42 | 43 | #ifdef ENABLE_XONXOFF 44 | volatile uint8_t flow_ctrl = XON_SENT; // Flow control state variable 45 | #endif 46 | 47 | 48 | // Returns the number of bytes used in the RX serial buffer. 49 | uint8_t serial_get_rx_buffer_count() 50 | { 51 | uint8_t rtail = serial_rx_buffer_tail; // Copy to limit multiple calls to volatile 52 | if (serial_rx_buffer_head >= rtail) { return(serial_rx_buffer_head-rtail); } 53 | return (RX_BUFFER_SIZE - (rtail-serial_rx_buffer_head)); 54 | } 55 | 56 | 57 | // Returns the number of bytes used in the TX serial buffer. 58 | // NOTE: Not used except for debugging and ensuring no TX bottlenecks. 59 | uint8_t serial_get_tx_buffer_count() 60 | { 61 | uint8_t ttail = serial_tx_buffer_tail; // Copy to limit multiple calls to volatile 62 | if (serial_tx_buffer_head >= ttail) { return(serial_tx_buffer_head-ttail); } 63 | return (TX_BUFFER_SIZE - (ttail-serial_tx_buffer_head)); 64 | } 65 | 66 | 67 | void serial_init() 68 | { 69 | // Set baud rate 70 | #if BAUD_RATE < 57600 71 | uint16_t UBRR0_value = ((F_CPU / (8L * BAUD_RATE)) - 1)/2 ; 72 | UCSR0A &= ~(1 << U2X0); // baud doubler off - Only needed on Uno XXX 73 | #else 74 | uint16_t UBRR0_value = ((F_CPU / (4L * BAUD_RATE)) - 1)/2; 75 | UCSR0A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200 76 | #endif 77 | UBRR0H = UBRR0_value >> 8; 78 | UBRR0L = UBRR0_value; 79 | 80 | // enable rx and tx 81 | UCSR0B |= 1<= RX_BUFFER_FULL) && flow_ctrl == XON_SENT) { 191 | flow_ctrl = SEND_XOFF; 192 | UCSR0B |= (1 << UDRIE0); // Force TX 193 | } 194 | #endif 195 | 196 | } 197 | //TODO: else alarm on overflow? 198 | } 199 | } 200 | 201 | 202 | void serial_reset_read_buffer() 203 | { 204 | serial_rx_buffer_tail = serial_rx_buffer_head; 205 | 206 | #ifdef ENABLE_XONXOFF 207 | flow_ctrl = XON_SENT; 208 | #endif 209 | } 210 | -------------------------------------------------------------------------------- /serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | serial.c - Low level functions for sending and recieving bytes via the serial port 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef serial_h 28 | #define serial_h 29 | 30 | 31 | #ifndef RX_BUFFER_SIZE 32 | #define RX_BUFFER_SIZE 128 33 | #endif 34 | #ifndef TX_BUFFER_SIZE 35 | #define TX_BUFFER_SIZE 64 36 | #endif 37 | 38 | #define SERIAL_NO_DATA 0xff 39 | 40 | #ifdef ENABLE_XONXOFF 41 | #define RX_BUFFER_FULL 96 // XOFF high watermark 42 | #define RX_BUFFER_LOW 64 // XON low watermark 43 | #define SEND_XOFF 1 44 | #define SEND_XON 2 45 | #define XOFF_SENT 3 46 | #define XON_SENT 4 47 | #define XOFF_CHAR 0x13 48 | #define XON_CHAR 0x11 49 | #endif 50 | 51 | void serial_init(); 52 | 53 | // Writes one byte to the TX serial buffer. Called by main program. 54 | void serial_write(uint8_t data); 55 | 56 | // Fetches the first byte in the serial read buffer. Called by main program. 57 | uint8_t serial_read(); 58 | 59 | // Reset and empty data in read buffer. Used by e-stop and reset. 60 | void serial_reset_read_buffer(); 61 | 62 | // Returns the number of bytes used in the RX serial buffer. 63 | uint8_t serial_get_rx_buffer_count(); 64 | 65 | // Returns the number of bytes used in the TX serial buffer. 66 | // NOTE: Not used except for debugging and ensuring no TX bottlenecks. 67 | uint8_t serial_get_tx_buffer_count(); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /settings.c: -------------------------------------------------------------------------------- 1 | /* 2 | settings.c - eeprom configuration handling 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "settings.h" 29 | #include "eeprom.h" 30 | #include "protocol.h" 31 | #include "report.h" 32 | #include "limits.h" 33 | #include "stepper.h" 34 | 35 | settings_t settings; 36 | 37 | 38 | // Method to store startup lines into EEPROM 39 | void settings_store_startup_line(uint8_t n, char *line) 40 | { 41 | uint32_t addr = n*(LINE_BUFFER_SIZE+1)+EEPROM_ADDR_STARTUP_BLOCK; 42 | memcpy_to_eeprom_with_checksum(addr,(char*)line, LINE_BUFFER_SIZE); 43 | } 44 | 45 | 46 | // Method to store build info into EEPROM 47 | void settings_store_build_info(char *line) 48 | { 49 | memcpy_to_eeprom_with_checksum(EEPROM_ADDR_BUILD_INFO,(char*)line, LINE_BUFFER_SIZE); 50 | } 51 | 52 | 53 | // Method to store coord data parameters into EEPROM 54 | void settings_write_coord_data(uint8_t coord_select, float *coord_data) 55 | { 56 | uint32_t addr = coord_select*(sizeof(float)*N_AXIS+1) + EEPROM_ADDR_PARAMETERS; 57 | memcpy_to_eeprom_with_checksum(addr,(char*)coord_data, sizeof(float)*N_AXIS); 58 | } 59 | 60 | 61 | // Method to store Grbl global settings struct and version number into EEPROM 62 | void write_global_settings() 63 | { 64 | eeprom_put_char(0, SETTINGS_VERSION); 65 | memcpy_to_eeprom_with_checksum(EEPROM_ADDR_GLOBAL, (char*)&settings, sizeof(settings_t)); 66 | } 67 | 68 | 69 | // Method to restore EEPROM-saved Grbl global settings back to defaults. 70 | void settings_restore_global_settings() { 71 | settings.pulse_microseconds = DEFAULT_STEP_PULSE_MICROSECONDS; 72 | settings.stepper_idle_lock_time = DEFAULT_STEPPER_IDLE_LOCK_TIME; 73 | settings.step_invert_mask = DEFAULT_STEPPING_INVERT_MASK; 74 | settings.dir_invert_mask = DEFAULT_DIRECTION_INVERT_MASK; 75 | settings.status_report_mask = DEFAULT_STATUS_REPORT_MASK; 76 | settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION; 77 | settings.arc_tolerance = DEFAULT_ARC_TOLERANCE; 78 | settings.homing_dir_mask = DEFAULT_HOMING_DIR_MASK; 79 | settings.homing_feed_rate = DEFAULT_HOMING_FEED_RATE; 80 | settings.homing_seek_rate = DEFAULT_HOMING_SEEK_RATE; 81 | settings.homing_debounce_delay = DEFAULT_HOMING_DEBOUNCE_DELAY; 82 | settings.homing_pulloff = DEFAULT_HOMING_PULLOFF; 83 | 84 | settings.flags = 0; 85 | if (DEFAULT_REPORT_INCHES) { settings.flags |= BITFLAG_REPORT_INCHES; } 86 | if (DEFAULT_AUTO_START) { settings.flags |= BITFLAG_AUTO_START; } 87 | if (DEFAULT_INVERT_ST_ENABLE) { settings.flags |= BITFLAG_INVERT_ST_ENABLE; } 88 | if (DEFAULT_INVERT_LIMIT_PINS) { settings.flags |= BITFLAG_INVERT_LIMIT_PINS; } 89 | if (DEFAULT_SOFT_LIMIT_ENABLE) { settings.flags |= BITFLAG_SOFT_LIMIT_ENABLE; } 90 | if (DEFAULT_HARD_LIMIT_ENABLE) { settings.flags |= BITFLAG_HARD_LIMIT_ENABLE; } 91 | if (DEFAULT_HOMING_ENABLE) { settings.flags |= BITFLAG_HOMING_ENABLE; } 92 | 93 | settings.steps_per_mm[X_AXIS] = DEFAULT_X_STEPS_PER_MM; 94 | settings.steps_per_mm[Y_AXIS] = DEFAULT_Y_STEPS_PER_MM; 95 | settings.steps_per_mm[Z_AXIS] = DEFAULT_Z_STEPS_PER_MM; 96 | settings.max_rate[X_AXIS] = DEFAULT_X_MAX_RATE; 97 | settings.max_rate[Y_AXIS] = DEFAULT_Y_MAX_RATE; 98 | settings.max_rate[Z_AXIS] = DEFAULT_Z_MAX_RATE; 99 | settings.acceleration[X_AXIS] = DEFAULT_X_ACCELERATION; 100 | settings.acceleration[Y_AXIS] = DEFAULT_Y_ACCELERATION; 101 | settings.acceleration[Z_AXIS] = DEFAULT_Z_ACCELERATION; 102 | settings.max_travel[X_AXIS] = (-DEFAULT_X_MAX_TRAVEL); 103 | settings.max_travel[Y_AXIS] = (-DEFAULT_Y_MAX_TRAVEL); 104 | settings.max_travel[Z_AXIS] = (-DEFAULT_Z_MAX_TRAVEL); 105 | 106 | write_global_settings(); 107 | } 108 | 109 | 110 | // Helper function to clear the EEPROM space containing parameter data. 111 | void settings_clear_parameters() { 112 | uint8_t idx; 113 | float coord_data[3]; 114 | memset(&coord_data, 0, sizeof(coord_data)); 115 | for (idx=0; idx < SETTING_INDEX_NCOORD; idx++) { settings_write_coord_data(idx, coord_data); } 116 | } 117 | 118 | 119 | // Helper function to clear the EEPROM space containing the startup lines. 120 | void settings_clear_startup_lines() { 121 | #if N_STARTUP_LINE > 0 122 | eeprom_put_char(EEPROM_ADDR_STARTUP_BLOCK, 0); 123 | #endif 124 | #if N_STARTUP_LINE > 1 125 | eeprom_put_char(EEPROM_ADDR_STARTUP_BLOCK+(LINE_BUFFER_SIZE+1), 0); 126 | #endif 127 | } 128 | 129 | 130 | // Helper function to clear the EEPROM space containing the user build info string. 131 | void settings_clear_build_info() { eeprom_put_char(EEPROM_ADDR_BUILD_INFO , 0); } 132 | 133 | 134 | // Reads startup line from EEPROM. Updated pointed line string data. 135 | uint8_t settings_read_startup_line(uint8_t n, char *line) 136 | { 137 | uint32_t addr = n*(LINE_BUFFER_SIZE+1)+EEPROM_ADDR_STARTUP_BLOCK; 138 | if (!(memcpy_from_eeprom_with_checksum((char*)line, addr, LINE_BUFFER_SIZE))) { 139 | // Reset line with default value 140 | line[0] = 0; // Empty line 141 | settings_store_startup_line(n, line); 142 | return(false); 143 | } 144 | return(true); 145 | } 146 | 147 | 148 | // Reads startup line from EEPROM. Updated pointed line string data. 149 | uint8_t settings_read_build_info(char *line) 150 | { 151 | if (!(memcpy_from_eeprom_with_checksum((char*)line, EEPROM_ADDR_BUILD_INFO, LINE_BUFFER_SIZE))) { 152 | // Reset line with default value 153 | line[0] = 0; // Empty line 154 | settings_store_build_info(line); 155 | return(false); 156 | } 157 | return(true); 158 | } 159 | 160 | 161 | // Read selected coordinate data from EEPROM. Updates pointed coord_data value. 162 | uint8_t settings_read_coord_data(uint8_t coord_select, float *coord_data) 163 | { 164 | uint32_t addr = coord_select*(sizeof(float)*N_AXIS+1) + EEPROM_ADDR_PARAMETERS; 165 | if (!(memcpy_from_eeprom_with_checksum((char*)coord_data, addr, sizeof(float)*N_AXIS))) { 166 | // Reset with default zero vector 167 | clear_vector_float(coord_data); 168 | settings_write_coord_data(coord_select,coord_data); 169 | return(false); 170 | } 171 | return(true); 172 | } 173 | 174 | 175 | // Reads Grbl global settings struct from EEPROM. 176 | uint8_t read_global_settings() { 177 | // Check version-byte of eeprom 178 | uint8_t version = eeprom_get_char(0); 179 | if (version == SETTINGS_VERSION) { 180 | // Read settings-record and check checksum 181 | if (!(memcpy_from_eeprom_with_checksum((char*)&settings, EEPROM_ADDR_GLOBAL, sizeof(settings_t)))) { 182 | return(false); 183 | } 184 | } else { 185 | return(false); 186 | } 187 | return(true); 188 | } 189 | 190 | 191 | // A helper method to set settings from command line 192 | uint8_t settings_store_global_setting(uint8_t parameter, float value) { 193 | if (value < 0.0) { return(STATUS_NEGATIVE_VALUE); } 194 | if (parameter >= AXIS_SETTINGS_START_VAL) { 195 | // Store axis configuration. Axis numbering sequence set by AXIS_SETTING defines. 196 | // NOTE: Ensure the setting index corresponds to the report.c settings printout. 197 | parameter -= AXIS_SETTINGS_START_VAL; 198 | uint8_t set_idx = 0; 199 | while (set_idx < AXIS_N_SETTINGS) { 200 | if (parameter < N_AXIS) { 201 | // Valid axis setting found. 202 | switch (set_idx) { 203 | case 0: settings.steps_per_mm[parameter] = value; break; 204 | case 1: settings.max_rate[parameter] = value; break; 205 | case 2: settings.acceleration[parameter] = value*60*60; break; // Convert to mm/min^2 for grbl internal use. 206 | case 3: settings.max_travel[parameter] = -value; break; // Store as negative for grbl internal use. 207 | } 208 | break; // Exit while-loop after setting has been configured and proceed to the EEPROM write call. 209 | } else { 210 | set_idx++; 211 | // If axis index greater than N_AXIS or setting index greater than number of axis settings, error out. 212 | if ((parameter < AXIS_SETTINGS_INCREMENT) || (set_idx == AXIS_N_SETTINGS)) { return(STATUS_INVALID_STATEMENT); } 213 | parameter -= AXIS_SETTINGS_INCREMENT; 214 | } 215 | } 216 | } else { 217 | // Store non-axis Grbl settings 218 | uint8_t int_value = trunc(value); 219 | switch(parameter) { 220 | case 0: 221 | if (int_value < 3) { return(STATUS_SETTING_STEP_PULSE_MIN); } 222 | settings.pulse_microseconds = int_value; break; 223 | case 1: settings.stepper_idle_lock_time = int_value; break; 224 | case 2: 225 | settings.step_invert_mask = int_value; 226 | st_generate_step_dir_invert_masks(); // Regenerate step and direction port invert masks. 227 | break; 228 | case 3: 229 | settings.dir_invert_mask = int_value; 230 | st_generate_step_dir_invert_masks(); // Regenerate step and direction port invert masks. 231 | break; 232 | case 4: // Reset to ensure change. Immediate re-init may cause problems. 233 | if (int_value) { settings.flags |= BITFLAG_INVERT_ST_ENABLE; } 234 | else { settings.flags &= ~BITFLAG_INVERT_ST_ENABLE; } 235 | break; 236 | case 5: // Reset to ensure change. Immediate re-init may cause problems. 237 | if (int_value) { settings.flags |= BITFLAG_INVERT_LIMIT_PINS; } 238 | else { settings.flags &= ~BITFLAG_INVERT_LIMIT_PINS; } 239 | break; 240 | case 6: // Reset to ensure change. Immediate re-init may cause problems. 241 | if (int_value) { settings.flags |= BITFLAG_INVERT_PROBE_PIN; } 242 | else { settings.flags &= ~BITFLAG_INVERT_PROBE_PIN; } 243 | break; 244 | case 10: settings.status_report_mask = int_value; 245 | case 11: settings.junction_deviation = value; break; 246 | case 12: settings.arc_tolerance = value; break; 247 | case 13: 248 | if (int_value) { settings.flags |= BITFLAG_REPORT_INCHES; } 249 | else { settings.flags &= ~BITFLAG_REPORT_INCHES; } 250 | break; 251 | case 14: // Reset to ensure change. Immediate re-init may cause problems. 252 | if (int_value) { settings.flags |= BITFLAG_AUTO_START; } 253 | else { settings.flags &= ~BITFLAG_AUTO_START; } 254 | break; 255 | case 20: 256 | if (int_value) { 257 | if (bit_isfalse(settings.flags, BITFLAG_HOMING_ENABLE)) { return(STATUS_SOFT_LIMIT_ERROR); } 258 | settings.flags |= BITFLAG_SOFT_LIMIT_ENABLE; 259 | } else { settings.flags &= ~BITFLAG_SOFT_LIMIT_ENABLE; } 260 | break; 261 | case 21: 262 | if (int_value) { settings.flags |= BITFLAG_HARD_LIMIT_ENABLE; } 263 | else { settings.flags &= ~BITFLAG_HARD_LIMIT_ENABLE; } 264 | limits_init(); // Re-init to immediately change. NOTE: Nice to have but could be problematic later. 265 | break; 266 | case 22: 267 | if (int_value) { settings.flags |= BITFLAG_HOMING_ENABLE; } 268 | else { 269 | settings.flags &= ~BITFLAG_HOMING_ENABLE; 270 | settings.flags &= ~BITFLAG_SOFT_LIMIT_ENABLE; // Force disable soft-limits. 271 | } 272 | break; 273 | case 23: settings.homing_dir_mask = int_value; break; 274 | case 24: settings.homing_feed_rate = value; break; 275 | case 25: settings.homing_seek_rate = value; break; 276 | case 26: settings.homing_debounce_delay = int_value; break; 277 | case 27: settings.homing_pulloff = value; break; 278 | default: 279 | return(STATUS_INVALID_STATEMENT); 280 | } 281 | } 282 | write_global_settings(); 283 | return(STATUS_OK); 284 | } 285 | 286 | 287 | // Initialize the config subsystem 288 | void settings_init() { 289 | if(!read_global_settings()) { 290 | report_status_message(STATUS_SETTING_READ_FAIL); 291 | 292 | settings_restore_global_settings(); 293 | 294 | // Force clear startup lines and build info user data. Parameters should be ok. 295 | // TODO: For next version, remove these clears. Only here because line buffer increased. 296 | settings_clear_startup_lines(); 297 | settings_clear_build_info(); 298 | 299 | report_grbl_settings(); 300 | } 301 | //settings_restore_global_settings(); // UNCOMMENT THIS TO RE-LOAD DEFAULT SETTINGS EVERY TIME. Once the values work, remember to comment it again so the EEPROM is not written every single time (would damage it longterm) 302 | // Check all parameter data into a dummy variable. If error, reset to zero, otherwise do nothing. 303 | float coord_data[N_AXIS]; 304 | uint8_t i; 305 | for (i=0; i<=SETTING_INDEX_NCOORD; i++) { 306 | if (!settings_read_coord_data(i, coord_data)) { 307 | report_status_message(STATUS_SETTING_READ_FAIL); 308 | } 309 | } 310 | // NOTE: Startup lines are checked and executed by protocol_main_loop at the end of initialization. 311 | // TODO: Build info should be checked here, but will wait until v1.0 to address this. Ok for now. 312 | } 313 | 314 | 315 | // Returns step pin mask according to Grbl internal axis indexing. 316 | uint8_t get_step_pin_mask(uint8_t axis_idx) 317 | { 318 | if ( axis_idx == X_AXIS ) { return((1<. 19 | */ 20 | /* 21 | This file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011-2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef settings_h 28 | #define settings_h 29 | 30 | 31 | #define GRBL_VERSION "0.9g" 32 | #define GRBL_VERSION_BUILD "20140822" 33 | 34 | // Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl 35 | // when firmware is upgraded. Always stored in byte 0 of eeprom 36 | #define SETTINGS_VERSION 9 // NOTE: Check settings_reset() when moving to next version. 37 | 38 | // Define bit flag masks for the boolean settings in settings.flag. 39 | #define BITFLAG_REPORT_INCHES bit(0) 40 | #define BITFLAG_AUTO_START bit(1) 41 | #define BITFLAG_INVERT_ST_ENABLE bit(2) 42 | #define BITFLAG_HARD_LIMIT_ENABLE bit(3) 43 | #define BITFLAG_HOMING_ENABLE bit(4) 44 | #define BITFLAG_SOFT_LIMIT_ENABLE bit(5) 45 | #define BITFLAG_INVERT_LIMIT_PINS bit(6) 46 | #define BITFLAG_INVERT_PROBE_PIN bit(7) 47 | 48 | // Define status reporting boolean enable bit flags in settings.status_report_mask 49 | #define BITFLAG_RT_STATUS_MACHINE_POSITION bit(0) 50 | #define BITFLAG_RT_STATUS_WORK_POSITION bit(1) 51 | #define BITFLAG_RT_STATUS_PLANNER_BUFFER bit(2) 52 | #define BITFLAG_RT_STATUS_SERIAL_RX bit(3) 53 | 54 | // Define EEPROM memory address location values for Grbl settings and parameters 55 | // NOTE: The Atmega328p has 1KB EEPROM. The upper half is reserved for parameters and 56 | // the startup script. The lower half contains the global settings and space for future 57 | // developments. 58 | #define EEPROM_ADDR_GLOBAL 1U 59 | #define EEPROM_ADDR_PARAMETERS 512U 60 | #define EEPROM_ADDR_STARTUP_BLOCK 768U 61 | #define EEPROM_ADDR_BUILD_INFO 942U 62 | 63 | // Define EEPROM address indexing for coordinate parameters 64 | #define N_COORDINATE_SYSTEM 6 // Number of supported work coordinate systems (from index 1) 65 | #define SETTING_INDEX_NCOORD N_COORDINATE_SYSTEM+1 // Total number of system stored (from index 0) 66 | // NOTE: Work coordinate indices are (0=G54, 1=G55, ... , 6=G59) 67 | #define SETTING_INDEX_G28 N_COORDINATE_SYSTEM // Home position 1 68 | #define SETTING_INDEX_G30 N_COORDINATE_SYSTEM+1 // Home position 2 69 | // #define SETTING_INDEX_G92 N_COORDINATE_SYSTEM+2 // Coordinate offset (G92.2,G92.3 not supported) 70 | 71 | // Define Grbl axis settings numbering scheme. Starts at START_VAL, every INCREMENT, over N_SETTINGS. 72 | #define AXIS_N_SETTINGS 4 73 | #define AXIS_SETTINGS_START_VAL 100 // NOTE: Reserving settings values >= 100 for axis settings. Up to 255. 74 | #define AXIS_SETTINGS_INCREMENT 10 // Must be greater than the number of axis settings 75 | 76 | // Global persistent settings (Stored from byte EEPROM_ADDR_GLOBAL onwards) 77 | typedef struct { 78 | // Axis settings 79 | float steps_per_mm[N_AXIS]; 80 | float max_rate[N_AXIS]; 81 | float acceleration[N_AXIS]; 82 | float max_travel[N_AXIS]; 83 | 84 | // Remaining Grbl settings 85 | uint8_t pulse_microseconds; 86 | uint8_t step_invert_mask; 87 | uint8_t dir_invert_mask; 88 | uint8_t stepper_idle_lock_time; // If max value 255, steppers do not disable. 89 | uint8_t status_report_mask; // Mask to indicate desired report data. 90 | float junction_deviation; 91 | float arc_tolerance; 92 | 93 | uint8_t flags; // Contains default boolean settings 94 | 95 | uint8_t homing_dir_mask; 96 | float homing_feed_rate; 97 | float homing_seek_rate; 98 | uint16_t homing_debounce_delay; 99 | float homing_pulloff; 100 | } settings_t; 101 | extern settings_t settings; 102 | 103 | // Initialize the configuration subsystem (load settings from EEPROM) 104 | void settings_init(); 105 | 106 | // Helper functions to clear and restore EEPROM defaults 107 | void settings_restore_global_settings(); 108 | void settings_clear_parameters(); 109 | void settings_clear_startup_line(); 110 | void settings_clear_build_info(); 111 | 112 | // A helper method to set new settings from command line 113 | uint8_t settings_store_global_setting(uint8_t parameter, float value); 114 | 115 | // Stores the protocol line variable as a startup line in EEPROM 116 | void settings_store_startup_line(uint8_t n, char *line); 117 | 118 | // Reads an EEPROM startup line to the protocol line variable 119 | uint8_t settings_read_startup_line(uint8_t n, char *line); 120 | 121 | // Stores build info user-defined string 122 | void settings_store_build_info(char *line); 123 | 124 | // Reads build info user-defined string 125 | uint8_t settings_read_build_info(char *line); 126 | 127 | // Writes selected coordinate data to EEPROM 128 | void settings_write_coord_data(uint8_t coord_select, float *coord_data); 129 | 130 | // Reads selected coordinate data from EEPROM 131 | uint8_t settings_read_coord_data(uint8_t coord_select, float *coord_data); 132 | 133 | // Returns the step pin mask according to Grbl's internal axis numbering 134 | uint8_t get_step_pin_mask(uint8_t i); 135 | 136 | // Returns the direction pin mask according to Grbl's internal axis numbering 137 | uint8_t get_direction_pin_mask(uint8_t i); 138 | 139 | // Returns the limit pin mask according to Grbl's internal axis numbering 140 | uint8_t get_limit_pin_mask(uint8_t i); 141 | 142 | 143 | #endif 144 | -------------------------------------------------------------------------------- /spindle_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | spindle_control.c - spindle control methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2012 Sungeun K. Jeon 25 | */ 26 | 27 | #include "system.h" 28 | #include "spindle_control.h" 29 | #include "protocol.h" 30 | #include "gcode.h" 31 | #include"fastio.h" 32 | 33 | extern void rampsInitCoolant(); 34 | extern void rampsInitSpindle(); 35 | extern void rampsInitSteppers(); 36 | extern void rampsWriteDisable(uint8_t value); 37 | extern void rampsWriteSteps(uint8_t value); 38 | extern void rampsWriteDirections(uint8_t value); 39 | extern void rampsStartSpindle(); 40 | extern void rampsStopSpindle(); 41 | extern void rampsLeftSpindle(); 42 | extern void rampsRightSpindle(); 43 | 44 | 45 | void spindle_init() { 46 | // On the Uno, spindle enable and PWM are shared. Other CPUs have seperate enable pin. 47 | #ifdef VARIABLE_SPINDLE 48 | SPINDLE_PWM_DDR |= (1 << SPINDLE_PWM_BIT); // Configure as PWM output pin. 49 | #ifndef CPU_MAP_ATMEGA328P 50 | SPINDLE_ENABLE_DDR |= (1 << SPINDLE_ENABLE_BIT); // Configure as output pin. 51 | #endif 52 | #else 53 | //SPINDLE_ENABLE_DDR |= (1< SPINDLE_RPM_RANGE) { 104 | rpm = SPINDLE_RPM_RANGE; 105 | } // Prevent uint8 overflow 106 | uint8_t current_pwm = floor(rpm * (255.0 / SPINDLE_RPM_RANGE) + 0.5); 107 | OCR_REGISTER = current_pwm; 108 | 109 | #ifndef CPU_MAP_ATMEGA328P // On the Uno, spindle enable and PWM are shared. 110 | SPINDLE_ENABLE_PORT |= (1 << SPINDLE_ENABLE_BIT); 111 | #endif 112 | #else 113 | // SPINDLE_ENABLE_PORT |= (1 << SPINDLE_ENABLE_BIT); 114 | rampsStartSpindle(); 115 | 116 | #endif 117 | 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /spindle_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | spindle_control.h - spindle control methods 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2012 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef spindle_control_h 28 | #define spindle_control_h 29 | 30 | 31 | // Initializes spindle pins and hardware PWM, if enabled. 32 | void spindle_init(); 33 | 34 | // Sets spindle direction and spindle rpm via PWM, if enabled. 35 | void spindle_run(uint8_t direction, float rpm); 36 | 37 | // Kills spindle. 38 | void spindle_stop(); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /stepper.h: -------------------------------------------------------------------------------- 1 | /* 2 | stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2012-2014 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 file is based on work from Grbl v0.8, distributed under the 22 | terms of the MIT-license. See COPYING for more details. 23 | Copyright (c) 2009-2011 Simen Svale Skogsrud 24 | Copyright (c) 2011 Sungeun K. Jeon 25 | */ 26 | 27 | #ifndef stepper_h 28 | #define stepper_h 29 | 30 | #ifndef SEGMENT_BUFFER_SIZE 31 | #define SEGMENT_BUFFER_SIZE 6 32 | #endif 33 | 34 | // Initialize and setup the stepper motor subsystem 35 | void stepper_init(); 36 | 37 | // Enable steppers, but cycle does not start unless called by motion control or runtime command. 38 | void st_wake_up(); 39 | 40 | // Immediately disables steppers 41 | void st_go_idle(); 42 | 43 | // Generate the step and direction port invert masks. 44 | void st_generate_step_dir_invert_masks(); 45 | 46 | // Reset the stepper subsystem variables 47 | void st_reset(); 48 | 49 | // Reloads step segment buffer. Called continuously by runtime execution system. 50 | void st_prep_buffer(); 51 | 52 | // Called by planner_recalculate() when the executing block is updated by the new plan. 53 | void st_update_plan_block_parameters(); 54 | 55 | // Called by runtime status reporting if realtime rate reporting is enabled in config.h. 56 | #ifdef REPORT_REALTIME_RATE 57 | float st_get_realtime_rate(); 58 | #endif 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /system.c: -------------------------------------------------------------------------------- 1 | /* 2 | system.c - Handles system level commands and real-time processes 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2014 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 "system.h" 22 | #include "settings.h" 23 | #include "gcode.h" 24 | #include "motion_control.h" 25 | #include "report.h" 26 | #include "print.h" 27 | 28 | 29 | void system_init() { 30 | // Ramps disabled 31 | // PINOUT_DDR &= ~(PINOUT_MASK); // Configure as input pins 32 | // PINOUT_PORT |= PINOUT_MASK; // Enable internal pull-up resistors. Normal high operation. 33 | // PINOUT_PCMSK |= PINOUT_MASK; // Enable specific pins of the Pin Change Interrupt 34 | // PCICR |= (1 << PINOUT_INT); // Enable Pin Change Interrupt 35 | } 36 | 37 | 38 | // Pin change interrupt for pin-out commands, i.e. cycle start, feed hold, and reset. Sets 39 | // only the runtime command execute variable to have the main program execute these when 40 | // its ready. This works exactly like the character-based runtime commands when picked off 41 | // directly from the incoming serial data stream. 42 | ISR(PINOUT_INT_vect) { 43 | // Enter only if any pinout pin is actively low. 44 | //ramps disabled 45 | // if ((PINOUT_PIN & PINOUT_MASK) ^ PINOUT_MASK) { 46 | // if (bit_isfalse(PINOUT_PIN,bit(PIN_RESET))) { 47 | // mc_reset(); 48 | // } else if (bit_isfalse(PINOUT_PIN,bit(PIN_FEED_HOLD))) { 49 | // bit_true(sys.execute, EXEC_FEED_HOLD); 50 | // } else if (bit_isfalse(PINOUT_PIN,bit(PIN_CYCLE_START))) { 51 | // bit_true(sys.execute, EXEC_CYCLE_START); 52 | // } 53 | // } 54 | } 55 | 56 | 57 | // Executes user startup script, if stored. 58 | void system_execute_startup(char *line) 59 | { 60 | uint8_t n; 61 | for (n=0; n < N_STARTUP_LINE; n++) { 62 | if (!(settings_read_startup_line(n, line))) { 63 | report_status_message(STATUS_SETTING_READ_FAIL); 64 | } else { 65 | if (line[0] != 0) { 66 | printString(line); // Echo startup line to indicate execution. 67 | report_status_message(gc_execute_line(line)); 68 | } 69 | } 70 | } 71 | } 72 | 73 | 74 | // Directs and executes one line of formatted input from protocol_process. While mostly 75 | // incoming streaming g-code blocks, this also executes Grbl internal commands, such as 76 | // settings, initiating the homing cycle, and toggling switch states. This differs from 77 | // the runtime command module by being susceptible to when Grbl is ready to execute the 78 | // next line during a cycle, so for switches like block delete, the switch only effects 79 | // the lines that are processed afterward, not necessarily real-time during a cycle, 80 | // since there are motions already stored in the buffer. However, this 'lag' should not 81 | // be an issue, since these commands are not typically used during a cycle. 82 | uint8_t system_execute_line(char *line) 83 | { 84 | uint8_t char_counter = 1; 85 | uint8_t helper_var = 0; // Helper variable 86 | float parameter, value; 87 | switch( line[char_counter] ) { 88 | case 0 : report_grbl_help(); break; 89 | case '$' : // Prints Grbl settings 90 | if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } 91 | if ( sys.state & (STATE_CYCLE | STATE_HOLD) ) { return(STATUS_IDLE_ERROR); } // Block during cycle. Takes too long to print. 92 | else { report_grbl_settings(); } 93 | break; 94 | case 'G' : // Prints gcode parser state 95 | if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } 96 | else { report_gcode_modes(); } 97 | break; 98 | case 'C' : // Set check g-code mode [IDLE/CHECK] 99 | if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } 100 | // Perform reset when toggling off. Check g-code mode should only work if Grbl 101 | // is idle and ready, regardless of alarm locks. This is mainly to keep things 102 | // simple and consistent. 103 | if ( sys.state == STATE_CHECK_MODE ) { 104 | mc_reset(); 105 | report_feedback_message(MESSAGE_DISABLED); 106 | } else { 107 | if (sys.state) { return(STATUS_IDLE_ERROR); } // Requires no alarm mode. 108 | sys.state = STATE_CHECK_MODE; 109 | report_feedback_message(MESSAGE_ENABLED); 110 | } 111 | break; 112 | case 'X' : // Disable alarm lock [ALARM] 113 | if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } 114 | if (sys.state == STATE_ALARM) { 115 | report_feedback_message(MESSAGE_ALARM_UNLOCK); 116 | sys.state = STATE_IDLE; 117 | // Don't run startup script. Prevents stored moves in startup from causing accidents. 118 | } // Otherwise, no effect. 119 | break; 120 | // case 'J' : break; // Jogging methods 121 | // TODO: Here jogging can be placed for execution as a seperate subprogram. It does not need to be 122 | // susceptible to other runtime commands except for e-stop. The jogging function is intended to 123 | // be a basic toggle on/off with controlled acceleration and deceleration to prevent skipped 124 | // steps. The user would supply the desired feedrate, axis to move, and direction. Toggle on would 125 | // start motion and toggle off would initiate a deceleration to stop. One could 'feather' the 126 | // motion by repeatedly toggling to slow the motion to the desired location. Location data would 127 | // need to be updated real-time and supplied to the user through status queries. 128 | // More controlled exact motions can be taken care of by inputting G0 or G1 commands, which are 129 | // handled by the planner. It would be possible for the jog subprogram to insert blocks into the 130 | // block buffer without having the planner plan them. It would need to manage de/ac-celerations 131 | // on its own carefully. This approach could be effective and possibly size/memory efficient. 132 | default : 133 | // Block any system command that requires the state as IDLE/ALARM. (i.e. EEPROM, homing) 134 | if ( !(sys.state == STATE_IDLE || sys.state == STATE_ALARM) ) { return(STATUS_IDLE_ERROR); } 135 | switch( line[char_counter] ) { 136 | case '#' : // Print Grbl NGC parameters 137 | if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } 138 | else { report_ngc_parameters(); } 139 | break; 140 | case 'H' : // Perform homing cycle [IDLE/ALARM] 141 | if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { 142 | // Only perform homing if Grbl is idle or lost. 143 | mc_homing_cycle(); 144 | if (!sys.abort) { system_execute_startup(line); } // Execute startup scripts after successful homing. 145 | } else { return(STATUS_SETTING_DISABLED); } 146 | break; 147 | case 'I' : // Print or store build info. [IDLE/ALARM] 148 | if ( line[++char_counter] == 0 ) { 149 | settings_read_build_info(line); 150 | report_build_info(line); 151 | } else { // Store startup line [IDLE/ALARM] 152 | if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); } 153 | helper_var = char_counter; // Set helper variable as counter to start of user info line. 154 | do { 155 | line[char_counter-helper_var] = line[char_counter]; 156 | } while (line[char_counter++] != 0); 157 | settings_store_build_info(line); 158 | } 159 | break; 160 | case 'N' : // Startup lines. [IDLE/ALARM] 161 | if ( line[++char_counter] == 0 ) { // Print startup lines 162 | for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) { 163 | if (!(settings_read_startup_line(helper_var, line))) { 164 | report_status_message(STATUS_SETTING_READ_FAIL); 165 | } else { 166 | report_startup_line(helper_var,line); 167 | } 168 | } 169 | break; 170 | } else { // Store startup line [IDLE Only] Prevents motion during ALARM. 171 | if (sys.state != STATE_IDLE) { return(STATUS_IDLE_ERROR); } // Store only when idle. 172 | helper_var = true; // Set helper_var to flag storing method. 173 | // No break. Continues into default: to read remaining command characters. 174 | } 175 | default : // Storing setting methods [IDLE/ALARM] 176 | if(!read_float(line, &char_counter, ¶meter)) { return(STATUS_BAD_NUMBER_FORMAT); } 177 | if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); } 178 | if (helper_var) { // Store startup line 179 | // Prepare sending gcode block to gcode parser by shifting all characters 180 | helper_var = char_counter; // Set helper variable as counter to start of gcode block 181 | do { 182 | line[char_counter-helper_var] = line[char_counter]; 183 | } while (line[char_counter++] != 0); 184 | // Execute gcode block to ensure block is valid. 185 | helper_var = gc_execute_line(line); // Set helper_var to returned status code. 186 | if (helper_var) { return(helper_var); } 187 | else { 188 | helper_var = trunc(parameter); // Set helper_var to int value of parameter 189 | settings_store_startup_line(helper_var,line); 190 | } 191 | } else { // Store global setting. 192 | if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); } 193 | if(line[char_counter] != 0) { return(STATUS_INVALID_STATEMENT); } 194 | return(settings_store_global_setting((uint8_t)parameter, value)); 195 | } 196 | } 197 | } 198 | return(STATUS_OK); // If '$' command makes it to here, then everything's ok. 199 | } 200 | -------------------------------------------------------------------------------- /system.h: -------------------------------------------------------------------------------- 1 | /* 2 | system.h - Header for system level commands and real-time processes 3 | Part of Grbl v0.9 4 | 5 | Copyright (c) 2014 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 | // Define system header files and standard libraries used by Grbl 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | // Define Grbl configuration and shared header files 38 | #include "config.h" 39 | #include "defaults.h" 40 | #include "cpu_map.h" 41 | #include "nuts_bolts.h" 42 | 43 | 44 | // Define system executor bit map. Used internally by runtime protocol as runtime command flags, 45 | // which notifies the main program to execute the specified runtime command asynchronously. 46 | // NOTE: The system executor uses an unsigned 8-bit volatile variable (8 flag limit.) The default 47 | // flags are always false, so the runtime protocol only needs to check for a non-zero value to 48 | // know when there is a runtime command to execute. 49 | #define EXEC_STATUS_REPORT bit(0) // bitmask 00000001 50 | #define EXEC_CYCLE_START bit(1) // bitmask 00000010 51 | #define EXEC_CYCLE_STOP bit(2) // bitmask 00000100 52 | #define EXEC_FEED_HOLD bit(3) // bitmask 00001000 53 | #define EXEC_RESET bit(4) // bitmask 00010000 54 | #define EXEC_ALARM bit(5) // bitmask 00100000 55 | #define EXEC_CRIT_EVENT bit(6) // bitmask 01000000 56 | // #define bit(7) // bitmask 10000000 57 | 58 | // Define system state bit map. The state variable primarily tracks the individual functions 59 | // of Grbl to manage each without overlapping. It is also used as a messaging flag for 60 | // critical events. 61 | #define STATE_IDLE 0 // Must be zero. No flags. 62 | #define STATE_ALARM bit(0) // In alarm state. Locks out all g-code processes. Allows settings access. 63 | #define STATE_CHECK_MODE bit(1) // G-code check mode. Locks out planner and motion only. 64 | #define STATE_HOMING bit(2) // Performing homing cycle 65 | #define STATE_QUEUED bit(3) // Indicates buffered blocks, awaiting cycle start. 66 | #define STATE_CYCLE bit(4) // Cycle is running 67 | #define STATE_HOLD bit(5) // Executing feed hold 68 | // #define STATE_JOG bit(6) // Jogging mode is unique like homing. 69 | 70 | 71 | // Define global system variables 72 | typedef struct { 73 | uint8_t abort; // System abort flag. Forces exit back to main loop for reset. 74 | uint8_t state; // Tracks the current state of Grbl. 75 | volatile uint8_t execute; // Global system runtime executor bitflag variable. See EXEC bitmasks. 76 | uint8_t homing_axis_lock; 77 | int32_t position[N_AXIS]; // Real-time machine (aka home) position vector in steps. 78 | // NOTE: This may need to be a volatile variable, if problems arise. 79 | uint8_t auto_start; // Planner auto-start flag. Toggled off during feed hold. Defaulted by settings. 80 | volatile uint8_t probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR. 81 | int32_t probe_position[N_AXIS]; // Last probe position in machine coordinates and steps. 82 | } system_t; 83 | extern system_t sys; 84 | 85 | 86 | // Initialize the serial protocol 87 | void system_init(); 88 | 89 | // Executes an internal system command, defined as a string starting with a '$' 90 | uint8_t system_execute_line(char *line); 91 | 92 | // Checks and executes a runtime command at various stop points in main program 93 | void system_execute_runtime(); 94 | 95 | // Execute the startup script lines stored in EEPROM upon initialization 96 | void system_execute_startup(char *line); 97 | 98 | #endif 99 | --------------------------------------------------------------------------------