Skip to content

Commit

Permalink
time to save, made debug output (524 bytes) and global clock (50 byte…
Browse files Browse the repository at this point in the history
…s) optional
  • Loading branch information
triffid committed Jan 26, 2010
1 parent 098f277 commit d302329
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 95 deletions.
2 changes: 1 addition & 1 deletion mendel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ F_CPU = 16000000L
##############################################################################

ARCH = avr-
OPTIMIZE = -Os -ffunction-sections -finline-functions-called-once
OPTIMIZE = -Os -ffunction-sections -finline-functions-called-once -DDEBUG=0
# OPTIMIZE = -O0
CFLAGS = -g -Wall -Wstrict-prototypes $(OPTIMIZE) -mmcu=$(MCU_TARGET) -DF_CPU=$(F_CPU) $(DEFS) -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -save-temps
LDFLAGS = -Wl,--as-needed -Wl,--gc-sections
Expand Down
7 changes: 6 additions & 1 deletion mendel/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include "arduino.h"

// global clock
#ifdef GLOBAL_CLOCK
volatile uint32_t clock = 0;
#endif

// 1/4 second tick
uint8_t clock_counter_250ms = 0;
Expand All @@ -37,15 +39,17 @@ void clock_setup() {

ISR(TIMER2_COMPA_vect) {
// global clock
#ifdef GLOBAL_CLOCK
clock++;

#endif
// 1/4 second tick
if (++clock_counter_250ms == 250) {
clock_flag_250ms = 255;
clock_counter_250ms = 0;
}
}

#ifdef GLOBAL_CLOCK
uint32_t clock_read() {
uint32_t c;

Expand All @@ -55,3 +59,4 @@ uint32_t clock_read() {

return c;
}
#endif
3 changes: 3 additions & 0 deletions mendel/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#include <stdint.h>

void clock_setup(void);

#ifdef GLOBAL_CLOCK
uint32_t clock_read(void);
#endif

extern volatile uint8_t clock_flag_250ms;

Expand Down
78 changes: 52 additions & 26 deletions mendel/dda.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#define ABSDELTA(a, b) (((a) >= (b))?((a) - (b)):((b) - (a)))
#endif

#ifndef DEBUG
#define DEBUG 0
#endif

/*
move queue
*/
Expand Down Expand Up @@ -136,14 +140,28 @@ uint32_t abs32(int32_t v) {
return (uint32_t) (v);
}


void print_queue() {
serial_writechar('Q');
serwrite_uint8(mb_tail);
serial_writechar('/');
serwrite_uint8(mb_head);
if (queue_full())
serial_writechar('F');
if (queue_empty())
serial_writechar('E');
serial_writechar('\n');
}

/*
CREATE
*/

void dda_create(TARGET *target, DDA *dda) {
uint32_t distance;

serial_writestr_P(PSTR("\n{DDA_CREATE: ["));
if (DEBUG)
serial_writestr_P(PSTR("\n{DDA_CREATE: ["));

// we end at the passed target
memcpy(&(dda->endpoint), target, sizeof(TARGET));
Expand All @@ -154,11 +172,13 @@ void dda_create(TARGET *target, DDA *dda) {
dda->e_delta = abs32(dda->endpoint.E - startpoint.E);
dda->f_delta = abs32(dda->endpoint.F - startpoint.F);

serwrite_uint32(dda->x_delta); serial_writechar(',');
serwrite_uint32(dda->y_delta); serial_writechar(',');
serwrite_uint32(dda->z_delta); serial_writechar(',');
serwrite_uint32(dda->e_delta); serial_writechar(',');
serwrite_uint32(dda->f_delta); serial_writestr_P(PSTR("] ["));
if (DEBUG) {
serwrite_uint32(dda->x_delta); serial_writechar(',');
serwrite_uint32(dda->y_delta); serial_writechar(',');
serwrite_uint32(dda->z_delta); serial_writechar(',');
serwrite_uint32(dda->e_delta); serial_writechar(',');
serwrite_uint32(dda->f_delta); serial_writestr_P(PSTR("] ["));
}

dda->total_steps = dda->x_delta;
if (dda->y_delta > dda->total_steps)
Expand All @@ -172,7 +192,8 @@ void dda_create(TARGET *target, DDA *dda) {
if (dda->total_steps == 0)
dda->nullmove = 1;

serwrite_uint32(dda->total_steps); serial_writechar(',');
if (DEBUG)
serwrite_uint32(dda->total_steps); serial_writechar(',');

if (dda->f_delta > dda->total_steps) {
dda->f_scale = dda->f_delta / dda->total_steps;
Expand All @@ -188,7 +209,8 @@ void dda_create(TARGET *target, DDA *dda) {
dda->f_scale = 1;
}

serwrite_uint32(dda->total_steps); serial_writechar(',');
if (DEBUG)
serwrite_uint32(dda->total_steps); serial_writechar(',');

dda->x_direction = (dda->endpoint.X >= startpoint.X)?1:0;
dda->y_direction = (dda->endpoint.Y >= startpoint.Y)?1:0;
Expand All @@ -201,27 +223,29 @@ void dda_create(TARGET *target, DDA *dda) {

// since it's unusual to combine X, Y and Z changes in a single move on reprap, check if we can use simpler approximations before trying the full 3d approximation.
if (dda->z_delta == 0)
distance = approx_distance(dda->x_delta * 1000, dda->y_delta * 1000) / ((uint32_t) STEPS_PER_MM_X);
distance = approx_distance(dda->x_delta * UM_PER_STEP_X, dda->y_delta * UM_PER_STEP_Y);
else if (dda->x_delta == 0 && dda->y_delta == 0)
distance = dda->z_delta * ((uint32_t) (1000 / STEPS_PER_MM_Z));
distance = dda->z_delta * UM_PER_STEP_Z;
else
distance = approx_distance_3(dda->x_delta * ((uint32_t) (1000 * STEPS_PER_MM_Z / STEPS_PER_MM_X)), dda->y_delta * ((uint32_t) (1000 * STEPS_PER_MM_Z / STEPS_PER_MM_Y)), dda->z_delta * 1000) / ((uint32_t) STEPS_PER_MM_Z);
distance = approx_distance_3(dda->x_delta * UM_PER_STEP_X, dda->y_delta * UM_PER_STEP_Y, dda->z_delta * UM_PER_STEP_Z);

if (distance < 2)
distance = dda->e_delta * ((uint32_t) (1000 / STEPS_PER_MM_E));
distance = dda->e_delta * UM_PER_STEP_E;
if (distance < 2)
distance = dda->f_delta;

// pre-calculate move speed in millimeter microseconds per step minute for less math in interrupt context
// mm (distance) * 1000 us/ms * 60000000 us/min / step (total_steps) = mm.us per step.min
// mm (distance) * 60000000 us/min / step (total_steps) = mm.us per step.min
// note: um (distance) * 60000 == mm * 60000000
// so in the interrupt we must simply calculate
// mm.us per step.min / mm per min (F) = us per step
if (dda->total_steps > 0)
dda->move_duration = distance * 60000 / dda->total_steps;
else
dda->move_duration = 0;

serwrite_uint32(dda->move_duration); serial_writestr_P(PSTR("] }\n"));
if (DEBUG)
serwrite_uint32(dda->move_duration); serial_writestr_P(PSTR("] }\n"));

// next dda starts where we finish
memcpy(&startpoint, target, sizeof(TARGET));
Expand Down Expand Up @@ -393,25 +417,27 @@ void dda_step(DDA *dda) {
}
}

serial_writechar('[');
serwrite_hex8(step_option);
serial_writechar(':');
serwrite_uint16(dda->f_scale);
serial_writechar(',');
serwrite_int32(current_position.F);
serial_writechar('/');
serwrite_int32(dda->endpoint.F);
serial_writechar('#');
serwrite_uint32(dda->move_duration);
serial_writechar(']');
if (DEBUG) {
serial_writechar('[');
serwrite_hex8(step_option);
serial_writechar(':');
serwrite_uint16(dda->f_scale);
serial_writechar(',');
serwrite_int32(current_position.F);
serial_writechar('/');
serwrite_int32(dda->endpoint.F);
serial_writechar('#');
serwrite_uint32(dda->move_duration);
serial_writechar(']');
}
} while ( ((step_option & REAL_MOVE ) == 0) &&
((step_option & F_CAN_STEP) != 0) );

// turn off step outputs, hopefully they've been on long enough by now to register with the drivers
unstep();

// we have stepped in speed and now need to recalculate our delay
if ((step_option & REAL_MOVE) && ((step_option & F_REAL_STEP) || (dda->firstep))) {
if (((step_option & REAL_MOVE) && (step_option & F_REAL_STEP)) || (dda->firstep)) {
setTimer(dda->move_duration / current_position.F);
dda->firstep = 0;
}
Expand Down
1 change: 1 addition & 0 deletions mendel/dda.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ uint8_t queue_full(void);
uint8_t queue_empty(void);
void enqueue(TARGET *t);
void next_move(void);
void print_queue(void);

uint32_t approx_distance( uint32_t dx, uint32_t dy );
uint32_t approx_distance_3( uint32_t dx, uint32_t dy, uint32_t dz );
Expand Down
62 changes: 44 additions & 18 deletions mendel/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,64 @@
#define _MACHINE_H

/*
machine variables
move buffer size, in number of moves
note that each move takes a fair chunk of ram (71 bytes as of this writing) so don't make the buffer too big - a bigger serial readbuffer may help more than increasing this unless your gcodes are more than 70 characters long on average.
however, a larger movebuffer will probably help with lots of short moves, as each move takes a bunch of math to set up
*/

#define MOVEBUFFER_SIZE 8

/*
axis calculations, adjust as necessary
*/

#define XY_STEPS_PER_REV 1600.0
#define XY_COG_CIRCUMFERENCE (4.77 * 16.0)
#define X_STEPS_PER_REV 1600.0
#define Y_STEPS_PER_REV X_STEPS_PER_REV
// we need more speed than precision on Z, turn off microstepping
#define Z_STEPS_PER_REV 200.0
// we need more torque and smoothness at very low speeds on E, maximum microstepping
#define E_STEPS_PER_REV 3200.0

#define X_COG_CIRCUMFERENCE (4.77 * 16.0)
#define Y_COG_CIRCUMFERENCE X_COG_CIRCUMFERENCE
// also try:
// #define XY_COG_RADIUS 9.5
// #define XY_COG_CIRCUMFERENCE (XY_COG_RADIUS * PI * 2)
#define Z_GEAR_RATIO 1.0

#define EXTRUDER_STEPS_PER_REV 3200
#define EXTRUDER_SHAFT_RADIUS 5
#define EXTRUDER_INLET_DIAMETER 3
#define EXTRUDER_STEPS_PER_REV 3200.0
#define EXTRUDER_SHAFT_RADIUS 5.0
#define EXTRUDER_INLET_DIAMETER 3.0
#define EXTRUDER_NOZZLE_DIAMETER 0.8

#define STEPS_PER_MM_X ((XY_STEPS_PER_REV / XY_COG_CIRCUMFERENCE) + 0.5)
#define STEPS_PER_MM_Y ((XY_STEPS_PER_REV / XY_COG_CIRCUMFERENCE) + 0.5)
#define STEPS_PER_MM_Z (200L)
#define STEPS_PER_MM_E (EXTRUDER_STEPS_PER_REV * EXTRUDER_SHAFT_RADIUS * PI * EXTRUDER_INLET_DIAMETER / EXTRUDER_NOZZLE_DIAMETER)
#define STEPS_PER_MM_X ((uint32_t) ((X_STEPS_PER_REV / X_COG_CIRCUMFERENCE) + 0.5))
#define STEPS_PER_MM_Y ((uint32_t) ((Y_STEPS_PER_REV / Y_COG_CIRCUMFERENCE) + 0.5))
#define STEPS_PER_MM_Z ((uint32_t) ((Z_STEPS_PER_REV * Z_GEAR_RATIO) + 0.5))

// http://blog.arcol.hu/?p=157 may help with this next one
// I haven't tuned this at all- it's just a placeholder until I read the above carefully enough
// does this refer to filament or extrudate? extrudate depends on layer thickness.. hm
#define STEPS_PER_MM_E ((uint32_t) ((EXTRUDER_STEPS_PER_REV / (EXTRUDER_SHAFT_RADIUS * PI * EXTRUDER_INLET_DIAMETER / EXTRUDER_NOZZLE_DIAMETER)) + 0.5))

#define FEEDRATE_FAST_XY 2400
#define FEEDRATE_SLOW_XY 120
#define FEEDRATE_FAST_Z 240
#define FEEDRATE_SLOW_Z 12

#define FEEDRATE_FAST_XY 2400
#define FEEDRATE_SLOW_XY 120
#define FEEDRATE_FAST_Z 240
#define FEEDRATE_SLOW_Z 12
#define E_STARTSTOP_STEPS 20
#define FEEDRATE_FAST_E 1200

/*
calculated values - you shouldn't need to touch these
however feel free to put in your own values if they can be more precise than the calculated approximations, remembering that they must end up being integers- floating point by preprocessor only thanks!
*/

#define E_STARTSTOP_STEPS 20
#define FEEDRATE_FAST_E 1200
#define UM_PER_STEP_X ((uint32_t) ((1000.0 / STEPS_PER_MM_X) + 0.5))
#define UM_PER_STEP_Y ((uint32_t) ((1000.0 / STEPS_PER_MM_Y) + 0.5))
#define UM_PER_STEP_Z ((uint32_t) ((1000.0 / STEPS_PER_MM_Z) + 0.5))
#define UM_PER_STEP_E ((uint32_t) ((1000.0 / STEPS_PER_MM_E) + 0.5))

/*
should be the same for all machines ;)
should be the same for all machines! ;)
*/
#define PI 3.1415926535

Expand Down
Loading

0 comments on commit d302329

Please sign in to comment.