2009-01-25 00:48:56 +01:00
|
|
|
/*
|
|
|
|
main.c - An embedded CNC Controller with rs274/ngc (g-code) support
|
2014-08-07 13:58:04 +02:00
|
|
|
Part of Grbl v0.9
|
2013-12-31 06:02:05 +01:00
|
|
|
|
2015-01-15 06:14:52 +01:00
|
|
|
Copyright (c) 2012-2015 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/>.
|
|
|
|
*/
|
2014-08-07 13:58:04 +02:00
|
|
|
/*
|
|
|
|
This file is based on work from Grbl v0.8, distributed under the
|
|
|
|
terms of the MIT-license. See COPYING for more details.
|
|
|
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
|
|
|
Copyright (c) 2011-2012 Sungeun K. Jeon
|
|
|
|
*/
|
2009-01-25 00:48:56 +01:00
|
|
|
|
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"
|
G38.2 probe feature rough draft installed. Working but needs testing.
- G38.2 straight probe now supported. Rough draft. May be tweaked more
as testing ramps up.
- G38.2 requires at least one axis word. Multiple axis words work too.
When commanded, the probe cycle will move at the last ‘F’ feed rate
specified in a straight line.
- During a probe cycle: If the probe pin goes low (normal high), Grbl
will record that immediate position and engage a feed hold. Meaning
that the CNC machine will move a little past the probe switch point, so
keep federates low to stop sooner. Once stopped, Grbl will issue a move
to go back to the recorded probe trigger point.
- During a probe cycle: If the probe switch does not engage by the time
the machine has traveled to its target coordinates, Grbl will issue an
ALARM and the user will be forced to reset Grbl. (Currently G38.3 probe
without error isn’t supported, but would be easy to implement later.)
- After a successful probe, Grbl will send a feedback message
containing the recorded probe coordinates in the machine coordinate
system. This is as the g-code standard on probe parameters specifies.
- The recorded probe parameters are retained in Grbl memory and can be
viewed with the ‘$#’ print parameters command. Upon a power-cycle, not
a soft-reset, Grbl will re-zero these values.
- Moved ‘$#’ command to require IDLE or ALARM mode, because it accesses
EEPROM to fetch the coordinate system offsets.
- Updated the Grbl version to v0.9d.
- The probe cycle is subject to change upon testing or user-feedback.
2014-03-01 06:03:26 +01:00
|
|
|
#include "probe.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();
|
G38.2 probe feature rough draft installed. Working but needs testing.
- G38.2 straight probe now supported. Rough draft. May be tweaked more
as testing ramps up.
- G38.2 requires at least one axis word. Multiple axis words work too.
When commanded, the probe cycle will move at the last ‘F’ feed rate
specified in a straight line.
- During a probe cycle: If the probe pin goes low (normal high), Grbl
will record that immediate position and engage a feed hold. Meaning
that the CNC machine will move a little past the probe switch point, so
keep federates low to stop sooner. Once stopped, Grbl will issue a move
to go back to the recorded probe trigger point.
- During a probe cycle: If the probe switch does not engage by the time
the machine has traveled to its target coordinates, Grbl will issue an
ALARM and the user will be forced to reset Grbl. (Currently G38.3 probe
without error isn’t supported, but would be easy to implement later.)
- After a successful probe, Grbl will send a feedback message
containing the recorded probe coordinates in the machine coordinate
system. This is as the g-code standard on probe parameters specifies.
- The recorded probe parameters are retained in Grbl memory and can be
viewed with the ‘$#’ print parameters command. Upon a power-cycle, not
a soft-reset, Grbl will re-zero these values.
- Moved ‘$#’ command to require IDLE or ALARM mode, because it accesses
EEPROM to fetch the coordinate system offsets.
- Updated the Grbl version to v0.9d.
- The probe cycle is subject to change upon testing or user-feedback.
2014-03-01 06:03:26 +01:00
|
|
|
probe_init();
|
2014-01-11 04:22:10 +01:00
|
|
|
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;
|
2015-01-15 06:14:52 +01:00
|
|
|
sys.rt_exec_state = 0;
|
|
|
|
sys.rt_exec_alarm = 0;
|
2014-01-11 04:22:10 +01:00
|
|
|
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
|
|
|
}
|