Skip to content

Commit

Permalink
merging: testing all features, sensors working (after grounding the s…
Browse files Browse the repository at this point in the history
…hield of the wire), multiple offsets, homing, stopping and resuming, ...
  • Loading branch information
Stefan Hechenberger committed Feb 13, 2012
1 parent 47ba7cf commit b9afa2d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 75 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Grbl - An embedded g-code interpreter and motion-controller for the Arduino/AVR3
For more information [on Grbl](https://github.com/simen/grbl)


TODO
------
- g55 wrong offset
- homing cycle cannot recover out of bounds when limit already triggering

mbed merger notes
------------------
- removed
Expand Down
7 changes: 5 additions & 2 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#define SENSE_DDR DDRD
#define SENSE_PORT PORTD
#define SENSE_PIN PIND
#define POWER_BIT 2
#define CHILLER_BIT 3
#define DOOR_BIT 5
Expand Down Expand Up @@ -107,8 +108,10 @@

#define CHAR_XOFF '\x13'
#define CHAR_XON '\x11'
#define CHAR_STOP '\x03'
#define CHAR_RESUME '\x02'
// #define CHAR_STOP '\x03'
// #define CHAR_RESUME '\x02'
#define CHAR_STOP '!'
#define CHAR_RESUME '~'

#define X_AXIS 0
#define Y_AXIS 1
Expand Down
30 changes: 21 additions & 9 deletions gcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#define STATUS_FLOATING_POINT_ERROR 4
#define STATUS_STOP_STATE_ERROR 5

#define OFFSET_G54 0
#define OFFSET_G55 1

#define BUFFER_LINE_SIZE 80
char rx_line[BUFFER_LINE_SIZE];

Expand Down Expand Up @@ -85,13 +88,13 @@ void gcode_init() {
gc.nominal_laser_intensity = 0U;
gc.offselect = 0; // default to G54 coordinate system
// prime G54 cs
// refine with "G10 L2 P1 X_ Y_ Z_"
// refine with "G10 L2 P0 X_ Y_ Z_"
gc.offsets[X_AXIS] = CONFIG_X_ORIGIN_OFFSET;
gc.offsets[Y_AXIS] = CONFIG_Y_ORIGIN_OFFSET;
gc.offsets[Z_AXIS] = CONFIG_Z_ORIGIN_OFFSET;
// prime G55 cs
// refine with "G10 L2 P2 X_ Y_ Z_"
// or set to any current location with "G10 L20 P2"
// refine with "G10 L2 P1 X_ Y_ Z_"
// or set to any current location with "G10 L20 P1"
gc.offsets[3+X_AXIS] = CONFIG_X_ORIGIN_OFFSET;
gc.offsets[3+Y_AXIS] = CONFIG_Y_ORIGIN_OFFSET;
gc.offsets[3+Z_AXIS] = CONFIG_Z_ORIGIN_OFFSET;
Expand Down Expand Up @@ -218,8 +221,8 @@ uint8_t gcode_execute_line(char *line) {
case 20: gc.inches_mode = true; break;
case 21: gc.inches_mode = false; break;
case 30: next_action = NEXT_ACTION_HOMING_CYCLE; break;
case 54: gc.offselect = 0; break;
case 55: gc.offselect = 1; break;
case 54: gc.offselect = OFFSET_G54; break;
case 55: gc.offselect = OFFSET_G55; break;
case 90: gc.absolute_mode = true; break;
case 91: gc.absolute_mode = false; break;
default: FAIL(STATUS_UNSUPPORTED_STATEMENT);
Expand Down Expand Up @@ -327,19 +330,28 @@ uint8_t gcode_execute_line(char *line) {
clear_vector(target);
stepper_set_position(0.0, 0.0, 0.0);
planner_set_position(0.0, 0.0, 0.0);
// move head to g54 offset
gc.offselect = OFFSET_G54;
target[X_AXIS] = 0;
target[Y_AXIS] = 0;
target[Z_AXIS] = 0;
planner_line( target[X_AXIS] + gc.offsets[3*gc.offselect+X_AXIS],
target[Y_AXIS] + gc.offsets[3*gc.offselect+Y_AXIS],
target[Z_AXIS] + gc.offsets[3*gc.offselect+Z_AXIS],
gc.seek_rate, 0 );
break;
case NEXT_ACTION_SET_COORDINATE_OFFSET:
if (cs == 0 || cs == 1) { // corresponds to G54, G55
if (cs == OFFSET_G54 || cs == OFFSET_G55) {
if (l == 2) {
//set offset to target, eg: G10 L2 P1 X15 Y15 Z0
gc.offsets[3*cs+X_AXIS] = target[X_AXIS];
gc.offsets[3*cs+Y_AXIS] = target[Y_AXIS];
gc.offsets[3*cs+Z_AXIS] = target[Z_AXIS];
} else if (l == 20) {
// set offset to current pos, eg: G10 L20 P2
gc.offsets[3*cs+X_AXIS] = gc.position[X_AXIS];
gc.offsets[3*cs+Y_AXIS] = gc.position[X_AXIS];
gc.offsets[3*cs+Z_AXIS] = gc.position[X_AXIS];
gc.offsets[3*cs+X_AXIS] = gc.position[X_AXIS] + gc.offsets[3*gc.offselect+X_AXIS];
gc.offsets[3*cs+Y_AXIS] = gc.position[Y_AXIS] + gc.offsets[3*gc.offselect+Y_AXIS];
gc.offsets[3*cs+Z_AXIS] = gc.position[Z_AXIS] + gc.offsets[3*gc.offselect+Z_AXIS];
}
}
break;
Expand Down
4 changes: 2 additions & 2 deletions sense_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
void sense_init() {
//// power, chiller, door
SENSE_DDR &= ~(SENSE_MASK); // set as input pins
// LIMIT_PORT |= LIMIT_MASK; //activate pull-up resistors
// SENSE_PORT |= SENSE_MASK; //activate pull-up resistors

//// x1_lmit, x2_limit, y1_limit, y2_limit
LIMIT_DDR &= ~(LIMIT_MASK); // set as input pins
LIMIT_PORT |= LIMIT_MASK; //activate pull-up resistors
// LIMIT_PORT |= LIMIT_MASK; //activate pull-up resistors
}


Expand Down
18 changes: 9 additions & 9 deletions sense_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@


void sense_init();
#define SENSE_POWER ((SENSE_PORT >> POWER_BIT) & 1)
#define SENSE_CHILLER ((SENSE_PORT >> CHILLER_BIT) & 1)
#define SENSE_DOOR ((SENSE_PORT >> DOOR_BIT) & 1)
#define SENSE_X1_LIMIT ((SENSE_PORT >> X1_LIMIT_BIT) & 1)
#define SENSE_X2_LIMIT ((SENSE_PORT >> X2_LIMIT_BIT) & 1)
#define SENSE_Y1_LIMIT ((SENSE_PORT >> Y1_LIMIT_BIT) & 1)
#define SENSE_Y2_LIMIT (SENSE_PORT >> Y2_LIMIT_BIT) & 1
#define SENSE_LIMITS ((LIMIT_PORT & LIMIT_MASK) > 0)
#define SENSE_ANY (((LIMIT_PORT & LIMIT_MASK) > 0) && ((SENSE_PORT & SENSE_MASK) > 0))
#define SENSE_POWER_OFF !((SENSE_PIN >> POWER_BIT) & 1)
#define SENSE_CHILLER_OFF !((SENSE_PIN >> CHILLER_BIT) & 1)
#define SENSE_DOOR_OPEN !((SENSE_PIN >> DOOR_BIT) & 1)
#define SENSE_X1_LIMIT !((LIMIT_PIN >> X1_LIMIT_BIT) & 1)
#define SENSE_X2_LIMIT !((LIMIT_PIN >> X2_LIMIT_BIT) & 1)
#define SENSE_Y1_LIMIT !((LIMIT_PIN >> Y1_LIMIT_BIT) & 1)
#define SENSE_Y2_LIMIT !((LIMIT_PIN >> Y2_LIMIT_BIT) & 1)
#define SENSE_LIMITS (SENSE_X1_LIMIT || SENSE_X2_LIMIT || SENSE_Y1_LIMIT || SENSE_Y2_LIMIT)
#define SENSE_ANY (SENSE_LIMITS || SENSE_POWER_OFF || SENSE_CHILLER_OFF || SENSE_DOOR_OPEN)


void control_init();
Expand Down
107 changes: 54 additions & 53 deletions stepper.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,15 @@ ISR(TIMER1_COMPA_vect) {
}

if (SENSE_ANY) {
// printString("sense_any\n");
// no power (e-stop), no chiller, door open, limit switch situation
if (SENSE_POWER || SENSE_CHILLER || SENSE_LIMITS) {
if (SENSE_POWER_OFF || SENSE_CHILLER_OFF || SENSE_LIMITS) {
// printString("sense\n");
// stop program
stepper_request_stop();
busy = false;
return;
} else {
} else if (SENSE_DOOR_OPEN) {
// printString("door\n");
// door open -> simply suspend processing
busy = false;
return;
Expand Down Expand Up @@ -447,66 +448,66 @@ static void adjust_speed( uint32_t steps_per_minute ) {

static void homing_cycle(bool x_axis, bool y_axis, bool z_axis, bool reverse_direction, uint32_t microseconds_per_pulse) {

// uint32_t step_delay = microseconds_per_pulse - CONFIG_PULSE_MICROSECONDS;
// uint8_t out_bits = DIRECTION_MASK;
// uint8_t limit_bits;
//
// if (x_axis) { out_bits |= (1<<X_STEP_BIT); }
// if (y_axis) { out_bits |= (1<<Y_STEP_BIT); }
// if (z_axis) { out_bits |= (1<<Z_STEP_BIT); }
//
// // Invert direction bits if this is a reverse homing_cycle
// if (reverse_direction) {
// out_bits ^= DIRECTION_MASK;
// }
//
// // Apply the global invert mask
// out_bits ^= INVERT_MASK;
//
// // Set direction pins
// STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK);
//
// for(;;) {
// limit_bits = LIMIT_PIN;
// if (reverse_direction) {
// // Invert limit_bits if this is a reverse homing_cycle
// limit_bits ^= LIMIT_MASK;
// }
// if (x_axis && !(limit_bits & (1<<X1_LIMIT_BIT))) {
// x_axis = false;
// out_bits ^= (1<<X_STEP_BIT);
// }
// if (y_axis && !(limit_bits & (1<<Y1_LIMIT_BIT))) {
// y_axis = false;
// out_bits ^= (1<<Y_STEP_BIT);
// }
// // if (z_axis && !(limit_bits & (1<<Z1_LIMIT_BIT))) {
// // z_axis = false;
// // out_bits ^= (1<<Z_STEP_BIT);
// // }
// if(x_axis || y_axis || z_axis) {
// // step all axes still in out_bits
// STEPPING_PORT |= out_bits & STEPPING_MASK;
// _delay_us(CONFIG_PULSE_MICROSECONDS);
// STEPPING_PORT ^= out_bits & STEPPING_MASK;
// _delay_us(step_delay);
// } else {
// return;
// }
// }
// return;
uint32_t step_delay = microseconds_per_pulse - CONFIG_PULSE_MICROSECONDS;
uint8_t out_bits = DIRECTION_MASK;
uint8_t limit_bits;

if (x_axis) { out_bits |= (1<<X_STEP_BIT); }
if (y_axis) { out_bits |= (1<<Y_STEP_BIT); }
if (z_axis) { out_bits |= (1<<Z_STEP_BIT); }

// Invert direction bits if this is a reverse homing_cycle
if (reverse_direction) {
out_bits ^= DIRECTION_MASK;
}

// Apply the global invert mask
out_bits ^= INVERT_MASK;

// Set direction pins
STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK);

for(;;) {
limit_bits = LIMIT_PIN;
if (reverse_direction) {
// Invert limit_bits if this is a reverse homing_cycle
limit_bits ^= LIMIT_MASK;
}
if (x_axis && !(limit_bits & (1<<X1_LIMIT_BIT))) {
x_axis = false;
out_bits ^= (1<<X_STEP_BIT);
}
if (y_axis && !(limit_bits & (1<<Y1_LIMIT_BIT))) {
y_axis = false;
out_bits ^= (1<<Y_STEP_BIT);
}
// if (z_axis && !(limit_bits & (1<<Z1_LIMIT_BIT))) {
// z_axis = false;
// out_bits ^= (1<<Z_STEP_BIT);
// }
if(x_axis || y_axis || z_axis) {
// step all axes still in out_bits
STEPPING_PORT |= out_bits & STEPPING_MASK;
_delay_us(CONFIG_PULSE_MICROSECONDS);
STEPPING_PORT ^= out_bits & STEPPING_MASK;
_delay_us(step_delay);
} else {
return;
}
}
return;
}

static void approach_limit_switch(bool x, bool y, bool z) {
homing_cycle(x, y, z,false, 1000);
}

static void leave_limit_switch(bool x, bool y, bool z) {
homing_cycle(x, y, z, true, 100000);
homing_cycle(x, y, z, true, 10000);
}

void stepper_homing_cycle() {
stepper_synchronize();
stepper_synchronize();
// home the x and y axis
approach_limit_switch(true, true, false);
leave_limit_switch(true, true, false);
Expand Down

0 comments on commit b9afa2d

Please sign in to comment.