From 4103e6ca000c0b6a5a2666077075d8ab83a5433d Mon Sep 17 00:00:00 2001 From: Simen Svale Skogsrud Date: Sat, 22 Jan 2011 23:29:02 +0100 Subject: [PATCH] still not running, but a lot further along --- config.c | 13 +++--- config.h | 4 +- main.c | 4 +- script/console | 2 +- serial_protocol.c | 3 +- stepper.c | 88 +++++++++++++++++------------------- stepper_plan.c | 111 +++++++++++++++++++++++++++++++++++----------- stepper_plan.h | 10 +++-- 8 files changed, 142 insertions(+), 93 deletions(-) diff --git a/config.c b/config.c index 7f058ab..6f41dd0 100644 --- a/config.c +++ b/config.c @@ -49,11 +49,10 @@ void dump_settings() { printPgmString(PSTR(" (microseconds step pulse)\r\n$4 = ")); printFloat(settings.default_feed_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^2 max acceleration)\r\n$7 = ")); printFloat(settings.acceleration); - printPgmString(PSTR(" (mm/arc segment)\r\n$8 = ")); 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(")\r\n$9 = ")); printFloat(settings.acceleration); - printPgmString(PSTR(" (acceleration in mm/min^2)\r\n$10 = ")); printFloat(settings.max_jerk); + printPgmString(PSTR(")\r\n$8 = ")); printFloat(settings.acceleration); + printPgmString(PSTR(" (acceleration in mm/sec^2)\r\n$9 = ")); printFloat(settings.max_jerk); printPgmString(PSTR(" (max instant cornering speed change in delta mm/min)")); printPgmString(PSTR("\r\n'$x=value' to set parameter or just '$' to dump current settings\r\n")); } @@ -83,9 +82,9 @@ void store_setting(int parameter, double value) { case 4: settings.default_feed_rate = value; break; case 5: settings.default_seek_rate = value; break; case 6: settings.mm_per_arc_segment = value; break; - case 7: settings.acceleration = value; break; - case 8: settings.invert_mask = trunc(value); break; - case 9: settings.acceleration = fabs(value); break; + case 7: settings.invert_mask = trunc(value); break; + case 8: settings.acceleration = value; break; + case 9: settings.max_jerk = fabs(value); break; default: printPgmString(PSTR("Unknown parameter\r\n")); return; diff --git a/config.h b/config.h index 20f2e57..e21d265 100644 --- a/config.h +++ b/config.h @@ -21,7 +21,7 @@ #ifndef config_h #define config_h -#define VERSION "0.51" +#define VERSION "0.6b" #include #include @@ -110,7 +110,7 @@ void store_setting(int parameter, double value); // #define STEPPING_INVERT_MASK (STEP_MASK | (1< // -// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates for +// The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates until +// // block->accelerate_ticks by block->rate_delta each tick, then stays up for block->plateau_ticks and // decelerates for the rest of the block until the trapezoid generator is reset for the next block. // The slope of acceleration is always +/- block->rate_delta. Any stage may be skipped by setting the // duration to 0 ticks. -#define TRAPEZOID_STAGE_ACCELERATING 0 -#define TRAPEZOID_STAGE_PLATEAU 1 -#define TRAPEZOID_STAGE_DECELERATING 2 -uint8_t trapezoid_stage; -uint16_t trapezoid_stage_ticks; -uint32_t trapezoid_rate; -int16_t trapezoid_delta; - // Initializes the trapezoid generator from the current block. Called whenever a new // block begins. -inline void reset_trapezoid_generator() { - trapezoid_stage = TRAPEZOID_STAGE_ACCELERATING; - trapezoid_stage_ticks = current_block->accelerate_ticks; - trapezoid_delta = current_block->rate_delta; +inline void reset_trapezoid_generator() { trapezoid_rate = current_block->initial_rate; set_step_events_per_minute(trapezoid_rate); } @@ -92,37 +86,31 @@ inline void reset_trapezoid_generator() { // interrupt. It can be assumed that the trapezoid-generator-parameters and the // current_block stays untouched by outside handlers for the duration of this function call. inline void trapezoid_generator_tick() { - if (trapezoid_stage_ticks) { - trapezoid_stage_ticks--; - if (trapezoid_delta) { - trapezoid_rate += trapezoid_delta; + PORTD ^= (1<<2); + if (current_block) { + if (step_event_count < current_block->accelerate_until) { + trapezoid_rate += current_block->rate_delta; set_step_events_per_minute(trapezoid_rate); - } - } else { - // Is there a block currently in execution? - if(!current_block) {return;} - // Trapezoid stage complete, move on - if(trapezoid_stage == TRAPEZOID_STAGE_ACCELERATING) { - // Progress to plateau stage - trapezoid_delta = 0; - trapezoid_stage_ticks = current_block->plateau_ticks; - trapezoid_stage = TRAPEZOID_STAGE_PLATEAU; - } else if (trapezoid_stage == TRAPEZOID_STAGE_PLATEAU) { - // Progress to deceleration stage - trapezoid_delta = -current_block->rate_delta; - trapezoid_stage_ticks = 0xffff; // "forever" until the block is complete - trapezoid_stage = TRAPEZOID_STAGE_DECELERATING; + } else if (step_event_count > current_block->decelerate_after) { + trapezoid_rate -= current_block->rate_delta; + set_step_events_per_minute(trapezoid_rate); + } else { + printInteger(trapezoid_rate); + while(1){}; } } + PORTD ^= (1<<2); } // Add a new linear movement to the buffer. steps_x, _y and _z is the signed, relative motion in // steps. Microseconds specify how many microseconds the move should take to perform. To aid acceleration // calculation the caller must also provide the physical length of the line in millimeters. void st_buffer_line(int32_t steps_x, int32_t steps_y, int32_t steps_z, uint32_t microseconds, double millimeters) { + PORTD ^= (1<<2); plan_buffer_line(steps_x, steps_y, steps_z, microseconds, millimeters); // Ensure that block processing is running by enabling The Stepper Driver Interrupt ENABLE_STEPPER_DRIVER_INTERRUPT(); + PORTD ^= (1<<2); } // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. It is executed at the rate set with @@ -135,7 +123,6 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) #endif { if(busy){ return; } // The busy-flag is used to avoid reentering this interrupt - // Set the direction pins a cuple of nanoseconds before we step the steppers STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK); // Then pulse the stepping pins @@ -159,7 +146,8 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) counter_x = -(current_block->step_event_count >> 1); counter_y = counter_x; counter_z = counter_x; - iterations = current_block->step_event_count; + step_events_left = current_block->step_event_count; + step_event_count = 0; } else { DISABLE_STEPPER_DRIVER_INTERRUPT(); } @@ -183,8 +171,8 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) counter_z -= current_block->step_event_count; } // If current block is finished, reset pointer - iterations -= 1; - if (iterations <= 0) { + step_events_left -= 1; step_event_count += 1; + if (step_events_left <= 0) { current_block = NULL; // move the block buffer tail to the next instruction block_buffer_tail = (block_buffer_tail + 1) % BLOCK_BUFFER_SIZE; @@ -241,12 +229,17 @@ void st_init() TCCR2A = 0; // Normal operation TCCR2B = (1<> 3; prescaler = 1; // prescaler: 8 - actual_cycles = ceiling * 8; + actual_cycles = ceiling * 8L; } else if (cycles <= 0x3fffffL) { ceiling = cycles >> 6; prescaler = 2; // prescaler: 64 - actual_cycles = ceiling * 64; + actual_cycles = ceiling * 64L; } else if (cycles <= 0xffffffL) { ceiling = (cycles >> 8); prescaler = 3; // prescaler: 256 - actual_cycles = ceiling * 256; + actual_cycles = ceiling * 256L; } else if (cycles <= 0x3ffffffL) { ceiling = (cycles >> 10); prescaler = 4; // prescaler: 1024 - actual_cycles = ceiling * 1024; + actual_cycles = ceiling * 1024L; } else { // Okay, that was slower than we actually go. Just set the slowest speed ceiling = 0xffff; @@ -306,6 +299,7 @@ uint32_t config_step_timer(uint32_t cycles) } 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; } cycles_per_step_event = config_step_timer((TICKS_PER_MICROSECOND*1000000*60)/steps_per_minute); } diff --git a/stepper_plan.c b/stepper_plan.c index d96aed6..5920ff8 100644 --- a/stepper_plan.c +++ b/stepper_plan.c @@ -26,50 +26,73 @@ #include "nuts_bolts.h" #include "stepper.h" #include "config.h" +#include "wiring_serial.h" struct Block block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions volatile int block_buffer_head; // Index of the next block to be pushed volatile int block_buffer_tail; // Index of the block to process now uint8_t acceleration_management; // Acceleration management active? -inline uint32_t estimate_acceleration_distance(int32_t current_rate, int32_t target_rate, int32_t acceleration) { - return((target_rate*target_rate-current_rate*current_rate)/(2*acceleration)); + + +// The distance it takes to accelerate from initial_rate to target_rate using the given acceleration +inline double estimate_acceleration_distance(double initial_rate, double target_rate, double acceleration) { + return((target_rate*target_rate-initial_rate*initial_rate)/(2L*acceleration)); } -inline uint32_t estimate_acceleration_ticks(int32_t start_rate, int32_t acceleration_per_tick, int32_t step_events) { - return( - round( - (sqrt(2*acceleration_per_tick*step_events+(start_rate*start_rate))-start_rate)/ - acceleration_per_tick)); +// 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 +// 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) +/* + + + <- some rate that must be < maximum allowable rate + /|\ + / | \ + / | + <- final_rate + / | | + initial_rate -> +----+--+ + 0 ^ ^ + | | + result distance +*/ +inline double intersection_distance(double initial_rate, double final_rate, double acceleration, double distance) { + return((2*acceleration*distance-initial_rate*initial_rate+final_rate*final_rate)/(4*acceleration)); } +// See bottom of this module for a comment outlining the reasoning behind the mathematics behind the +// preceding functions. + // Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors. // In practice both factors must be in the range 0 ... 1.0 void calculate_trapezoid_for_block(struct Block *block, double entry_factor, double exit_factor) { block->initial_rate = round(block->nominal_rate*entry_factor); int32_t final_rate = round(block->nominal_rate*entry_factor); int32_t acceleration_per_second = block->rate_delta*ACCELERATION_TICKS_PER_SECOND; - int32_t acceleration_steps = - estimate_acceleration_distance(block->initial_rate, block->nominal_rate, acceleration_per_second); - int32_t decelleration_steps = + int32_t accelerate_steps = + round(estimate_acceleration_distance(block->initial_rate, block->nominal_rate, acceleration_per_second)); + int32_t decelerate_steps = estimate_acceleration_distance(block->nominal_rate, final_rate, -acceleration_per_second); + printString("ir="); printInteger(block->initial_rate); printString("\n\r"); + printString("nr="); printInteger(block->nominal_rate); printString("\n\r"); + printString("rd="); printInteger(block->rate_delta); printString("\n\r"); + printString("aps="); printInteger(acceleration_per_second); printString("\n\r"); + printString("acs="); printInteger(accelerate_steps); printString("\n\r"); + printString("dcs="); printInteger(decelerate_steps); printString("\n\r"); + printString("ts="); printInteger(block->step_event_count); printString("\n\r"); // Check if the acceleration and decelleration periods overlap. In that case nominal_speed will // never be reached but that's okay. Just truncate both periods proportionally so that they // fit within the allotted step events. - int32_t plateau_steps = block->step_event_count-acceleration_steps-decelleration_steps; - if (plateau_steps < 0) { - int32_t half_overlap_region = fabs(plateau_steps)/2; + int32_t plateau_steps = block->step_event_count-accelerate_steps-decelerate_steps; + if (plateau_steps < 0) { + accelerate_steps = round( + intersection_distance(block->initial_rate, final_rate, acceleration_per_second, block->step_event_count)); plateau_steps = 0; - acceleration_steps = max(acceleration_steps-half_overlap_region,0); - decelleration_steps = max(decelleration_steps-half_overlap_region,0); - } - block->accelerate_ticks = estimate_acceleration_ticks(block->initial_rate, block->rate_delta, acceleration_steps); - if (plateau_steps) { - block->plateau_ticks = round(1.0*plateau_steps/(block->nominal_rate*ACCELERATION_TICKS_PER_SECOND)); - } else { - block->plateau_ticks = 0; - } -} + printString("No plateau, so: acs="); printInteger(accelerate_steps); printString("\n\r"); + } + block->accelerate_until = accelerate_steps; + block->decelerate_after = accelerate_steps+plateau_steps; +} inline double estimate_max_speed(double max_acceleration, double target_velocity, double distance) { return(sqrt(-2*max_acceleration*distance+target_velocity*target_velocity)); @@ -185,15 +208,17 @@ void plan_buffer_line(int32_t steps_x, int32_t steps_y, int32_t steps_z, uint32_ // 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 // specifically for each line to compensate for this phenomenon: - double travel_per_step = (1.0*millimeters)/block->step_event_count; + double travel_per_step = millimeters/block->step_event_count; + printString("travel_per_step*10000="); + printInteger(travel_per_step*10000);printString("\n\r"); block->rate_delta = round( - (settings.acceleration/(60.0*ACCELERATION_TICKS_PER_SECOND))/ // acceleration mm/min per acceleration_tick + ((settings.acceleration*60.0)/(ACCELERATION_TICKS_PER_SECOND))/ // acceleration mm/sec/sec per acceleration_tick travel_per_step); // convert to: acceleration steps/min/acceleration_tick if (acceleration_management) { calculate_trapezoid_for_block(block,0,0); // compute a conservative acceleration trapezoid for now } else { - block->accelerate_ticks = 0; - block->plateau_ticks = 0; + block->accelerate_until = 0; + block->decelerate_after = 0; block->rate_delta = 0; } @@ -206,3 +231,35 @@ void plan_buffer_line(int32_t steps_x, int32_t steps_y, int32_t steps_z, uint32_ block_buffer_head = next_buffer_head; } + +/* + Mathematica reasoning behind the mathematics in this module: + + 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) +*/ + diff --git a/stepper_plan.h b/stepper_plan.h index f9cb78c..46f7fa5 100644 --- a/stepper_plan.h +++ b/stepper_plan.h @@ -44,10 +44,12 @@ struct Block { double nominal_speed; // The nominal speed for this block in mm/min double millimeters; double entry_factor; // The factors representing the change in speed at the start of the trapezoid - uint32_t initial_rate; // The jerk-adjusted step rate at start of block - int16_t rate_delta; // The steps/minute to add or subtract when changing speed (must be positive) - uint16_t accelerate_ticks; // The number of acceleration-ticks to accelerate - uint16_t plateau_ticks; // The number of acceleration-ticks to maintain top speed + + // Settings for the trapezoid generator + uint32_t initial_rate; // The jerk-adjusted step rate at start of block + 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 decelerate_after; // The index of the step event on which to start decelerating }; extern struct Block block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions