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