adding comments to modules/
[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 // The conveyor holds the queue of blocks, takes care of creating them, and starting the executing chain of blocks
22
23 Conveyor::Conveyor(){
24 this->current_block = NULL;
25 this->looking_for_new_block = false;
26 flush_blocks = 0;
27 }
28
29 void Conveyor::on_module_loaded(){
30 register_for_event(ON_IDLE);
31 }
32
33 // Delete blocks here, because they can't be deleted in interrupt context ( see Block.cpp:release )
34 void Conveyor::on_idle(void* argument){
35 if (flush_blocks){
36 // Cleanly delete block
37 Block* block = queue.get_tail_ref();
38 block->gcodes.clear();
39 queue.delete_first();
40 __disable_irq();
41 flush_blocks--;
42 __enable_irq();
43 }
44 }
45
46 // Append a block to the list
47 Block* Conveyor::new_block(){
48
49 // Take the next untaken block on the queue ( the one after the last one )
50 Block* block = this->queue.get_tail_ref();
51 // Then clean it up
52 if( block->conveyor == this ){
53 block->gcodes.clear();
54 }
55
56 // Create a new virgin Block in the queue
57 this->queue.push_back(Block());
58 block = this->queue.get_ref( this->queue.size()-1 );
59 while( block == NULL ){
60 block = this->queue.get_ref( this->queue.size()-1 );
61 }
62 block->is_ready = false;
63 block->initial_rate = -2;
64 block->final_rate = -2;
65 block->conveyor = this;
66
67 return block;
68 }
69
70 // Used by blocks to signal when they are ready to be used by the system
71 void Conveyor::new_block_added(){
72 if( this->current_block == NULL ){
73 this->pop_and_process_new_block(33);
74 }
75 }
76
77 // Process a new block in the queue
78 void Conveyor::pop_and_process_new_block(int debug){
79 if( this->looking_for_new_block ){ return; }
80 this->looking_for_new_block = true;
81
82 if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
83
84 // Return if queue is empty
85 if( this->queue.size() == 0 ){
86 this->current_block = NULL;
87 // TODO : ON_QUEUE_EMPTY event
88 this->looking_for_new_block = false;
89 return;
90 }
91
92 // Get a new block
93 this->current_block = this->queue.get_ref(0);
94
95 // Tell all modules about it
96 this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
97
98 // In case the module was not taken
99 if( this->current_block->times_taken < 1 ){
100 Block* temp = this->current_block;
101 this->current_block = NULL; // It seems this was missing and adding it fixes things, if something breaks, this may be a suspect
102 temp->take();
103 temp->release();
104 }
105
106 this->looking_for_new_block = false;
107
108 }
109
110 // Wait for the queue to have a given number of free blocks
111 void Conveyor::wait_for_queue(int free_blocks){
112 while( this->queue.size() >= this->queue.capacity()-free_blocks ){
113 this->kernel->call_event(ON_IDLE);
114 }
115 }
116
117 // Wait for the queue to be empty
118 void Conveyor::wait_for_empty_queue(){
119 while( this->queue.size() > 0){
120 this->kernel->call_event(ON_IDLE);
121 }
122 }
123