Merge pull request #39 from chamnit/edge
Merging chamnit/grbl edge v0.8a to grbl/grbl edge
This commit is contained in:
commit
0c0ee24820
141
config.h
141
config.h
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -21,70 +22,108 @@
|
|||||||
#ifndef config_h
|
#ifndef config_h
|
||||||
#define config_h
|
#define config_h
|
||||||
|
|
||||||
|
// IMPORTANT: Any changes here requires a full re-compiling of the source code to propagate them.
|
||||||
|
|
||||||
#define BAUD_RATE 9600
|
#define BAUD_RATE 9600
|
||||||
|
|
||||||
// Updated default pin-assignments from 0.6 onwards
|
// Define pin-assignments
|
||||||
// (see bottom of file for a copy of the old config)
|
|
||||||
|
|
||||||
#define STEPPERS_DISABLE_DDR DDRB
|
|
||||||
#define STEPPERS_DISABLE_PORT PORTB
|
|
||||||
#define STEPPERS_DISABLE_BIT 0
|
|
||||||
|
|
||||||
#define STEPPING_DDR DDRD
|
#define STEPPING_DDR DDRD
|
||||||
#define STEPPING_PORT PORTD
|
#define STEPPING_PORT PORTD
|
||||||
#define X_STEP_BIT 2
|
#define X_STEP_BIT 2 // Uno Digital Pin 2
|
||||||
#define Y_STEP_BIT 3
|
#define Y_STEP_BIT 3 // Uno Digital Pin 3
|
||||||
#define Z_STEP_BIT 4
|
#define Z_STEP_BIT 4 // Uno Digital Pin 4
|
||||||
#define X_DIRECTION_BIT 5
|
#define X_DIRECTION_BIT 5 // Uno Digital Pin 5
|
||||||
#define Y_DIRECTION_BIT 6
|
#define Y_DIRECTION_BIT 6 // Uno Digital Pin 6
|
||||||
#define Z_DIRECTION_BIT 7
|
#define Z_DIRECTION_BIT 7 // Uno Digital Pin 7
|
||||||
|
|
||||||
#define LIMIT_DDR DDRB
|
#define STEPPERS_DISABLE_DDR DDRB
|
||||||
|
#define STEPPERS_DISABLE_PORT PORTB
|
||||||
|
#define STEPPERS_DISABLE_BIT 0 // Uno Digital Pin 8
|
||||||
|
|
||||||
|
#define LIMIT_DDR DDRB
|
||||||
#define LIMIT_PIN PINB
|
#define LIMIT_PIN PINB
|
||||||
#define X_LIMIT_BIT 1
|
#define X_LIMIT_BIT 1 // Uno Digital Pin 9
|
||||||
#define Y_LIMIT_BIT 2
|
#define Y_LIMIT_BIT 2 // Uno Digital Pin 10
|
||||||
#define Z_LIMIT_BIT 3
|
#define Z_LIMIT_BIT 3 // Uno Digital Pin 11
|
||||||
|
|
||||||
#define SPINDLE_ENABLE_DDR DDRB
|
#define SPINDLE_ENABLE_DDR DDRB
|
||||||
#define SPINDLE_ENABLE_PORT PORTB
|
#define SPINDLE_ENABLE_PORT PORTB
|
||||||
#define SPINDLE_ENABLE_BIT 4
|
#define SPINDLE_ENABLE_BIT 4 // Uno Digital Pin 12
|
||||||
|
|
||||||
#define SPINDLE_DIRECTION_DDR DDRB
|
#define SPINDLE_DIRECTION_DDR DDRB
|
||||||
#define SPINDLE_DIRECTION_PORT PORTB
|
#define SPINDLE_DIRECTION_PORT PORTB
|
||||||
#define SPINDLE_DIRECTION_BIT 5
|
#define SPINDLE_DIRECTION_BIT 5 // Uno Digital Pin 13
|
||||||
|
|
||||||
// The temporal resolution of the acceleration management subsystem. Higher number
|
// Define runtime command special characters. These characters are 'picked-off' directly from the
|
||||||
// give smoother acceleration but may impact performance
|
// serial read data stream and are not passed to the grbl line execution parser. Select characters
|
||||||
#define ACCELERATION_TICKS_PER_SECOND 40L
|
// that do not and must not exist in the streamed g-code program. ASCII control characters may be
|
||||||
|
// used, if they are available per user setup. Also, extended ASCII codes (>127), which are never in
|
||||||
|
// g-code programs, maybe selected for interface programs.
|
||||||
|
// TODO: Solidify these default characters. Temporary for now.
|
||||||
|
#define CMD_STATUS_REPORT '?'
|
||||||
|
#define CMD_FEED_HOLD '!'
|
||||||
|
#define CMD_CYCLE_START '~'
|
||||||
|
#define CMD_RESET 0x18 // ctrl-x
|
||||||
|
|
||||||
|
// This parameter sets the delay time before disabling the steppers after the final block of movement.
|
||||||
|
// A short delay ensures the steppers come to a complete stop and the residual inertial force in the
|
||||||
|
// CNC axes don't cause the axes to drift off position. This is particularly important when manually
|
||||||
|
// entering g-code into grbl, i.e. locating part zero or simple manual machining. If the axes drift,
|
||||||
|
// grbl has no way to know this has happened, since stepper motors are open-loop control. Depending
|
||||||
|
// on the machine, this parameter may need to be larger or smaller than the default time.
|
||||||
|
// NOTE: If commented out, the delay will not be compiled.
|
||||||
|
#define STEPPER_IDLE_LOCK_TIME 25 // (milliseconds) - Integer > 0
|
||||||
|
|
||||||
|
// The temporal resolution of the acceleration management subsystem. Higher number give smoother
|
||||||
|
// acceleration but may impact performance.
|
||||||
|
// NOTE: Increasing this parameter will help any resolution related issues, especially with machines
|
||||||
|
// requiring very high accelerations and/or very fast feedrates. In general, this will reduce the
|
||||||
|
// error between how the planner plans the motions and how the stepper program actually performs them.
|
||||||
|
// However, at some point, the resolution can be high enough, where the errors related to numerical
|
||||||
|
// round-off can be great enough to cause problems and/or it's too fast for the Arduino. The correct
|
||||||
|
// value for this parameter is machine dependent, so it's advised to set this only as high as needed.
|
||||||
|
// Approximate successful values can range from 30L to 100L or more.
|
||||||
|
#define ACCELERATION_TICKS_PER_SECOND 50L
|
||||||
|
|
||||||
|
// Minimum planner junction speed. Sets the default minimum speed the planner plans for at the end
|
||||||
|
// of the buffer and all stops. This should not be much greater than zero and should only be changed
|
||||||
|
// if unwanted behavior is observed on a user's machine when running at very slow speeds.
|
||||||
|
#define MINIMUM_PLANNER_SPEED 0.0 // (mm/min)
|
||||||
|
|
||||||
|
// Minimum stepper rate. Sets the absolute minimum stepper rate in the stepper program and never runs
|
||||||
|
// slower than this value, except when sleeping. This parameter overrides the minimum planner speed.
|
||||||
|
// This is primarily used to guarantee that the end of a movement is always reached and not stop to
|
||||||
|
// never reach its target. This parameter should always be greater than zero.
|
||||||
|
#define MINIMUM_STEPS_PER_MINUTE 800 // (steps/min) - Integer value only
|
||||||
|
|
||||||
|
// Number of arc generation iterations by small angle approximation before exact arc trajectory
|
||||||
|
// correction. This parameter maybe decreased if there are issues with the accuracy of the arc
|
||||||
|
// generations. In general, the default value is more than enough for the intended CNC applications
|
||||||
|
// of grbl, and should be on the order or greater than the size of the buffer to help with the
|
||||||
|
// computational efficiency of generating arcs.
|
||||||
|
#define N_ARC_CORRECTION 25 // Integer (1-255)
|
||||||
|
|
||||||
|
// Time delay increments performed during a dwell. The default value is set at 50ms, which provides
|
||||||
|
// a maximum time delay of roughly 55 minutes, more than enough for most any application. Increasing
|
||||||
|
// this delay will increase the maximum dwell time linearly, but also reduces the responsiveness of
|
||||||
|
// run-time command executions, like status reports, since these are performed between each dwell
|
||||||
|
// time step. Also, keep in mind that the Arduino delay timer is not very accurate for long delays.
|
||||||
|
#define DWELL_TIME_STEP 50 // Integer (1-255) (milliseconds)
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
// TODO: The following options are set as compile-time options for now, until the next EEPROM
|
||||||
|
// settings version has solidified.
|
||||||
|
#define CYCLE_AUTO_START 1 // Cycle auto-start boolean flag for the planner.
|
||||||
|
#define BLOCK_DELETE_ENABLE 0 // Block delete enable/disable flag during g-code parsing
|
||||||
|
#define REPORT_INCH_MODE 0 // Status reporting unit mode (1 = inch, 0 = mm)
|
||||||
|
#if REPORT_INCH_MODE
|
||||||
|
#define DECIMAL_PLACES 3
|
||||||
|
#define DECIMAL_MULTIPLIER 1000 // 10^DECIMAL_PLACES
|
||||||
|
#else
|
||||||
|
#define DECIMAL_PLACES 2 // mm-mode
|
||||||
|
#define DECIMAL_MULTIPLIER 100
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Pin-assignments from Grbl 0.5
|
#endif
|
||||||
|
|
||||||
// #define STEPPERS_DISABLE_DDR DDRD
|
|
||||||
// #define STEPPERS_DISABLE_PORT PORTD
|
|
||||||
// #define STEPPERS_DISABLE_BIT 2
|
|
||||||
//
|
|
||||||
// #define STEPPING_DDR DDRC
|
|
||||||
// #define STEPPING_PORT PORTC
|
|
||||||
// #define X_STEP_BIT 0
|
|
||||||
// #define Y_STEP_BIT 1
|
|
||||||
// #define Z_STEP_BIT 2
|
|
||||||
// #define X_DIRECTION_BIT 3
|
|
||||||
// #define Y_DIRECTION_BIT 4
|
|
||||||
// #define Z_DIRECTION_BIT 5
|
|
||||||
//
|
|
||||||
// #define LIMIT_DDR DDRD
|
|
||||||
// #define LIMIT_PORT PORTD
|
|
||||||
// #define X_LIMIT_BIT 3
|
|
||||||
// #define Y_LIMIT_BIT 4
|
|
||||||
// #define Z_LIMIT_BIT 5
|
|
||||||
//
|
|
||||||
// #define SPINDLE_ENABLE_DDR DDRD
|
|
||||||
// #define SPINDLE_ENABLE_PORT PORTD
|
|
||||||
// #define SPINDLE_ENABLE_BIT 6
|
|
||||||
//
|
|
||||||
// #define SPINDLE_DIRECTION_DDR DDRD
|
|
||||||
// #define SPINDLE_DIRECTION_PORT PORTD
|
|
||||||
// #define SPINDLE_DIRECTION_BIT 7
|
|
||||||
|
|
||||||
|
21
doc/commands.txt
Normal file
21
doc/commands.txt
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
Runtime commands for Grbl
|
||||||
|
=========================
|
||||||
|
|
||||||
|
In normal operation, grbl accepts g-code blocks followed by a carriage return. Each block is then parsed, processed, and placed into a ring buffer with computed acceleration profiles. Grbl will respond with an 'ok' or 'error:XXX' for each block received.
|
||||||
|
|
||||||
|
As of v0.8, grbl features multi-tasking events, which allow for immediate execution of run-time commands regardless of what grbl is doing. With this functionality, direct control of grbl may be possible, such as a controlled decelerating feed hold, resume, and system abort/reset. In addition, this provides the ability to report the real-time status of the CNC machine's current location and feed rates.
|
||||||
|
|
||||||
|
How it works: The run-time commands are defined as special characters, which are picked off the serial read buffer at an interrupt level. The serial interrupt then sets a run-time command system flag for the main program to execute when ready. The main program consists of run-time command check points placed strategically in various points in the program, where grbl maybe idle waiting for room in a buffer or the execution time from the last check point may exceed a fraction of a second.
|
||||||
|
|
||||||
|
How to interface: From a terminal connection, run-time commands may be sent at anytime via keystrokes. When streaming g-code, the user interface should be able to send these special characters independently of the stream. Grbl will not write these run-time command special characters to the buffer, so they may be placed anywhere in the stream at anytime, as long as there is room in the buffer. Also, ensure that the g-code program being streamed to grbl does not contain any of these special characters in the program. These characters may be defined per user requirements in the 'config.h' file.
|
||||||
|
|
||||||
|
Run-time commands:
|
||||||
|
|
||||||
|
- Feed Hold: This initiates an immediate controlled deceleration of the streaming g-code program to a stop. The deceleration, limited by the machine acceleration settings, ensures no steps are lost and positioning is maintained. Grbl may still receive and buffer g-code blocks as the feed hold is being executed. Once the feed hold completes, grbl will replan the buffer and resume upon a 'cycle start' command.
|
||||||
|
|
||||||
|
- Cycle Start: (a.k.a. Resume) For now, cycle start only resumes the g-code program after a feed hold. In later releases, this may also function as a way to initiate the steppers manually when a user would like to fill the planner buffer completely before starting the cycle.
|
||||||
|
|
||||||
|
- Reset: This issues an immediate shutdown of the stepper motors and a system abort. The main program will exit back to the main loop and re-initialize grbl.
|
||||||
|
|
||||||
|
- Status Report: (TODO) In future releases, this will provide real-time positioning, feed rate, and block processed data, as well as other important data to the user. This also may be considered a 'poor-man's' DRO (digital read-out), where grbl thinks it is, rather than a direct and absolute measurement.
|
||||||
|
|
@ -1,30 +0,0 @@
|
|||||||
Reasoning behind the mathematics in 'planner' module (in the key of 'Mathematica')
|
|
||||||
==================================================================================
|
|
||||||
|
|
||||||
|
|
||||||
s == speed, a == acceleration, t == time, d == distance
|
|
||||||
|
|
||||||
Basic definitions:
|
|
||||||
|
|
||||||
Speed[s_, a_, t_] := s + (a*t)
|
|
||||||
Travel[s_, a_, t_] := Integrate[Speed[s, a, t], t]
|
|
||||||
|
|
||||||
Distance to reach a specific speed with a constant acceleration:
|
|
||||||
|
|
||||||
Solve[{Speed[s, a, t] == m, Travel[s, a, t] == d}, d, t]
|
|
||||||
d -> (m^2 - s^2)/(2 a) --> estimate_acceleration_distance()
|
|
||||||
|
|
||||||
Speed after a given distance of travel with constant acceleration:
|
|
||||||
|
|
||||||
Solve[{Speed[s, a, t] == m, Travel[s, a, t] == d}, m, t]
|
|
||||||
m -> Sqrt[2 a d + s^2]
|
|
||||||
|
|
||||||
DestinationSpeed[s_, a_, d_] := Sqrt[2 a d + s^2]
|
|
||||||
|
|
||||||
When to start braking (di) to reach a specified destionation speed (s2) after accelerating
|
|
||||||
from initial speed s1 without ever stopping at a plateau:
|
|
||||||
|
|
||||||
Solve[{DestinationSpeed[s1, a, di] == DestinationSpeed[s2, a, d - di]}, di]
|
|
||||||
di -> (2 a d - s1^2 + s2^2)/(4 a) --> intersection_distance()
|
|
||||||
|
|
||||||
IntersectionDistance[s1_, s2_, a_, d_] := (2 a d - s1^2 + s2^2)/(4 a)
|
|
@ -4,7 +4,8 @@ The general structure of Grbl
|
|||||||
The main processing stack:
|
The main processing stack:
|
||||||
|
|
||||||
'protocol' : Accepts command lines from the serial port and passes them to 'gcode' for execution.
|
'protocol' : Accepts command lines from the serial port and passes them to 'gcode' for execution.
|
||||||
Provides status responses for each command.
|
Provides status responses for each command. Also manages run-time commands set by
|
||||||
|
the serial interrupt.
|
||||||
|
|
||||||
'gcode' : Recieves gcode from 'protocol', parses it according to the current state
|
'gcode' : Recieves gcode from 'protocol', parses it according to the current state
|
||||||
of the parser and issues commands via '..._control' modules
|
of the parser and issues commands via '..._control' modules
|
||||||
@ -14,8 +15,8 @@ The main processing stack:
|
|||||||
'motion_control' : Accepts motion commands from 'gcode' and passes them to the 'planner'. This module
|
'motion_control' : Accepts motion commands from 'gcode' and passes them to the 'planner'. This module
|
||||||
represents the public interface of the planner/stepper duo.
|
represents the public interface of the planner/stepper duo.
|
||||||
|
|
||||||
'planner' : Recieves linear motion commands from 'motion_control' and adds them to the plan of
|
'planner' : Receives linear motion commands from 'motion_control' and adds them to the plan of
|
||||||
prepared motions. It takes care of continously optimizing the acceleration profile
|
prepared motions. It takes care of continuously optimizing the acceleration profile
|
||||||
as motions are added.
|
as motions are added.
|
||||||
|
|
||||||
'stepper' : Executes the motions by stepping the steppers according to the plan.
|
'stepper' : Executes the motions by stepping the steppers according to the plan.
|
||||||
@ -27,15 +28,16 @@ Supporting files:
|
|||||||
|
|
||||||
'config.h' : Compile time user settings
|
'config.h' : Compile time user settings
|
||||||
|
|
||||||
'settings' : Maintains the run time settings record in eeprom and makes it availible
|
'settings' : Maintains the run time settings record in eeprom and makes it available
|
||||||
to all modules.
|
to all modules.
|
||||||
|
|
||||||
'eeprom' : A library from Atmel that provides methods for reading and writing the eeprom with
|
'eeprom' : A library from Atmel that provides methods for reading and writing the eeprom with
|
||||||
a small addition from us that read and write binary streams with check sums used
|
a small addition from us that read and write binary streams with check sums used
|
||||||
to verify validity of the settings record.
|
to verify validity of the settings record.
|
||||||
|
|
||||||
'nuts_bolts.h' : A tiny collection of useful constants and macros used everywhere
|
'nuts_bolts.h' : A collection of global variable definitions, useful constants, and macros used everywhere
|
||||||
|
|
||||||
'serial' : Low level serial communications
|
'serial' : Low level serial communications and picks off run-time commands real-time for asynchronous
|
||||||
|
control.
|
||||||
|
|
||||||
'print' : Functions to print strings of different formats (using serial)
|
'print' : Functions to print strings of different formats (using serial)
|
108
gcode.c
108
gcode.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -31,8 +32,6 @@
|
|||||||
#include "errno.h"
|
#include "errno.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
|
||||||
#define MM_PER_INCH (25.4)
|
|
||||||
|
|
||||||
#define NEXT_ACTION_DEFAULT 0
|
#define NEXT_ACTION_DEFAULT 0
|
||||||
#define NEXT_ACTION_DWELL 1
|
#define NEXT_ACTION_DWELL 1
|
||||||
#define NEXT_ACTION_GO_HOME 2
|
#define NEXT_ACTION_GO_HOME 2
|
||||||
@ -87,8 +86,8 @@ static void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2)
|
|||||||
|
|
||||||
void gc_init() {
|
void gc_init() {
|
||||||
memset(&gc, 0, sizeof(gc));
|
memset(&gc, 0, sizeof(gc));
|
||||||
gc.feed_rate = settings.default_feed_rate/60;
|
gc.feed_rate = settings.default_feed_rate;
|
||||||
gc.seek_rate = settings.default_seek_rate/60;
|
gc.seek_rate = settings.default_seek_rate;
|
||||||
select_plane(X_AXIS, Y_AXIS, Z_AXIS);
|
select_plane(X_AXIS, Y_AXIS, Z_AXIS);
|
||||||
gc.absolute_mode = true;
|
gc.absolute_mode = true;
|
||||||
}
|
}
|
||||||
@ -97,34 +96,16 @@ static float to_millimeters(double value) {
|
|||||||
return(gc.inches_mode ? (value * MM_PER_INCH) : value);
|
return(gc.inches_mode ? (value * MM_PER_INCH) : value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __AVR_ATmega328P__
|
|
||||||
// Find the angle in radians of deviance from the positive y axis. negative angles to the left of y-axis,
|
|
||||||
// positive to the right.
|
|
||||||
static double theta(double x, double y)
|
|
||||||
{
|
|
||||||
double theta = atan(x/fabs(y));
|
|
||||||
if (y>0) {
|
|
||||||
return(theta);
|
|
||||||
} else {
|
|
||||||
if (theta>0)
|
|
||||||
{
|
|
||||||
return(M_PI-theta);
|
|
||||||
} else {
|
|
||||||
return(-M_PI-theta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Executes one line of 0-terminated G-Code. The line is assumed to contain only uppercase
|
// Executes one line of 0-terminated G-Code. The line is assumed to contain only uppercase
|
||||||
// characters and signed floating point values (no whitespace).
|
// characters and signed floating point values (no whitespace). Comments and block delete
|
||||||
|
// characters have been removed.
|
||||||
uint8_t gc_execute_line(char *line) {
|
uint8_t gc_execute_line(char *line) {
|
||||||
uint8_t char_counter = 0;
|
uint8_t char_counter = 0;
|
||||||
char letter;
|
char letter;
|
||||||
double value;
|
double value;
|
||||||
double unit_converted_value;
|
double unit_converted_value;
|
||||||
double inverse_feed_rate = -1; // negative inverse_feed_rate means no inverse_feed_rate specified
|
double inverse_feed_rate = -1; // negative inverse_feed_rate means no inverse_feed_rate specified
|
||||||
int radius_mode = false;
|
uint8_t radius_mode = false;
|
||||||
|
|
||||||
uint8_t absolute_override = false; /* 1 = absolute motion for this block only {G53} */
|
uint8_t absolute_override = false; /* 1 = absolute motion for this block only {G53} */
|
||||||
uint8_t next_action = NEXT_ACTION_DEFAULT; /* The action that will be taken by the parsed line */
|
uint8_t next_action = NEXT_ACTION_DEFAULT; /* The action that will be taken by the parsed line */
|
||||||
@ -134,15 +115,8 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
double p = 0, r = 0;
|
double p = 0, r = 0;
|
||||||
int int_value;
|
int int_value;
|
||||||
|
|
||||||
clear_vector(target);
|
|
||||||
clear_vector(offset);
|
|
||||||
|
|
||||||
gc.status_code = STATUS_OK;
|
gc.status_code = STATUS_OK;
|
||||||
|
|
||||||
// Disregard comments and block delete
|
|
||||||
if (line[0] == '(') { return(gc.status_code); }
|
|
||||||
if (line[0] == '/') { char_counter++; } // ignore block delete
|
|
||||||
|
|
||||||
// Pass 1: Commands
|
// Pass 1: Commands
|
||||||
while(next_statement(&letter, &value, line, &char_counter)) {
|
while(next_statement(&letter, &value, line, &char_counter)) {
|
||||||
int_value = trunc(value);
|
int_value = trunc(value);
|
||||||
@ -151,10 +125,8 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
switch(int_value) {
|
switch(int_value) {
|
||||||
case 0: gc.motion_mode = MOTION_MODE_SEEK; break;
|
case 0: gc.motion_mode = MOTION_MODE_SEEK; break;
|
||||||
case 1: gc.motion_mode = MOTION_MODE_LINEAR; break;
|
case 1: gc.motion_mode = MOTION_MODE_LINEAR; break;
|
||||||
#ifdef __AVR_ATmega328P__
|
|
||||||
case 2: gc.motion_mode = MOTION_MODE_CW_ARC; break;
|
case 2: gc.motion_mode = MOTION_MODE_CW_ARC; break;
|
||||||
case 3: gc.motion_mode = MOTION_MODE_CCW_ARC; break;
|
case 3: gc.motion_mode = MOTION_MODE_CCW_ARC; break;
|
||||||
#endif
|
|
||||||
case 4: next_action = NEXT_ACTION_DWELL; break;
|
case 4: next_action = NEXT_ACTION_DWELL; break;
|
||||||
case 17: select_plane(X_AXIS, Y_AXIS, Z_AXIS); break;
|
case 17: select_plane(X_AXIS, Y_AXIS, Z_AXIS); break;
|
||||||
case 18: select_plane(X_AXIS, Z_AXIS, Y_AXIS); break;
|
case 18: select_plane(X_AXIS, Z_AXIS, Y_AXIS); break;
|
||||||
@ -192,6 +164,7 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
if (gc.status_code) { return(gc.status_code); }
|
if (gc.status_code) { return(gc.status_code); }
|
||||||
|
|
||||||
char_counter = 0;
|
char_counter = 0;
|
||||||
|
clear_vector(target);
|
||||||
clear_vector(offset);
|
clear_vector(offset);
|
||||||
memcpy(target, gc.position, sizeof(target)); // i.e. target = gc.position
|
memcpy(target, gc.position, sizeof(target)); // i.e. target = gc.position
|
||||||
|
|
||||||
@ -201,13 +174,14 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
unit_converted_value = to_millimeters(value);
|
unit_converted_value = to_millimeters(value);
|
||||||
switch(letter) {
|
switch(letter) {
|
||||||
case 'F':
|
case 'F':
|
||||||
|
if (unit_converted_value <= 0) { FAIL(STATUS_BAD_NUMBER_FORMAT); } // Must be greater than zero
|
||||||
if (gc.inverse_feed_rate_mode) {
|
if (gc.inverse_feed_rate_mode) {
|
||||||
inverse_feed_rate = unit_converted_value; // seconds per motion for this motion only
|
inverse_feed_rate = unit_converted_value; // seconds per motion for this motion only
|
||||||
} else {
|
} else {
|
||||||
if (gc.motion_mode == MOTION_MODE_SEEK) {
|
if (gc.motion_mode == MOTION_MODE_SEEK) {
|
||||||
gc.seek_rate = unit_converted_value/60;
|
gc.seek_rate = unit_converted_value;
|
||||||
} else {
|
} else {
|
||||||
gc.feed_rate = unit_converted_value/60; // millimeters pr second
|
gc.feed_rate = unit_converted_value; // millimeters per minute
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -233,8 +207,8 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
|
|
||||||
// Perform any physical actions
|
// Perform any physical actions
|
||||||
switch (next_action) {
|
switch (next_action) {
|
||||||
case NEXT_ACTION_GO_HOME: mc_go_home(); clear_vector(gc.position); break;
|
case NEXT_ACTION_GO_HOME: mc_go_home(); clear_vector(target); break;
|
||||||
case NEXT_ACTION_DWELL: mc_dwell(trunc(p*1000)); break;
|
case NEXT_ACTION_DWELL: mc_dwell(p); break;
|
||||||
case NEXT_ACTION_SET_COORDINATE_OFFSET:
|
case NEXT_ACTION_SET_COORDINATE_OFFSET:
|
||||||
mc_set_current_position(target[X_AXIS], target[Y_AXIS], target[Z_AXIS]);
|
mc_set_current_position(target[X_AXIS], target[Y_AXIS], target[Z_AXIS]);
|
||||||
break;
|
break;
|
||||||
@ -248,7 +222,6 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
mc_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS],
|
mc_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS],
|
||||||
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode);
|
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode);
|
||||||
break;
|
break;
|
||||||
#ifdef __AVR_ATmega328P__
|
|
||||||
case MOTION_MODE_CW_ARC: case MOTION_MODE_CCW_ARC:
|
case MOTION_MODE_CW_ARC: case MOTION_MODE_CCW_ARC:
|
||||||
if (radius_mode) {
|
if (radius_mode) {
|
||||||
/*
|
/*
|
||||||
@ -334,52 +307,30 @@ uint8_t gc_execute_line(char *line) {
|
|||||||
// even though it is advised against ever generating such circles in a single line of g-code. By
|
// even though it is advised against ever generating such circles in a single line of g-code. By
|
||||||
// inverting the sign of h_x2_div_d the center of the circles is placed on the opposite side of the line of
|
// inverting the sign of h_x2_div_d the center of the circles is placed on the opposite side of the line of
|
||||||
// travel and thus we get the unadvisably long arcs as prescribed.
|
// travel and thus we get the unadvisably long arcs as prescribed.
|
||||||
if (r < 0) { h_x2_div_d = -h_x2_div_d; }
|
if (r < 0) {
|
||||||
|
h_x2_div_d = -h_x2_div_d;
|
||||||
|
r = -r; // Finished with r. Set to positive for mc_arc
|
||||||
|
}
|
||||||
// Complete the operation by calculating the actual center of the arc
|
// Complete the operation by calculating the actual center of the arc
|
||||||
offset[gc.plane_axis_0] = (x-(y*h_x2_div_d))/2;
|
offset[gc.plane_axis_0] = 0.5*(x-(y*h_x2_div_d));
|
||||||
offset[gc.plane_axis_1] = (y+(x*h_x2_div_d))/2;
|
offset[gc.plane_axis_1] = 0.5*(y+(x*h_x2_div_d));
|
||||||
|
|
||||||
|
} else { // Offset mode specific computations
|
||||||
|
|
||||||
|
r = hypot(offset[gc.plane_axis_0], offset[gc.plane_axis_1]); // Compute arc radius for mc_arc
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Set clockwise/counter-clockwise sign for mc_arc computations
|
||||||
This segment sets up an clockwise or counterclockwise arc from the current position to the target position around
|
uint8_t isclockwise = false;
|
||||||
the center designated by the offset vector. All theta-values measured in radians of deviance from the positive
|
if (gc.motion_mode == MOTION_MODE_CW_ARC) { isclockwise = true; }
|
||||||
y-axis.
|
|
||||||
|
|
||||||
| <- theta == 0
|
|
||||||
* * *
|
|
||||||
* *
|
|
||||||
* *
|
|
||||||
* O ----T <- theta_end (e.g. 90 degrees: theta_end == PI/2)
|
|
||||||
* /
|
|
||||||
C <- theta_start (e.g. -145 degrees: theta_start == -PI*(3/4))
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
// calculate the theta (angle) of the current point
|
|
||||||
double theta_start = theta(-offset[gc.plane_axis_0], -offset[gc.plane_axis_1]);
|
|
||||||
// calculate the theta (angle) of the target point
|
|
||||||
double theta_end = theta(target[gc.plane_axis_0] - offset[gc.plane_axis_0] - gc.position[gc.plane_axis_0],
|
|
||||||
target[gc.plane_axis_1] - offset[gc.plane_axis_1] - gc.position[gc.plane_axis_1]);
|
|
||||||
// ensure that the difference is positive so that we have clockwise travel
|
|
||||||
if (theta_end < theta_start) { theta_end += 2*M_PI; }
|
|
||||||
double angular_travel = theta_end-theta_start;
|
|
||||||
// Invert angular motion if the g-code wanted a counterclockwise arc
|
|
||||||
if (gc.motion_mode == MOTION_MODE_CCW_ARC) {
|
|
||||||
angular_travel = angular_travel-2*M_PI;
|
|
||||||
}
|
|
||||||
// Find the radius
|
|
||||||
double radius = hypot(offset[gc.plane_axis_0], offset[gc.plane_axis_1]);
|
|
||||||
// Calculate the motion along the depth axis of the helix
|
|
||||||
double depth = target[gc.plane_axis_2]-gc.position[gc.plane_axis_2];
|
|
||||||
// Trace the arc
|
// Trace the arc
|
||||||
mc_arc(theta_start, angular_travel, radius, depth, gc.plane_axis_0, gc.plane_axis_1, gc.plane_axis_2,
|
mc_arc(gc.position, target, offset, gc.plane_axis_0, gc.plane_axis_1, gc.plane_axis_2,
|
||||||
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode,
|
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode,
|
||||||
gc.position);
|
r, isclockwise);
|
||||||
// Finish off with a line to make sure we arrive exactly where we think we are
|
|
||||||
mc_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS],
|
|
||||||
(gc.inverse_feed_rate_mode) ? inverse_feed_rate : gc.feed_rate, gc.inverse_feed_rate_mode);
|
|
||||||
break;
|
break;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -430,4 +381,3 @@ static int next_statement(char *letter, double *double_ptr, char *line, uint8_t
|
|||||||
group 12 = {G54, G55, G56, G57, G58, G59, G59.1, G59.2, G59.3} coordinate system selection
|
group 12 = {G54, G55, G56, G57, G58, G59, G59.1, G59.2, G59.3} coordinate system selection
|
||||||
group 13 = {G61, G61.1, G64} path control mode
|
group 13 = {G61, G61.1, G64} path control mode
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
7
limits.c
7
limits.c
@ -24,6 +24,11 @@
|
|||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "nuts_bolts.h"
|
#include "nuts_bolts.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "motion_control.h"
|
||||||
|
#include "planner.h"
|
||||||
|
|
||||||
|
// TODO: Deprecated. Need to update for new version. Sys.position now tracks position relative
|
||||||
|
// to the home position. Limits should update this vector directly.
|
||||||
|
|
||||||
void limits_init() {
|
void limits_init() {
|
||||||
LIMIT_DDR &= ~(LIMIT_MASK);
|
LIMIT_DDR &= ~(LIMIT_MASK);
|
||||||
@ -87,7 +92,7 @@ static void leave_limit_switch(bool x, bool y, bool z) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void limits_go_home() {
|
void limits_go_home() {
|
||||||
st_synchronize();
|
plan_synchronize();
|
||||||
// Store the current limit switch state
|
// Store the current limit switch state
|
||||||
uint8_t original_limit_state = LIMIT_PIN;
|
uint8_t original_limit_state = LIMIT_PIN;
|
||||||
approach_limit_switch(false, false, true); // First home the z axis
|
approach_limit_switch(false, false, true); // First home the z axis
|
||||||
|
68
main.c
68
main.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -18,38 +19,75 @@
|
|||||||
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <avr/io.h>
|
|
||||||
#include <avr/sleep.h>
|
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <util/delay.h>
|
#include <avr/pgmspace.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "planner.h"
|
#include "planner.h"
|
||||||
|
#include "nuts_bolts.h"
|
||||||
#include "stepper.h"
|
#include "stepper.h"
|
||||||
#include "spindle_control.h"
|
#include "spindle_control.h"
|
||||||
#include "motion_control.h"
|
#include "motion_control.h"
|
||||||
#include "gcode.h"
|
#include "gcode.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include "limits.h"
|
#include "limits.h"
|
||||||
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
|
|
||||||
|
// Declare system global variable structure
|
||||||
|
system_t sys;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
sei();
|
// Initialize system
|
||||||
|
serial_init(BAUD_RATE); // Setup serial baud rate and interrupts
|
||||||
|
st_init(); // Setup stepper pins and interrupt timers
|
||||||
|
sei(); // Enable interrupts
|
||||||
|
|
||||||
serial_init(BAUD_RATE);
|
memset(&sys, 0, sizeof(sys)); // Clear all system variables
|
||||||
protocol_init();
|
sys.abort = true; // Set abort to complete initialization
|
||||||
settings_init();
|
|
||||||
plan_init();
|
|
||||||
st_init();
|
|
||||||
spindle_init();
|
|
||||||
gc_init();
|
|
||||||
limits_init();
|
|
||||||
|
|
||||||
for(;;){
|
while(1) {
|
||||||
sleep_mode(); // Wait for it ...
|
|
||||||
|
// 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) {
|
||||||
|
|
||||||
|
// Retain last known machine position. If the system abort occurred while in motion, machine
|
||||||
|
// position is not guaranteed, since a hard stop can cause the steppers to lose steps. Always
|
||||||
|
// perform a feedhold before an abort, if maintaining accurate machine position is required.
|
||||||
|
int32_t last_position[3];
|
||||||
|
memcpy(last_position, sys.position, sizeof(sys.position)); // last_position[] = sys.position[]
|
||||||
|
|
||||||
|
// Clear all system variables
|
||||||
|
memset(&sys, 0, sizeof(sys));
|
||||||
|
|
||||||
|
// Update last known machine position. Set the post-abort work position as the origin [0,0,0],
|
||||||
|
// which corresponds to the g-code parser and planner positions after re-initialization.
|
||||||
|
memcpy(sys.position, last_position, sizeof(last_position)); // sys.position[] = last_position[]
|
||||||
|
memcpy(sys.coord_offset, last_position, sizeof(last_position)); // sys.coord_offset[] = last_position[]
|
||||||
|
|
||||||
|
// Reset system.
|
||||||
|
serial_reset_read_buffer(); // Clear serial read buffer
|
||||||
|
settings_init(); // Load grbl settings from EEPROM
|
||||||
|
protocol_init(); // Clear incoming line data
|
||||||
|
plan_init(); // Clear block buffer and planner variables
|
||||||
|
gc_init(); // Set g-code parser to default state
|
||||||
|
spindle_init();
|
||||||
|
limits_init();
|
||||||
|
st_reset(); // Clear stepper subsystem variables.
|
||||||
|
|
||||||
|
// Set system runtime defaults
|
||||||
|
// TODO: Eventual move to EEPROM from config.h when all of the new settings are worked out.
|
||||||
|
// Mainly to avoid having to maintain several different versions.
|
||||||
|
#ifdef CYCLE_AUTO_START
|
||||||
|
sys.auto_start = true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol_execute_runtime();
|
||||||
protocol_process(); // ... process the serial protocol
|
protocol_process(); // ... process the serial protocol
|
||||||
|
|
||||||
}
|
}
|
||||||
return 0; /* never reached */
|
return 0; /* never reached */
|
||||||
}
|
}
|
||||||
|
194
motion_control.c
194
motion_control.c
@ -3,6 +3,8 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
Copyright (c) 2011 Jens Geisler
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -20,6 +22,7 @@
|
|||||||
|
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "config.h"
|
||||||
#include "motion_control.h"
|
#include "motion_control.h"
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@ -27,59 +30,172 @@
|
|||||||
#include "nuts_bolts.h"
|
#include "nuts_bolts.h"
|
||||||
#include "stepper.h"
|
#include "stepper.h"
|
||||||
#include "planner.h"
|
#include "planner.h"
|
||||||
|
#include "limits.h"
|
||||||
|
#include "protocol.h"
|
||||||
|
|
||||||
|
// Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second
|
||||||
void mc_dwell(uint32_t milliseconds)
|
// unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in
|
||||||
|
// (1 minute)/feed_rate time.
|
||||||
|
// NOTE: This is the primary gateway to the grbl planner. All line motions, including arc line
|
||||||
|
// segments, must pass through this routine before being passed to the planner. The seperation of
|
||||||
|
// mc_line and plan_buffer_line is done primarily to make backlash compensation integration simple
|
||||||
|
// and direct.
|
||||||
|
// TODO: Check for a better way to avoid having to push the arguments twice for non-backlash cases.
|
||||||
|
// However, this keeps the memory requirements lower since it doesn't have to call and hold two
|
||||||
|
// plan_buffer_lines in memory. Grbl only has to retain the original line input variables during a
|
||||||
|
// backlash segment(s).
|
||||||
|
void mc_line(double x, double y, double z, double feed_rate, uint8_t invert_feed_rate)
|
||||||
{
|
{
|
||||||
st_synchronize();
|
// TODO: Backlash compensation may be installed here. Only need direction info to track when
|
||||||
_delay_ms(milliseconds);
|
// to insert a backlash line motion(s) before the intended line motion. Requires its own
|
||||||
|
// plan_check_full_buffer() and check for system abort loop. Also for position reporting
|
||||||
|
// backlash steps will need to be also tracked. Not sure what the best strategy is for this,
|
||||||
|
// i.e. keep the planner independent and do the computations in the status reporting, or let
|
||||||
|
// the planner handle the position corrections. The latter may get complicated.
|
||||||
|
|
||||||
|
// If the buffer is full: good! That means we are well ahead of the robot.
|
||||||
|
// Remain in this loop until there is room in the buffer.
|
||||||
|
do {
|
||||||
|
protocol_execute_runtime(); // Check for any run-time commands
|
||||||
|
if (sys.abort) { return; } // Bail, if system abort.
|
||||||
|
} while ( plan_check_full_buffer() );
|
||||||
|
plan_buffer_line(x, y, z, feed_rate, invert_feed_rate);
|
||||||
|
|
||||||
|
// Auto-cycle start immediately after planner finishes. Enabled/disabled by grbl settings. During
|
||||||
|
// a feed hold, auto-start is disabled momentarily until the cycle is resumed by the cycle-start
|
||||||
|
// runtime command.
|
||||||
|
// NOTE: This is allows the user to decide to exclusively use the cycle start runtime command to
|
||||||
|
// begin motion or let grbl auto-start it for them. This is useful when: manually cycle-starting
|
||||||
|
// when the buffer is completely full and primed; auto-starting, if there was only one g-code
|
||||||
|
// command sent during manual operation; or if a system is prone to buffer starvation, auto-start
|
||||||
|
// helps make sure it minimizes any dwelling/motion hiccups and keeps the cycle going.
|
||||||
|
if (sys.auto_start) { st_cycle_start(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute an arc. theta == start angle, angular_travel == number of radians to go along the arc,
|
|
||||||
// positive angular_travel means clockwise, negative means counterclockwise. Radius == the radius of the
|
|
||||||
// circle in millimeters. axis_1 and axis_2 selects the circle plane in tool space. Stick the remaining
|
|
||||||
// axis in axis_l which will be the axis for linear travel if you are tracing a helical motion.
|
|
||||||
// position is a pointer to a vector representing the current position in millimeters.
|
|
||||||
|
|
||||||
#ifdef __AVR_ATmega328P__
|
// Execute an arc in offset mode format. position == current xyz, target == target xyz,
|
||||||
|
// offset == offset from current xyz, axis_XXX defines circle plane in tool space, axis_linear is
|
||||||
|
// the direction of helical travel, radius == circle radius, isclockwise boolean. Used
|
||||||
|
// for vector transformation direction.
|
||||||
// The arc is approximated by generating a huge number of tiny, linear segments. The length of each
|
// The arc is approximated by generating a huge number of tiny, linear segments. The length of each
|
||||||
// segment is configured in settings.mm_per_arc_segment.
|
// segment is configured in settings.mm_per_arc_segment.
|
||||||
void mc_arc(double theta, double angular_travel, double radius, double linear_travel, int axis_1, int axis_2,
|
void mc_arc(double *position, double *target, double *offset, uint8_t axis_0, uint8_t axis_1,
|
||||||
int axis_linear, double feed_rate, int invert_feed_rate, double *position)
|
uint8_t axis_linear, double feed_rate, uint8_t invert_feed_rate, double radius, uint8_t isclockwise)
|
||||||
{
|
{
|
||||||
int acceleration_manager_was_enabled = plan_is_acceleration_manager_enabled();
|
double center_axis0 = position[axis_0] + offset[axis_0];
|
||||||
plan_set_acceleration_manager_enabled(false); // disable acceleration management for the duration of the arc
|
double center_axis1 = position[axis_1] + offset[axis_1];
|
||||||
double millimeters_of_travel = hypot(angular_travel*radius, labs(linear_travel));
|
double linear_travel = target[axis_linear] - position[axis_linear];
|
||||||
|
double r_axis0 = -offset[axis_0]; // Radius vector from center to current location
|
||||||
|
double r_axis1 = -offset[axis_1];
|
||||||
|
double rt_axis0 = target[axis_0] - center_axis0;
|
||||||
|
double rt_axis1 = target[axis_1] - center_axis1;
|
||||||
|
|
||||||
|
// CCW angle between position and target from circle center. Only one atan2() trig computation required.
|
||||||
|
double angular_travel = atan2(r_axis0*rt_axis1-r_axis1*rt_axis0, r_axis0*rt_axis0+r_axis1*rt_axis1);
|
||||||
|
if (angular_travel < 0) { angular_travel += 2*M_PI; }
|
||||||
|
if (isclockwise) { angular_travel -= 2*M_PI; }
|
||||||
|
|
||||||
|
double millimeters_of_travel = hypot(angular_travel*radius, fabs(linear_travel));
|
||||||
if (millimeters_of_travel == 0.0) { return; }
|
if (millimeters_of_travel == 0.0) { return; }
|
||||||
uint16_t segments = round(millimeters_of_travel/settings.mm_per_arc_segment);
|
uint16_t segments = floor(millimeters_of_travel/settings.mm_per_arc_segment);
|
||||||
// Multiply inverse feed_rate to compensate for the fact that this movement is approximated
|
// Multiply inverse feed_rate to compensate for the fact that this movement is approximated
|
||||||
// by a number of discrete segments. The inverse feed_rate should be correct for the sum of
|
// by a number of discrete segments. The inverse feed_rate should be correct for the sum of
|
||||||
// all segments.
|
// all segments.
|
||||||
if (invert_feed_rate) { feed_rate *= segments; }
|
if (invert_feed_rate) { feed_rate *= segments; }
|
||||||
// The angular motion for each segment
|
|
||||||
double theta_per_segment = angular_travel/segments;
|
|
||||||
// The linear motion for each segment
|
|
||||||
double linear_per_segment = linear_travel/segments;
|
|
||||||
// Compute the center of this circle
|
|
||||||
double center_x = position[axis_1]-sin(theta)*radius;
|
|
||||||
double center_y = position[axis_2]-cos(theta)*radius;
|
|
||||||
// a vector to track the end point of each segment
|
|
||||||
double target[3];
|
|
||||||
int i;
|
|
||||||
// Initialize the linear axis
|
|
||||||
target[axis_linear] = position[axis_linear];
|
|
||||||
for (i=0; i<segments; i++) {
|
|
||||||
target[axis_linear] += linear_per_segment;
|
|
||||||
theta += theta_per_segment;
|
|
||||||
target[axis_1] = center_x+sin(theta)*radius;
|
|
||||||
target[axis_2] = center_y+cos(theta)*radius;
|
|
||||||
plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], feed_rate, invert_feed_rate);
|
|
||||||
}
|
|
||||||
plan_set_acceleration_manager_enabled(acceleration_manager_was_enabled);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
double theta_per_segment = angular_travel/segments;
|
||||||
|
double linear_per_segment = linear_travel/segments;
|
||||||
|
|
||||||
|
/* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
|
||||||
|
and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
|
||||||
|
r_T = [cos(phi) -sin(phi);
|
||||||
|
sin(phi) cos(phi] * r ;
|
||||||
|
|
||||||
|
For arc generation, the center of the circle is the axis of rotation and the radius vector is
|
||||||
|
defined from the circle center to the initial position. Each line segment is formed by successive
|
||||||
|
vector rotations. This requires only two cos() and sin() computations to form the rotation
|
||||||
|
matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
|
||||||
|
all double numbers are single precision on the Arduino. (True double precision will not have
|
||||||
|
round off issues for CNC applications.) Single precision error can accumulate to be greater than
|
||||||
|
tool precision in some cases. Therefore, arc path correction is implemented.
|
||||||
|
|
||||||
|
Small angle approximation may be used to reduce computation overhead further. This approximation
|
||||||
|
holds for everything, but very small circles and large mm_per_arc_segment values. In other words,
|
||||||
|
theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
|
||||||
|
to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
|
||||||
|
numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
|
||||||
|
issue for CNC machines with the single precision Arduino calculations.
|
||||||
|
|
||||||
|
This approximation also allows mc_arc to immediately insert a line segment into the planner
|
||||||
|
without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
|
||||||
|
a correction, the planner should have caught up to the lag caused by the initial mc_arc overhead.
|
||||||
|
This is important when there are successive arc motions.
|
||||||
|
*/
|
||||||
|
// Vector rotation matrix values
|
||||||
|
double cos_T = 1-0.5*theta_per_segment*theta_per_segment; // Small angle approximation
|
||||||
|
double sin_T = theta_per_segment;
|
||||||
|
|
||||||
|
double arc_target[3];
|
||||||
|
double sin_Ti;
|
||||||
|
double cos_Ti;
|
||||||
|
double r_axisi;
|
||||||
|
uint16_t i;
|
||||||
|
int8_t count = 0;
|
||||||
|
|
||||||
|
// Initialize the linear axis
|
||||||
|
arc_target[axis_linear] = position[axis_linear];
|
||||||
|
|
||||||
|
for (i = 1; i<segments; i++) { // Increment (segments-1)
|
||||||
|
|
||||||
|
if (count < N_ARC_CORRECTION) {
|
||||||
|
// Apply vector rotation matrix
|
||||||
|
r_axisi = r_axis0*sin_T + r_axis1*cos_T;
|
||||||
|
r_axis0 = r_axis0*cos_T - r_axis1*sin_T;
|
||||||
|
r_axis1 = r_axisi;
|
||||||
|
count++;
|
||||||
|
} else {
|
||||||
|
// Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
|
||||||
|
// Compute exact location by applying transformation matrix from initial radius vector(=-offset).
|
||||||
|
cos_Ti = cos(i*theta_per_segment);
|
||||||
|
sin_Ti = sin(i*theta_per_segment);
|
||||||
|
r_axis0 = -offset[axis_0]*cos_Ti + offset[axis_1]*sin_Ti;
|
||||||
|
r_axis1 = -offset[axis_0]*sin_Ti - offset[axis_1]*cos_Ti;
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update arc_target location
|
||||||
|
arc_target[axis_0] = center_axis0 + r_axis0;
|
||||||
|
arc_target[axis_1] = center_axis1 + r_axis1;
|
||||||
|
arc_target[axis_linear] += linear_per_segment;
|
||||||
|
mc_line(arc_target[X_AXIS], arc_target[Y_AXIS], arc_target[Z_AXIS], feed_rate, invert_feed_rate);
|
||||||
|
|
||||||
|
// Bail mid-circle on system abort. Runtime command check already performed by mc_line.
|
||||||
|
if (sys.abort) { return; }
|
||||||
|
}
|
||||||
|
// Ensure last segment arrives at target location.
|
||||||
|
mc_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], feed_rate, invert_feed_rate);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Execute dwell in seconds.
|
||||||
|
void mc_dwell(double seconds)
|
||||||
|
{
|
||||||
|
uint16_t i = floor(1000/DWELL_TIME_STEP*seconds);
|
||||||
|
plan_synchronize();
|
||||||
|
delay_ms(floor(1000*seconds-i*DWELL_TIME_STEP)); // Delay millisecond remainder
|
||||||
|
while (i > 0) {
|
||||||
|
// NOTE: Check and execute runtime commands during dwell every <= DWELL_TIME_STEP milliseconds.
|
||||||
|
protocol_execute_runtime();
|
||||||
|
if (sys.abort) { return; }
|
||||||
|
_delay_ms(DWELL_TIME_STEP); // Delay DWELL_TIME_STEP increment
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Update limits and homing cycle subprograms for better integration with new features.
|
||||||
void mc_go_home()
|
void mc_go_home()
|
||||||
{
|
{
|
||||||
st_go_home();
|
limits_go_home();
|
||||||
|
plan_set_current_position(0,0,0);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -24,28 +25,24 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include "planner.h"
|
#include "planner.h"
|
||||||
|
|
||||||
// NOTE: Although the following functions structurally belongs in this module, there is nothing to do but
|
// NOTE: Although the following function structurally belongs in this module, there is nothing to do but
|
||||||
// to forward the request to the planner.
|
// to forward the request to the planner.
|
||||||
|
#define mc_set_current_position(x, y, z) plan_set_current_position(x, y, z)
|
||||||
|
|
||||||
// Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second
|
// Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second
|
||||||
// unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in
|
// unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in
|
||||||
// (1 minute)/feed_rate time.
|
// (1 minute)/feed_rate time.
|
||||||
#define mc_line(x, y, z, feed_rate, invert_feed_rate) plan_buffer_line(x, y, z, feed_rate, invert_feed_rate)
|
void mc_line(double x, double y, double z, double feed_rate, uint8_t invert_feed_rate);
|
||||||
|
|
||||||
#define mc_set_current_position(x, y, z) plan_set_current_position(x, y, z)
|
// Execute an arc in offset mode format. position == current xyz, target == target xyz,
|
||||||
|
// offset == offset from current xyz, axis_XXX defines circle plane in tool space, axis_linear is
|
||||||
|
// the direction of helical travel, radius == circle radius, isclockwise boolean. Used
|
||||||
|
// for vector transformation direction.
|
||||||
|
void mc_arc(double *position, double *target, double *offset, uint8_t axis_0, uint8_t axis_1,
|
||||||
|
uint8_t axis_linear, double feed_rate, uint8_t invert_feed_rate, double radius, uint8_t isclockwise);
|
||||||
|
|
||||||
#ifdef __AVR_ATmega328P__
|
// Dwell for a specific number of seconds
|
||||||
// Execute an arc. theta == start angle, angular_travel == number of radians to go along the arc,
|
void mc_dwell(double seconds);
|
||||||
// positive angular_travel means clockwise, negative means counterclockwise. Radius == the radius of the
|
|
||||||
// circle in millimeters. axis_1 and axis_2 selects the circle plane in tool space. Stick the remaining
|
|
||||||
// axis in axis_l which will be the axis for linear travel if you are tracing a helical motion.
|
|
||||||
|
|
||||||
void mc_arc(double theta, double angular_travel, double radius, double linear_travel, int axis_1, int axis_2,
|
|
||||||
int axis_linear, double feed_rate, int invert_feed_rate, double *position);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Dwell for a couple of time units
|
|
||||||
void mc_dwell(uint32_t milliseconds);
|
|
||||||
|
|
||||||
// Send the tool home (not implemented)
|
// Send the tool home (not implemented)
|
||||||
void mc_go_home();
|
void mc_go_home();
|
||||||
|
28
nuts_bolts.c
28
nuts_bolts.c
@ -1,6 +1,27 @@
|
|||||||
|
/*
|
||||||
|
nuts_bolts.c - Shared functions
|
||||||
|
Part of Grbl
|
||||||
|
|
||||||
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "nuts_bolts.h"
|
#include "nuts_bolts.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
|
||||||
int read_double(char *line, uint8_t *char_counter, double *double_ptr)
|
int read_double(char *line, uint8_t *char_counter, double *double_ptr)
|
||||||
{
|
{
|
||||||
@ -16,3 +37,10 @@ int read_double(char *line, uint8_t *char_counter, double *double_ptr)
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Delays variable defined milliseconds. Compiler compatibility fix for _delay_ms(),
|
||||||
|
// which only accepts constants in future compiler releases.
|
||||||
|
void delay_ms(uint16_t ms)
|
||||||
|
{
|
||||||
|
while ( ms-- ) { _delay_ms(1); }
|
||||||
|
}
|
49
nuts_bolts.h
49
nuts_bolts.h
@ -1,8 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
motion_control.h - cartesian robot controller.
|
nuts_bolts.h - Header file for shared definitions, variables, and functions
|
||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -31,12 +32,58 @@
|
|||||||
#define Y_AXIS 1
|
#define Y_AXIS 1
|
||||||
#define Z_AXIS 2
|
#define Z_AXIS 2
|
||||||
|
|
||||||
|
#define MM_PER_INCH (25.4)
|
||||||
|
|
||||||
|
// Useful macros
|
||||||
#define clear_vector(a) memset(a, 0, sizeof(a))
|
#define clear_vector(a) memset(a, 0, sizeof(a))
|
||||||
|
#define clear_vector_double(a) memset(a, 0.0, sizeof(a))
|
||||||
#define max(a,b) (((a) > (b)) ? (a) : (b))
|
#define max(a,b) (((a) > (b)) ? (a) : (b))
|
||||||
|
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||||
|
|
||||||
|
// Bit field and masking macros
|
||||||
|
#define bit(n) (1 << n)
|
||||||
|
#define bit_true(x,mask) (x |= mask)
|
||||||
|
#define bit_false(x,mask) (x &= ~mask)
|
||||||
|
#define bit_toggle(x,mask) (x ^= mask)
|
||||||
|
#define bit_istrue(x,mask) ((x & mask) != 0)
|
||||||
|
#define bit_isfalse(x,mask) ((x & mask) == 0)
|
||||||
|
|
||||||
|
// Define system executor bit map. Used internally by runtime protocol as runtime command flags,
|
||||||
|
// which notifies the main program to execute the specified runtime command asynchronously.
|
||||||
|
// NOTE: The system executor uses an unsigned 8-bit volatile variable (8 flag limit.) The default
|
||||||
|
// flags are always false, so the runtime protocol only needs to check for a non-zero value to
|
||||||
|
// know when there is a runtime command to execute.
|
||||||
|
#define EXEC_STATUS_REPORT bit(0) // bitmask 00000001
|
||||||
|
#define EXEC_CYCLE_START bit(1) // bitmask 00000010
|
||||||
|
#define EXEC_CYCLE_STOP bit(2) // bitmask 00000100
|
||||||
|
#define EXEC_FEED_HOLD bit(3) // bitmask 00001000
|
||||||
|
#define EXEC_RESET bit(4) // bitmask 00010000
|
||||||
|
// #define bit(5) // bitmask 00100000
|
||||||
|
// #define bit(6) // bitmask 01000000
|
||||||
|
// #define bit(7) // bitmask 10000000
|
||||||
|
|
||||||
|
// Define global system variables
|
||||||
|
typedef struct {
|
||||||
|
uint8_t abort; // System abort flag. Forces exit back to main loop for reset.
|
||||||
|
uint8_t feed_hold; // Feed hold flag. Held true during feed hold. Released when ready to resume.
|
||||||
|
uint8_t auto_start; // Planner auto-start flag. Toggled off during feed hold. Defaulted by settings.
|
||||||
|
|
||||||
|
int32_t position[3]; // Real-time machine (aka home) position vector in steps.
|
||||||
|
// NOTE: This may need to be a volatile variable, if problems arise.
|
||||||
|
int32_t coord_offset[3]; // Retains the G92 coordinate offset (work coordinates) relative to
|
||||||
|
// machine zero in steps.
|
||||||
|
|
||||||
|
volatile uint8_t cycle_start; // Cycle start flag. Set by stepper subsystem or main program.
|
||||||
|
volatile uint8_t execute; // Global system runtime executor bitflag variable. See EXEC bitmasks.
|
||||||
|
} system_t;
|
||||||
|
extern system_t sys;
|
||||||
|
|
||||||
// Read a floating point value from a string. Line points to the input buffer, char_counter
|
// Read a floating point value from a string. Line points to the input buffer, char_counter
|
||||||
// is the indexer pointing to the current character of the line, while double_ptr is
|
// is the indexer pointing to the current character of the line, while double_ptr is
|
||||||
// a pointer to the result variable. Returns true when it succeeds
|
// a pointer to the result variable. Returns true when it succeeds
|
||||||
int read_double(char *line, uint8_t *char_counter, double *double_ptr);
|
int read_double(char *line, uint8_t *char_counter, double *double_ptr);
|
||||||
|
|
||||||
|
// Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms().
|
||||||
|
void delay_ms(uint16_t ms);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
519
planner.c
519
planner.c
@ -3,6 +3,8 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
Copyright (c) 2011 Jens Geisler
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -29,34 +31,53 @@
|
|||||||
#include "stepper.h"
|
#include "stepper.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "protocol.h"
|
||||||
|
|
||||||
// The number of linear motions that can be in the plan at any give time
|
// The number of linear motions that can be in the plan at any give time
|
||||||
#ifdef __AVR_ATmega328P__
|
#define BLOCK_BUFFER_SIZE 18
|
||||||
#define BLOCK_BUFFER_SIZE 16
|
|
||||||
#else
|
|
||||||
#define BLOCK_BUFFER_SIZE 5
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions
|
static block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions
|
||||||
static volatile uint8_t block_buffer_head; // Index of the next block to be pushed
|
static volatile uint8_t block_buffer_head; // Index of the next block to be pushed
|
||||||
static volatile uint8_t block_buffer_tail; // Index of the block to process now
|
static volatile uint8_t block_buffer_tail; // Index of the block to process now
|
||||||
|
static uint8_t next_buffer_head; // Index of the next buffer head
|
||||||
|
|
||||||
// The current position of the tool in absolute steps
|
// Define planner variables
|
||||||
static int32_t position[3];
|
typedef struct {
|
||||||
|
int32_t position[3]; // The planner position of the tool in absolute steps. Kept separate
|
||||||
|
// from g-code position for movements requiring multiple line motions,
|
||||||
|
// i.e. arcs, canned cycles, and backlash compensation.
|
||||||
|
double previous_unit_vec[3]; // Unit vector of previous path line segment
|
||||||
|
double previous_nominal_speed; // Nominal speed of previous path line segment
|
||||||
|
} planner_t;
|
||||||
|
static planner_t pl;
|
||||||
|
|
||||||
static uint8_t acceleration_manager_enabled; // Acceleration management active?
|
// Returns the index of the next block in the ring buffer
|
||||||
|
// NOTE: Removed modulo (%) operator, which uses an expensive divide and multiplication.
|
||||||
|
static uint8_t next_block_index(uint8_t block_index)
|
||||||
|
{
|
||||||
|
block_index++;
|
||||||
|
if (block_index == BLOCK_BUFFER_SIZE) { block_index = 0; }
|
||||||
|
return(block_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Returns the index of the previous block in the ring buffer
|
||||||
|
static uint8_t prev_block_index(uint8_t block_index)
|
||||||
|
{
|
||||||
|
if (block_index == 0) { block_index = BLOCK_BUFFER_SIZE; }
|
||||||
|
block_index--;
|
||||||
|
return(block_index);
|
||||||
|
}
|
||||||
|
|
||||||
#define ONE_MINUTE_OF_MICROSECONDS 60000000.0
|
|
||||||
|
|
||||||
// Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
|
// Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
|
||||||
// given acceleration:
|
// given acceleration:
|
||||||
static double estimate_acceleration_distance(double initial_rate, double target_rate, double acceleration) {
|
static double estimate_acceleration_distance(double initial_rate, double target_rate, double acceleration)
|
||||||
return(
|
{
|
||||||
(target_rate*target_rate-initial_rate*initial_rate)/
|
return( (target_rate*target_rate-initial_rate*initial_rate)/(2*acceleration) );
|
||||||
(2L*acceleration)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* + <- some maximum rate we don't care about
|
/* + <- some maximum rate we don't care about
|
||||||
/|\
|
/|\
|
||||||
/ | \
|
/ | \
|
||||||
@ -66,128 +87,99 @@ static double estimate_acceleration_distance(double initial_rate, double target_
|
|||||||
^ ^
|
^ ^
|
||||||
| |
|
| |
|
||||||
intersection_distance distance */
|
intersection_distance distance */
|
||||||
|
|
||||||
|
|
||||||
// This function gives you the point at which you must start braking (at the rate of -acceleration) if
|
// This function gives you the point at which you must start braking (at the rate of -acceleration) if
|
||||||
// you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
|
// you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
|
||||||
// a total travel of distance. This can be used to compute the intersection point between acceleration and
|
// a total travel of distance. This can be used to compute the intersection point between acceleration and
|
||||||
// deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
|
// deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
|
||||||
|
static double intersection_distance(double initial_rate, double final_rate, double acceleration, double distance)
|
||||||
static double intersection_distance(double initial_rate, double final_rate, double acceleration, double distance) {
|
{
|
||||||
return(
|
return( (2*acceleration*distance-initial_rate*initial_rate+final_rate*final_rate)/(4*acceleration) );
|
||||||
(2*acceleration*distance-initial_rate*initial_rate+final_rate*final_rate)/
|
|
||||||
(4*acceleration)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
|
// Calculates the maximum allowable speed at this point when you must be able to reach target_velocity
|
||||||
// acceleration within the allotted distance.
|
// using the acceleration within the allotted distance.
|
||||||
static double max_allowable_speed(double acceleration, double target_velocity, double distance) {
|
// NOTE: sqrt() reimplimented here from prior version due to improved planner logic. Increases speed
|
||||||
return(
|
// in time critical computations, i.e. arcs or rapid short lines from curves. Guaranteed to not exceed
|
||||||
sqrt(target_velocity*target_velocity-2*acceleration*60*60*distance)
|
// BLOCK_BUFFER_SIZE calls per planner cycle.
|
||||||
);
|
static double max_allowable_speed(double acceleration, double target_velocity, double distance)
|
||||||
}
|
{
|
||||||
|
return( sqrt(target_velocity*target_velocity-2*acceleration*distance) );
|
||||||
// "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
|
|
||||||
// This method will calculate the junction jerk as the euclidean distance between the nominal
|
|
||||||
// velocities of the respective blocks.
|
|
||||||
static double junction_jerk(block_t *before, block_t *after) {
|
|
||||||
return(sqrt(
|
|
||||||
pow(before->speed_x-after->speed_x, 2)+
|
|
||||||
pow(before->speed_y-after->speed_y, 2)+
|
|
||||||
pow(before->speed_z-after->speed_z, 2))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate a braking factor to reach baseline speed which is max_jerk/2, e.g. the
|
|
||||||
// speed under which you cannot exceed max_jerk no matter what you do.
|
|
||||||
static double factor_for_safe_speed(block_t *block) {
|
|
||||||
if(settings.max_jerk < block->nominal_speed) {
|
|
||||||
return(settings.max_jerk/block->nominal_speed);
|
|
||||||
} else {
|
|
||||||
return(1.0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// The kernel called by planner_recalculate() when scanning the plan from last to first entry.
|
// The kernel called by planner_recalculate() when scanning the plan from last to first entry.
|
||||||
static void planner_reverse_pass_kernel(block_t *previous, block_t *current, block_t *next) {
|
static void planner_reverse_pass_kernel(block_t *previous, block_t *current, block_t *next)
|
||||||
if(!current) { return; }
|
{
|
||||||
|
if (!current) { return; } // Cannot operate on nothing.
|
||||||
|
|
||||||
double entry_factor = 1.0;
|
|
||||||
double exit_factor;
|
|
||||||
if (next) {
|
if (next) {
|
||||||
exit_factor = next->entry_factor;
|
// If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
|
||||||
} else {
|
// If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
|
||||||
exit_factor = factor_for_safe_speed(current);
|
// check for maximum allowable speed reductions to ensure maximum possible planned speed.
|
||||||
}
|
if (current->entry_speed != current->max_entry_speed) {
|
||||||
|
|
||||||
// Calculate the entry_factor for the current block.
|
// If nominal length true, max junction speed is guaranteed to be reached. Only compute
|
||||||
if (previous) {
|
// for max allowable speed if block is decelerating and nominal length is false.
|
||||||
// Reduce speed so that junction_jerk is within the maximum allowed
|
if ((!current->nominal_length_flag) && (current->max_entry_speed > next->entry_speed)) {
|
||||||
double jerk = junction_jerk(previous, current);
|
current->entry_speed = min( current->max_entry_speed,
|
||||||
if (jerk > settings.max_jerk) {
|
max_allowable_speed(-settings.acceleration,next->entry_speed,current->millimeters));
|
||||||
entry_factor = (settings.max_jerk/jerk);
|
} else {
|
||||||
}
|
current->entry_speed = current->max_entry_speed;
|
||||||
// If the required deceleration across the block is too rapid, reduce the entry_factor accordingly.
|
|
||||||
if (entry_factor > exit_factor) {
|
|
||||||
double max_entry_speed = max_allowable_speed(-settings.acceleration,current->nominal_speed*exit_factor,
|
|
||||||
current->millimeters);
|
|
||||||
double max_entry_factor = max_entry_speed/current->nominal_speed;
|
|
||||||
if (max_entry_factor < entry_factor) {
|
|
||||||
entry_factor = max_entry_factor;
|
|
||||||
}
|
}
|
||||||
}
|
current->recalculate_flag = true;
|
||||||
} else {
|
|
||||||
entry_factor = factor_for_safe_speed(current);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store result
|
}
|
||||||
current->entry_factor = entry_factor;
|
} // Skip last block. Already initialized and set for recalculation.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
|
// planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
|
||||||
// implements the reverse pass.
|
// implements the reverse pass.
|
||||||
static void planner_reverse_pass() {
|
static void planner_reverse_pass()
|
||||||
auto int8_t block_index = block_buffer_head;
|
{
|
||||||
|
uint8_t block_index = block_buffer_head;
|
||||||
block_t *block[3] = {NULL, NULL, NULL};
|
block_t *block[3] = {NULL, NULL, NULL};
|
||||||
while(block_index != block_buffer_tail) {
|
while(block_index != block_buffer_tail) {
|
||||||
block_index--;
|
block_index = prev_block_index( block_index );
|
||||||
if(block_index < 0) {
|
|
||||||
block_index = BLOCK_BUFFER_SIZE-1;
|
|
||||||
}
|
|
||||||
block[2]= block[1];
|
block[2]= block[1];
|
||||||
block[1]= block[0];
|
block[1]= block[0];
|
||||||
block[0] = &block_buffer[block_index];
|
block[0] = &block_buffer[block_index];
|
||||||
planner_reverse_pass_kernel(block[0], block[1], block[2]);
|
planner_reverse_pass_kernel(block[0], block[1], block[2]);
|
||||||
}
|
}
|
||||||
planner_reverse_pass_kernel(NULL, block[0], block[1]);
|
// Skip buffer tail/first block to prevent over-writing the initial entry speed.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// The kernel called by planner_recalculate() when scanning the plan from first to last entry.
|
// The kernel called by planner_recalculate() when scanning the plan from first to last entry.
|
||||||
static void planner_forward_pass_kernel(block_t *previous, block_t *current, block_t *next) {
|
static void planner_forward_pass_kernel(block_t *previous, block_t *current, block_t *next)
|
||||||
if(!current) { return; }
|
{
|
||||||
// If the previous block is an acceleration block, but it is not long enough to
|
if(!previous) { return; } // Begin planning after buffer_tail
|
||||||
// complete the full speed change within the block, we need to adjust out entry
|
|
||||||
// speed accordingly. Remember current->entry_factor equals the exit factor of
|
// If the previous block is an acceleration block, but it is not long enough to complete the
|
||||||
// the previous block.¨
|
// full speed change within the block, we need to adjust the entry speed accordingly. Entry
|
||||||
if(previous) {
|
// speeds have already been reset, maximized, and reverse planned by reverse planner.
|
||||||
if(previous->entry_factor < current->entry_factor) {
|
// If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
|
||||||
double max_entry_speed = max_allowable_speed(-settings.acceleration,
|
if (!previous->nominal_length_flag) {
|
||||||
current->nominal_speed*previous->entry_factor, previous->millimeters);
|
if (previous->entry_speed < current->entry_speed) {
|
||||||
double max_entry_factor = max_entry_speed/current->nominal_speed;
|
double entry_speed = min( current->entry_speed,
|
||||||
if (max_entry_factor < current->entry_factor) {
|
max_allowable_speed(-settings.acceleration,previous->entry_speed,previous->millimeters) );
|
||||||
current->entry_factor = max_entry_factor;
|
|
||||||
|
// Check for junction speed change
|
||||||
|
if (current->entry_speed != entry_speed) {
|
||||||
|
current->entry_speed = entry_speed;
|
||||||
|
current->recalculate_flag = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
|
// planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
|
||||||
// implements the forward pass.
|
// implements the forward pass.
|
||||||
static void planner_forward_pass() {
|
static void planner_forward_pass()
|
||||||
int8_t block_index = block_buffer_tail;
|
{
|
||||||
|
uint8_t block_index = block_buffer_tail;
|
||||||
block_t *block[3] = {NULL, NULL, NULL};
|
block_t *block[3] = {NULL, NULL, NULL};
|
||||||
|
|
||||||
while(block_index != block_buffer_head) {
|
while(block_index != block_buffer_head) {
|
||||||
@ -195,12 +187,13 @@ static void planner_forward_pass() {
|
|||||||
block[1] = block[2];
|
block[1] = block[2];
|
||||||
block[2] = &block_buffer[block_index];
|
block[2] = &block_buffer[block_index];
|
||||||
planner_forward_pass_kernel(block[0],block[1],block[2]);
|
planner_forward_pass_kernel(block[0],block[1],block[2]);
|
||||||
block_index = (block_index+1) % BLOCK_BUFFER_SIZE;
|
block_index = next_block_index( block_index );
|
||||||
}
|
}
|
||||||
planner_forward_pass_kernel(block[1], block[2], NULL);
|
planner_forward_pass_kernel(block[1], block[2], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
|
/* STEPPER RATE DEFINITION
|
||||||
+--------+ <- nominal_rate
|
+--------+ <- nominal_rate
|
||||||
/ \
|
/ \
|
||||||
nominal_rate*entry_factor -> + \
|
nominal_rate*entry_factor -> + \
|
||||||
@ -208,13 +201,15 @@ static void planner_forward_pass() {
|
|||||||
+-------------+
|
+-------------+
|
||||||
time -->
|
time -->
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
|
// Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
|
||||||
// The factors represent a factor of braking and must be in the range 0.0-1.0.
|
// The factors represent a factor of braking and must be in the range 0.0-1.0.
|
||||||
static void calculate_trapezoid_for_block(block_t *block, double entry_factor, double exit_factor) {
|
// This converts the planner parameters to the data required by the stepper controller.
|
||||||
block->initial_rate = ceil(block->nominal_rate*entry_factor);
|
// NOTE: Final rates must be computed in terms of their respective blocks.
|
||||||
block->final_rate = ceil(block->nominal_rate*exit_factor);
|
static void calculate_trapezoid_for_block(block_t *block, double entry_factor, double exit_factor)
|
||||||
int32_t acceleration_per_minute = block->rate_delta*ACCELERATION_TICKS_PER_SECOND*60.0;
|
{
|
||||||
|
block->initial_rate = ceil(block->nominal_rate*entry_factor); // (step/min)
|
||||||
|
block->final_rate = ceil(block->nominal_rate*exit_factor); // (step/min)
|
||||||
|
int32_t acceleration_per_minute = block->rate_delta*ACCELERATION_TICKS_PER_SECOND*60.0; // (step/min^2)
|
||||||
int32_t accelerate_steps =
|
int32_t accelerate_steps =
|
||||||
ceil(estimate_acceleration_distance(block->initial_rate, block->nominal_rate, acceleration_per_minute));
|
ceil(estimate_acceleration_distance(block->initial_rate, block->nominal_rate, acceleration_per_minute));
|
||||||
int32_t decelerate_steps =
|
int32_t decelerate_steps =
|
||||||
@ -229,6 +224,8 @@ static void calculate_trapezoid_for_block(block_t *block, double entry_factor, d
|
|||||||
if (plateau_steps < 0) {
|
if (plateau_steps < 0) {
|
||||||
accelerate_steps = ceil(
|
accelerate_steps = ceil(
|
||||||
intersection_distance(block->initial_rate, block->final_rate, acceleration_per_minute, block->step_event_count));
|
intersection_distance(block->initial_rate, block->final_rate, acceleration_per_minute, block->step_event_count));
|
||||||
|
accelerate_steps = max(accelerate_steps,0); // Check limits due to numerical round-off
|
||||||
|
accelerate_steps = min(accelerate_steps,block->step_event_count);
|
||||||
plateau_steps = 0;
|
plateau_steps = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,11 +233,22 @@ static void calculate_trapezoid_for_block(block_t *block, double entry_factor, d
|
|||||||
block->decelerate_after = accelerate_steps+plateau_steps;
|
block->decelerate_after = accelerate_steps+plateau_steps;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculates the trapezoid speed profiles for all blocks in the plan according to the
|
/* PLANNER SPEED DEFINITION
|
||||||
// entry_factor for each junction. Must be called by planner_recalculate() after
|
+--------+ <- current->nominal_speed
|
||||||
// updating the blocks.
|
/ \
|
||||||
static void planner_recalculate_trapezoids() {
|
current->entry_speed -> + \
|
||||||
int8_t block_index = block_buffer_tail;
|
| + <- next->entry_speed
|
||||||
|
+-------------+
|
||||||
|
time -->
|
||||||
|
*/
|
||||||
|
// Recalculates the trapezoid speed profiles for flagged blocks in the plan according to the
|
||||||
|
// entry_speed for each junction and the entry_speed of the next junction. Must be called by
|
||||||
|
// planner_recalculate() after updating the blocks. Any recalulate flagged junction will
|
||||||
|
// compute the two adjacent trapezoids to the junction, since the junction speed corresponds
|
||||||
|
// to exit speed and entry speed of one another.
|
||||||
|
static void planner_recalculate_trapezoids()
|
||||||
|
{
|
||||||
|
uint8_t block_index = block_buffer_tail;
|
||||||
block_t *current;
|
block_t *current;
|
||||||
block_t *next = NULL;
|
block_t *next = NULL;
|
||||||
|
|
||||||
@ -248,70 +256,99 @@ static void planner_recalculate_trapezoids() {
|
|||||||
current = next;
|
current = next;
|
||||||
next = &block_buffer[block_index];
|
next = &block_buffer[block_index];
|
||||||
if (current) {
|
if (current) {
|
||||||
calculate_trapezoid_for_block(current, current->entry_factor, next->entry_factor);
|
// Recalculate if current block entry or exit junction speed has changed.
|
||||||
|
if (current->recalculate_flag || next->recalculate_flag) {
|
||||||
|
// NOTE: Entry and exit factors always > 0 by all previous logic operations.
|
||||||
|
calculate_trapezoid_for_block(current, current->entry_speed/current->nominal_speed,
|
||||||
|
next->entry_speed/current->nominal_speed);
|
||||||
|
current->recalculate_flag = false; // Reset current only to ensure next trapezoid is computed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
block_index = (block_index+1) % BLOCK_BUFFER_SIZE;
|
block_index = next_block_index( block_index );
|
||||||
}
|
}
|
||||||
calculate_trapezoid_for_block(next, next->entry_factor, factor_for_safe_speed(next));
|
// Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated.
|
||||||
|
calculate_trapezoid_for_block(next, next->entry_speed/next->nominal_speed,
|
||||||
|
MINIMUM_PLANNER_SPEED/next->nominal_speed);
|
||||||
|
next->recalculate_flag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculates the motion plan according to the following algorithm:
|
// Recalculates the motion plan according to the following algorithm:
|
||||||
//
|
//
|
||||||
// 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_factor)
|
// 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_speed)
|
||||||
// so that:
|
// so that:
|
||||||
// a. The junction jerk is within the set limit
|
// a. The junction speed is equal to or less than the maximum junction speed limit
|
||||||
// b. No speed reduction within one block requires faster deceleration than the one, true constant
|
// b. No speed reduction within one block requires faster deceleration than the one, true constant
|
||||||
// acceleration.
|
// acceleration.
|
||||||
// 2. Go over every block in chronological order and dial down junction speed reduction values if
|
// 2. Go over every block in chronological order and dial down junction speed values if
|
||||||
// a. The speed increase within one block would require faster accelleration than the one, true
|
// a. The speed increase within one block would require faster acceleration than the one, true
|
||||||
// constant acceleration.
|
// constant acceleration.
|
||||||
//
|
//
|
||||||
// When these stages are complete all blocks have an entry_factor that will allow all speed changes to
|
// When these stages are complete all blocks have an entry speed that will allow all speed changes to
|
||||||
// be performed using only the one, true constant acceleration, and where no junction jerk is jerkier than
|
// be performed using only the one, true constant acceleration, and where no junction speed is greater
|
||||||
// the set limit. Finally it will:
|
// than the max limit. Finally it will:
|
||||||
//
|
//
|
||||||
// 3. Recalculate trapezoids for all blocks using the recently updated factors
|
// 3. Recalculate trapezoids for all blocks using the recently updated junction speeds. Block trapezoids
|
||||||
|
// with no updated junction speeds will not be recalculated and assumed ok as is.
|
||||||
|
//
|
||||||
|
// All planner computations are performed with doubles (float on Arduinos) to minimize numerical round-
|
||||||
|
// off errors. Only when planned values are converted to stepper rate parameters, these are integers.
|
||||||
|
|
||||||
static void planner_recalculate() {
|
static void planner_recalculate()
|
||||||
|
{
|
||||||
planner_reverse_pass();
|
planner_reverse_pass();
|
||||||
planner_forward_pass();
|
planner_forward_pass();
|
||||||
planner_recalculate_trapezoids();
|
planner_recalculate_trapezoids();
|
||||||
}
|
}
|
||||||
|
|
||||||
void plan_init() {
|
void plan_reset_buffer()
|
||||||
block_buffer_head = 0;
|
{
|
||||||
block_buffer_tail = 0;
|
block_buffer_tail = block_buffer_head;
|
||||||
plan_set_acceleration_manager_enabled(true);
|
next_buffer_head = next_block_index(block_buffer_head);
|
||||||
clear_vector(position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void plan_set_acceleration_manager_enabled(int enabled) {
|
void plan_init()
|
||||||
if ((!!acceleration_manager_enabled) != (!!enabled)) {
|
{
|
||||||
st_synchronize();
|
plan_reset_buffer();
|
||||||
acceleration_manager_enabled = !!enabled;
|
memset(&pl, 0, sizeof(pl)); // Clear planner struct
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int plan_is_acceleration_manager_enabled() {
|
void plan_discard_current_block()
|
||||||
return(acceleration_manager_enabled);
|
{
|
||||||
}
|
|
||||||
|
|
||||||
void plan_discard_current_block() {
|
|
||||||
if (block_buffer_head != block_buffer_tail) {
|
if (block_buffer_head != block_buffer_tail) {
|
||||||
block_buffer_tail = (block_buffer_tail + 1) % BLOCK_BUFFER_SIZE;
|
block_buffer_tail = next_block_index( block_buffer_tail );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
block_t *plan_get_current_block() {
|
block_t *plan_get_current_block()
|
||||||
|
{
|
||||||
if (block_buffer_head == block_buffer_tail) { return(NULL); }
|
if (block_buffer_head == block_buffer_tail) { return(NULL); }
|
||||||
return(&block_buffer[block_buffer_tail]);
|
return(&block_buffer[block_buffer_tail]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the availability status of the block ring buffer. True, if full.
|
||||||
|
uint8_t plan_check_full_buffer()
|
||||||
|
{
|
||||||
|
if (block_buffer_tail == next_buffer_head) { return(true); }
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block until all buffered steps are executed.
|
||||||
|
void plan_synchronize()
|
||||||
|
{
|
||||||
|
while(plan_get_current_block()) {
|
||||||
|
protocol_execute_runtime(); // Check and execute run-time commands
|
||||||
|
if (sys.abort) { return; } // Check for system abort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in
|
// Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in
|
||||||
// millimaters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
|
// millimeters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
|
||||||
// rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
|
// rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
|
||||||
void plan_buffer_line(double x, double y, double z, double feed_rate, int invert_feed_rate) {
|
// NOTE: Assumes buffer is available. Buffer checks are handled at a higher level by motion_control.
|
||||||
// The target position of the tool in absolute steps
|
void plan_buffer_line(double x, double y, double z, double feed_rate, uint8_t invert_feed_rate)
|
||||||
|
{
|
||||||
|
// Prepare to set up new block
|
||||||
|
block_t *block = &block_buffer[block_buffer_head];
|
||||||
|
|
||||||
// Calculate target position in absolute steps
|
// Calculate target position in absolute steps
|
||||||
int32_t target[3];
|
int32_t target[3];
|
||||||
@ -319,46 +356,40 @@ void plan_buffer_line(double x, double y, double z, double feed_rate, int invert
|
|||||||
target[Y_AXIS] = lround(y*settings.steps_per_mm[Y_AXIS]);
|
target[Y_AXIS] = lround(y*settings.steps_per_mm[Y_AXIS]);
|
||||||
target[Z_AXIS] = lround(z*settings.steps_per_mm[Z_AXIS]);
|
target[Z_AXIS] = lround(z*settings.steps_per_mm[Z_AXIS]);
|
||||||
|
|
||||||
// Calculate the buffer head after we push this byte
|
// Compute direction bits for this block
|
||||||
int next_buffer_head = (block_buffer_head + 1) % BLOCK_BUFFER_SIZE;
|
block->direction_bits = 0;
|
||||||
// If the buffer is full: good! That means we are well ahead of the robot.
|
if (target[X_AXIS] < pl.position[X_AXIS]) { block->direction_bits |= (1<<X_DIRECTION_BIT); }
|
||||||
// Rest here until there is room in the buffer.
|
if (target[Y_AXIS] < pl.position[Y_AXIS]) { block->direction_bits |= (1<<Y_DIRECTION_BIT); }
|
||||||
while(block_buffer_tail == next_buffer_head) { sleep_mode(); }
|
if (target[Z_AXIS] < pl.position[Z_AXIS]) { block->direction_bits |= (1<<Z_DIRECTION_BIT); }
|
||||||
// Prepare to set up new block
|
|
||||||
block_t *block = &block_buffer[block_buffer_head];
|
|
||||||
// Number of steps for each axis
|
// Number of steps for each axis
|
||||||
block->steps_x = labs(target[X_AXIS]-position[X_AXIS]);
|
block->steps_x = labs(target[X_AXIS]-pl.position[X_AXIS]);
|
||||||
block->steps_y = labs(target[Y_AXIS]-position[Y_AXIS]);
|
block->steps_y = labs(target[Y_AXIS]-pl.position[Y_AXIS]);
|
||||||
block->steps_z = labs(target[Z_AXIS]-position[Z_AXIS]);
|
block->steps_z = labs(target[Z_AXIS]-pl.position[Z_AXIS]);
|
||||||
block->step_event_count = max(block->steps_x, max(block->steps_y, block->steps_z));
|
block->step_event_count = max(block->steps_x, max(block->steps_y, block->steps_z));
|
||||||
|
|
||||||
// Bail if this is a zero-length block
|
// Bail if this is a zero-length block
|
||||||
if (block->step_event_count == 0) { return; };
|
if (block->step_event_count == 0) { return; };
|
||||||
|
|
||||||
double delta_x_mm = (target[X_AXIS]-position[X_AXIS])/settings.steps_per_mm[X_AXIS];
|
// Compute path vector in terms of absolute step target and current positions
|
||||||
double delta_y_mm = (target[Y_AXIS]-position[Y_AXIS])/settings.steps_per_mm[Y_AXIS];
|
double delta_mm[3];
|
||||||
double delta_z_mm = (target[Z_AXIS]-position[Z_AXIS])/settings.steps_per_mm[Z_AXIS];
|
delta_mm[X_AXIS] = (target[X_AXIS]-pl.position[X_AXIS])/settings.steps_per_mm[X_AXIS];
|
||||||
block->millimeters = sqrt(square(delta_x_mm) + square(delta_y_mm) + square(delta_z_mm));
|
delta_mm[Y_AXIS] = (target[Y_AXIS]-pl.position[Y_AXIS])/settings.steps_per_mm[Y_AXIS];
|
||||||
|
delta_mm[Z_AXIS] = (target[Z_AXIS]-pl.position[Z_AXIS])/settings.steps_per_mm[Z_AXIS];
|
||||||
|
block->millimeters = sqrt(delta_mm[X_AXIS]*delta_mm[X_AXIS] + delta_mm[Y_AXIS]*delta_mm[Y_AXIS] +
|
||||||
|
delta_mm[Z_AXIS]*delta_mm[Z_AXIS]);
|
||||||
|
double inverse_millimeters = 1.0/block->millimeters; // Inverse millimeters to remove multiple divides
|
||||||
|
|
||||||
|
// Calculate speed in mm/minute for each axis. No divide by zero due to previous checks.
|
||||||
uint32_t microseconds;
|
// NOTE: Minimum stepper speed is limited by MINIMUM_STEPS_PER_MINUTE in stepper.c
|
||||||
|
double inverse_minute;
|
||||||
if (!invert_feed_rate) {
|
if (!invert_feed_rate) {
|
||||||
microseconds = lround((block->millimeters/feed_rate)*1000000);
|
inverse_minute = feed_rate * inverse_millimeters;
|
||||||
} else {
|
} else {
|
||||||
microseconds = lround(ONE_MINUTE_OF_MICROSECONDS/feed_rate);
|
inverse_minute = 1.0 / feed_rate;
|
||||||
}
|
}
|
||||||
|
block->nominal_speed = block->millimeters * inverse_minute; // (mm/min) Always > 0
|
||||||
// Calculate speed in mm/minute for each axis
|
block->nominal_rate = ceil(block->step_event_count * inverse_minute); // (step/min) Always > 0
|
||||||
double multiplier = 60.0*1000000.0/microseconds;
|
|
||||||
block->speed_x = delta_x_mm * multiplier;
|
|
||||||
block->speed_y = delta_y_mm * multiplier;
|
|
||||||
block->speed_z = delta_z_mm * multiplier;
|
|
||||||
block->nominal_speed = block->millimeters * multiplier;
|
|
||||||
block->nominal_rate = ceil(block->step_event_count * multiplier);
|
|
||||||
block->entry_factor = 0.0;
|
|
||||||
|
|
||||||
// This is a temporary fix to avoid a situation where very low nominal_speeds would be rounded
|
|
||||||
// down to zero and cause a division by zero. TODO: Grbl deserves a less patchy fix for this problem
|
|
||||||
if (block->nominal_speed < 60.0) { block->nominal_speed = 60.0; }
|
|
||||||
|
|
||||||
// Compute the acceleration rate for the trapezoid generator. Depending on the slope of the line
|
// Compute the acceleration rate for the trapezoid generator. Depending on the slope of the line
|
||||||
// average travel per step event changes. For a line along one axis the travel per step event
|
// average travel per step event changes. For a line along one axis the travel per step event
|
||||||
@ -366,40 +397,122 @@ void plan_buffer_line(double x, double y, double z, double feed_rate, int invert
|
|||||||
// axes might step for every step event. Travel per step event is then sqrt(travel_x^2+travel_y^2).
|
// axes might step for every step event. Travel per step event is then sqrt(travel_x^2+travel_y^2).
|
||||||
// To generate trapezoids with contant acceleration between blocks the rate_delta must be computed
|
// To generate trapezoids with contant acceleration between blocks the rate_delta must be computed
|
||||||
// specifically for each line to compensate for this phenomenon:
|
// specifically for each line to compensate for this phenomenon:
|
||||||
double travel_per_step = block->millimeters/block->step_event_count;
|
// Convert universal acceleration for direction-dependent stepper rate change parameter
|
||||||
block->rate_delta = ceil(
|
block->rate_delta = ceil( block->step_event_count*inverse_millimeters *
|
||||||
((settings.acceleration*60.0)/(ACCELERATION_TICKS_PER_SECOND))/ // acceleration mm/sec/sec per acceleration_tick
|
settings.acceleration / (60 * ACCELERATION_TICKS_PER_SECOND )); // (step/min/acceleration_tick)
|
||||||
travel_per_step); // convert to: acceleration steps/min/acceleration_tick
|
|
||||||
if (acceleration_manager_enabled) {
|
// Compute path unit vector
|
||||||
// compute a preliminary conservative acceleration trapezoid
|
double unit_vec[3];
|
||||||
double safe_speed_factor = factor_for_safe_speed(block);
|
|
||||||
calculate_trapezoid_for_block(block, safe_speed_factor, safe_speed_factor);
|
unit_vec[X_AXIS] = delta_mm[X_AXIS]*inverse_millimeters;
|
||||||
} else {
|
unit_vec[Y_AXIS] = delta_mm[Y_AXIS]*inverse_millimeters;
|
||||||
block->initial_rate = block->nominal_rate;
|
unit_vec[Z_AXIS] = delta_mm[Z_AXIS]*inverse_millimeters;
|
||||||
block->final_rate = block->nominal_rate;
|
|
||||||
block->accelerate_until = 0;
|
// Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
|
||||||
block->decelerate_after = block->step_event_count;
|
// Let a circle be tangent to both previous and current path line segments, where the junction
|
||||||
block->rate_delta = 0;
|
// deviation is defined as the distance from the junction to the closest edge of the circle,
|
||||||
|
// colinear with the circle center. The circular segment joining the two paths represents the
|
||||||
|
// path of centripetal acceleration. Solve for max velocity based on max acceleration about the
|
||||||
|
// radius of the circle, defined indirectly by junction deviation. This may be also viewed as
|
||||||
|
// path width or max_jerk in the previous grbl version. This approach does not actually deviate
|
||||||
|
// from path, but used as a robust way to compute cornering speeds, as it takes into account the
|
||||||
|
// nonlinearities of both the junction angle and junction velocity.
|
||||||
|
double vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
|
||||||
|
|
||||||
|
// Skip first block or when previous_nominal_speed is used as a flag for homing and offset cycles.
|
||||||
|
if ((block_buffer_head != block_buffer_tail) && (pl.previous_nominal_speed > 0.0)) {
|
||||||
|
// Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
|
||||||
|
// NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
|
||||||
|
double cos_theta = - pl.previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
|
||||||
|
- pl.previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
|
||||||
|
- pl.previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS] ;
|
||||||
|
|
||||||
|
// Skip and use default max junction speed for 0 degree acute junction.
|
||||||
|
if (cos_theta < 0.95) {
|
||||||
|
vmax_junction = min(pl.previous_nominal_speed,block->nominal_speed);
|
||||||
|
// Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
|
||||||
|
if (cos_theta > -0.95) {
|
||||||
|
// Compute maximum junction velocity based on maximum acceleration and junction deviation
|
||||||
|
double sin_theta_d2 = sqrt(0.5*(1.0-cos_theta)); // Trig half angle identity. Always positive.
|
||||||
|
vmax_junction = min(vmax_junction,
|
||||||
|
sqrt(settings.acceleration * settings.junction_deviation * sin_theta_d2/(1.0-sin_theta_d2)) );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
block->max_entry_speed = vmax_junction;
|
||||||
|
|
||||||
// Compute direction bits for this block
|
// Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
|
||||||
block->direction_bits = 0;
|
double v_allowable = max_allowable_speed(-settings.acceleration,MINIMUM_PLANNER_SPEED,block->millimeters);
|
||||||
if (target[X_AXIS] < position[X_AXIS]) { block->direction_bits |= (1<<X_DIRECTION_BIT); }
|
block->entry_speed = min(vmax_junction, v_allowable);
|
||||||
if (target[Y_AXIS] < position[Y_AXIS]) { block->direction_bits |= (1<<Y_DIRECTION_BIT); }
|
|
||||||
if (target[Z_AXIS] < position[Z_AXIS]) { block->direction_bits |= (1<<Z_DIRECTION_BIT); }
|
|
||||||
|
|
||||||
// Move buffer head
|
// Initialize planner efficiency flags
|
||||||
|
// Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
|
||||||
|
// If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
|
||||||
|
// the current block and next block junction speeds are guaranteed to always be at their maximum
|
||||||
|
// junction speeds in deceleration and acceleration, respectively. This is due to how the current
|
||||||
|
// block nominal speed limits both the current and next maximum junction speeds. Hence, in both
|
||||||
|
// the reverse and forward planners, the corresponding block junction speed will always be at the
|
||||||
|
// the maximum junction speed and may always be ignored for any speed reduction checks.
|
||||||
|
if (block->nominal_speed <= v_allowable) { block->nominal_length_flag = true; }
|
||||||
|
else { block->nominal_length_flag = false; }
|
||||||
|
block->recalculate_flag = true; // Always calculate trapezoid for new block
|
||||||
|
|
||||||
|
// Update previous path unit_vector and nominal speed
|
||||||
|
memcpy(pl.previous_unit_vec, unit_vec, sizeof(unit_vec)); // pl.previous_unit_vec[] = unit_vec[]
|
||||||
|
pl.previous_nominal_speed = block->nominal_speed;
|
||||||
|
|
||||||
|
// Update buffer head and next buffer head indices
|
||||||
block_buffer_head = next_buffer_head;
|
block_buffer_head = next_buffer_head;
|
||||||
// Update position
|
next_buffer_head = next_block_index(block_buffer_head);
|
||||||
memcpy(position, target, sizeof(target)); // position[] = target[]
|
|
||||||
|
|
||||||
if (acceleration_manager_enabled) { planner_recalculate(); }
|
// Update planner position
|
||||||
st_wake_up();
|
memcpy(pl.position, target, sizeof(target)); // pl.position[] = target[]
|
||||||
|
|
||||||
|
planner_recalculate();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the planner position vector
|
// Reset the planner position vector and planner speed
|
||||||
void plan_set_current_position(double x, double y, double z) {
|
void plan_set_current_position(double x, double y, double z)
|
||||||
position[X_AXIS] = lround(x*settings.steps_per_mm[X_AXIS]);
|
{
|
||||||
position[Y_AXIS] = lround(y*settings.steps_per_mm[Y_AXIS]);
|
// To correlate status reporting work position correctly, the planner must force the steppers to
|
||||||
position[Z_AXIS] = lround(z*settings.steps_per_mm[Z_AXIS]);
|
// empty the block buffer and synchronize with the planner, as the real-time machine position and
|
||||||
|
// the planner position at the end of the buffer can be and are usually different. This function is
|
||||||
|
// only called with a G92, which typically is used only at the beginning of a g-code program or
|
||||||
|
// between different operations.
|
||||||
|
// TODO: Find a robust way to avoid a planner synchronize, but this may require a bit of ingenuity.
|
||||||
|
plan_synchronize();
|
||||||
|
|
||||||
|
// Update the system coordinate offsets from machine zero
|
||||||
|
sys.coord_offset[X_AXIS] += pl.position[X_AXIS];
|
||||||
|
sys.coord_offset[Y_AXIS] += pl.position[Y_AXIS];
|
||||||
|
sys.coord_offset[Z_AXIS] += pl.position[Z_AXIS];
|
||||||
|
|
||||||
|
memset(&pl, 0, sizeof(pl)); // Clear planner variables. Assume start from rest.
|
||||||
|
|
||||||
|
pl.position[X_AXIS] = lround(x*settings.steps_per_mm[X_AXIS]); // Update planner position
|
||||||
|
pl.position[Y_AXIS] = lround(y*settings.steps_per_mm[Y_AXIS]);
|
||||||
|
pl.position[Z_AXIS] = lround(z*settings.steps_per_mm[Z_AXIS]);
|
||||||
|
sys.coord_offset[X_AXIS] -= pl.position[X_AXIS];
|
||||||
|
sys.coord_offset[Y_AXIS] -= pl.position[Y_AXIS];
|
||||||
|
sys.coord_offset[Z_AXIS] -= pl.position[Z_AXIS];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-initialize buffer plan with a partially completed block, assumed to exist at the buffer tail.
|
||||||
|
// Called after a steppers have come to a complete stop for a feed hold and the cycle is stopped.
|
||||||
|
void plan_cycle_reinitialize(int32_t step_events_remaining)
|
||||||
|
{
|
||||||
|
block_t *block = &block_buffer[block_buffer_tail]; // Point to partially completed block
|
||||||
|
|
||||||
|
// Only remaining millimeters and step_event_count need to be updated for planner recalculate.
|
||||||
|
// Other variables (step_x, step_y, step_z, rate_delta, etc.) all need to remain the same to
|
||||||
|
// ensure the original planned motion is resumed exactly.
|
||||||
|
block->millimeters = (block->millimeters*step_events_remaining)/block->step_event_count;
|
||||||
|
block->step_event_count = step_events_remaining;
|
||||||
|
|
||||||
|
// Re-plan from a complete stop. Reset planner entry speeds and flags.
|
||||||
|
block->entry_speed = 0.0;
|
||||||
|
block->max_entry_speed = 0.0;
|
||||||
|
block->nominal_length_flag = false;
|
||||||
|
block->recalculate_flag = true;
|
||||||
|
planner_recalculate();
|
||||||
}
|
}
|
||||||
|
38
planner.h
38
planner.h
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -26,26 +27,27 @@
|
|||||||
// This struct is used when buffering the setup for each linear movement "nominal" values are as specified in
|
// This struct is used when buffering the setup for each linear movement "nominal" values are as specified in
|
||||||
// the source g-code and may never actually be reached if acceleration management is active.
|
// the source g-code and may never actually be reached if acceleration management is active.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
// Fields used by the bresenham algorithm for tracing the line
|
// Fields used by the bresenham algorithm for tracing the line
|
||||||
uint32_t steps_x, steps_y, steps_z; // Step count along each axis
|
|
||||||
uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
|
uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
|
||||||
|
uint32_t steps_x, steps_y, steps_z; // Step count along each axis
|
||||||
int32_t step_event_count; // The number of step events required to complete this block
|
int32_t step_event_count; // The number of step events required to complete this block
|
||||||
uint32_t nominal_rate; // The nominal step rate for this block in step_events/minute
|
|
||||||
|
|
||||||
// Fields used by the motion planner to manage acceleration
|
// Fields used by the motion planner to manage acceleration
|
||||||
double speed_x, speed_y, speed_z; // Nominal mm/minute for each axis
|
|
||||||
double nominal_speed; // The nominal speed for this block in mm/min
|
double nominal_speed; // The nominal speed for this block in mm/min
|
||||||
|
double entry_speed; // Entry speed at previous-current block junction in mm/min
|
||||||
|
double max_entry_speed; // Maximum allowable junction entry speed in mm/min
|
||||||
double millimeters; // The total travel of this block in mm
|
double millimeters; // The total travel of this block in mm
|
||||||
double entry_factor; // The factor representing the change in speed at the start of this trapezoid.
|
uint8_t recalculate_flag; // Planner flag to recalculate trapezoids on entry junction
|
||||||
// (The end of the curren speed trapezoid is defined by the entry_factor of the
|
uint8_t nominal_length_flag; // Planner flag for nominal speed always reached
|
||||||
// next block)
|
|
||||||
|
|
||||||
// Settings for the trapezoid generator
|
// Settings for the trapezoid generator
|
||||||
uint32_t initial_rate; // The jerk-adjusted step rate at start of block
|
uint32_t initial_rate; // The step rate at start of block
|
||||||
uint32_t final_rate; // The minimal rate at exit
|
uint32_t final_rate; // The step rate at end of block
|
||||||
int32_t rate_delta; // The steps/minute to add or subtract when changing speed (must be positive)
|
int32_t rate_delta; // The steps/minute to add or subtract when changing speed (must be positive)
|
||||||
uint32_t accelerate_until; // The index of the step event on which to stop acceleration
|
uint32_t accelerate_until; // The index of the step event on which to stop acceleration
|
||||||
uint32_t decelerate_after; // The index of the step event on which to start decelerating
|
uint32_t decelerate_after; // The index of the step event on which to start decelerating
|
||||||
|
uint32_t nominal_rate; // The nominal step rate for this block in step_events/minute
|
||||||
|
|
||||||
} block_t;
|
} block_t;
|
||||||
|
|
||||||
@ -55,7 +57,7 @@ void plan_init();
|
|||||||
// Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in
|
// Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in
|
||||||
// millimaters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
|
// millimaters. Feed rate specifies the speed of the motion. If feed rate is inverted, the feed
|
||||||
// rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
|
// rate is taken to mean "frequency" and would complete the operation in 1/feed_rate minutes.
|
||||||
void plan_buffer_line(double x, double y, double z, double feed_rate, int invert_feed_rate);
|
void plan_buffer_line(double x, double y, double z, double feed_rate, uint8_t invert_feed_rate);
|
||||||
|
|
||||||
// Called when the current block is no longer needed. Discards the block and makes the memory
|
// Called when the current block is no longer needed. Discards the block and makes the memory
|
||||||
// availible for new blocks.
|
// availible for new blocks.
|
||||||
@ -64,13 +66,19 @@ void plan_discard_current_block();
|
|||||||
// Gets the current block. Returns NULL if buffer empty
|
// Gets the current block. Returns NULL if buffer empty
|
||||||
block_t *plan_get_current_block();
|
block_t *plan_get_current_block();
|
||||||
|
|
||||||
// Enables or disables acceleration-management for upcoming blocks
|
|
||||||
void plan_set_acceleration_manager_enabled(int enabled);
|
|
||||||
|
|
||||||
// Is acceleration-management currently enabled?
|
|
||||||
int plan_is_acceleration_manager_enabled();
|
|
||||||
|
|
||||||
// Reset the position vector
|
// Reset the position vector
|
||||||
void plan_set_current_position(double x, double y, double z);
|
void plan_set_current_position(double x, double y, double z);
|
||||||
|
|
||||||
|
// Reinitialize plan with a partially completed block
|
||||||
|
void plan_cycle_reinitialize(int32_t step_events_remaining);
|
||||||
|
|
||||||
|
// Reset buffer
|
||||||
|
void plan_reset_buffer();
|
||||||
|
|
||||||
|
// Returns the status of the block ring buffer. True, if buffer is full.
|
||||||
|
uint8_t plan_check_full_buffer();
|
||||||
|
|
||||||
|
// Block until all buffered steps are executed
|
||||||
|
void plan_synchronize();
|
||||||
|
|
||||||
#endif
|
#endif
|
112
print.c
112
print.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -24,72 +25,107 @@
|
|||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
|
#include "config.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
|
|
||||||
#ifndef DECIMAL_PLACES
|
|
||||||
#define DECIMAL_PLACES 3
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void printString(const char *s)
|
void printString(const char *s)
|
||||||
{
|
{
|
||||||
while (*s)
|
while (*s)
|
||||||
serial_write(*s++);
|
serial_write(*s++);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print a string stored in PGM-memory
|
// Print a string stored in PGM-memory
|
||||||
void printPgmString(const char *s)
|
void printPgmString(const char *s)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
while ((c = pgm_read_byte_near(s++)))
|
while ((c = pgm_read_byte_near(s++)))
|
||||||
serial_write(c);
|
serial_write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printIntegerInBase(unsigned long n, unsigned long base)
|
// void printIntegerInBase(unsigned long n, unsigned long base)
|
||||||
|
// {
|
||||||
|
// unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
|
||||||
|
// unsigned long i = 0;
|
||||||
|
//
|
||||||
|
// if (n == 0) {
|
||||||
|
// serial_write('0');
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// while (n > 0) {
|
||||||
|
// buf[i++] = n % base;
|
||||||
|
// n /= base;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for (; i > 0; i--)
|
||||||
|
// serial_write(buf[i - 1] < 10 ?
|
||||||
|
// '0' + buf[i - 1] :
|
||||||
|
// 'A' + buf[i - 1] - 10);
|
||||||
|
// }
|
||||||
|
|
||||||
|
void print_uint8_base2(uint8_t n)
|
||||||
{
|
{
|
||||||
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
|
unsigned char buf[8];
|
||||||
unsigned long i = 0;
|
uint8_t i = 0;
|
||||||
|
|
||||||
if (n == 0) {
|
for (; i < 8; i++) {
|
||||||
serial_write('0');
|
buf[i] = n & 1;
|
||||||
return;
|
n >>= 1;
|
||||||
}
|
|
||||||
|
|
||||||
while (n > 0) {
|
|
||||||
buf[i++] = n % base;
|
|
||||||
n /= base;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; i > 0; i--)
|
for (; i > 0; i--)
|
||||||
serial_write(buf[i - 1] < 10 ?
|
serial_write('0' + buf[i - 1]);
|
||||||
'0' + buf[i - 1] :
|
}
|
||||||
'A' + buf[i - 1] - 10);
|
|
||||||
|
static void print_uint32_base10(unsigned long n)
|
||||||
|
{
|
||||||
|
unsigned char buf[32];
|
||||||
|
uint8_t i = 0;
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
serial_write('0');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (n > 0) {
|
||||||
|
buf[i++] = n % 10;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i > 0; i--)
|
||||||
|
serial_write('0' + buf[i - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printInteger(long n)
|
void printInteger(long n)
|
||||||
{
|
{
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
serial_write('-');
|
serial_write('-');
|
||||||
n = -n;
|
n = -n;
|
||||||
}
|
}
|
||||||
|
print_uint32_base10(n);
|
||||||
printIntegerInBase(n, 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A very simple
|
|
||||||
void printFloat(double n)
|
void printFloat(double n)
|
||||||
{
|
{
|
||||||
double integer_part, fractional_part;
|
if (n < 0) {
|
||||||
uint8_t decimal_part;
|
serial_write('-');
|
||||||
fractional_part = modf(n, &integer_part);
|
n = -n;
|
||||||
printInteger(integer_part);
|
}
|
||||||
|
n += 0.5/DECIMAL_MULTIPLIER; // Add rounding factor
|
||||||
|
|
||||||
|
long integer_part;
|
||||||
|
integer_part = (int)n;
|
||||||
|
print_uint32_base10(integer_part);
|
||||||
|
|
||||||
serial_write('.');
|
serial_write('.');
|
||||||
fractional_part *= 10;
|
|
||||||
|
n -= integer_part;
|
||||||
int decimals = DECIMAL_PLACES;
|
int decimals = DECIMAL_PLACES;
|
||||||
|
uint8_t decimal_part;
|
||||||
while(decimals-- > 0) {
|
while(decimals-- > 0) {
|
||||||
decimal_part = floor(fractional_part);
|
n *= 10;
|
||||||
|
decimal_part = (int) n;
|
||||||
serial_write('0'+decimal_part);
|
serial_write('0'+decimal_part);
|
||||||
fractional_part -= decimal_part;
|
n -= decimal_part;
|
||||||
fractional_part *= 10;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
print.h
3
print.h
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -30,7 +31,7 @@ void printPgmString(const char *s);
|
|||||||
|
|
||||||
void printInteger(long n);
|
void printInteger(long n);
|
||||||
|
|
||||||
void printIntegerInBase(unsigned long n, unsigned long base);
|
void print_uint8_base2(uint8_t n);
|
||||||
|
|
||||||
void printFloat(double n);
|
void printFloat(double n);
|
||||||
|
|
||||||
|
204
protocol.c
204
protocol.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -28,12 +29,17 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "nuts_bolts.h"
|
#include "nuts_bolts.h"
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
|
#include "stepper.h"
|
||||||
|
#include "planner.h"
|
||||||
|
|
||||||
#define LINE_BUFFER_SIZE 50
|
#define LINE_BUFFER_SIZE 50
|
||||||
|
|
||||||
static char line[LINE_BUFFER_SIZE];
|
static char line[LINE_BUFFER_SIZE]; // Line to be executed. Zero-terminated.
|
||||||
static uint8_t char_counter;
|
static uint8_t char_counter; // Last character counter in line variable.
|
||||||
|
static uint8_t iscomment; // Comment/block delete flag for processor to ignore comment characters.
|
||||||
|
|
||||||
static void status_message(int status_code) {
|
static void status_message(int status_code)
|
||||||
|
{
|
||||||
if (status_code == 0) {
|
if (status_code == 0) {
|
||||||
printPgmString(PSTR("ok\r\n"));
|
printPgmString(PSTR("ok\r\n"));
|
||||||
} else {
|
} else {
|
||||||
@ -54,38 +60,194 @@ static void status_message(int status_code) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void protocol_init()
|
|
||||||
|
void protocol_status_report()
|
||||||
{
|
{
|
||||||
printPgmString(PSTR("\r\nGrbl " GRBL_VERSION));
|
// TODO: Status report data is written to the user here. This function should be able to grab a
|
||||||
printPgmString(PSTR("\r\n"));
|
// real-time snapshot of the stepper subprogram and the actual location of the CNC machine. At a
|
||||||
|
// minimum, status report should return real-time location information. Other important information
|
||||||
|
// may be distance to go on block, processed block id, and feed rate. A secondary, non-critical
|
||||||
|
// status report may include g-code state, i.e. inch mode, plane mode, absolute mode, etc.
|
||||||
|
// The report generated must be as short as possible, yet still provide the user easily readable
|
||||||
|
// information, i.e. 'x0.23,y120.4,z2.4'. This is necessary as it minimizes the computational
|
||||||
|
// overhead and allows grbl to keep running smoothly, especially with g-code programs with fast,
|
||||||
|
// short line segments and interface setups that require real-time status reports (5-20Hz).
|
||||||
|
|
||||||
|
// **Under construction** Bare-bones status report. Provides real-time machine position relative to
|
||||||
|
// the system power on location (0,0,0) and work coordinate position, updatable by the G92 command.
|
||||||
|
// The following are still needed: user setting of output units (mm|inch), compressed (non-human
|
||||||
|
// readable) data for interfaces?, save last known position in EEPROM?, code optimizations, solidify
|
||||||
|
// the reporting schemes, move to a separate .c file for easy user accessibility, and setting the
|
||||||
|
// home position by the user (likely through '$' setting interface).
|
||||||
|
// Successfully tested at a query rate of 10-20Hz while running a gauntlet of programs at various
|
||||||
|
// speeds.
|
||||||
|
int32_t print_position[3];
|
||||||
|
memcpy(print_position,sys.position,sizeof(sys.position));
|
||||||
|
#if REPORT_INCH_MODE
|
||||||
|
printString("MPos: x"); printFloat(print_position[X_AXIS]/(settings.steps_per_mm[X_AXIS]*MM_PER_INCH));
|
||||||
|
printString(",y"); printFloat(print_position[Y_AXIS]/(settings.steps_per_mm[Y_AXIS]*MM_PER_INCH));
|
||||||
|
printString(",z"); printFloat(print_position[Z_AXIS]/(settings.steps_per_mm[Z_AXIS]*MM_PER_INCH));
|
||||||
|
printString(" WPos: x"); printFloat((print_position[X_AXIS]-sys.coord_offset[X_AXIS])/(settings.steps_per_mm[X_AXIS]*MM_PER_INCH));
|
||||||
|
printString(",y"); printFloat((print_position[Y_AXIS]-sys.coord_offset[Y_AXIS])/(settings.steps_per_mm[Y_AXIS]*MM_PER_INCH));
|
||||||
|
printString(",z"); printFloat((print_position[Z_AXIS]-sys.coord_offset[Z_AXIS])/(settings.steps_per_mm[Z_AXIS]*MM_PER_INCH));
|
||||||
|
#else
|
||||||
|
printString("MPos: x"); printFloat(print_position[X_AXIS]/(settings.steps_per_mm[X_AXIS]));
|
||||||
|
printString(",y"); printFloat(print_position[Y_AXIS]/(settings.steps_per_mm[Y_AXIS]));
|
||||||
|
printString(",z"); printFloat(print_position[Z_AXIS]/(settings.steps_per_mm[Z_AXIS]));
|
||||||
|
printString(" WPos: x"); printFloat((print_position[X_AXIS]-sys.coord_offset[X_AXIS])/(settings.steps_per_mm[X_AXIS]));
|
||||||
|
printString(",y"); printFloat((print_position[Y_AXIS]-sys.coord_offset[Y_AXIS])/(settings.steps_per_mm[Y_AXIS]));
|
||||||
|
printString(",z"); printFloat((print_position[Z_AXIS]-sys.coord_offset[Z_AXIS])/(settings.steps_per_mm[Z_AXIS]));
|
||||||
|
#endif
|
||||||
|
printString("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void protocol_init()
|
||||||
|
{
|
||||||
|
// Print grbl initialization message
|
||||||
|
printPgmString(PSTR("\r\nGrbl " GRBL_VERSION));
|
||||||
|
printPgmString(PSTR("\r\n'$' to dump current settings\r\n"));
|
||||||
|
|
||||||
|
char_counter = 0; // Reset line input
|
||||||
|
iscomment = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Executes run-time commands, when required. This is called from various check points in the main
|
||||||
|
// program, primarily where there may be a while loop waiting for a buffer to clear space or any
|
||||||
|
// point where the execution time from the last check point may be more than a fraction of a second.
|
||||||
|
// This is a way to execute runtime commands asynchronously (aka multitasking) with grbl's g-code
|
||||||
|
// parsing and planning functions. This function also serves as an interface for the interrupts to
|
||||||
|
// set the system runtime flags, where only the main program to handles them, removing the need to
|
||||||
|
// define more computationally-expensive volatile variables.
|
||||||
|
// NOTE: The sys.execute variable flags are set by the serial read subprogram, except where noted.
|
||||||
|
void protocol_execute_runtime()
|
||||||
|
{
|
||||||
|
if (sys.execute) { // Enter only if any bit flag is true
|
||||||
|
uint8_t rt_exec = sys.execute; // Avoid calling volatile multiple times
|
||||||
|
|
||||||
|
// System abort. Steppers have already been force stopped.
|
||||||
|
if (rt_exec & EXEC_RESET) {
|
||||||
|
sys.abort = true;
|
||||||
|
return; // Nothing else to do but exit.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute and serial print status
|
||||||
|
if (rt_exec & EXEC_STATUS_REPORT) {
|
||||||
|
protocol_status_report();
|
||||||
|
bit_false(sys.execute,EXEC_STATUS_REPORT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initiate stepper feed hold
|
||||||
|
if (rt_exec & EXEC_FEED_HOLD) {
|
||||||
|
st_feed_hold(); // Initiate feed hold.
|
||||||
|
bit_false(sys.execute,EXEC_FEED_HOLD);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reinitializes the stepper module running flags and re-plans the buffer after a feed hold.
|
||||||
|
// NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes.
|
||||||
|
if (rt_exec & EXEC_CYCLE_STOP) {
|
||||||
|
st_cycle_reinitialize();
|
||||||
|
bit_false(sys.execute,EXEC_CYCLE_STOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rt_exec & EXEC_CYCLE_START) {
|
||||||
|
st_cycle_start(); // Issue cycle start command to stepper subsystem
|
||||||
|
#ifdef CYCLE_AUTO_START
|
||||||
|
sys.auto_start = true; // Re-enable auto start after feed hold.
|
||||||
|
#endif
|
||||||
|
bit_false(sys.execute,EXEC_CYCLE_START);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Executes one line of input according to protocol
|
// Executes one line of input according to protocol
|
||||||
uint8_t protocol_execute_line(char *line) {
|
uint8_t protocol_execute_line(char *line)
|
||||||
|
{
|
||||||
if(line[0] == '$') {
|
if(line[0] == '$') {
|
||||||
|
|
||||||
|
// TODO: Re-write this '$' as a way to change runtime settings without having to reset, i.e.
|
||||||
|
// auto-starting, status query output formatting and type, jog mode (axes, direction, and
|
||||||
|
// nominal feedrate), toggle block delete, etc. This differs from the EEPROM settings, as they
|
||||||
|
// are considered defaults and loaded upon startup/reset.
|
||||||
|
// This use is envisioned where '$' itself dumps settings and help. Defined characters
|
||||||
|
// proceeding the '$' may be used to setup modes, such as jog mode with a '$J=X100' for X-axis
|
||||||
|
// motion with a nominal feedrate of 100mm/min. Writing EEPROM settings will likely stay the
|
||||||
|
// same or similar. Should be worked out in upcoming releases.
|
||||||
return(settings_execute_line(line)); // Delegate lines starting with '$' to the settings module
|
return(settings_execute_line(line)); // Delegate lines starting with '$' to the settings module
|
||||||
|
|
||||||
|
// } else if {
|
||||||
|
//
|
||||||
|
// JOG MODE
|
||||||
|
//
|
||||||
|
// TODO: Here jogging can be placed for execution as a seperate subprogram. It does not need to be
|
||||||
|
// susceptible to other runtime commands except for e-stop. The jogging function is intended to
|
||||||
|
// be a basic toggle on/off with controlled acceleration and deceleration to prevent skipped
|
||||||
|
// steps. The user would supply the desired feedrate, axis to move, and direction. Toggle on would
|
||||||
|
// start motion and toggle off would initiate a deceleration to stop. One could 'feather' the
|
||||||
|
// motion by repeatedly toggling to slow the motion to the desired location. Location data would
|
||||||
|
// need to be updated real-time and supplied to the user through status queries.
|
||||||
|
// More controlled exact motions can be taken care of by inputting G0 or G1 commands, which are
|
||||||
|
// handled by the planner. It would be possible for the jog subprogram to insert blocks into the
|
||||||
|
// block buffer without having the planner plan them. It would need to manage de/ac-celerations
|
||||||
|
// on its own carefully. This approach could be effective and possibly size/memory efficient.
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return(gc_execute_line(line)); // Everything else is gcode
|
return(gc_execute_line(line)); // Everything else is gcode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Process one line of incoming serial data. Remove unneeded characters and capitalize.
|
||||||
void protocol_process()
|
void protocol_process()
|
||||||
{
|
{
|
||||||
char c;
|
uint8_t c;
|
||||||
while((c = serial_read()) != SERIAL_NO_DATA)
|
while((c = serial_read()) != SERIAL_NO_DATA) {
|
||||||
{
|
if ((c == '\n') || (c == '\r')) { // End of line reached
|
||||||
if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute!
|
|
||||||
line[char_counter] = 0; // treminate string
|
// Runtime command check point before executing line. Prevent any furthur line executions.
|
||||||
status_message(protocol_execute_line(line));
|
// NOTE: If there is no line, this function should quickly return to the main program when
|
||||||
char_counter = 0; // reset line buffer index
|
// the buffer empties of non-executable data.
|
||||||
} else if (c <= ' ') {
|
protocol_execute_runtime();
|
||||||
// Throw away whitepace and control characters
|
if (sys.abort) { return; } // Bail to main program upon system abort
|
||||||
} else if (char_counter >= LINE_BUFFER_SIZE-1) {
|
|
||||||
// Throw away any characters beyond the end of the line buffer
|
if (char_counter > 0) {// Line is complete. Then execute!
|
||||||
} else if (c >= 'a' && c <= 'z') { // Upcase lowercase
|
line[char_counter] = 0; // Terminate string
|
||||||
line[char_counter++] = c-'a'+'A';
|
status_message(protocol_execute_line(line));
|
||||||
|
} else {
|
||||||
|
// Empty or comment line. Skip block.
|
||||||
|
status_message(STATUS_OK); // Send status message for syncing purposes.
|
||||||
|
}
|
||||||
|
char_counter = 0; // Reset line buffer index
|
||||||
|
iscomment = false; // Reset comment flag
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
line[char_counter++] = c;
|
if (iscomment) {
|
||||||
|
// Throw away all comment characters
|
||||||
|
if (c == ')') {
|
||||||
|
// End of comment. Resume line.
|
||||||
|
iscomment = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (c <= ' ') {
|
||||||
|
// Throw away whitepace and control characters
|
||||||
|
} else if (c == '/') {
|
||||||
|
// Disable block delete and throw away characters. Will ignore until EOL.
|
||||||
|
#if BLOCK_DELETE_ENABLE
|
||||||
|
iscomment = true;
|
||||||
|
#endif
|
||||||
|
} else if (c == '(') {
|
||||||
|
// Enable comments flag and ignore all characters until ')' or EOL.
|
||||||
|
iscomment = true;
|
||||||
|
} else if (char_counter >= LINE_BUFFER_SIZE-1) {
|
||||||
|
// Throw away any characters beyond the end of the line buffer
|
||||||
|
} else if (c >= 'a' && c <= 'z') { // Upcase lowercase
|
||||||
|
line[char_counter++] = c-'a'+'A';
|
||||||
|
} else {
|
||||||
|
line[char_counter++] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -36,4 +37,7 @@ void protocol_process();
|
|||||||
// Executes one line of input according to protocol
|
// Executes one line of input according to protocol
|
||||||
uint8_t protocol_execute_line(char *line);
|
uint8_t protocol_execute_line(char *line);
|
||||||
|
|
||||||
|
// Checks and executes a runtime command at various stop points in main program
|
||||||
|
void protocol_execute_runtime();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,12 +4,33 @@ Grbl is a no-compromise, high performance, low cost alternative to parallel-port
|
|||||||
|
|
||||||
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 more than 30kHz of stable, jitter free control pulses.
|
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 more than 30kHz of stable, jitter free control pulses.
|
||||||
|
|
||||||
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 - but no support for tool offsets, functions or variables as these are apocryphal and fell into disuse after humans left G-code authoring to machines some time in the 80s.
|
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, other basic functional g-code commands. Functions and variables are not currently supported, but may be included in future releases in a form of a pre-processor.
|
||||||
|
|
||||||
Grbl includes full acceleration management with look ahead. That means the controller will look up to 20 motions into the future and plan its velocities ahead to deliver smooth acceleration and jerk-free cornering.
|
Grbl includes full acceleration management with look ahead. That means the controller will look up to 20 motions into the future and plan its velocities ahead to deliver smooth acceleration and jerk-free cornering.
|
||||||
|
|
||||||
*Important note for Atmega 168 users:* Grbl used to be compatible with both the older Ardunios running atmega 168 and the newer with 328p. The full version of Grbl now compiles without support for circles/arcs if you target 168. If you need arcs, but not acceleration-management I am still maintaining Grbl 0.51 "in the branch called 'v0_51'":https://github.com/simen/grbl/tree/v0_51.
|
*Changelog for v0.8 from v0.7:*
|
||||||
|
- *ALPHA STATUS*: Major structural overhaul to allow for multi-tasking events and new feature sets
|
||||||
|
- New run-time command control: Feed hold (pause), Cycle start (resume), Reset (abort), Status report (TBD)
|
||||||
|
- Controlled feed hold with deceleration to ensure no skipped steps and loss of location.
|
||||||
|
- After feed hold, cycle accelerations are re-planned and may be resumed.
|
||||||
|
- System reset re-initializes grbl without resetting the Arduino.
|
||||||
|
- Updated dwell function.
|
||||||
|
- Restructured planner and stepper modules to become independent and ready for future features.
|
||||||
|
- Planned features: Jog mode, status reporting, backlash compensation, improved flow control, planner optimizations
|
||||||
|
- Reduce serial read buffer to 127 characters and increased write buffer to 31 characters (-1 ring buffer).
|
||||||
|
- Increased planner buffer size to 20 blocks.
|
||||||
|
- Misc bug fixes and removed deprecated acceleration enabled code.
|
||||||
|
|
||||||
*Note for users upgrading from 0.51 to 0.6:* The new version has new and improved default pin-out. If nothing works when you upgrade, that is because the pulse trains are coming from the wrong pins. This is a simple matter of editing config.h – the whole legacy pin assignment is there for you to uncomment.
|
*Changelog for v0.7 from v0.6:*
|
||||||
|
- Significantly improved and optimized planner re-factoring.
|
||||||
|
- New robust cornering algorithm, enabling smoother and faster motions.
|
||||||
|
- Arc acceleration planning enabled by efficient vector transformation implementation.
|
||||||
|
- Stepper subsystem re-factoring to help remove some motion issues from pre-v0.7 builds.
|
||||||
|
- Increased dwell times.
|
||||||
|
- G92 Coordinate offset support.
|
||||||
|
- (Beta) Limit switch and homing cycle support.
|
||||||
|
- Many other bug fixes and efficiency improvements.
|
||||||
|
|
||||||
|
*Important note for Atmega 168 users:* Going forward, support for Atmega 168 will be dropped due to its limited memory and speed. However, legacy Grbl v0.51 "in the branch called 'v0_51' is still available for use.
|
||||||
|
|
||||||
_The project was initially inspired by the Arduino GCode Interpreter by Mike Ellery_
|
_The project was initially inspired by the Arduino GCode Interpreter by Mike Ellery_
|
||||||
|
42
script/simple_stream.py
Executable file
42
script/simple_stream.py
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""\
|
||||||
|
Simple g-code streaming script for grbl
|
||||||
|
|
||||||
|
Provided as an illustration of the basic communication interface
|
||||||
|
for grbl. When grbl has finished parsing the g-code block, it will
|
||||||
|
return an 'ok' or 'error' response. When the planner buffer is full,
|
||||||
|
grbl will not send a response until the planner buffer clears space.
|
||||||
|
|
||||||
|
G02/03 arcs are special exceptions, where they inject short line
|
||||||
|
segments directly into the planner. So there may not be a response
|
||||||
|
from grbl for the duration of the arc.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import serial
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Open grbl serial port
|
||||||
|
s = serial.Serial('/dev/tty.usbmodem1811',9600)
|
||||||
|
|
||||||
|
# Open g-code file
|
||||||
|
f = open('grbl.gcode','r');
|
||||||
|
|
||||||
|
# Wake up grbl
|
||||||
|
s.write("\r\n\r\n")
|
||||||
|
time.sleep(2) # Wait for grbl to initialize
|
||||||
|
s.flushInput() # Flush startup text in serial input
|
||||||
|
|
||||||
|
# Stream g-code to grbl
|
||||||
|
for line in f:
|
||||||
|
l = line.strip() # Strip all EOL characters for consistency
|
||||||
|
print 'Sending: ' + l,
|
||||||
|
s.write(l + '\n') # Send g-code block to grbl
|
||||||
|
grbl_out = s.readline() # Wait for grbl response with carriage return
|
||||||
|
print ' : ' + grbl_out.strip()
|
||||||
|
|
||||||
|
# Wait here until grbl is finished to close serial port and file.
|
||||||
|
raw_input(" Press <Enter> to exit and disable grbl.")
|
||||||
|
|
||||||
|
# Close file and serial port
|
||||||
|
f.close()
|
||||||
|
s.close()
|
89
script/stream.py
Executable file
89
script/stream.py
Executable file
@ -0,0 +1,89 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""\
|
||||||
|
Stream g-code to grbl controller
|
||||||
|
|
||||||
|
This script differs from the simple_stream.py script by
|
||||||
|
tracking the number of characters in grbl's serial read
|
||||||
|
buffer. This allows grbl to fetch the next line directly
|
||||||
|
from the serial buffer and does not have to wait for a
|
||||||
|
response from the computer. This effectively adds another
|
||||||
|
buffer layer to prevent buffer starvation.
|
||||||
|
|
||||||
|
TODO: - Add runtime command capabilities
|
||||||
|
|
||||||
|
Version: SKJ.20120110
|
||||||
|
"""
|
||||||
|
|
||||||
|
import serial
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
# import threading
|
||||||
|
|
||||||
|
RX_BUFFER_SIZE = 128
|
||||||
|
|
||||||
|
# Define command line argument interface
|
||||||
|
parser = argparse.ArgumentParser(description='Stream g-code file to grbl. (pySerial library required)')
|
||||||
|
parser.add_argument('gcode', type=argparse.FileType('r'),
|
||||||
|
help='g-code filename to be streamed')
|
||||||
|
parser.add_argument('device',
|
||||||
|
help='serial device path')
|
||||||
|
parser.add_argument('-q','--quiet',action='store_true', default=False,
|
||||||
|
help='suppress output text')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Periodic timer to query for status reports
|
||||||
|
# TODO: Need to track down why this doesn't restart consistently before a release.
|
||||||
|
# def periodic():
|
||||||
|
# s.write('?')
|
||||||
|
# t = threading.Timer(0.1, periodic) # In seconds
|
||||||
|
# t.start()
|
||||||
|
|
||||||
|
# Initialize
|
||||||
|
s = serial.Serial(args.device_file,9600)
|
||||||
|
f = args.gcode_file
|
||||||
|
verbose = True
|
||||||
|
if args.quiet : verbose = False
|
||||||
|
|
||||||
|
# Wake up grbl
|
||||||
|
print "Initializing grbl..."
|
||||||
|
s.write("\r\n\r\n")
|
||||||
|
|
||||||
|
# Wait for grbl to initialize and flush startup text in serial input
|
||||||
|
time.sleep(2)
|
||||||
|
s.flushInput()
|
||||||
|
|
||||||
|
# Stream g-code to grbl
|
||||||
|
print "Streaming ", args.gcode_file.name, " to ", args.device_file
|
||||||
|
l_count = 0
|
||||||
|
g_count = 0
|
||||||
|
c_line = []
|
||||||
|
# periodic() # Start status report periodic timer
|
||||||
|
for line in f:
|
||||||
|
l_count += 1 # Iterate line counter
|
||||||
|
# l_block = re.sub('\s|\(.*?\)','',line).upper() # Strip comments/spaces/new line and capitalize
|
||||||
|
l_block = line.strip()
|
||||||
|
c_line.append(len(l_block)+1) # Track number of characters in grbl serial read buffer
|
||||||
|
grbl_out = ''
|
||||||
|
while sum(c_line) >= RX_BUFFER_SIZE-1 | s.inWaiting() :
|
||||||
|
out_temp = s.readline().strip() # Wait for grbl response
|
||||||
|
if out_temp.find('ok') < 0 and out_temp.find('error') < 0 :
|
||||||
|
print " Debug: ",out_temp # Debug response
|
||||||
|
else :
|
||||||
|
grbl_out += out_temp;
|
||||||
|
g_count += 1 # Iterate g-code counter
|
||||||
|
grbl_out += str(g_count); # Add line finished indicator
|
||||||
|
del c_line[0]
|
||||||
|
if verbose: print "SND: " + str(l_count) + " : " + l_block,
|
||||||
|
s.write(l_block + '\n') # Send block to grbl
|
||||||
|
if verbose : print "BUF:",str(sum(c_line)),"REC:",grbl_out
|
||||||
|
|
||||||
|
# Wait for user input after streaming is completed
|
||||||
|
print "G-code streaming finished!\n"
|
||||||
|
print "WARNING: Wait until grbl completes buffered g-code blocks before exiting."
|
||||||
|
raw_input(" Press <Enter> to exit and disable grbl.")
|
||||||
|
|
||||||
|
# Close file and serial port
|
||||||
|
f.close()
|
||||||
|
s.close()
|
94
serial.c
94
serial.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -24,15 +25,13 @@
|
|||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "stepper.h"
|
||||||
|
#include "nuts_bolts.h"
|
||||||
|
#include "protocol.h"
|
||||||
|
|
||||||
|
#define RX_BUFFER_SIZE 128
|
||||||
#ifdef __AVR_ATmega328P__
|
#define TX_BUFFER_SIZE 64
|
||||||
#define RX_BUFFER_SIZE 256
|
|
||||||
#else
|
|
||||||
#define RX_BUFFER_SIZE 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define TX_BUFFER_SIZE 16
|
|
||||||
|
|
||||||
uint8_t rx_buffer[RX_BUFFER_SIZE];
|
uint8_t rx_buffer[RX_BUFFER_SIZE];
|
||||||
uint8_t rx_buffer_head = 0;
|
uint8_t rx_buffer_head = 0;
|
||||||
@ -44,45 +43,48 @@ volatile uint8_t tx_buffer_tail = 0;
|
|||||||
|
|
||||||
static void set_baud_rate(long baud) {
|
static void set_baud_rate(long baud) {
|
||||||
uint16_t UBRR0_value = ((F_CPU / 16 + baud / 2) / baud - 1);
|
uint16_t UBRR0_value = ((F_CPU / 16 + baud / 2) / baud - 1);
|
||||||
UBRR0H = UBRR0_value >> 8;
|
UBRR0H = UBRR0_value >> 8;
|
||||||
UBRR0L = UBRR0_value;
|
UBRR0L = UBRR0_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_init(long baud)
|
void serial_init(long baud)
|
||||||
{
|
{
|
||||||
set_baud_rate(baud);
|
set_baud_rate(baud);
|
||||||
|
|
||||||
/* baud doubler off - Only needed on Uno XXX */
|
/* baud doubler off - Only needed on Uno XXX */
|
||||||
UCSR0A &= ~(1 << U2X0);
|
UCSR0A &= ~(1 << U2X0);
|
||||||
|
|
||||||
// enable rx and tx
|
// enable rx and tx
|
||||||
UCSR0B |= 1<<RXEN0;
|
UCSR0B |= 1<<RXEN0;
|
||||||
UCSR0B |= 1<<TXEN0;
|
UCSR0B |= 1<<TXEN0;
|
||||||
|
|
||||||
// enable interrupt on complete reception of a byte
|
// enable interrupt on complete reception of a byte
|
||||||
UCSR0B |= 1<<RXCIE0;
|
UCSR0B |= 1<<RXCIE0;
|
||||||
|
|
||||||
// defaults to 8-bit, no parity, 1 stop bit
|
// defaults to 8-bit, no parity, 1 stop bit
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_write(uint8_t data) {
|
void serial_write(uint8_t data) {
|
||||||
// Calculate next head
|
// Calculate next head
|
||||||
uint8_t next_head = (tx_buffer_head + 1) % TX_BUFFER_SIZE;
|
uint8_t next_head = tx_buffer_head + 1;
|
||||||
|
if (next_head == TX_BUFFER_SIZE) { next_head = 0; }
|
||||||
|
|
||||||
// Wait until there's a space in the buffer
|
// Wait until there is space in the buffer
|
||||||
while (next_head == tx_buffer_tail) { sleep_mode(); };
|
while (next_head == tx_buffer_tail) {
|
||||||
|
if (sys.execute & EXEC_RESET) { return; } // Only check for abort to avoid an endless loop.
|
||||||
|
}
|
||||||
|
|
||||||
// Store data and advance head
|
// Store data and advance head
|
||||||
tx_buffer[tx_buffer_head] = data;
|
tx_buffer[tx_buffer_head] = data;
|
||||||
tx_buffer_head = next_head;
|
tx_buffer_head = next_head;
|
||||||
|
|
||||||
// Enable Data Register Empty Interrupt to make sure tx-streaming is running
|
// Enable Data Register Empty Interrupt to make sure tx-streaming is running
|
||||||
UCSR0B |= (1 << UDRIE0);
|
UCSR0B |= (1 << UDRIE0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data Register Empty Interrupt handler
|
// Data Register Empty Interrupt handler
|
||||||
SIGNAL(USART_UDRE_vect) {
|
ISR(USART_UDRE_vect) {
|
||||||
// temporary tx_buffer_tail (to optimize for volatile)
|
// Temporary tx_buffer_tail (to optimize for volatile)
|
||||||
uint8_t tail = tx_buffer_tail;
|
uint8_t tail = tx_buffer_tail;
|
||||||
|
|
||||||
// Send a byte from the buffer
|
// Send a byte from the buffer
|
||||||
@ -90,7 +92,7 @@ SIGNAL(USART_UDRE_vect) {
|
|||||||
|
|
||||||
// Update tail position
|
// Update tail position
|
||||||
tail ++;
|
tail ++;
|
||||||
tail %= TX_BUFFER_SIZE;
|
if (tail == TX_BUFFER_SIZE) { tail = 0; }
|
||||||
|
|
||||||
// Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
|
// Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
|
||||||
if (tail == tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }
|
if (tail == tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }
|
||||||
@ -100,23 +102,45 @@ SIGNAL(USART_UDRE_vect) {
|
|||||||
|
|
||||||
uint8_t serial_read()
|
uint8_t serial_read()
|
||||||
{
|
{
|
||||||
if (rx_buffer_head == rx_buffer_tail) {
|
if (rx_buffer_head == rx_buffer_tail) {
|
||||||
return SERIAL_NO_DATA;
|
return SERIAL_NO_DATA;
|
||||||
} else {
|
} else {
|
||||||
uint8_t data = rx_buffer[rx_buffer_tail];
|
uint8_t data = rx_buffer[rx_buffer_tail];
|
||||||
rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE;
|
rx_buffer_tail++;
|
||||||
return data;
|
if (rx_buffer_tail == RX_BUFFER_SIZE) { rx_buffer_tail = 0; }
|
||||||
}
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SIGNAL(USART_RX_vect)
|
ISR(USART_RX_vect)
|
||||||
{
|
{
|
||||||
uint8_t data = UDR0;
|
uint8_t data = UDR0;
|
||||||
uint8_t next_head = (rx_buffer_head + 1) % RX_BUFFER_SIZE;
|
uint8_t next_head = rx_buffer_head + 1;
|
||||||
|
if (next_head == RX_BUFFER_SIZE) { next_head = 0; }
|
||||||
|
|
||||||
// Write data to buffer unless it is full.
|
// Write data to buffer unless it is full.
|
||||||
if (next_head != rx_buffer_tail) {
|
if (next_head != rx_buffer_tail) {
|
||||||
rx_buffer[rx_buffer_head] = data;
|
// Pick off runtime command characters directly from the serial stream. These characters are
|
||||||
rx_buffer_head = next_head;
|
// not passed into the buffer, but these set system state flag bits for runtime execution.
|
||||||
}
|
switch (data) {
|
||||||
|
case CMD_STATUS_REPORT: sys.execute |= EXEC_STATUS_REPORT; break; // Set as true
|
||||||
|
case CMD_CYCLE_START: sys.execute |= EXEC_CYCLE_START; break; // Set as true
|
||||||
|
case CMD_FEED_HOLD: sys.execute |= EXEC_FEED_HOLD; break; // Set as true
|
||||||
|
case CMD_RESET:
|
||||||
|
// Immediately force stepper subsystem idle at an interrupt level.
|
||||||
|
if (!(sys.execute & EXEC_RESET)) { // Force stop only first time.
|
||||||
|
st_go_idle();
|
||||||
|
}
|
||||||
|
sys.execute |= EXEC_RESET; // Set as true
|
||||||
|
break;
|
||||||
|
default : // Write character to buffer
|
||||||
|
rx_buffer[rx_buffer_head] = data;
|
||||||
|
rx_buffer_head = next_head;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void serial_reset_read_buffer()
|
||||||
|
{
|
||||||
|
rx_buffer_tail = rx_buffer_head;
|
||||||
}
|
}
|
||||||
|
4
serial.h
4
serial.h
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -32,4 +33,7 @@ void serial_write(uint8_t data);
|
|||||||
|
|
||||||
uint8_t serial_read();
|
uint8_t serial_read();
|
||||||
|
|
||||||
|
// Reset and empty data in read buffer. Used by e-stop and reset.
|
||||||
|
void serial_reset_read_buffer();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
50
settings.c
50
settings.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -48,11 +49,12 @@ typedef struct {
|
|||||||
#define DEFAULT_Z_STEPS_PER_MM (94.488188976378*MICROSTEPS)
|
#define DEFAULT_Z_STEPS_PER_MM (94.488188976378*MICROSTEPS)
|
||||||
#define DEFAULT_STEP_PULSE_MICROSECONDS 30
|
#define DEFAULT_STEP_PULSE_MICROSECONDS 30
|
||||||
#define DEFAULT_MM_PER_ARC_SEGMENT 0.1
|
#define DEFAULT_MM_PER_ARC_SEGMENT 0.1
|
||||||
#define DEFAULT_RAPID_FEEDRATE 500.0 // in millimeters per minute
|
#define DEFAULT_RAPID_FEEDRATE 500.0 // mm/min
|
||||||
#define DEFAULT_FEEDRATE 500.0
|
#define DEFAULT_FEEDRATE 500.0
|
||||||
#define DEFAULT_ACCELERATION (DEFAULT_FEEDRATE/10.0)
|
#define DEFAULT_ACCELERATION (DEFAULT_FEEDRATE*60*60/10.0) // mm/min^2
|
||||||
#define DEFAULT_MAX_JERK 300.0
|
#define DEFAULT_JUNCTION_DEVIATION 0.05 // mm
|
||||||
#define DEFAULT_STEPPING_INVERT_MASK ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT))
|
#define DEFAULT_STEPPING_INVERT_MASK ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT))
|
||||||
|
// #define DEFAULT_AUTO_START 1 // Boolean
|
||||||
|
|
||||||
void settings_reset() {
|
void settings_reset() {
|
||||||
settings.steps_per_mm[X_AXIS] = DEFAULT_X_STEPS_PER_MM;
|
settings.steps_per_mm[X_AXIS] = DEFAULT_X_STEPS_PER_MM;
|
||||||
@ -64,7 +66,7 @@ void settings_reset() {
|
|||||||
settings.acceleration = DEFAULT_ACCELERATION;
|
settings.acceleration = DEFAULT_ACCELERATION;
|
||||||
settings.mm_per_arc_segment = DEFAULT_MM_PER_ARC_SEGMENT;
|
settings.mm_per_arc_segment = DEFAULT_MM_PER_ARC_SEGMENT;
|
||||||
settings.invert_mask = DEFAULT_STEPPING_INVERT_MASK;
|
settings.invert_mask = DEFAULT_STEPPING_INVERT_MASK;
|
||||||
settings.max_jerk = DEFAULT_MAX_JERK;
|
settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
void settings_dump() {
|
void settings_dump() {
|
||||||
@ -76,10 +78,11 @@ void settings_dump() {
|
|||||||
printPgmString(PSTR(" (mm/min default feed rate)\r\n$5 = ")); printFloat(settings.default_seek_rate);
|
printPgmString(PSTR(" (mm/min default feed rate)\r\n$5 = ")); printFloat(settings.default_seek_rate);
|
||||||
printPgmString(PSTR(" (mm/min default seek rate)\r\n$6 = ")); printFloat(settings.mm_per_arc_segment);
|
printPgmString(PSTR(" (mm/min default seek rate)\r\n$6 = ")); printFloat(settings.mm_per_arc_segment);
|
||||||
printPgmString(PSTR(" (mm/arc segment)\r\n$7 = ")); printInteger(settings.invert_mask);
|
printPgmString(PSTR(" (mm/arc segment)\r\n$7 = ")); printInteger(settings.invert_mask);
|
||||||
printPgmString(PSTR(" (step port invert mask. binary = ")); printIntegerInBase(settings.invert_mask, 2);
|
printPgmString(PSTR(" (step port invert mask. binary = ")); print_uint8_base2(settings.invert_mask);
|
||||||
printPgmString(PSTR(")\r\n$8 = ")); printFloat(settings.acceleration);
|
printPgmString(PSTR(")\r\n$8 = ")); printFloat(settings.acceleration/(60*60)); // Convert from mm/min^2 for human readability
|
||||||
printPgmString(PSTR(" (acceleration in mm/sec^2)\r\n$9 = ")); printFloat(settings.max_jerk);
|
printPgmString(PSTR(" (acceleration in mm/sec^2)\r\n$9 = ")); printFloat(settings.junction_deviation);
|
||||||
printPgmString(PSTR(" (max instant cornering speed change in delta mm/min)"));
|
printPgmString(PSTR(" (cornering junction deviation in mm)"));//\r\n$10 = ")); // printInteger(settings.auto_start);
|
||||||
|
// printPgmString(PSTR(" (auto-start boolean)"));
|
||||||
printPgmString(PSTR("\r\n'$x=value' to set parameter or just '$' to dump current settings\r\n"));
|
printPgmString(PSTR("\r\n'$x=value' to set parameter or just '$' to dump current settings\r\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,12 +127,30 @@ int read_settings() {
|
|||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
} else if (version == 1) {
|
} else if (version == 1) {
|
||||||
// Migrate from old settings version
|
// Migrate from settings version 1
|
||||||
if (!(memcpy_from_eeprom_with_checksum((char*)&settings, 1, sizeof(settings_v1_t)))) {
|
if (!(memcpy_from_eeprom_with_checksum((char*)&settings, 1, sizeof(settings_v1_t)))) {
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
settings.acceleration = DEFAULT_ACCELERATION;
|
settings.acceleration = DEFAULT_ACCELERATION;
|
||||||
settings.max_jerk = DEFAULT_MAX_JERK;
|
settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION;
|
||||||
|
// settings.auto_start = DEFAULT_AUTO_START;
|
||||||
|
write_settings();
|
||||||
|
} else if ((version == 2) || (version == 3)) {
|
||||||
|
// Migrate from settings version 2 and 3
|
||||||
|
if (!(memcpy_from_eeprom_with_checksum((char*)&settings, 1, sizeof(settings_t)))) {
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
if (version == 2) { settings.junction_deviation = DEFAULT_JUNCTION_DEVIATION; }
|
||||||
|
settings.acceleration *= 3600; // Convert to mm/min^2 from mm/sec^2
|
||||||
|
// settings.auto_start = DEFAULT_AUTO_START;
|
||||||
|
write_settings();
|
||||||
|
// } else if (version == 4) {
|
||||||
|
// // Migrate from settings version 4
|
||||||
|
// if (!(memcpy_from_eeprom_with_checksum((char*)&settings, 1, sizeof(settings_t)))) {
|
||||||
|
// return(false);
|
||||||
|
// }
|
||||||
|
// settings.auto_start = DEFAULT_AUTO_START;
|
||||||
|
// write_settings();
|
||||||
} else {
|
} else {
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
@ -155,8 +176,9 @@ void settings_store_setting(int parameter, double value) {
|
|||||||
case 5: settings.default_seek_rate = value; break;
|
case 5: settings.default_seek_rate = value; break;
|
||||||
case 6: settings.mm_per_arc_segment = value; break;
|
case 6: settings.mm_per_arc_segment = value; break;
|
||||||
case 7: settings.invert_mask = trunc(value); break;
|
case 7: settings.invert_mask = trunc(value); break;
|
||||||
case 8: settings.acceleration = value; break;
|
case 8: settings.acceleration = value*60*60; break; // Convert to mm/min^2 for grbl internal use.
|
||||||
case 9: settings.max_jerk = fabs(value); break;
|
case 9: settings.junction_deviation = fabs(value); break;
|
||||||
|
// case 10: settings.auto_start = value; break;
|
||||||
default:
|
default:
|
||||||
printPgmString(PSTR("Unknown parameter\r\n"));
|
printPgmString(PSTR("Unknown parameter\r\n"));
|
||||||
return;
|
return;
|
||||||
@ -167,9 +189,7 @@ void settings_store_setting(int parameter, double value) {
|
|||||||
|
|
||||||
// Initialize the config subsystem
|
// Initialize the config subsystem
|
||||||
void settings_init() {
|
void settings_init() {
|
||||||
if(read_settings()) {
|
if(!read_settings()) {
|
||||||
printPgmString(PSTR("'$' to dump current settings\r\n"));
|
|
||||||
} else {
|
|
||||||
printPgmString(PSTR("Warning: Failed to read EEPROM settings. Using defaults.\r\n"));
|
printPgmString(PSTR("Warning: Failed to read EEPROM settings. Using defaults.\r\n"));
|
||||||
settings_reset();
|
settings_reset();
|
||||||
write_settings();
|
write_settings();
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -21,15 +22,14 @@
|
|||||||
#ifndef settings_h
|
#ifndef settings_h
|
||||||
#define settings_h
|
#define settings_h
|
||||||
|
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
#define GRBL_VERSION "0.7b"
|
#define GRBL_VERSION "0.8a"
|
||||||
|
|
||||||
// Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl
|
// Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl
|
||||||
// when firmware is upgraded. Always stored in byte 0 of eeprom
|
// when firmware is upgraded. Always stored in byte 0 of eeprom
|
||||||
#define SETTINGS_VERSION 2
|
#define SETTINGS_VERSION 4
|
||||||
|
|
||||||
// Current global settings (persisted in EEPROM from byte 1 onwards)
|
// Current global settings (persisted in EEPROM from byte 1 onwards)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -41,7 +41,7 @@ typedef struct {
|
|||||||
uint8_t invert_mask;
|
uint8_t invert_mask;
|
||||||
double mm_per_arc_segment;
|
double mm_per_arc_segment;
|
||||||
double acceleration;
|
double acceleration;
|
||||||
double max_jerk;
|
double junction_deviation;
|
||||||
} settings_t;
|
} settings_t;
|
||||||
extern settings_t settings;
|
extern settings_t settings;
|
||||||
|
|
||||||
|
@ -20,11 +20,14 @@
|
|||||||
|
|
||||||
#include "spindle_control.h"
|
#include "spindle_control.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "motion_control.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "stepper.h"
|
#include "planner.h"
|
||||||
|
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
// TODO: Deprecated. Need to update for new version.
|
||||||
|
|
||||||
static int current_direction;
|
static int current_direction;
|
||||||
|
|
||||||
void spindle_init()
|
void spindle_init()
|
||||||
@ -35,7 +38,7 @@ void spindle_init()
|
|||||||
void spindle_run(int direction, uint32_t rpm)
|
void spindle_run(int direction, uint32_t rpm)
|
||||||
{
|
{
|
||||||
if (direction != current_direction) {
|
if (direction != current_direction) {
|
||||||
st_synchronize();
|
plan_synchronize();
|
||||||
if(direction) {
|
if(direction) {
|
||||||
if(direction > 0) {
|
if(direction > 0) {
|
||||||
SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT);
|
SPINDLE_DIRECTION_PORT &= ~(1<<SPINDLE_DIRECTION_BIT);
|
||||||
|
408
stepper.c
408
stepper.c
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -40,23 +41,30 @@
|
|||||||
#define TICKS_PER_MICROSECOND (F_CPU/1000000)
|
#define TICKS_PER_MICROSECOND (F_CPU/1000000)
|
||||||
#define CYCLES_PER_ACCELERATION_TICK ((TICKS_PER_MICROSECOND*1000000)/ACCELERATION_TICKS_PER_SECOND)
|
#define CYCLES_PER_ACCELERATION_TICK ((TICKS_PER_MICROSECOND*1000000)/ACCELERATION_TICKS_PER_SECOND)
|
||||||
|
|
||||||
#define MINIMUM_STEPS_PER_MINUTE 1200 // The stepper subsystem will never run slower than this, exept when sleeping
|
// Stepper state variable. Contains running data and trapezoid variables.
|
||||||
|
typedef struct {
|
||||||
|
// Used by the bresenham line algorithm
|
||||||
|
int32_t counter_x, // Counter variables for the bresenham line tracer
|
||||||
|
counter_y,
|
||||||
|
counter_z;
|
||||||
|
uint32_t event_count;
|
||||||
|
uint32_t step_events_completed; // The number of step events left in current motion
|
||||||
|
|
||||||
|
// Used by the trapezoid generator
|
||||||
|
uint32_t cycles_per_step_event; // The number of machine cycles between each step event
|
||||||
|
uint32_t trapezoid_tick_cycle_counter; // The cycles since last trapezoid_tick. Used to generate ticks at a steady
|
||||||
|
// pace without allocating a separate timer
|
||||||
|
uint32_t trapezoid_adjusted_rate; // The current rate of step_events according to the trapezoid generator
|
||||||
|
uint32_t min_safe_rate; // Minimum safe rate for full deceleration rate reduction step. Otherwise halves step_rate.
|
||||||
|
} stepper_t;
|
||||||
|
|
||||||
|
static stepper_t st;
|
||||||
static block_t *current_block; // A pointer to the block currently being traced
|
static block_t *current_block; // A pointer to the block currently being traced
|
||||||
|
|
||||||
// Variables used by The Stepper Driver Interrupt
|
// Used by the stepper driver interrupt
|
||||||
|
static uint8_t step_pulse_time; // Step pulse reset time after step rise
|
||||||
static uint8_t out_bits; // The next stepping-bits to be output
|
static uint8_t out_bits; // The next stepping-bits to be output
|
||||||
static int32_t counter_x, // Counter variables for the bresenham line tracer
|
static volatile uint8_t busy; // True when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
|
||||||
counter_y,
|
|
||||||
counter_z;
|
|
||||||
static uint32_t step_events_completed; // The number of step events executed in the current block
|
|
||||||
static volatile int busy; // true when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
|
|
||||||
|
|
||||||
// Variables used by the trapezoid generation
|
|
||||||
static uint32_t cycles_per_step_event; // The number of machine cycles between each step event
|
|
||||||
static uint32_t trapezoid_tick_cycle_counter; // The cycles since last trapezoid_tick. Used to generate ticks at a steady
|
|
||||||
// pace without allocating a separate timer
|
|
||||||
static uint32_t trapezoid_adjusted_rate; // The current rate of step_events according to the trapezoid generator
|
|
||||||
|
|
||||||
// __________________________
|
// __________________________
|
||||||
// /| |\ _________________ ^
|
// /| |\ _________________ ^
|
||||||
@ -72,181 +80,264 @@ static uint32_t trapezoid_adjusted_rate; // The current rate of step_events
|
|||||||
// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta
|
// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates by block->rate_delta
|
||||||
// during the first block->accelerate_until step_events_completed, then keeps going at constant speed until
|
// during the first block->accelerate_until step_events_completed, then keeps going at constant speed until
|
||||||
// step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
|
// step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
|
||||||
// The slope of acceleration is always +/- block->rate_delta and is applied at a constant rate by trapezoid_generator_tick()
|
// The slope of acceleration is always +/- block->rate_delta and is applied at a constant rate following the midpoint rule
|
||||||
// that is called ACCELERATION_TICKS_PER_SECOND times per second.
|
// by the trapezoid generator, which is called ACCELERATION_TICKS_PER_SECOND times per second.
|
||||||
|
|
||||||
static void set_step_events_per_minute(uint32_t steps_per_minute);
|
static void set_step_events_per_minute(uint32_t steps_per_minute);
|
||||||
|
|
||||||
void st_wake_up() {
|
// Stepper state initialization
|
||||||
|
static void st_wake_up()
|
||||||
|
{
|
||||||
|
// Initialize stepper output bits
|
||||||
|
out_bits = (0) ^ (settings.invert_mask);
|
||||||
|
// Set step pulse time. Ad hoc computation from oscilloscope.
|
||||||
|
step_pulse_time = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND) >> 3);
|
||||||
// Enable steppers by resetting the stepper disable port
|
// Enable steppers by resetting the stepper disable port
|
||||||
STEPPERS_DISABLE_PORT &= ~(1<<STEPPERS_DISABLE_BIT);
|
STEPPERS_DISABLE_PORT &= ~(1<<STEPPERS_DISABLE_BIT);
|
||||||
// Enable stepper driver interrupt
|
// Enable stepper driver interrupt
|
||||||
TIMSK1 |= (1<<OCIE1A);
|
TIMSK1 |= (1<<OCIE1A);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void st_go_idle() {
|
// Stepper shutdown
|
||||||
// Disable steppers by setting stepper disable
|
void st_go_idle()
|
||||||
STEPPERS_DISABLE_PORT |= (1<<STEPPERS_DISABLE_BIT);
|
{
|
||||||
// Disable stepper driver interrupt
|
// Disable stepper driver interrupt
|
||||||
TIMSK1 &= ~(1<<OCIE1A);
|
TIMSK1 &= ~(1<<OCIE1A);
|
||||||
|
// Force stepper dwell to lock axes for a defined amount of time to ensure the axes come to a complete
|
||||||
|
// stop and not drift from residual inertial forces at the end of the last movement.
|
||||||
|
#ifdef STEPPER_IDLE_LOCK_TIME
|
||||||
|
_delay_ms(STEPPER_IDLE_LOCK_TIME);
|
||||||
|
#endif
|
||||||
|
// Disable steppers by setting stepper disable
|
||||||
|
STEPPERS_DISABLE_PORT |= (1<<STEPPERS_DISABLE_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initializes the trapezoid generator from the current block. Called whenever a new
|
// This function determines an acceleration velocity change every CYCLES_PER_ACCELERATION_TICK by
|
||||||
// block begins.
|
// keeping track of the number of elapsed cycles during a de/ac-celeration. The code assumes that
|
||||||
static void trapezoid_generator_reset() {
|
// step_events occur significantly more often than the acceleration velocity iterations.
|
||||||
trapezoid_adjusted_rate = current_block->initial_rate;
|
static uint8_t iterate_trapezoid_cycle_counter()
|
||||||
trapezoid_tick_cycle_counter = 0; // Always start a new trapezoid with a full acceleration tick
|
{
|
||||||
set_step_events_per_minute(trapezoid_adjusted_rate);
|
st.trapezoid_tick_cycle_counter += st.cycles_per_step_event;
|
||||||
}
|
if(st.trapezoid_tick_cycle_counter > CYCLES_PER_ACCELERATION_TICK) {
|
||||||
|
st.trapezoid_tick_cycle_counter -= CYCLES_PER_ACCELERATION_TICK;
|
||||||
// This is called ACCELERATION_TICKS_PER_SECOND times per second by the step_event
|
return(true);
|
||||||
// interrupt. It can be assumed that the trapezoid-generator-parameters and the
|
} else {
|
||||||
// current_block stays untouched by outside handlers for the duration of this function call.
|
return(false);
|
||||||
static void trapezoid_generator_tick() {
|
|
||||||
if (current_block) {
|
|
||||||
if (step_events_completed < current_block->accelerate_until) {
|
|
||||||
trapezoid_adjusted_rate += current_block->rate_delta;
|
|
||||||
set_step_events_per_minute(trapezoid_adjusted_rate);
|
|
||||||
} else if (step_events_completed > current_block->decelerate_after) {
|
|
||||||
// NOTE: We will only reduce speed if the result will be > 0. This catches small
|
|
||||||
// rounding errors that might leave steps hanging after the last trapezoid tick.
|
|
||||||
if (trapezoid_adjusted_rate > current_block->rate_delta) {
|
|
||||||
trapezoid_adjusted_rate -= current_block->rate_delta;
|
|
||||||
}
|
|
||||||
if (trapezoid_adjusted_rate < current_block->final_rate) {
|
|
||||||
trapezoid_adjusted_rate = current_block->final_rate;
|
|
||||||
}
|
|
||||||
set_step_events_per_minute(trapezoid_adjusted_rate);
|
|
||||||
} else {
|
|
||||||
// Make sure we cruise at exactly nominal rate
|
|
||||||
if (trapezoid_adjusted_rate != current_block->nominal_rate) {
|
|
||||||
trapezoid_adjusted_rate = current_block->nominal_rate;
|
|
||||||
set_step_events_per_minute(trapezoid_adjusted_rate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. It is executed at the rate set with
|
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. It is executed at the rate set with
|
||||||
// config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
|
// config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
|
||||||
// It is supported by The Stepper Port Reset Interrupt which it uses to reset the stepper port after each pulse.
|
// It is supported by The Stepper Port Reset Interrupt which it uses to reset the stepper port after each pulse.
|
||||||
SIGNAL(TIMER1_COMPA_vect)
|
// The bresenham line tracer algorithm controls all three stepper outputs simultaneously with these two interrupts.
|
||||||
|
ISR(TIMER1_COMPA_vect)
|
||||||
{
|
{
|
||||||
// TODO: Check if the busy-flag can be eliminated by just disabeling this interrupt while we are in it
|
if (busy) { return; } // The busy-flag is used to avoid reentering this interrupt
|
||||||
|
|
||||||
if(busy){ return; } // The busy-flag is used to avoid reentering this interrupt
|
// Set the direction pins a couple of nanoseconds before we step the steppers
|
||||||
// Set the direction pins a cuple of nanoseconds before we step the steppers
|
|
||||||
STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK);
|
STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK);
|
||||||
// Then pulse the stepping pins
|
// Then pulse the stepping pins
|
||||||
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | out_bits;
|
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | out_bits;
|
||||||
// Reset step pulse reset timer so that The Stepper Port Reset Interrupt can reset the signal after
|
// Enable step pulse reset timer so that The Stepper Port Reset Interrupt can reset the signal after
|
||||||
// exactly settings.pulse_microseconds microseconds.
|
// exactly settings.pulse_microseconds microseconds, independent of the main Timer1 prescaler.
|
||||||
TCNT2 = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND)/8);
|
TCNT2 = step_pulse_time; // Reload timer counter
|
||||||
|
TCCR2B = (1<<CS21); // Begin timer2. Full speed, 1/8 prescaler
|
||||||
|
|
||||||
busy = true;
|
busy = true;
|
||||||
sei(); // Re enable interrupts (normally disabled while inside an interrupt handler)
|
// Re-enable interrupts to allow ISR_TIMER2_OVERFLOW to trigger on-time and allow serial communications
|
||||||
// ((We re-enable interrupts in order for SIG_OVERFLOW2 to be able to be triggered
|
// regardless of time in this handler. The following code prepares the stepper driver for the next
|
||||||
// at exactly the right time even if we occasionally spend a lot of time inside this handler.))
|
// step interrupt compare and will always finish before returning to the main program.
|
||||||
|
sei();
|
||||||
|
|
||||||
// If there is no current block, attempt to pop one from the buffer
|
// If there is no current block, attempt to pop one from the buffer
|
||||||
if (current_block == NULL) {
|
if (current_block == NULL) {
|
||||||
// Anything in the buffer?
|
// Anything in the buffer? If so, initialize next motion.
|
||||||
current_block = plan_get_current_block();
|
current_block = plan_get_current_block();
|
||||||
if (current_block != NULL) {
|
if (current_block != NULL) {
|
||||||
trapezoid_generator_reset();
|
if (!sys.feed_hold) {
|
||||||
counter_x = -(current_block->step_event_count >> 1);
|
// During feed hold, do not update rate and trap counter. Keep decelerating.
|
||||||
counter_y = counter_x;
|
st.trapezoid_adjusted_rate = current_block->initial_rate;
|
||||||
counter_z = counter_x;
|
set_step_events_per_minute(st.trapezoid_adjusted_rate); // Initialize cycles_per_step_event
|
||||||
step_events_completed = 0;
|
st.trapezoid_tick_cycle_counter = CYCLES_PER_ACCELERATION_TICK/2; // Start halfway for midpoint rule.
|
||||||
|
}
|
||||||
|
st.min_safe_rate = current_block->rate_delta + (current_block->rate_delta >> 1); // 1.5 x rate_delta
|
||||||
|
st.counter_x = -(current_block->step_event_count >> 1);
|
||||||
|
st.counter_y = st.counter_x;
|
||||||
|
st.counter_z = st.counter_x;
|
||||||
|
st.event_count = current_block->step_event_count;
|
||||||
|
st.step_events_completed = 0;
|
||||||
} else {
|
} else {
|
||||||
st_go_idle();
|
st_go_idle();
|
||||||
|
sys.cycle_start = false;
|
||||||
|
bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program for cycle end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current_block != NULL) {
|
if (current_block != NULL) {
|
||||||
|
// Execute step displacement profile by bresenham line algorithm
|
||||||
out_bits = current_block->direction_bits;
|
out_bits = current_block->direction_bits;
|
||||||
counter_x += current_block->steps_x;
|
st.counter_x += current_block->steps_x;
|
||||||
if (counter_x > 0) {
|
if (st.counter_x > 0) {
|
||||||
out_bits |= (1<<X_STEP_BIT);
|
out_bits |= (1<<X_STEP_BIT);
|
||||||
counter_x -= current_block->step_event_count;
|
st.counter_x -= st.event_count;
|
||||||
|
if (out_bits & (1<<X_DIRECTION_BIT)) { sys.position[X_AXIS]--; }
|
||||||
|
else { sys.position[X_AXIS]++; }
|
||||||
}
|
}
|
||||||
counter_y += current_block->steps_y;
|
st.counter_y += current_block->steps_y;
|
||||||
if (counter_y > 0) {
|
if (st.counter_y > 0) {
|
||||||
out_bits |= (1<<Y_STEP_BIT);
|
out_bits |= (1<<Y_STEP_BIT);
|
||||||
counter_y -= current_block->step_event_count;
|
st.counter_y -= st.event_count;
|
||||||
|
if (out_bits & (1<<Y_DIRECTION_BIT)) { sys.position[Y_AXIS]--; }
|
||||||
|
else { sys.position[Y_AXIS]++; }
|
||||||
}
|
}
|
||||||
counter_z += current_block->steps_z;
|
st.counter_z += current_block->steps_z;
|
||||||
if (counter_z > 0) {
|
if (st.counter_z > 0) {
|
||||||
out_bits |= (1<<Z_STEP_BIT);
|
out_bits |= (1<<Z_STEP_BIT);
|
||||||
counter_z -= current_block->step_event_count;
|
st.counter_z -= st.event_count;
|
||||||
|
if (out_bits & (1<<Z_DIRECTION_BIT)) { sys.position[Z_AXIS]--; }
|
||||||
|
else { sys.position[Z_AXIS]++; }
|
||||||
}
|
}
|
||||||
// If current block is finished, reset pointer
|
|
||||||
step_events_completed += 1;
|
st.step_events_completed++; // Iterate step events
|
||||||
if (step_events_completed >= current_block->step_event_count) {
|
|
||||||
|
// While in block steps, check for de/ac-celeration events and execute them accordingly.
|
||||||
|
if (st.step_events_completed < current_block->step_event_count) {
|
||||||
|
if (sys.feed_hold) {
|
||||||
|
// Check for and execute feed hold by enforcing a steady deceleration from the moment of
|
||||||
|
// execution. The rate of deceleration is limited by rate_delta and will never decelerate
|
||||||
|
// faster or slower than in normal operation. If the distance required for the feed hold
|
||||||
|
// deceleration spans more than one block, the initial rate of the following blocks are not
|
||||||
|
// updated and deceleration is continued according to their corresponding rate_delta.
|
||||||
|
// NOTE: The trapezoid tick cycle counter is not updated intentionally. This ensures that
|
||||||
|
// the deceleration is smooth regardless of where the feed hold is initiated and if the
|
||||||
|
// deceleration distance spans multiple blocks.
|
||||||
|
if ( iterate_trapezoid_cycle_counter() ) {
|
||||||
|
// If deceleration complete, set system flags and shutdown steppers.
|
||||||
|
if (st.trapezoid_adjusted_rate <= current_block->rate_delta) {
|
||||||
|
// Just go idle. Do not NULL current block. The bresenham algorithm variables must
|
||||||
|
// remain intact to ensure the stepper path is exactly the same. Feed hold is still
|
||||||
|
// active and is released after the buffer has been reinitialized.
|
||||||
|
st_go_idle();
|
||||||
|
sys.cycle_start = false;
|
||||||
|
bit_true(sys.execute,EXEC_CYCLE_STOP); // Flag main program that feed hold is complete.
|
||||||
|
} else {
|
||||||
|
st.trapezoid_adjusted_rate -= current_block->rate_delta;
|
||||||
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// The trapezoid generator always checks step event location to ensure de/ac-celerations are
|
||||||
|
// executed and terminated at exactly the right time. This helps prevent over/under-shooting
|
||||||
|
// the target position and speed.
|
||||||
|
// NOTE: By increasing the ACCELERATION_TICKS_PER_SECOND in config.h, the resolution of the
|
||||||
|
// discrete velocity changes increase and accuracy can increase as well to a point. Numerical
|
||||||
|
// round-off errors can effect this, if set too high. This is important to note if a user has
|
||||||
|
// very high acceleration and/or feedrate requirements for their machine.
|
||||||
|
if (st.step_events_completed < current_block->accelerate_until) {
|
||||||
|
// Iterate cycle counter and check if speeds need to be increased.
|
||||||
|
if ( iterate_trapezoid_cycle_counter() ) {
|
||||||
|
st.trapezoid_adjusted_rate += current_block->rate_delta;
|
||||||
|
if (st.trapezoid_adjusted_rate >= current_block->nominal_rate) {
|
||||||
|
// Reached nominal rate a little early. Cruise at nominal rate until decelerate_after.
|
||||||
|
st.trapezoid_adjusted_rate = current_block->nominal_rate;
|
||||||
|
}
|
||||||
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
||||||
|
}
|
||||||
|
} else if (st.step_events_completed >= current_block->decelerate_after) {
|
||||||
|
// Reset trapezoid tick cycle counter to make sure that the deceleration is performed the
|
||||||
|
// same every time. Reset to CYCLES_PER_ACCELERATION_TICK/2 to follow the midpoint rule for
|
||||||
|
// an accurate approximation of the deceleration curve.
|
||||||
|
if (st.step_events_completed == current_block-> decelerate_after) {
|
||||||
|
st.trapezoid_tick_cycle_counter = CYCLES_PER_ACCELERATION_TICK/2;
|
||||||
|
} else {
|
||||||
|
// Iterate cycle counter and check if speeds need to be reduced.
|
||||||
|
if ( iterate_trapezoid_cycle_counter() ) {
|
||||||
|
// NOTE: We will only do a full speed reduction if the result is more than the minimum safe
|
||||||
|
// rate, initialized in trapezoid reset as 1.5 x rate_delta. Otherwise, reduce the speed by
|
||||||
|
// half increments until finished. The half increments are guaranteed not to exceed the
|
||||||
|
// CNC acceleration limits, because they will never be greater than rate_delta. This catches
|
||||||
|
// small errors that might leave steps hanging after the last trapezoid tick or a very slow
|
||||||
|
// step rate at the end of a full stop deceleration in certain situations. The half rate
|
||||||
|
// reductions should only be called once or twice per block and create a nice smooth
|
||||||
|
// end deceleration.
|
||||||
|
if (st.trapezoid_adjusted_rate > st.min_safe_rate) {
|
||||||
|
st.trapezoid_adjusted_rate -= current_block->rate_delta;
|
||||||
|
} else {
|
||||||
|
st.trapezoid_adjusted_rate >>= 1; // Bit shift divide by 2
|
||||||
|
}
|
||||||
|
if (st.trapezoid_adjusted_rate < current_block->final_rate) {
|
||||||
|
// Reached final rate a little early. Cruise to end of block at final rate.
|
||||||
|
st.trapezoid_adjusted_rate = current_block->final_rate;
|
||||||
|
}
|
||||||
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// No accelerations. Make sure we cruise exactly at the nominal rate.
|
||||||
|
if (st.trapezoid_adjusted_rate != current_block->nominal_rate) {
|
||||||
|
st.trapezoid_adjusted_rate = current_block->nominal_rate;
|
||||||
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If current block is finished, reset pointer
|
||||||
current_block = NULL;
|
current_block = NULL;
|
||||||
plan_discard_current_block();
|
plan_discard_current_block();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
out_bits = 0;
|
|
||||||
}
|
}
|
||||||
out_bits ^= settings.invert_mask;
|
out_bits ^= settings.invert_mask; // Apply stepper invert mask
|
||||||
|
busy = false;
|
||||||
// In average this generates a trapezoid_generator_tick every CYCLES_PER_ACCELERATION_TICK by keeping track
|
|
||||||
// of the number of elapsed cycles. The code assumes that step_events occur significantly more often than
|
|
||||||
// trapezoid_generator_ticks as they well should.
|
|
||||||
trapezoid_tick_cycle_counter += cycles_per_step_event;
|
|
||||||
if(trapezoid_tick_cycle_counter > CYCLES_PER_ACCELERATION_TICK) {
|
|
||||||
trapezoid_tick_cycle_counter -= CYCLES_PER_ACCELERATION_TICK;
|
|
||||||
trapezoid_generator_tick();
|
|
||||||
}
|
|
||||||
|
|
||||||
busy=false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This interrupt is set up by SIG_OUTPUT_COMPARE1A when it sets the motor port bits. It resets
|
// This interrupt is set up by ISR_TIMER1_COMPAREA when it sets the motor port bits. It resets
|
||||||
// the motor port after a short period (settings.pulse_microseconds) completing one step cycle.
|
// the motor port after a short period (settings.pulse_microseconds) completing one step cycle.
|
||||||
SIGNAL(TIMER2_OVF_vect)
|
// TODO: It is possible for the serial interrupts to delay this interrupt by a few microseconds, if
|
||||||
|
// they execute right before this interrupt. Not a big deal, but could use some TLC at some point.
|
||||||
|
ISR(TIMER2_OVF_vect)
|
||||||
{
|
{
|
||||||
// reset stepping pins (leave the direction pins)
|
// Reset stepping pins (leave the direction pins)
|
||||||
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (settings.invert_mask & STEP_MASK);
|
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (settings.invert_mask & STEP_MASK);
|
||||||
|
TCCR2B = 0; // Disable Timer2 to prevent re-entering this interrupt when it's not needed.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset and clear stepper subsystem variables
|
||||||
|
void st_reset()
|
||||||
|
{
|
||||||
|
memset(&st, 0, sizeof(st));
|
||||||
|
set_step_events_per_minute(MINIMUM_STEPS_PER_MINUTE);
|
||||||
|
current_block = NULL;
|
||||||
|
busy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize and start the stepper motor subsystem
|
// Initialize and start the stepper motor subsystem
|
||||||
void st_init()
|
void st_init()
|
||||||
{
|
{
|
||||||
// Configure directions of interface pins
|
// Configure directions of interface pins
|
||||||
STEPPING_DDR |= STEPPING_MASK;
|
STEPPING_DDR |= STEPPING_MASK;
|
||||||
STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | settings.invert_mask;
|
STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | settings.invert_mask;
|
||||||
STEPPERS_DISABLE_DDR |= 1<<STEPPERS_DISABLE_BIT;
|
STEPPERS_DISABLE_DDR |= 1<<STEPPERS_DISABLE_BIT;
|
||||||
|
|
||||||
// waveform generation = 0100 = CTC
|
// waveform generation = 0100 = CTC
|
||||||
TCCR1B &= ~(1<<WGM13);
|
TCCR1B &= ~(1<<WGM13);
|
||||||
TCCR1B |= (1<<WGM12);
|
TCCR1B |= (1<<WGM12);
|
||||||
TCCR1A &= ~(1<<WGM11);
|
TCCR1A &= ~(1<<WGM11);
|
||||||
TCCR1A &= ~(1<<WGM10);
|
TCCR1A &= ~(1<<WGM10);
|
||||||
|
|
||||||
// output mode = 00 (disconnected)
|
// output mode = 00 (disconnected)
|
||||||
TCCR1A &= ~(3<<COM1A0);
|
TCCR1A &= ~(3<<COM1A0);
|
||||||
TCCR1A &= ~(3<<COM1B0);
|
TCCR1A &= ~(3<<COM1B0);
|
||||||
|
|
||||||
// Configure Timer 2
|
// Configure Timer 2
|
||||||
TCCR2A = 0; // Normal operation
|
TCCR2A = 0; // Normal operation
|
||||||
TCCR2B = (1<<CS21); // Full speed, 1/8 prescaler
|
TCCR2B = 0; // Disable timer until needed.
|
||||||
TIMSK2 |= (1<<TOIE2);
|
TIMSK2 |= (1<<TOIE2);
|
||||||
|
|
||||||
set_step_events_per_minute(6000);
|
|
||||||
trapezoid_tick_cycle_counter = 0;
|
|
||||||
|
|
||||||
// Start in the idle state
|
// Start in the idle state
|
||||||
st_go_idle();
|
st_go_idle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block until all buffered steps are executed
|
|
||||||
void st_synchronize()
|
|
||||||
{
|
|
||||||
while(plan_get_current_block()) { sleep_mode(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configures the prescaler and ceiling of timer 1 to produce the given rate as accurately as possible.
|
// Configures the prescaler and ceiling of timer 1 to produce the given rate as accurately as possible.
|
||||||
// Returns the actual number of cycles per interrupt
|
// Returns the actual number of cycles per interrupt
|
||||||
static uint32_t config_step_timer(uint32_t cycles)
|
static uint32_t config_step_timer(uint32_t cycles)
|
||||||
@ -254,46 +345,85 @@ static uint32_t config_step_timer(uint32_t cycles)
|
|||||||
uint16_t ceiling;
|
uint16_t ceiling;
|
||||||
uint16_t prescaler;
|
uint16_t prescaler;
|
||||||
uint32_t actual_cycles;
|
uint32_t actual_cycles;
|
||||||
if (cycles <= 0xffffL) {
|
if (cycles <= 0xffffL) {
|
||||||
ceiling = cycles;
|
ceiling = cycles;
|
||||||
prescaler = 0; // prescaler: 0
|
prescaler = 0; // prescaler: 0
|
||||||
actual_cycles = ceiling;
|
actual_cycles = ceiling;
|
||||||
} else if (cycles <= 0x7ffffL) {
|
} else if (cycles <= 0x7ffffL) {
|
||||||
ceiling = cycles >> 3;
|
ceiling = cycles >> 3;
|
||||||
prescaler = 1; // prescaler: 8
|
prescaler = 1; // prescaler: 8
|
||||||
actual_cycles = ceiling * 8L;
|
actual_cycles = ceiling * 8L;
|
||||||
} else if (cycles <= 0x3fffffL) {
|
} else if (cycles <= 0x3fffffL) {
|
||||||
ceiling = cycles >> 6;
|
ceiling = cycles >> 6;
|
||||||
prescaler = 2; // prescaler: 64
|
prescaler = 2; // prescaler: 64
|
||||||
actual_cycles = ceiling * 64L;
|
actual_cycles = ceiling * 64L;
|
||||||
} else if (cycles <= 0xffffffL) {
|
} else if (cycles <= 0xffffffL) {
|
||||||
ceiling = (cycles >> 8);
|
ceiling = (cycles >> 8);
|
||||||
prescaler = 3; // prescaler: 256
|
prescaler = 3; // prescaler: 256
|
||||||
actual_cycles = ceiling * 256L;
|
actual_cycles = ceiling * 256L;
|
||||||
} else if (cycles <= 0x3ffffffL) {
|
} else if (cycles <= 0x3ffffffL) {
|
||||||
ceiling = (cycles >> 10);
|
ceiling = (cycles >> 10);
|
||||||
prescaler = 4; // prescaler: 1024
|
prescaler = 4; // prescaler: 1024
|
||||||
actual_cycles = ceiling * 1024L;
|
actual_cycles = ceiling * 1024L;
|
||||||
} else {
|
} else {
|
||||||
// Okay, that was slower than we actually go. Just set the slowest speed
|
// Okay, that was slower than we actually go. Just set the slowest speed
|
||||||
ceiling = 0xffff;
|
ceiling = 0xffff;
|
||||||
prescaler = 4;
|
prescaler = 4;
|
||||||
actual_cycles = 0xffff * 1024;
|
actual_cycles = 0xffff * 1024;
|
||||||
}
|
}
|
||||||
// Set prescaler
|
// Set prescaler
|
||||||
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | ((prescaler+1)<<CS10);
|
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | ((prescaler+1)<<CS10);
|
||||||
// Set ceiling
|
// Set ceiling
|
||||||
OCR1A = ceiling;
|
OCR1A = ceiling;
|
||||||
return(actual_cycles);
|
return(actual_cycles);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_step_events_per_minute(uint32_t steps_per_minute) {
|
static void set_step_events_per_minute(uint32_t steps_per_minute)
|
||||||
|
{
|
||||||
if (steps_per_minute < MINIMUM_STEPS_PER_MINUTE) { steps_per_minute = MINIMUM_STEPS_PER_MINUTE; }
|
if (steps_per_minute < MINIMUM_STEPS_PER_MINUTE) { steps_per_minute = MINIMUM_STEPS_PER_MINUTE; }
|
||||||
cycles_per_step_event = config_step_timer((TICKS_PER_MICROSECOND*1000000*60)/steps_per_minute);
|
st.cycles_per_step_event = config_step_timer((TICKS_PER_MICROSECOND*1000000*60)/steps_per_minute);
|
||||||
}
|
}
|
||||||
|
|
||||||
void st_go_home()
|
// Planner external interface to start stepper interrupt and execute the blocks in queue. Called
|
||||||
|
// by the main program functions: planner auto-start and run-time command execution.
|
||||||
|
void st_cycle_start()
|
||||||
{
|
{
|
||||||
limits_go_home();
|
if (!sys.cycle_start) {
|
||||||
plan_set_current_position(0,0,0);
|
if (!sys.feed_hold) {
|
||||||
|
sys.cycle_start = true;
|
||||||
|
st_wake_up();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute a feed hold with deceleration, only during cycle. Called by main program.
|
||||||
|
void st_feed_hold()
|
||||||
|
{
|
||||||
|
if (!sys.feed_hold) {
|
||||||
|
if (sys.cycle_start) {
|
||||||
|
sys.auto_start = false; // Disable planner auto start upon feed hold.
|
||||||
|
sys.feed_hold = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reinitializes the cycle plan and stepper system after a feed hold for a resume. Called by
|
||||||
|
// runtime command execution in the main program, ensuring that the planner re-plans safely.
|
||||||
|
// NOTE: Bresenham algorithm variables are still maintained through both the planner and stepper
|
||||||
|
// cycle reinitializations. The stepper path should continue exactly as if nothing has happened.
|
||||||
|
// Only the planner de/ac-celerations profiles and stepper rates have been updated.
|
||||||
|
void st_cycle_reinitialize()
|
||||||
|
{
|
||||||
|
if (current_block != NULL) {
|
||||||
|
// Replan buffer from the feed hold stop location.
|
||||||
|
plan_cycle_reinitialize(current_block->step_event_count - st.step_events_completed);
|
||||||
|
// Update initial rate and timers after feed hold.
|
||||||
|
st.trapezoid_adjusted_rate = 0; // Resumes from rest
|
||||||
|
set_step_events_per_minute(st.trapezoid_adjusted_rate);
|
||||||
|
st.trapezoid_tick_cycle_counter = CYCLES_PER_ACCELERATION_TICK/2; // Start halfway for midpoint rule.
|
||||||
|
st.step_events_completed = 0;
|
||||||
|
}
|
||||||
|
sys.feed_hold = false; // Release feed hold. Cycle is ready to re-start.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
22
stepper.h
22
stepper.h
@ -3,6 +3,7 @@
|
|||||||
Part of Grbl
|
Part of Grbl
|
||||||
|
|
||||||
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
Copyright (c) 2009-2011 Simen Svale Skogsrud
|
||||||
|
Copyright (c) 2011 Sungeun K. Jeon
|
||||||
|
|
||||||
Grbl is free software: you can redistribute it and/or modify
|
Grbl is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -29,17 +30,22 @@
|
|||||||
#define DIRECTION_MASK ((1<<X_DIRECTION_BIT)|(1<<Y_DIRECTION_BIT)|(1<<Z_DIRECTION_BIT)) // All direction bits
|
#define DIRECTION_MASK ((1<<X_DIRECTION_BIT)|(1<<Y_DIRECTION_BIT)|(1<<Z_DIRECTION_BIT)) // All direction bits
|
||||||
#define STEPPING_MASK (STEP_MASK | DIRECTION_MASK) // All stepping-related bits (step/direction)
|
#define STEPPING_MASK (STEP_MASK | DIRECTION_MASK) // All stepping-related bits (step/direction)
|
||||||
|
|
||||||
// Initialize and start the stepper motor subsystem
|
// Initialize and setup the stepper motor subsystem
|
||||||
void st_init();
|
void st_init();
|
||||||
|
|
||||||
// Block until all buffered steps are executed
|
// Immediately disables steppers
|
||||||
void st_synchronize();
|
void st_go_idle();
|
||||||
|
|
||||||
// Execute the homing cycle
|
// Reset the stepper subsystem variables
|
||||||
void st_go_home();
|
void st_reset();
|
||||||
|
|
||||||
// The stepper subsystem goes to sleep when it runs out of things to execute. Call this
|
// Notify the stepper subsystem to start executing the g-code program in buffer.
|
||||||
// to notify the subsystem that it is time to go to work.
|
void st_cycle_start();
|
||||||
void st_wake_up();
|
|
||||||
|
// Reinitializes the buffer after a feed hold for a resume.
|
||||||
|
void st_cycle_reinitialize();
|
||||||
|
|
||||||
|
// Initiates a feed hold of the running program
|
||||||
|
void st_feed_hold();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user