0fcd9a59660f9c6267f6dac0fc50edcc1a756de8
[clinton/Smoothieware.git] / src / modules / robot / Conveyor.cpp
1 /*
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/>.
6 */
7
8 using namespace std;
9 #include <vector>
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
17 #include "Block.h"
18 #include "Conveyor.h"
19 #include "Planner.h"
20
21 Conveyor::Conveyor(){
22 this->current_block = NULL;
23 this->looking_for_new_block = false;
24 flush_blocks = 0;
25 }
26
27 void Conveyor::on_module_loaded(){
28 register_for_event(ON_IDLE);
29 }
30
31 void Conveyor::on_idle(void* argument){
32 if (flush_blocks){
33
34 Block* block = queue.get_tail_ref();
35 while (block->gcodes.size()){
36 Gcode* gcode = block->gcodes.back();
37 block->gcodes.pop_back();
38
39 // Do we just delete this gcode from this vector, or do we also actually delete the gcode ?
40 // This is pretty complex and explained in detail in Gcode.h
41 if( gcode->passed_thru_all_blocks_added_context_without_deleting == true ){
42 delete gcode;
43 }else{
44 gcode->passed_thru_new_block_context_without_deleting = false;
45 }
46
47 }
48 queue.delete_first();
49
50 __disable_irq();
51 flush_blocks--;
52 __enable_irq();
53 }
54 }
55
56 // Append a block to the list
57 Block* Conveyor::new_block(){
58
59 // Clean up the vector of commands in the block we are about to replace
60 // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
61 // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
62
63 // Take the next untaken block on the queue ( the one after the last one )
64 Block* block = this->queue.get_tail_ref();
65 // Then clean it up
66 if( block->conveyor == this ){
67 for(; block->gcodes.size(); ){
68 Gcode* gcode = block->gcodes.back();
69 block->gcodes.pop_back();
70
71 // Do we just delete this gcode from this vector, or do we also actually delete the gcode ?
72 // This is pretty complex and explained in detail in Gcode.h
73 if( gcode->passed_thru_all_blocks_added_context_without_deleting == true ){
74 delete gcode;
75 }else{
76 gcode->passed_thru_new_block_context_without_deleting = false;
77 }
78
79 }
80 }
81
82 // Create a new virgin Block in the queue
83 this->queue.push_back(Block());
84 block = this->queue.get_ref( this->queue.size()-1 );
85 while( block == NULL ){
86 block = this->queue.get_ref( this->queue.size()-1 );
87 }
88 block->is_ready = false;
89 block->initial_rate = -2;
90 block->final_rate = -2;
91 block->conveyor = this;
92
93 return block;
94 }
95
96 // Used by blocks to signal when they are ready to be used by the system
97 void Conveyor::new_block_added(){
98 if( this->current_block == NULL ){
99 this->pop_and_process_new_block(33);
100 }
101 }
102
103 // Process a new block in the queue
104 void Conveyor::pop_and_process_new_block(int debug){
105 if( this->looking_for_new_block ){ return; }
106 this->looking_for_new_block = true;
107
108 if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
109
110 // Return if queue is empty
111 if( this->queue.size() == 0 ){
112 this->current_block = NULL;
113 // TODO : ON_QUEUE_EMPTY event
114 this->looking_for_new_block = false;
115 return;
116 }
117
118 // Get a new block
119 this->current_block = this->queue.get_ref(0);
120
121 // Tell all modules about it
122 this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
123
124 // In case the module was not taken
125 if( this->current_block->times_taken < 1 ){
126 Block* temp = this->current_block;
127 this->current_block = NULL; // It seems this was missing and adding it fixes things, if something breaks, this may be a suspect
128 temp->take();
129 temp->release();
130 }
131
132 this->looking_for_new_block = false;
133
134 }
135
136 void Conveyor::wait_for_queue(int free_blocks)
137 {
138 while( this->queue.size() >= this->queue.capacity()-free_blocks ){
139 this->kernel->call_event(ON_IDLE);
140 }
141 }
142
143 void Conveyor::wait_for_empty_queue(){
144 while( this->queue.size() > 0){
145 this->kernel->call_event(ON_IDLE);
146 }
147 }
148