From a42c03601d6ee7dc4267fce5293ad53c0500cffa Mon Sep 17 00:00:00 2001 From: Simen Svale Skogsrud Date: Sat, 27 Feb 2010 19:55:09 +0100 Subject: [PATCH] Fixed a number of bugs caused by using abs() on floats and long ints. Added support for selectively inverting bits of the stepping port. Debugged, optimized and cleaned up timing code for the step-pulses. --- Makefile | 2 +- config.h | 50 +- gcode.c | 4 +- gcode/braid_two_decimals.gcode | 2670 ++++++++++++++++++++++++++++++++ gcode/miller.gcode | 1674 ++++++++++++++++++++ geometry.c | 2 +- main.c | 4 +- motion_control.c | 33 +- script/console | 2 +- script/stream.rb | 9 +- serial_protocol.c | 7 +- stepper.c | 75 +- stepper.h | 2 +- wiring_serial.c | 2 +- 14 files changed, 4430 insertions(+), 106 deletions(-) create mode 100644 gcode/braid_two_decimals.gcode create mode 100644 gcode/miller.gcode diff --git a/Makefile b/Makefile index f430a2a..b099521 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ FUSES = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m # Tune the lines below only if you know what you are doing: -AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE) -B 10 +AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE) -B 10 -F COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE) -I. # symbolic targets: diff --git a/config.h b/config.h index 7ee1d9d..c6027b9 100644 --- a/config.h +++ b/config.h @@ -23,11 +23,13 @@ #define VERSION "0.0" -#define X_STEPS_PER_MM 128.0 -#define Y_STEPS_PER_MM 128.0 -#define Z_STEPS_PER_MM 128.0 +#define X_STEPS_PER_MM 94.488188976378 +#define Y_STEPS_PER_MM 94.488188976378 +#define Z_STEPS_PER_MM 94.488188976378 -#define INCHES_PER_MM 25.4 +#define STEP_PULSE_MICROSECONDS 30 + +#define INCHES_PER_MM (1.0/25.4) #define X_STEPS_PER_INCH X_STEPS_PER_MM*INCHES_PER_MM #define Y_STEPS_PER_INCH Y_STEPS_PER_MM*INCHES_PER_MM #define Z_STEPS_PER_INCH Z_STEPS_PER_MM*INCHES_PER_MM @@ -35,12 +37,12 @@ #define RAPID_FEEDRATE 960.0 // in millimeters per minute #define DEFAULT_FEEDRATE 960.0 -#define STEPPERS_ENABLE_DDR DDRB -#define STEPPERS_ENABLE_PORT PORTB -#define STEPPERS_ENABLE_BIT 6 +#define STEPPERS_ENABLE_DDR DDRD +#define STEPPERS_ENABLE_PORT PORTD +#define STEPPERS_ENABLE_BIT 2 -#define STEPPING_DDR DDRB -#define STEPPING_PORT PORTB +#define STEPPING_DDR DDRC +#define STEPPING_PORT PORTC #define X_STEP_BIT 0 #define Y_STEP_BIT 1 #define Z_STEP_BIT 2 @@ -48,19 +50,20 @@ #define Y_DIRECTION_BIT 4 #define Z_DIRECTION_BIT 5 -#define LIMIT_DDR DDRC -#define LIMIT_PORT PORTC -#define X_LIMIT_BIT 0 -#define Y_LIMIT_BIT 1 -#define Z_LIMIT_BIT 2 -#define SPINDLE_ENABLE_DDR DDRC -#define SPINDLE_ENABLE_PORT PORTC -#define SPINDLE_ENABLE_BIT 3 +#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_DIRECTION_DDR DDRC -#define SPINDLE_DIRECTION_PORT PORTC -#define SPINDLE_DIRECTION_BIT 4 +#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 #define BAUD_RATE 9600 @@ -69,4 +72,11 @@ #define STEPPING_MASK (STEP_MASK | DIRECTION_MASK) #define LIMIT_MASK ((1<0) { return(0); } else { diff --git a/main.c b/main.c index 879bc45..a6f509c 100644 --- a/main.c +++ b/main.c @@ -33,12 +33,14 @@ int main(void) { beginSerial(BAUD_RATE); - st_init(); + st_init(); // initialize the stepper subsystem mc_init(); // initialize motion control subsystem spindle_init(); // initialize spindle controller gc_init(); // initialize gcode-parser sp_init(); // initialize the serial protocol + st_start(); // start the stepper subsystem + for(;;){ sleep_mode(); sp_process(); // process the serial protocol diff --git a/motion_control.c b/motion_control.c index b90a26a..b9939b4 100644 --- a/motion_control.c +++ b/motion_control.c @@ -93,10 +93,10 @@ void mc_line(double x, double y, double z, float feed_rate, int invert_feed_rate target[X_AXIS] = round(x*X_STEPS_PER_MM); target[Y_AXIS] = round(y*Y_STEPS_PER_MM); - target[Z_AXIS] = round(z*Z_STEPS_PER_MM); + target[Z_AXIS] = round(z*Z_STEPS_PER_MM); // Determine direction and travel magnitude for each axis for(axis = X_AXIS; axis <= Z_AXIS; axis++) { - step_count[axis] = abs(target[axis] - position[axis]); + step_count[axis] = labs(target[axis] - position[axis]); direction[axis] = signof(target[axis] - position[axis]); } // Find the magnitude of the axis with the longest travel @@ -138,7 +138,9 @@ void mc_line(double x, double y, double z, float feed_rate, int invert_feed_rate } } } - if(step_bits) { step_steppers(step_bits); } + if(step_bits) { + step_steppers(step_bits); + } } while (step_bits); mode = MC_MODE_AT_REST; } @@ -210,11 +212,11 @@ void mc_arc(double theta, double angular_travel, double radius, double linear_tr int target_quadrant = quadrant_of_the_circle(target_x, target_y); uint32_t arc_steps=0; // Will this whole arc take place within the same quadrant? - if (start_quadrant == target_quadrant && (abs(angular_travel) <= (M_PI/2))) { + if (start_quadrant == target_quadrant && (fabs(angular_travel) <= (M_PI/2))) { if(quadrant_horizontal(start_quadrant)) { // a horizontal quadrant where x will be the primary direction - arc_steps = abs(target_x-start_x); + arc_steps = labs(target_x-start_x); } else { // a vertical quadrant where y will be the primary direction - arc_steps = abs(target_y-start_y); + arc_steps = labs(target_y-start_y); } } else { // the start and target points are in different quadrants // Lets estimate the amount of steps along half a quadrant @@ -234,7 +236,7 @@ void mc_arc(double theta, double angular_travel, double radius, double linear_tr // Set up the linear interpolation of the "depth" axis ----------------------------------------------------- - int32_t linear_steps = abs(st_millimeters_to_steps(linear_travel, axis_linear)); + int32_t linear_steps = labs(st_millimeters_to_steps(linear_travel, axis_linear)); int linear_direction = signof(linear_travel); // The number of steppings needed to trace this motion is equal to the motion that require the maximum // amount of steps: the arc or the line: @@ -247,7 +249,7 @@ void mc_arc(double theta, double angular_travel, double radius, double linear_tr // Calculate feed rate ------------------------------------------------------------------------------------- // We then calculate the millimeters of helical travel - double millimeters_of_travel = hypot(angular_travel*radius, abs(linear_travel)); + double millimeters_of_travel = hypot(angular_travel*radius, labs(linear_travel)); // Then we calculate the microseconds between each step as if we will trace the full circle. // It doesn't matter what fraction of the circle we are actually going to trace. The pace is the same. compute_and_set_step_pace(feed_rate, millimeters_of_travel, maximum_steps, invert_feed_rate); @@ -288,12 +290,12 @@ void mc_arc(double theta, double angular_travel, double radius, double linear_tr direction[axis_1] = dx; direction[axis_2] = dy; // Check which axis will be "major" for this stepping - if (abs(x)= abs(diagonal_error)) { + if(labs(error) >= labs(diagonal_error)) { y += dy; error = diagonal_error; step_bits |= diagonal_bits; // step diagonal @@ -305,7 +307,7 @@ void mc_arc(double theta, double angular_travel, double radius, double linear_tr error += 1 + 2*y * dy; y+=dy; diagonal_error = error + 1 + 2*x * dx; - if(abs(error) >= abs(diagonal_error)) { + if(labs(error) >= labs(diagonal_error)) { x += dx; error = diagonal_error; step_bits |= diagonal_bits; // step diagonal @@ -362,16 +364,7 @@ void set_stepper_directions(int8_t *direction) ((direction[Z_AXIS]&0x80)>>(7-Z_DIRECTION_BIT))); } -// Step enabled steppers. Enabled should be an array of three bytes. Each byte represent one -// stepper motor in the order X, Y, Z. Set the bytes of the steppers you want to step to -// 1, and the rest to 0. inline void step_steppers(uint8_t bits) { st_buffer_step(direction_bits | bits); } - -// Step only one motor -inline void step_axis(uint8_t axis) -{ - st_buffer_step(direction_bits | st_bit_for_stepper(axis)); -} diff --git a/script/console b/script/console index c75dc26..5d72f72 100755 --- a/script/console +++ b/script/console @@ -1,2 +1,2 @@ -socat -d -d READLINE /dev/tty.usbserial-A4001o6L,clocal=1,nonblock=1,cread=1,cs8,ixon=1,ixoff=1 +socat -d -d READLINE /dev/tty.usbserial-A9007QcR,clocal=1,nonblock=1,cread=1,cs8,ixon=1,ixoff=1 diff --git a/script/stream.rb b/script/stream.rb index d589717..2b11d24 100644 --- a/script/stream.rb +++ b/script/stream.rb @@ -24,18 +24,21 @@ if ARGV.empty? exit end -SerialPort.open('/dev/tty.usbserial-A4001o6L', 9600) do |sp| +SerialPort.open('/dev/tty.usbserial-A9007QcR', 9600) do |sp| + sp.write("\r\n\r\n"); + sleep 5 ARGV.each do |file| puts "Processing file #{file}" - prebuffer = $prebuffer ? 10 : 0 + prebuffer = $prebuffer ? 7 : 0 File.readlines(file).each do |line| next if line.strip == '' puts line.strip sp.write("#{line.strip}\r\n"); if prebuffer == 0 + sleep 0.1 begin result = sp.gets.strip - puts result unless result == '' or result == 'ok' + puts "Grbl >> #{result}" unless result == '' or result == 'ok' end while result != 'ok' else prebuffer -= 1 diff --git a/serial_protocol.c b/serial_protocol.c index 2b7545c..5a3739f 100644 --- a/serial_protocol.c +++ b/serial_protocol.c @@ -26,9 +26,9 @@ #include #include "nuts_bolts.h" -#define BLOCK_BUFFER_SIZE 128 +#define LINE_BUFFER_SIZE 128 -char line[BLOCK_BUFFER_SIZE]; +char line[LINE_BUFFER_SIZE]; uint8_t line_counter; void prompt() { @@ -79,6 +79,9 @@ void sp_process() { if((c < 32)) { // Line is complete. Then execute! line[line_counter] = 0; + // printString("->"); + // printString(line); + // printString("<-\r\n"); gc_execute_line(line); line_counter = 0; prompt(); diff --git a/stepper.c b/stepper.c index 0c0e28e..bb218dd 100644 --- a/stepper.c +++ b/stepper.c @@ -25,6 +25,7 @@ #include "stepper.h" #include "config.h" #include +#include #include "nuts_bolts.h" #include @@ -57,15 +58,14 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) // This is not a step-instruction, but a pace-change-marker: change pace config_pace_timer(next_pace); next_pace = 0; - } else { - // Set the direction pins a nanosecond or two before you step the steppers + } else { + popped ^= STEPPING_INVERT_MASK; + // Set the direction pins a cuple of nanoseconds before we step the steppers STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (popped & DIRECTION_MASK); // Then pulse the stepping pins STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | popped; - // Reset and start timer 2 which will reset the motor port after 5 microsecond - TCNT2 = 0; // reset counter - OCR2A = 5*TICKS_PER_MICROSECOND; // set the trigger time - TIMSK2 |= (1<