2009-01-25 00:48:56 +01:00
|
|
|
/*
|
|
|
|
main.c - An embedded CNC Controller with rs274/ngc (g-code) support
|
|
|
|
Part of Grbl
|
|
|
|
|
2011-01-14 16:45:18 +01:00
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
2013-03-22 02:22:07 +01:00
|
|
|
Copyright (c) 2011-2013 Sungeun K. Jeon
|
2012-01-06 18:10:41 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
Grbl is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Grbl is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
/* A big thanks to Alden Hart of Synthetos, supplier of grblshield and TinyG, who has
|
|
|
|
been integral throughout the development of the higher level details of Grbl, as well
|
|
|
|
as being a consistent sounding board for the future of accessible and free CNC. */
|
|
|
|
|
2011-05-31 13:08:42 +02:00
|
|
|
#include <avr/interrupt.h>
|
2011-12-09 02:47:48 +01:00
|
|
|
#include <avr/pgmspace.h>
|
2011-06-03 15:28:14 +02:00
|
|
|
#include "config.h"
|
2011-02-11 00:34:53 +01:00
|
|
|
#include "planner.h"
|
2011-12-09 02:47:48 +01:00
|
|
|
#include "nuts_bolts.h"
|
2009-01-28 23:48:21 +01:00
|
|
|
#include "stepper.h"
|
|
|
|
#include "spindle_control.h"
|
2012-09-21 19:14:13 +02:00
|
|
|
#include "coolant_control.h"
|
2009-01-25 00:48:56 +01:00
|
|
|
#include "motion_control.h"
|
|
|
|
#include "gcode.h"
|
2011-02-18 22:11:53 +01:00
|
|
|
#include "protocol.h"
|
2011-02-20 00:29:56 +01:00
|
|
|
#include "limits.h"
|
2012-11-01 16:37:27 +01:00
|
|
|
#include "report.h"
|
2011-02-05 00:45:41 +01:00
|
|
|
#include "settings.h"
|
2011-05-31 22:45:38 +02:00
|
|
|
#include "serial.h"
|
2009-02-03 09:56:45 +01:00
|
|
|
|
2012-01-06 18:10:41 +01:00
|
|
|
// Declare system global variable structure
|
|
|
|
system_t sys;
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2011-12-09 02:47:48 +01:00
|
|
|
// Initialize system
|
2012-11-15 01:36:29 +01:00
|
|
|
serial_init(); // Setup serial baud rate and interrupts
|
2012-11-08 04:53:03 +01:00
|
|
|
settings_init(); // Load grbl settings from EEPROM
|
2011-12-09 02:47:48 +01:00
|
|
|
st_init(); // Setup stepper pins and interrupt timers
|
2012-01-16 02:25:12 +01:00
|
|
|
sei(); // Enable interrupts
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
|
2012-01-10 16:34:48 +01:00
|
|
|
memset(&sys, 0, sizeof(sys)); // Clear all system variables
|
2012-01-06 18:10:41 +01:00
|
|
|
sys.abort = true; // Set abort to complete initialization
|
2012-11-15 01:36:29 +01:00
|
|
|
sys.state = STATE_INIT; // Set alarm state to indicate unknown initial position
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
|
2012-01-29 04:41:08 +01:00
|
|
|
for(;;) {
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2012-01-06 18:10:41 +01:00
|
|
|
// Execute system reset upon a system abort, where the main program will return to this loop.
|
|
|
|
// Once here, it is safe to re-initialize the system. At startup, the system will automatically
|
|
|
|
// reset to finish the initialization process.
|
|
|
|
if (sys.abort) {
|
2011-12-09 02:47:48 +01:00
|
|
|
// Reset system.
|
|
|
|
serial_reset_read_buffer(); // Clear serial read buffer
|
|
|
|
plan_init(); // Clear block buffer and planner variables
|
|
|
|
gc_init(); // Set g-code parser to default state
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
protocol_init(); // Clear incoming line data and execute startup lines
|
2012-03-10 20:34:09 +01:00
|
|
|
spindle_init();
|
2012-09-21 19:14:13 +02:00
|
|
|
coolant_init();
|
2011-12-09 02:47:48 +01:00
|
|
|
limits_init();
|
2012-01-06 18:10:41 +01:00
|
|
|
st_reset(); // Clear stepper subsystem variables.
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
|
2012-11-04 16:44:54 +01:00
|
|
|
// Sync cleared gcode and planner positions to current system position, which is only
|
2012-11-15 01:36:29 +01:00
|
|
|
// cleared upon startup, not a reset/abort.
|
2013-08-19 17:24:22 +02:00
|
|
|
plan_sync_position();
|
|
|
|
gc_sync_position();
|
2012-11-04 16:44:54 +01:00
|
|
|
|
2012-11-06 05:40:52 +01:00
|
|
|
// Reset system variables.
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
sys.abort = false;
|
|
|
|
sys.execute = 0;
|
2012-11-15 01:36:29 +01:00
|
|
|
if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { sys.auto_start = true; }
|
|
|
|
|
|
|
|
// Check for power-up and set system alarm if homing is enabled to force homing cycle
|
|
|
|
// by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
|
|
|
|
// startup scripts, but allows access to settings and internal commands. Only a homing
|
|
|
|
// cycle '$H' or kill alarm locks '$X' will disable the alarm.
|
|
|
|
// NOTE: The startup script will run after successful completion of the homing cycle, but
|
|
|
|
// not after disabling the alarm locks. Prevents motion startup blocks from crashing into
|
|
|
|
// things uncontrollably. Very bad.
|
|
|
|
#ifdef HOMING_INIT_LOCK
|
|
|
|
if (sys.state == STATE_INIT && bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Check for and report alarm state after a reset, error, or an initial power up.
|
|
|
|
if (sys.state == STATE_ALARM) {
|
|
|
|
report_feedback_message(MESSAGE_ALARM_LOCK);
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
} else {
|
2012-11-15 01:36:29 +01:00
|
|
|
// All systems go. Set system to ready and execute startup script.
|
New startup script setting. New dry run, check gcode switches. New system state variable. Lots of reorganizing.
(All v0.8 features installed. Still likely buggy, but now thourough
testing will need to start to squash them all. As soon as we're done,
this will be pushed to master and v0.9 development will be started.
Please report ANY issues to us so we can get this rolled out ASAP.)
- User startup script! A user can now save one (up to 5 as compile-time
option) block of g-code in EEPROM memory. This will be run everytime
Grbl resets. Mainly to be used as a way to set your preferences, like
G21, G54, etc.
- New dry run and check g-code switches. Dry run moves ALL motions at
rapids rate ignoring spindle, coolant, and dwell commands. For rapid
physical proofing of your code. The check g-code switch ignores all
motion and provides the user a way to check if there are any errors in
their program that Grbl may not like.
- Program restart! (sort of). Program restart is typically an advanced
feature that allows users to restart a program mid-stream. The check
g-code switch can perform this feature by enabling the switch at the
start of the program, and disabling it at the desired point with some
minimal changes.
- New system state variable. This state variable tracks all of the
different state processes that Grbl performs, i.e. cycle start, feed
hold, homing, etc. This is mainly for making managing of these task
easier and more clear.
- Position lost state variable. Only when homing is enabled, Grbl will
refuse to move until homing is completed and position is known. This is
mainly for safety. Otherwise, it will let users fend for themselves.
- Moved the default settings defines into config.h. The plan is to
eventually create a set of config.h's for particular as-built machines
to help users from doing it themselves.
- Moved around misc defines into .h files. And lots of other little
things.
2012-11-03 18:32:23 +01:00
|
|
|
sys.state = STATE_IDLE;
|
2012-11-15 01:36:29 +01:00
|
|
|
protocol_execute_startup();
|
Hard limits, homing direction, pull-off limits after homing, status reports in mm or inches, system alarm, and more.
- Thank you statement added for Alden Hart of Synthetos.
- Hard limits option added, which also works with homing by pulling off
the switches to help prevent unintended triggering. Hard limits use a
interrupt to sense a falling edge pin change and immediately go into
alarm mode, which stops everything and forces the user to issue a reset
(Ctrl-x) or reboot.
- Auto cycle start now a configuration option.
- Alarm mode: A new method to kill all Grbl processes in the event of
something catastrophic or potentially catastropic. Just works with hard
limits for now, but will be expanded to include g-code errors (most
likely) and other events.
- Updated status reports to be configurable in inches or mm mode. Much
more to do here, but this is the first step.
- New settings: auto cycle start, hard limit enable, homing direction
mask (which works the same as the stepper mask), homing pulloff
distance (or distance traveled from homed machine zero to prevent
accidental limit trip).
- Minor memory liberation and calculation speed ups.
2012-10-17 05:29:45 +02:00
|
|
|
}
|
2011-12-09 02:47:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protocol_execute_runtime();
|
|
|
|
|
2012-12-17 00:23:24 +01:00
|
|
|
// When the serial protocol returns, there are no more characters in the serial read buffer to
|
|
|
|
// be processed and executed. This indicates that individual commands are being issued or
|
|
|
|
// streaming is finished. In either case, auto-cycle start, if enabled, any queued moves.
|
2013-03-22 02:22:07 +01:00
|
|
|
mc_auto_cycle_start();
|
2013-08-19 17:24:22 +02:00
|
|
|
protocol_process(); // ... process the serial protocol
|
2012-12-17 00:23:24 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
|
|
|
return 0; /* never reached */
|
|
|
|
}
|