2009-01-25 00:48:56 +01:00
|
|
|
/*
|
|
|
|
main.c - An embedded CNC Controller with rs274/ngc (g-code) support
|
|
|
|
Part of Grbl
|
2013-12-31 06:02:05 +01:00
|
|
|
|
|
|
|
Copyright (c) 2011-2014 Sungeun K. Jeon
|
2011-01-14 16:45:18 +01:00
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
#include "system.h"
|
|
|
|
#include "serial.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "gcode.h"
|
2011-02-11 00:34:53 +01:00
|
|
|
#include "planner.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"
|
2011-02-20 00:29:56 +01:00
|
|
|
#include "limits.h"
|
2012-11-01 16:37:27 +01:00
|
|
|
#include "report.h"
|
2014-01-11 04:22:10 +01:00
|
|
|
|
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
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2014-01-11 04:22:10 +01:00
|
|
|
// Initialize system upon power-up.
|
|
|
|
serial_init(); // Setup serial baud rate and interrupts
|
2012-11-08 04:53:03 +01:00
|
|
|
settings_init(); // Load grbl settings from EEPROM
|
2014-01-11 04:22:10 +01:00
|
|
|
stepper_init(); // Configure stepper pins and interrupt timers
|
|
|
|
system_init(); // Configure pinout pins and pin-change interrupt
|
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
|
2014-02-09 18:46:34 +01:00
|
|
|
sei(); // Enable interrupts
|
2013-12-30 04:34:51 +01:00
|
|
|
|
|
|
|
// 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 (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
|
|
|
|
#endif
|
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
|
|
|
|
2014-02-09 18:46:34 +01:00
|
|
|
// Grbl initialization loop upon power-up or a system abort. For the latter, all processes
|
|
|
|
// will return to this loop to be cleanly re-initialized.
|
2012-01-29 04:41:08 +01:00
|
|
|
for(;;) {
|
2014-02-09 18:46:34 +01:00
|
|
|
|
|
|
|
// TODO: Separate configure task that require interrupts to be disabled, especially upon
|
|
|
|
// a system abort and ensuring any active interrupts are cleanly reset.
|
2011-12-09 02:47:48 +01:00
|
|
|
|
2014-02-09 18:46:34 +01:00
|
|
|
// Reset Grbl primary systems.
|
2014-01-11 04:22:10 +01:00
|
|
|
serial_reset_read_buffer(); // Clear serial read buffer
|
|
|
|
gc_init(); // Set g-code parser to default state
|
|
|
|
spindle_init();
|
|
|
|
coolant_init();
|
|
|
|
limits_init();
|
|
|
|
plan_reset(); // Clear block buffer and planner variables
|
|
|
|
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
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
// Sync cleared gcode and planner positions to current system position.
|
|
|
|
plan_sync_position();
|
|
|
|
gc_sync_position();
|
2012-11-04 16:44:54 +01:00
|
|
|
|
2014-01-11 04:22:10 +01:00
|
|
|
// Reset system variables.
|
|
|
|
sys.abort = false;
|
|
|
|
sys.execute = 0;
|
|
|
|
if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { sys.auto_start = true; }
|
|
|
|
else { sys.auto_start = false; }
|
|
|
|
|
2014-02-09 18:46:34 +01:00
|
|
|
// Start Grbl main loop. Processes program inputs and executes them.
|
|
|
|
protocol_main_loop();
|
2012-12-17 00:23:24 +01:00
|
|
|
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|
2014-01-11 04:22:10 +01:00
|
|
|
return 0; /* Never reached */
|
2009-01-25 00:48:56 +01:00
|
|
|
}
|