smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

For documentation, license, ..., please check http://smoothieware.org/

This version has been tested with a 3 axis machine

Committer:
scachat
Date:
Tue Jul 31 21:11:18 2012 +0000
Revision:
0:31e91bb0ef3c
smoothie port to mbed online compiler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scachat 0:31e91bb0ef3c 1 /*
scachat 0:31e91bb0ef3c 2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl) with additions from Sungeun K. Jeon (https://github.com/chamnit/grbl)
scachat 0:31e91bb0ef3c 3 Smoothie 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.
scachat 0:31e91bb0ef3c 4 Smoothie 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.
scachat 0:31e91bb0ef3c 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
scachat 0:31e91bb0ef3c 6 */
scachat 0:31e91bb0ef3c 7
scachat 0:31e91bb0ef3c 8 using namespace std;
scachat 0:31e91bb0ef3c 9 #include <vector>
scachat 0:31e91bb0ef3c 10 #include "libs/nuts_bolts.h"
scachat 0:31e91bb0ef3c 11 #include "libs/RingBuffer.h"
scachat 0:31e91bb0ef3c 12 #include "../communication/utils/Gcode.h"
scachat 0:31e91bb0ef3c 13 #include "libs/Module.h"
scachat 0:31e91bb0ef3c 14 #include "libs/Kernel.h"
scachat 0:31e91bb0ef3c 15 #include "Block.h"
scachat 0:31e91bb0ef3c 16 #include "Planner.h"
scachat 0:31e91bb0ef3c 17 #include "Player.h"
scachat 0:31e91bb0ef3c 18
scachat 0:31e91bb0ef3c 19
scachat 0:31e91bb0ef3c 20 Planner::Planner(){
scachat 0:31e91bb0ef3c 21 clear_vector(this->position);
scachat 0:31e91bb0ef3c 22 clear_vector_double(this->previous_unit_vec);
scachat 0:31e91bb0ef3c 23 this->previous_nominal_speed = 0.0;
scachat 0:31e91bb0ef3c 24 this->has_deleted_block = false;
scachat 0:31e91bb0ef3c 25 }
scachat 0:31e91bb0ef3c 26
scachat 0:31e91bb0ef3c 27 void Planner::on_module_loaded(){
scachat 0:31e91bb0ef3c 28 this->on_config_reload(this);
scachat 0:31e91bb0ef3c 29 }
scachat 0:31e91bb0ef3c 30
scachat 0:31e91bb0ef3c 31 void Planner::on_config_reload(void* argument){
scachat 0:31e91bb0ef3c 32 this->acceleration = this->kernel->config->value(acceleration_checksum )->by_default(100 )->as_number();
scachat 0:31e91bb0ef3c 33 this->max_jerk = this->kernel->config->value(max_jerk_checksum )->by_default(100 )->as_number();
scachat 0:31e91bb0ef3c 34 this->junction_deviation = this->kernel->config->value(junction_deviation_checksum )->by_default(0.05)->as_number();
scachat 0:31e91bb0ef3c 35 }
scachat 0:31e91bb0ef3c 36
scachat 0:31e91bb0ef3c 37
scachat 0:31e91bb0ef3c 38 // Append a block to the queue, compute it's speed factors
scachat 0:31e91bb0ef3c 39 void Planner::append_block( int target[], double feed_rate, double distance, double deltas[] ){
scachat 0:31e91bb0ef3c 40
scachat 0:31e91bb0ef3c 41 // Stall here if the queue is ful
scachat 0:31e91bb0ef3c 42 this->kernel->player->wait_for_queue(2);
scachat 0:31e91bb0ef3c 43
scachat 0:31e91bb0ef3c 44 Block* block = this->kernel->player->new_block();
scachat 0:31e91bb0ef3c 45 block->planner = this;
scachat 0:31e91bb0ef3c 46
scachat 0:31e91bb0ef3c 47 // Direction bits
scachat 0:31e91bb0ef3c 48 block->direction_bits = 0;
scachat 0:31e91bb0ef3c 49 for( int stepper=ALPHA_STEPPER; stepper<=GAMMA_STEPPER; stepper++){
scachat 0:31e91bb0ef3c 50 if( target[stepper] < position[stepper] ){ block->direction_bits |= (1<<stepper); }
scachat 0:31e91bb0ef3c 51 }
scachat 0:31e91bb0ef3c 52
scachat 0:31e91bb0ef3c 53 // Number of steps for each stepper
scachat 0:31e91bb0ef3c 54 for( int stepper=ALPHA_STEPPER; stepper<=GAMMA_STEPPER; stepper++){ block->steps[stepper] = labs(target[stepper] - this->position[stepper]); }
scachat 0:31e91bb0ef3c 55
scachat 0:31e91bb0ef3c 56 // Max number of steps, for all axes
scachat 0:31e91bb0ef3c 57 block->steps_event_count = max( block->steps[ALPHA_STEPPER], max( block->steps[BETA_STEPPER], block->steps[GAMMA_STEPPER] ) );
scachat 0:31e91bb0ef3c 58 //if( block->steps_event_count == 0 ){ this->computing = false; return; }
scachat 0:31e91bb0ef3c 59
scachat 0:31e91bb0ef3c 60 block->millimeters = distance;
scachat 0:31e91bb0ef3c 61 double inverse_millimeters = 0;
scachat 0:31e91bb0ef3c 62 if( distance > 0 ){ inverse_millimeters = 1.0/distance; }
scachat 0:31e91bb0ef3c 63
scachat 0:31e91bb0ef3c 64 // Calculate speed in mm/minute for each axis. No divide by zero due to previous checks.
scachat 0:31e91bb0ef3c 65 // NOTE: Minimum stepper speed is limited by MINIMUM_STEPS_PER_MINUTE in stepper.c
scachat 0:31e91bb0ef3c 66 double inverse_minute = feed_rate * inverse_millimeters;
scachat 0:31e91bb0ef3c 67 if( distance > 0 ){
scachat 0:31e91bb0ef3c 68 block->nominal_speed = block->millimeters * inverse_minute; // (mm/min) Always > 0
scachat 0:31e91bb0ef3c 69 block->nominal_rate = ceil(block->steps_event_count * inverse_minute); // (step/min) Always > 0
scachat 0:31e91bb0ef3c 70 }else{
scachat 0:31e91bb0ef3c 71 block->nominal_speed = 0;
scachat 0:31e91bb0ef3c 72 block->nominal_rate = 0;
scachat 0:31e91bb0ef3c 73 }
scachat 0:31e91bb0ef3c 74
scachat 0:31e91bb0ef3c 75 //this->kernel->serial->printf("nom_speed: %f nom_rate: %u step_event_count: %u block->steps_z: %u \r\n", block->nominal_speed, block->nominal_rate, block->steps_event_count, block->steps[2] );
scachat 0:31e91bb0ef3c 76
scachat 0:31e91bb0ef3c 77 // Compute the acceleration rate for the trapezoid generator. Depending on the slope of the line
scachat 0:31e91bb0ef3c 78 // average travel per step event changes. For a line along one axis the travel per step event
scachat 0:31e91bb0ef3c 79 // is equal to the travel/step in the particular axis. For a 45 degree line the steppers of both
scachat 0:31e91bb0ef3c 80 // axes might step for every step event. Travel per step event is then sqrt(travel_x^2+travel_y^2).
scachat 0:31e91bb0ef3c 81 // To generate trapezoids with contant acceleration between blocks the rate_delta must be computed
scachat 0:31e91bb0ef3c 82 // specifically for each line to compensate for this phenomenon:
scachat 0:31e91bb0ef3c 83 // Convert universal acceleration for direction-dependent stepper rate change parameter
scachat 0:31e91bb0ef3c 84 block->rate_delta = ceil( block->steps_event_count*inverse_millimeters * this->acceleration*60.0 / this->kernel->stepper->acceleration_ticks_per_second ); // (step/min/acceleration_tick)
scachat 0:31e91bb0ef3c 85
scachat 0:31e91bb0ef3c 86 // Compute path unit vector
scachat 0:31e91bb0ef3c 87 double unit_vec[3];
scachat 0:31e91bb0ef3c 88 unit_vec[X_AXIS] = deltas[X_AXIS]*inverse_millimeters;
scachat 0:31e91bb0ef3c 89 unit_vec[Y_AXIS] = deltas[Y_AXIS]*inverse_millimeters;
scachat 0:31e91bb0ef3c 90 unit_vec[Z_AXIS] = deltas[Z_AXIS]*inverse_millimeters;
scachat 0:31e91bb0ef3c 91
scachat 0:31e91bb0ef3c 92 // Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
scachat 0:31e91bb0ef3c 93 // Let a circle be tangent to both previous and current path line segments, where the junction
scachat 0:31e91bb0ef3c 94 // deviation is defined as the distance from the junction to the closest edge of the circle,
scachat 0:31e91bb0ef3c 95 // colinear with the circle center. The circular segment joining the two paths represents the
scachat 0:31e91bb0ef3c 96 // path of centripetal acceleration. Solve for max velocity based on max acceleration about the
scachat 0:31e91bb0ef3c 97 // radius of the circle, defined indirectly by junction deviation. This may be also viewed as
scachat 0:31e91bb0ef3c 98 // path width or max_jerk in the previous grbl version. This approach does not actually deviate
scachat 0:31e91bb0ef3c 99 // from path, but used as a robust way to compute cornering speeds, as it takes into account the
scachat 0:31e91bb0ef3c 100 // nonlinearities of both the junction angle and junction velocity.
scachat 0:31e91bb0ef3c 101 double vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
scachat 0:31e91bb0ef3c 102
scachat 0:31e91bb0ef3c 103 if (this->kernel->player->queue.size() > 1 && (this->previous_nominal_speed > 0.0)) {
scachat 0:31e91bb0ef3c 104 // Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
scachat 0:31e91bb0ef3c 105 // NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
scachat 0:31e91bb0ef3c 106 double cos_theta = - this->previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
scachat 0:31e91bb0ef3c 107 - this->previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
scachat 0:31e91bb0ef3c 108 - this->previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS] ;
scachat 0:31e91bb0ef3c 109
scachat 0:31e91bb0ef3c 110 // Skip and use default max junction speed for 0 degree acute junction.
scachat 0:31e91bb0ef3c 111 if (cos_theta < 0.95) {
scachat 0:31e91bb0ef3c 112 vmax_junction = min(this->previous_nominal_speed,block->nominal_speed);
scachat 0:31e91bb0ef3c 113 // Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
scachat 0:31e91bb0ef3c 114 if (cos_theta > -0.95) {
scachat 0:31e91bb0ef3c 115 // Compute maximum junction velocity based on maximum acceleration and junction deviation
scachat 0:31e91bb0ef3c 116 double sin_theta_d2 = sqrt(0.5*(1.0-cos_theta)); // Trig half angle identity. Always positive.
scachat 0:31e91bb0ef3c 117 vmax_junction = min(vmax_junction,
scachat 0:31e91bb0ef3c 118 sqrt(this->acceleration*60*60 * this->junction_deviation * sin_theta_d2/(1.0-sin_theta_d2)) );
scachat 0:31e91bb0ef3c 119 }
scachat 0:31e91bb0ef3c 120 }
scachat 0:31e91bb0ef3c 121 }
scachat 0:31e91bb0ef3c 122 block->max_entry_speed = vmax_junction;
scachat 0:31e91bb0ef3c 123
scachat 0:31e91bb0ef3c 124 // Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
scachat 0:31e91bb0ef3c 125 double v_allowable = this->max_allowable_speed(-this->acceleration,0.0,block->millimeters); //TODO: Get from config
scachat 0:31e91bb0ef3c 126 block->entry_speed = min(vmax_junction, v_allowable);
scachat 0:31e91bb0ef3c 127
scachat 0:31e91bb0ef3c 128 // Initialize planner efficiency flags
scachat 0:31e91bb0ef3c 129 // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
scachat 0:31e91bb0ef3c 130 // If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
scachat 0:31e91bb0ef3c 131 // the current block and next block junction speeds are guaranteed to always be at their maximum
scachat 0:31e91bb0ef3c 132 // junction speeds in deceleration and acceleration, respectively. This is due to how the current
scachat 0:31e91bb0ef3c 133 // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
scachat 0:31e91bb0ef3c 134 // the reverse and forward planners, the corresponding block junction speed will always be at the
scachat 0:31e91bb0ef3c 135 // the maximum junction speed and may always be ignored for any speed reduction checks.
scachat 0:31e91bb0ef3c 136 if (block->nominal_speed <= v_allowable) { block->nominal_length_flag = true; }
scachat 0:31e91bb0ef3c 137 else { block->nominal_length_flag = false; }
scachat 0:31e91bb0ef3c 138 block->recalculate_flag = true; // Always calculate trapezoid for new block
scachat 0:31e91bb0ef3c 139
scachat 0:31e91bb0ef3c 140 // Update previous path unit_vector and nominal speed
scachat 0:31e91bb0ef3c 141 memcpy(this->previous_unit_vec, unit_vec, sizeof(unit_vec)); // previous_unit_vec[] = unit_vec[]
scachat 0:31e91bb0ef3c 142 this->previous_nominal_speed = block->nominal_speed;
scachat 0:31e91bb0ef3c 143
scachat 0:31e91bb0ef3c 144 // Update current position
scachat 0:31e91bb0ef3c 145 memcpy(this->position, target, sizeof(int)*3);
scachat 0:31e91bb0ef3c 146
scachat 0:31e91bb0ef3c 147 // Math-heavy re-computing of the whole queue to take the new
scachat 0:31e91bb0ef3c 148 this->recalculate();
scachat 0:31e91bb0ef3c 149
scachat 0:31e91bb0ef3c 150 // The block can now be used
scachat 0:31e91bb0ef3c 151 block->ready();
scachat 0:31e91bb0ef3c 152
scachat 0:31e91bb0ef3c 153 }
scachat 0:31e91bb0ef3c 154
scachat 0:31e91bb0ef3c 155
scachat 0:31e91bb0ef3c 156 // Recalculates the motion plan according to the following algorithm:
scachat 0:31e91bb0ef3c 157 //
scachat 0:31e91bb0ef3c 158 // 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_factor)
scachat 0:31e91bb0ef3c 159 // so that:
scachat 0:31e91bb0ef3c 160 // a. The junction jerk is within the set limit
scachat 0:31e91bb0ef3c 161 // b. No speed reduction within one block requires faster deceleration than the one, true constant
scachat 0:31e91bb0ef3c 162 // acceleration.
scachat 0:31e91bb0ef3c 163 // 2. Go over every block in chronological order and dial down junction speed reduction values if
scachat 0:31e91bb0ef3c 164 // a. The speed increase within one block would require faster accelleration than the one, true
scachat 0:31e91bb0ef3c 165 // constant acceleration.
scachat 0:31e91bb0ef3c 166 //
scachat 0:31e91bb0ef3c 167 // When these stages are complete all blocks have an entry_factor that will allow all speed changes to
scachat 0:31e91bb0ef3c 168 // be performed using only the one, true constant acceleration, and where no junction jerk is jerkier than
scachat 0:31e91bb0ef3c 169 // the set limit. Finally it will:
scachat 0:31e91bb0ef3c 170 //
scachat 0:31e91bb0ef3c 171 // 3. Recalculate trapezoids for all blocks.
scachat 0:31e91bb0ef3c 172 //
scachat 0:31e91bb0ef3c 173 void Planner::recalculate() {
scachat 0:31e91bb0ef3c 174 //this->kernel->serial->printf("recalculate last: %p, queue size: %d \r\n", this->kernel->player->queue.get_ref( this->kernel->player->queue.size()-1 ), this->kernel->player->queue.size() );
scachat 0:31e91bb0ef3c 175 this->reverse_pass();
scachat 0:31e91bb0ef3c 176 this->forward_pass();
scachat 0:31e91bb0ef3c 177 this->recalculate_trapezoids();
scachat 0:31e91bb0ef3c 178 }
scachat 0:31e91bb0ef3c 179
scachat 0:31e91bb0ef3c 180 // Planner::recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
scachat 0:31e91bb0ef3c 181 // implements the reverse pass.
scachat 0:31e91bb0ef3c 182 void Planner::reverse_pass(){
scachat 0:31e91bb0ef3c 183 // For each block
scachat 0:31e91bb0ef3c 184 int block_index = this->kernel->player->queue.tail;
scachat 0:31e91bb0ef3c 185 Block* blocks[3] = {NULL,NULL,NULL};
scachat 0:31e91bb0ef3c 186
scachat 0:31e91bb0ef3c 187 while(block_index!=this->kernel->player->queue.head){
scachat 0:31e91bb0ef3c 188 block_index = this->kernel->player->queue.prev_block_index( block_index );
scachat 0:31e91bb0ef3c 189 blocks[2] = blocks[1];
scachat 0:31e91bb0ef3c 190 blocks[1] = blocks[0];
scachat 0:31e91bb0ef3c 191 blocks[0] = &this->kernel->player->queue.buffer[block_index];
scachat 0:31e91bb0ef3c 192 if( blocks[1] == NULL ){ continue; }
scachat 0:31e91bb0ef3c 193 blocks[1]->reverse_pass(blocks[2], blocks[0]);
scachat 0:31e91bb0ef3c 194 }
scachat 0:31e91bb0ef3c 195
scachat 0:31e91bb0ef3c 196 }
scachat 0:31e91bb0ef3c 197
scachat 0:31e91bb0ef3c 198 // Planner::recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
scachat 0:31e91bb0ef3c 199 // implements the forward pass.
scachat 0:31e91bb0ef3c 200 void Planner::forward_pass() {
scachat 0:31e91bb0ef3c 201 // For each block
scachat 0:31e91bb0ef3c 202 int block_index = this->kernel->player->queue.head;
scachat 0:31e91bb0ef3c 203 Block* blocks[3] = {NULL,NULL,NULL};
scachat 0:31e91bb0ef3c 204
scachat 0:31e91bb0ef3c 205 while(block_index!=this->kernel->player->queue.tail){
scachat 0:31e91bb0ef3c 206 blocks[0] = blocks[1];
scachat 0:31e91bb0ef3c 207 blocks[1] = blocks[2];
scachat 0:31e91bb0ef3c 208 blocks[2] = &this->kernel->player->queue.buffer[block_index];
scachat 0:31e91bb0ef3c 209 if( blocks[0] == NULL ){ continue; }
scachat 0:31e91bb0ef3c 210 blocks[1]->forward_pass(blocks[0],blocks[2]);
scachat 0:31e91bb0ef3c 211 block_index = this->kernel->player->queue.next_block_index( block_index );
scachat 0:31e91bb0ef3c 212 }
scachat 0:31e91bb0ef3c 213 blocks[2]->forward_pass(blocks[1],NULL);
scachat 0:31e91bb0ef3c 214
scachat 0:31e91bb0ef3c 215 }
scachat 0:31e91bb0ef3c 216
scachat 0:31e91bb0ef3c 217 // Recalculates the trapezoid speed profiles for flagged blocks in the plan according to the
scachat 0:31e91bb0ef3c 218 // entry_speed for each junction and the entry_speed of the next junction. Must be called by
scachat 0:31e91bb0ef3c 219 // planner_recalculate() after updating the blocks. Any recalulate flagged junction will
scachat 0:31e91bb0ef3c 220 // compute the two adjacent trapezoids to the junction, since the junction speed corresponds
scachat 0:31e91bb0ef3c 221 // to exit speed and entry speed of one another.
scachat 0:31e91bb0ef3c 222 void Planner::recalculate_trapezoids() {
scachat 0:31e91bb0ef3c 223 int block_index = this->kernel->player->queue.head;
scachat 0:31e91bb0ef3c 224 Block* current;
scachat 0:31e91bb0ef3c 225 Block* next = NULL;
scachat 0:31e91bb0ef3c 226
scachat 0:31e91bb0ef3c 227 while(block_index != this->kernel->player->queue.tail){
scachat 0:31e91bb0ef3c 228 current = next;
scachat 0:31e91bb0ef3c 229 next = &this->kernel->player->queue.buffer[block_index];
scachat 0:31e91bb0ef3c 230 //this->kernel->serial->printf("index:%d current:%p next:%p \r\n", block_index, current, next );
scachat 0:31e91bb0ef3c 231 if( current ){
scachat 0:31e91bb0ef3c 232 // Recalculate if current block entry or exit junction speed has changed.
scachat 0:31e91bb0ef3c 233 if( current->recalculate_flag || next->recalculate_flag ){
scachat 0:31e91bb0ef3c 234 current->calculate_trapezoid( current->entry_speed/current->nominal_speed, next->entry_speed/current->nominal_speed );
scachat 0:31e91bb0ef3c 235 current->recalculate_flag = false;
scachat 0:31e91bb0ef3c 236 }
scachat 0:31e91bb0ef3c 237 }
scachat 0:31e91bb0ef3c 238 block_index = this->kernel->player->queue.next_block_index( block_index );
scachat 0:31e91bb0ef3c 239 }
scachat 0:31e91bb0ef3c 240
scachat 0:31e91bb0ef3c 241 // Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated.
scachat 0:31e91bb0ef3c 242 next->calculate_trapezoid( next->entry_speed/next->nominal_speed, MINIMUM_PLANNER_SPEED/next->nominal_speed); //TODO: Make configuration option
scachat 0:31e91bb0ef3c 243 next->recalculate_flag = false;
scachat 0:31e91bb0ef3c 244
scachat 0:31e91bb0ef3c 245 }
scachat 0:31e91bb0ef3c 246
scachat 0:31e91bb0ef3c 247 // Debug function
scachat 0:31e91bb0ef3c 248 void Planner::dump_queue(){
scachat 0:31e91bb0ef3c 249 for( int index = 0; index <= this->kernel->player->queue.size()-1; index++ ){
scachat 0:31e91bb0ef3c 250 if( index > 10 && index < this->kernel->player->queue.size()-10 ){ continue; }
scachat 0:31e91bb0ef3c 251 this->kernel->serial->printf("block %03d > ", index);
scachat 0:31e91bb0ef3c 252 this->kernel->player->queue.get_ref(index)->debug(this->kernel);
scachat 0:31e91bb0ef3c 253 }
scachat 0:31e91bb0ef3c 254 }
scachat 0:31e91bb0ef3c 255
scachat 0:31e91bb0ef3c 256 // Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
scachat 0:31e91bb0ef3c 257 // acceleration within the allotted distance.
scachat 0:31e91bb0ef3c 258 double Planner::max_allowable_speed(double acceleration, double target_velocity, double distance) {
scachat 0:31e91bb0ef3c 259 return(
scachat 0:31e91bb0ef3c 260 sqrt(target_velocity*target_velocity-2L*acceleration*60*60*distance) //Was acceleration*60*60*distance, in case this breaks, but here we prefer to use seconds instead of minutes
scachat 0:31e91bb0ef3c 261 );
scachat 0:31e91bb0ef3c 262 }
scachat 0:31e91bb0ef3c 263
scachat 0:31e91bb0ef3c 264