![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
smoothie port to mbed online compiler (smoothieware.org)
For documentation, license, ..., please check http://smoothieware.org/
This version has been tested with a 3 axis machine
modules/tools/extruder/Extruder.cpp@0:31e91bb0ef3c, 2012-07-31 (annotated)
- 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?
User | Revision | Line number | New 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). |
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 | |
scachat | 0:31e91bb0ef3c | 9 | #include "libs/Module.h" |
scachat | 0:31e91bb0ef3c | 10 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 11 | #include "modules/robot/Player.h" |
scachat | 0:31e91bb0ef3c | 12 | #include "modules/robot/Block.h" |
scachat | 0:31e91bb0ef3c | 13 | #include "modules/tools/extruder/Extruder.h" |
scachat | 0:31e91bb0ef3c | 14 | |
scachat | 0:31e91bb0ef3c | 15 | #define extruder_step_pin_checksum 40763 |
scachat | 0:31e91bb0ef3c | 16 | #define extruder_dir_pin_checksum 57277 |
scachat | 0:31e91bb0ef3c | 17 | #define extruder_en_pin_checksum 8017 |
scachat | 0:31e91bb0ef3c | 18 | |
scachat | 0:31e91bb0ef3c | 19 | Extruder::Extruder() { |
scachat | 0:31e91bb0ef3c | 20 | this->absolute_mode = true; |
scachat | 0:31e91bb0ef3c | 21 | this->direction = 1; |
scachat | 0:31e91bb0ef3c | 22 | this->acceleration_lock = false; |
scachat | 0:31e91bb0ef3c | 23 | this->step_counter = 0; |
scachat | 0:31e91bb0ef3c | 24 | this->counter_increment = 0; |
scachat | 0:31e91bb0ef3c | 25 | this->paused = false; |
scachat | 0:31e91bb0ef3c | 26 | } |
scachat | 0:31e91bb0ef3c | 27 | |
scachat | 0:31e91bb0ef3c | 28 | void Extruder::on_module_loaded() { |
scachat | 0:31e91bb0ef3c | 29 | |
scachat | 0:31e91bb0ef3c | 30 | // Do not do anything if not enabledd |
scachat | 0:31e91bb0ef3c | 31 | if( this->kernel->config->value( extruder_module_enable_checksum )->by_default(false)->as_bool() == false ){ return; } |
scachat | 0:31e91bb0ef3c | 32 | |
scachat | 0:31e91bb0ef3c | 33 | // Settings |
scachat | 0:31e91bb0ef3c | 34 | this->on_config_reload(this); |
scachat | 0:31e91bb0ef3c | 35 | |
scachat | 0:31e91bb0ef3c | 36 | // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one |
scachat | 0:31e91bb0ef3c | 37 | this->register_for_event(ON_BLOCK_BEGIN); |
scachat | 0:31e91bb0ef3c | 38 | this->register_for_event(ON_BLOCK_END); |
scachat | 0:31e91bb0ef3c | 39 | this->register_for_event(ON_GCODE_EXECUTE); |
scachat | 0:31e91bb0ef3c | 40 | this->register_for_event(ON_PLAY); |
scachat | 0:31e91bb0ef3c | 41 | this->register_for_event(ON_PAUSE); |
scachat | 0:31e91bb0ef3c | 42 | |
scachat | 0:31e91bb0ef3c | 43 | // Start values |
scachat | 0:31e91bb0ef3c | 44 | this->start_position = 0; |
scachat | 0:31e91bb0ef3c | 45 | this->target_position = 0; |
scachat | 0:31e91bb0ef3c | 46 | this->current_position = 0; |
scachat | 0:31e91bb0ef3c | 47 | this->current_block = NULL; |
scachat | 0:31e91bb0ef3c | 48 | this->mode = OFF; |
scachat | 0:31e91bb0ef3c | 49 | |
scachat | 0:31e91bb0ef3c | 50 | // Update speed every *acceleration_ticks_per_second* |
scachat | 0:31e91bb0ef3c | 51 | // TODO: Make this an independent setting |
scachat | 0:31e91bb0ef3c | 52 | this->kernel->slow_ticker->attach( this->kernel->stepper->acceleration_ticks_per_second , this, &Extruder::acceleration_tick ); |
scachat | 0:31e91bb0ef3c | 53 | |
scachat | 0:31e91bb0ef3c | 54 | // Initiate main_interrupt timer and step reset timer |
scachat | 0:31e91bb0ef3c | 55 | this->kernel->step_ticker->attach( this, &Extruder::stepping_tick ); |
scachat | 0:31e91bb0ef3c | 56 | this->kernel->step_ticker->reset_attach( this, &Extruder::reset_step_pin ); |
scachat | 0:31e91bb0ef3c | 57 | |
scachat | 0:31e91bb0ef3c | 58 | } |
scachat | 0:31e91bb0ef3c | 59 | |
scachat | 0:31e91bb0ef3c | 60 | // Get config |
scachat | 0:31e91bb0ef3c | 61 | void Extruder::on_config_reload(void* argument){ |
scachat | 0:31e91bb0ef3c | 62 | this->microseconds_per_step_pulse = this->kernel->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number(); |
scachat | 0:31e91bb0ef3c | 63 | this->steps_per_millimeter = this->kernel->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number(); |
scachat | 0:31e91bb0ef3c | 64 | this->feed_rate = this->kernel->config->value(default_feed_rate_checksum )->by_default(1)->as_number(); |
scachat | 0:31e91bb0ef3c | 65 | this->acceleration = this->kernel->config->value(acceleration_checksum )->by_default(1)->as_number(); |
scachat | 0:31e91bb0ef3c | 66 | |
scachat | 0:31e91bb0ef3c | 67 | this->step_pin = this->kernel->config->value(extruder_step_pin_checksum )->by_default("1.22" )->as_pin()->as_output(); |
scachat | 0:31e91bb0ef3c | 68 | this->dir_pin = this->kernel->config->value(extruder_dir_pin_checksum )->by_default("1.19" )->as_pin()->as_output(); |
scachat | 0:31e91bb0ef3c | 69 | this->en_pin = this->kernel->config->value(extruder_en_pin_checksum )->by_default("0.19" )->as_pin()->as_output(); |
scachat | 0:31e91bb0ef3c | 70 | } |
scachat | 0:31e91bb0ef3c | 71 | |
scachat | 0:31e91bb0ef3c | 72 | |
scachat | 0:31e91bb0ef3c | 73 | // When the play/pause button is set to pause, or a module calls the ON_PAUSE event |
scachat | 0:31e91bb0ef3c | 74 | void Extruder::on_pause(void* argument){ |
scachat | 0:31e91bb0ef3c | 75 | this->paused = true; |
scachat | 0:31e91bb0ef3c | 76 | } |
scachat | 0:31e91bb0ef3c | 77 | |
scachat | 0:31e91bb0ef3c | 78 | // When the play/pause button is set to play, or a module calls the ON_PLAY event |
scachat | 0:31e91bb0ef3c | 79 | void Extruder::on_play(void* argument){ |
scachat | 0:31e91bb0ef3c | 80 | this->paused = false; |
scachat | 0:31e91bb0ef3c | 81 | } |
scachat | 0:31e91bb0ef3c | 82 | |
scachat | 0:31e91bb0ef3c | 83 | |
scachat | 0:31e91bb0ef3c | 84 | |
scachat | 0:31e91bb0ef3c | 85 | // Compute extrusion speed based on parameters and gcode distance of travel |
scachat | 0:31e91bb0ef3c | 86 | void Extruder::on_gcode_execute(void* argument){ |
scachat | 0:31e91bb0ef3c | 87 | Gcode* gcode = static_cast<Gcode*>(argument); |
scachat | 0:31e91bb0ef3c | 88 | |
scachat | 0:31e91bb0ef3c | 89 | // Absolute/relative mode |
scachat | 0:31e91bb0ef3c | 90 | if( gcode->has_letter('M')){ |
scachat | 0:31e91bb0ef3c | 91 | int code = (int) gcode->get_value('M'); |
scachat | 0:31e91bb0ef3c | 92 | if( code == 82 ){ this->absolute_mode = true; } |
scachat | 0:31e91bb0ef3c | 93 | if( code == 83 ){ this->absolute_mode = false; } |
scachat | 0:31e91bb0ef3c | 94 | if( code == 84 ){ this->en_pin->set(0); } |
scachat | 0:31e91bb0ef3c | 95 | } |
scachat | 0:31e91bb0ef3c | 96 | |
scachat | 0:31e91bb0ef3c | 97 | // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude |
scachat | 0:31e91bb0ef3c | 98 | this->mode = OFF; |
scachat | 0:31e91bb0ef3c | 99 | |
scachat | 0:31e91bb0ef3c | 100 | if( gcode->has_letter('G') ){ |
scachat | 0:31e91bb0ef3c | 101 | // G92: Reset extruder position |
scachat | 0:31e91bb0ef3c | 102 | if( gcode->get_value('G') == 92 ){ |
scachat | 0:31e91bb0ef3c | 103 | if( gcode->has_letter('E') ){ |
scachat | 0:31e91bb0ef3c | 104 | this->current_position = gcode->get_value('E'); |
scachat | 0:31e91bb0ef3c | 105 | this->target_position = this->current_position; |
scachat | 0:31e91bb0ef3c | 106 | this->start_position = this->current_position; |
scachat | 0:31e91bb0ef3c | 107 | } |
scachat | 0:31e91bb0ef3c | 108 | }else{ |
scachat | 0:31e91bb0ef3c | 109 | // Extrusion length from 'G' Gcode |
scachat | 0:31e91bb0ef3c | 110 | if( gcode->has_letter('E' )){ |
scachat | 0:31e91bb0ef3c | 111 | // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position ) |
scachat | 0:31e91bb0ef3c | 112 | double relative_extrusion_distance = gcode->get_value('E'); |
scachat | 0:31e91bb0ef3c | 113 | if( this->absolute_mode == true ){ relative_extrusion_distance = relative_extrusion_distance - this->target_position; } |
scachat | 0:31e91bb0ef3c | 114 | |
scachat | 0:31e91bb0ef3c | 115 | // If the robot is moving, we follow it's movement, otherwise, we move alone |
scachat | 0:31e91bb0ef3c | 116 | if( fabs(gcode->millimeters_of_travel) < 0.0001 ){ // With floating numbers, we can have 0 != 0 ... beeeh |
scachat | 0:31e91bb0ef3c | 117 | this->mode = SOLO; |
scachat | 0:31e91bb0ef3c | 118 | this->travel_distance = relative_extrusion_distance; |
scachat | 0:31e91bb0ef3c | 119 | if( gcode->has_letter('F') ){ this->feed_rate = gcode->get_value('F'); } |
scachat | 0:31e91bb0ef3c | 120 | }else{ |
scachat | 0:31e91bb0ef3c | 121 | this->mode = FOLLOW; |
scachat | 0:31e91bb0ef3c | 122 | // We move proportionally to the robot's movement |
scachat | 0:31e91bb0ef3c | 123 | this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel; |
scachat | 0:31e91bb0ef3c | 124 | } |
scachat | 0:31e91bb0ef3c | 125 | |
scachat | 0:31e91bb0ef3c | 126 | this->en_pin->set(1); |
scachat | 0:31e91bb0ef3c | 127 | } |
scachat | 0:31e91bb0ef3c | 128 | } |
scachat | 0:31e91bb0ef3c | 129 | } |
scachat | 0:31e91bb0ef3c | 130 | |
scachat | 0:31e91bb0ef3c | 131 | } |
scachat | 0:31e91bb0ef3c | 132 | |
scachat | 0:31e91bb0ef3c | 133 | // When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing ) |
scachat | 0:31e91bb0ef3c | 134 | void Extruder::on_block_begin(void* argument){ |
scachat | 0:31e91bb0ef3c | 135 | Block* block = static_cast<Block*>(argument); |
scachat | 0:31e91bb0ef3c | 136 | if( this->mode == SOLO ){ |
scachat | 0:31e91bb0ef3c | 137 | // In solo mode we take the block so we can move even if the stepper has nothing to do |
scachat | 0:31e91bb0ef3c | 138 | block->take(); |
scachat | 0:31e91bb0ef3c | 139 | this->current_block = block; |
scachat | 0:31e91bb0ef3c | 140 | this->start_position = this->target_position; |
scachat | 0:31e91bb0ef3c | 141 | this->target_position = this->start_position + this->travel_distance ; |
scachat | 0:31e91bb0ef3c | 142 | this->travel_ratio = 0.2; // TODO : Make a real acceleration thing |
scachat | 0:31e91bb0ef3c | 143 | if( this->target_position > this->current_position ){ this->direction = 1; }else if( this->target_position < this->current_position ){ this->direction = -1; } |
scachat | 0:31e91bb0ef3c | 144 | this->set_speed(int(floor((this->feed_rate/60)*this->steps_per_millimeter)));//Speed in steps per second |
scachat | 0:31e91bb0ef3c | 145 | }else if( this->mode == FOLLOW ){ |
scachat | 0:31e91bb0ef3c | 146 | // In non-solo mode, we just follow the stepper module |
scachat | 0:31e91bb0ef3c | 147 | this->current_block = block; |
scachat | 0:31e91bb0ef3c | 148 | this->start_position = this->target_position; |
scachat | 0:31e91bb0ef3c | 149 | this->target_position = this->start_position + ( this->current_block->millimeters * this->travel_ratio ); |
scachat | 0:31e91bb0ef3c | 150 | if( this->target_position > this->current_position ){ this->direction = 1; }else if( this->target_position < this->current_position ){ this->direction = -1; } |
scachat | 0:31e91bb0ef3c | 151 | this->acceleration_tick(0); |
scachat | 0:31e91bb0ef3c | 152 | } |
scachat | 0:31e91bb0ef3c | 153 | |
scachat | 0:31e91bb0ef3c | 154 | } |
scachat | 0:31e91bb0ef3c | 155 | |
scachat | 0:31e91bb0ef3c | 156 | // When a block ends, pause the stepping interrupt |
scachat | 0:31e91bb0ef3c | 157 | void Extruder::on_block_end(void* argument){ |
scachat | 0:31e91bb0ef3c | 158 | Block* block = static_cast<Block*>(argument); |
scachat | 0:31e91bb0ef3c | 159 | this->current_block = NULL; |
scachat | 0:31e91bb0ef3c | 160 | } |
scachat | 0:31e91bb0ef3c | 161 | |
scachat | 0:31e91bb0ef3c | 162 | // Called periodically to change the speed to match acceleration or to match the speed of the robot |
scachat | 0:31e91bb0ef3c | 163 | uint32_t Extruder::acceleration_tick(uint32_t dummy){ |
scachat | 0:31e91bb0ef3c | 164 | |
scachat | 0:31e91bb0ef3c | 165 | // Avoid trying to work when we really shouldn't ( between blocks or re-entry ) |
scachat | 0:31e91bb0ef3c | 166 | if( this->current_block == NULL || this->acceleration_lock || this->paused ){ return 0; } |
scachat | 0:31e91bb0ef3c | 167 | this->acceleration_lock = true; |
scachat | 0:31e91bb0ef3c | 168 | |
scachat | 0:31e91bb0ef3c | 169 | // In solo mode, we mode independently from the robot |
scachat | 0:31e91bb0ef3c | 170 | if( this->mode == SOLO ){ |
scachat | 0:31e91bb0ef3c | 171 | // TODO : Do real acceleration here |
scachat | 0:31e91bb0ef3c | 172 | this->travel_ratio += 0.03; |
scachat | 0:31e91bb0ef3c | 173 | if( this->travel_ratio > 1 ){ this->travel_ratio = 1; } |
scachat | 0:31e91bb0ef3c | 174 | this->set_speed( int(floor(((this->feed_rate/60)*this->steps_per_millimeter)*this->travel_ratio)) ); // Speed in steps per second |
scachat | 0:31e91bb0ef3c | 175 | |
scachat | 0:31e91bb0ef3c | 176 | // In follow mode we match the speed of the robot, + eventually advance |
scachat | 0:31e91bb0ef3c | 177 | }else if( this->mode == FOLLOW ){ |
scachat | 0:31e91bb0ef3c | 178 | Stepper* stepper = this->kernel->stepper; // Just for convenience |
scachat | 0:31e91bb0ef3c | 179 | |
scachat | 0:31e91bb0ef3c | 180 | // Strategy : |
scachat | 0:31e91bb0ef3c | 181 | // * Find where in the block will the stepper be at the next tick ( if the block will have ended then, don't change speed ) |
scachat | 0:31e91bb0ef3c | 182 | // * Find what position this is for us |
scachat | 0:31e91bb0ef3c | 183 | // * Find what speed we must go at to be at that position for the next acceleration tick |
scachat | 0:31e91bb0ef3c | 184 | // TODO : This works, but PLEASE PLEASE PLEASE if you know a better way to do it, do it better, I don't find this elegant at all, it's just the best I could think of |
scachat | 0:31e91bb0ef3c | 185 | // UPDATE: Yes, this sucks, I have ideas on how to do it better. If this is really bugging you, open a ticket and I'll make it a priority |
scachat | 0:31e91bb0ef3c | 186 | |
scachat | 0:31e91bb0ef3c | 187 | int ticks_forward = 3; |
scachat | 0:31e91bb0ef3c | 188 | // We need to take those values here, and then use those instead of the live values, because using the live values inside the loop can break things ( infinite loops etc ... ) |
scachat | 0:31e91bb0ef3c | 189 | double next_stepper_rate = stepper->trapezoid_adjusted_rate; |
scachat | 0:31e91bb0ef3c | 190 | double step_events_completed = (double(double(stepper->step_events_completed)/double(1<<16))); |
scachat | 0:31e91bb0ef3c | 191 | double position = ( this->current_position - this->start_position ) * this->direction ; |
scachat | 0:31e91bb0ef3c | 192 | double length = fabs( this->start_position - this->target_position ); |
scachat | 0:31e91bb0ef3c | 193 | double last_ratio = -1; |
scachat | 0:31e91bb0ef3c | 194 | |
scachat | 0:31e91bb0ef3c | 195 | // Do the startegy above, but if it does not work, look a bit further and try again, and again ... |
scachat | 0:31e91bb0ef3c | 196 | while(1){ |
scachat | 0:31e91bb0ef3c | 197 | |
scachat | 0:31e91bb0ef3c | 198 | // Find the position where we should be at the next tick |
scachat | 0:31e91bb0ef3c | 199 | double next_ratio = double( step_events_completed + ( next_stepper_rate / 60 / ((double(stepper->acceleration_ticks_per_second)/ticks_forward)) ) ) / double( this->current_block->steps_event_count ); |
scachat | 0:31e91bb0ef3c | 200 | double next_relative_position = ( length * next_ratio ); |
scachat | 0:31e91bb0ef3c | 201 | |
scachat | 0:31e91bb0ef3c | 202 | // Advance |
scachat | 0:31e91bb0ef3c | 203 | // TODO: Proper advance configuration |
scachat | 0:31e91bb0ef3c | 204 | double advance = double(next_stepper_rate) * ( 0.00001 * 0.15 ) * 0.4 ; |
scachat | 0:31e91bb0ef3c | 205 | //double advance = 0; |
scachat | 0:31e91bb0ef3c | 206 | next_relative_position += ( advance ); |
scachat | 0:31e91bb0ef3c | 207 | |
scachat | 0:31e91bb0ef3c | 208 | // TODO : all of those "if->return" is very hacky, we should do the math in a way where most of those don't happen, but that requires doing tons of drawing ... |
scachat | 0:31e91bb0ef3c | 209 | if( last_ratio == next_ratio ){ this->acceleration_lock = false; return 0; }else{ last_ratio = next_ratio; } |
scachat | 0:31e91bb0ef3c | 210 | if( next_ratio == 0 || next_ratio > 1 ){ this->acceleration_lock = false; return 0; } |
scachat | 0:31e91bb0ef3c | 211 | if( ticks_forward > 1000 ){ this->acceleration_lock = false; return 0; } // This is very ugly |
scachat | 0:31e91bb0ef3c | 212 | |
scachat | 0:31e91bb0ef3c | 213 | // Hack : We have not looked far enough, we compute how far ahead we must look to get a relevant value |
scachat | 0:31e91bb0ef3c | 214 | if( position > next_relative_position ){ |
scachat | 0:31e91bb0ef3c | 215 | double far_back = position - next_relative_position; |
scachat | 0:31e91bb0ef3c | 216 | double far_back_ratio = far_back / length; |
scachat | 0:31e91bb0ef3c | 217 | double move_duration = double( this->current_block->steps_event_count ) / ( double(next_stepper_rate) / 60 ) ; |
scachat | 0:31e91bb0ef3c | 218 | double ticks_in_a_move = floor( stepper->acceleration_ticks_per_second * move_duration +0.5); |
scachat | 0:31e91bb0ef3c | 219 | double ratio_per_tick = 1 / ticks_in_a_move; |
scachat | 0:31e91bb0ef3c | 220 | double ticks_to_equilibrium = ceil(far_back_ratio / ratio_per_tick) + 1; |
scachat | 0:31e91bb0ef3c | 221 | ticks_forward += ticks_to_equilibrium; |
scachat | 0:31e91bb0ef3c | 222 | // Because this is a loop, and we can be interrupted by the stepping interrupt, if that interrupt changes block, the new block may not be solo, and we may get trapped into an infinite loop |
scachat | 0:31e91bb0ef3c | 223 | if( this->mode != FOLLOW ){ this->acceleration_lock = false; return 0; } |
scachat | 0:31e91bb0ef3c | 224 | continue; |
scachat | 0:31e91bb0ef3c | 225 | } |
scachat | 0:31e91bb0ef3c | 226 | |
scachat | 0:31e91bb0ef3c | 227 | // Finally, compute the speed to get to that next position |
scachat | 0:31e91bb0ef3c | 228 | double next_absolute_position = this->start_position + ( this->direction * next_relative_position ); |
scachat | 0:31e91bb0ef3c | 229 | double steps_to_next_tick = ( next_relative_position - position ) * this->steps_per_millimeter; |
scachat | 0:31e91bb0ef3c | 230 | double speed_to_next_tick = steps_to_next_tick / ( 1 / double(double(this->kernel->stepper->acceleration_ticks_per_second) / ticks_forward) ); |
scachat | 0:31e91bb0ef3c | 231 | |
scachat | 0:31e91bb0ef3c | 232 | // Change stepping speed |
scachat | 0:31e91bb0ef3c | 233 | this->set_speed( speed_to_next_tick ); |
scachat | 0:31e91bb0ef3c | 234 | |
scachat | 0:31e91bb0ef3c | 235 | this->acceleration_lock = false; |
scachat | 0:31e91bb0ef3c | 236 | return 0; |
scachat | 0:31e91bb0ef3c | 237 | } |
scachat | 0:31e91bb0ef3c | 238 | } |
scachat | 0:31e91bb0ef3c | 239 | |
scachat | 0:31e91bb0ef3c | 240 | this->acceleration_lock = false; |
scachat | 0:31e91bb0ef3c | 241 | return 0; |
scachat | 0:31e91bb0ef3c | 242 | } |
scachat | 0:31e91bb0ef3c | 243 | |
scachat | 0:31e91bb0ef3c | 244 | // Convenience function to set stepping speed |
scachat | 0:31e91bb0ef3c | 245 | void Extruder::set_speed( int steps_per_second ){ |
scachat | 0:31e91bb0ef3c | 246 | |
scachat | 0:31e91bb0ef3c | 247 | if( steps_per_second < 10 ){ steps_per_second = 10; } |
scachat | 0:31e91bb0ef3c | 248 | |
scachat | 0:31e91bb0ef3c | 249 | // TODO : Proper limit config value |
scachat | 0:31e91bb0ef3c | 250 | if( steps_per_second > (this->feed_rate*double(this->steps_per_millimeter))/60 ){ |
scachat | 0:31e91bb0ef3c | 251 | steps_per_second = (this->feed_rate*double(this->steps_per_millimeter))/60; |
scachat | 0:31e91bb0ef3c | 252 | } |
scachat | 0:31e91bb0ef3c | 253 | |
scachat | 0:31e91bb0ef3c | 254 | this->counter_increment = int(floor(double(1<<16)/double(this->kernel->stepper->base_stepping_frequency / steps_per_second))); |
scachat | 0:31e91bb0ef3c | 255 | |
scachat | 0:31e91bb0ef3c | 256 | } |
scachat | 0:31e91bb0ef3c | 257 | |
scachat | 0:31e91bb0ef3c | 258 | inline uint32_t Extruder::stepping_tick(uint32_t dummy){ |
scachat | 0:31e91bb0ef3c | 259 | if( this->paused ){ return 0; } |
scachat | 0:31e91bb0ef3c | 260 | |
scachat | 0:31e91bb0ef3c | 261 | this->step_counter += this->counter_increment; |
scachat | 0:31e91bb0ef3c | 262 | if( this->step_counter > 1<<16 ){ |
scachat | 0:31e91bb0ef3c | 263 | this->step_counter -= 1<<16; |
scachat | 0:31e91bb0ef3c | 264 | |
scachat | 0:31e91bb0ef3c | 265 | // If we still have steps to do |
scachat | 0:31e91bb0ef3c | 266 | // TODO: Step using the same timer as the robot, and count steps instead of absolute float position |
scachat | 0:31e91bb0ef3c | 267 | if( ( this->current_position < this->target_position && this->direction == 1 ) || ( this->current_position > this->target_position && this->direction == -1 ) ){ |
scachat | 0:31e91bb0ef3c | 268 | this->current_position += (double(double(1)/double(this->steps_per_millimeter)))*double(this->direction); |
scachat | 0:31e91bb0ef3c | 269 | this->dir_pin->set((this->direction > 0) ? 1 : 0); |
scachat | 0:31e91bb0ef3c | 270 | this->step_pin->set(1); |
scachat | 0:31e91bb0ef3c | 271 | }else{ |
scachat | 0:31e91bb0ef3c | 272 | // Move finished |
scachat | 0:31e91bb0ef3c | 273 | if( this->mode == SOLO && this->current_block != NULL ){ |
scachat | 0:31e91bb0ef3c | 274 | // In follow mode, the robot takes and releases the block, in solo mode we do |
scachat | 0:31e91bb0ef3c | 275 | this->current_block->release(); |
scachat | 0:31e91bb0ef3c | 276 | } |
scachat | 0:31e91bb0ef3c | 277 | } |
scachat | 0:31e91bb0ef3c | 278 | } |
scachat | 0:31e91bb0ef3c | 279 | return 0; |
scachat | 0:31e91bb0ef3c | 280 | } |
scachat | 0:31e91bb0ef3c | 281 | |
scachat | 0:31e91bb0ef3c | 282 | uint32_t Extruder::reset_step_pin(uint32_t dummy){ |
scachat | 0:31e91bb0ef3c | 283 | this->step_pin->set(0); |
scachat | 0:31e91bb0ef3c | 284 | return 0; |
scachat | 0:31e91bb0ef3c | 285 | } |