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)
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.
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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
10 #include "libs/nuts_bolts.h"
11 #include "libs/RingBuffer.h"
12 #include "../communication/utils/Gcode.h"
13 #include "libs/Module.h"
14 #include "libs/Kernel.h"
15 #include "Timer.h" // mbed.h lib
16 #include "wait_api.h" // mbed.h lib
22 this->current_block
= NULL
;
23 this->looking_for_new_block
= false;
27 void Conveyor::on_module_loaded()
29 register_for_event(ON_IDLE
);
32 void Conveyor::on_idle(void* argument
)
36 Block
* block
= queue
.get_ref(0);
37 // printf("Block: clean %p\n", block);
38 while (block
->gcodes
.size())
40 Gcode
* g
= block
->gcodes
.back();
41 block
->gcodes
.pop_back();
52 // Append a block to the list
53 Block
* Conveyor::new_block(){
55 // Clean up the vector of commands in the block we are about to replace
56 // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
57 // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
59 // Take the next untaken block on the queue ( the one after the last one )
60 Block
* block
= this->queue
.get_ref( this->queue
.size() );
61 // printf("cleanup %p\n", block);
63 if( block
->conveyor
== this ){
64 for(; block
->gcodes
.size(); ){
65 Gcode
* g
= block
->gcodes
.back();
66 // printf("Block:pop %p (%d refs)\n", g, g->queued);
67 block
->gcodes
.pop_back();
73 // Create a new virgin Block in the queue
74 this->queue
.push_back(Block());
75 block
= this->queue
.get_ref( this->queue
.size()-1 );
76 while( block
== NULL
){
77 block
= this->queue
.get_ref( this->queue
.size()-1 );
79 block
->is_ready
= false;
80 block
->initial_rate
= -2;
81 block
->final_rate
= -2;
82 block
->conveyor
= this;
87 // Used by blocks to signal when they are ready to be used by the system
88 void Conveyor::new_block_added(){
89 if( this->current_block
== NULL
){
90 this->pop_and_process_new_block(33);
94 // Process a new block in the queue
95 void Conveyor::pop_and_process_new_block(int debug
){
96 if( this->looking_for_new_block
){ return; }
97 this->looking_for_new_block
= true;
99 if( this->current_block
!= NULL
){ this->looking_for_new_block
= false; return; }
101 // Return if queue is empty
102 if( this->queue
.size() == 0 ){
103 this->current_block
= NULL
;
104 // TODO : ON_QUEUE_EMPTY event
105 this->looking_for_new_block
= false;
110 this->current_block
= this->queue
.get_ref(0);
112 // Tell all modules about it
113 this->kernel
->call_event(ON_BLOCK_BEGIN
, this->current_block
);
115 // In case the module was not taken
116 if( this->current_block
->times_taken
< 1 ){
117 Block
* temp
= this->current_block
;
118 this->current_block
= NULL
; // It seems this was missing and adding it fixes things, if something breaks, this may be a suspect
123 this->looking_for_new_block
= false;
127 void Conveyor::wait_for_queue(int free_blocks
)
129 while( this->queue
.size() >= this->queue
.capacity()-free_blocks
){
130 this->kernel
->call_event(ON_IDLE
);