move back to get_ref in conveyors flush
[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
21Conveyor::Conveyor(){
22 this->current_block = NULL;
23 this->looking_for_new_block = false;
702023f3
MM
24 flush_blocks = 0;
25}
26
addfb453 27void Conveyor::on_module_loaded()
702023f3
MM
28{
29 register_for_event(ON_IDLE);
30}
31
addfb453 32void Conveyor::on_idle(void* argument)
702023f3
MM
33{
34 if (flush_blocks)
35 {
b3f57927 36 Block* block = queue.get_ref(0);
702023f3
MM
37// printf("Block: clean %p\n", block);
38 while (block->gcodes.size())
39 {
40 Gcode* g = block->gcodes.back();
41 block->gcodes.pop_back();
42 delete g;
43 }
44 queue.delete_first();
45
46 __disable_irq();
47 flush_blocks--;
48 __enable_irq();
49 }
f80d18b9
L
50}
51
52// Append a block to the list
53Block* Conveyor::new_block(){
54
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
702023f3 58
f80d18b9 59 // Take the next untaken block on the queue ( the one after the last one )
6476be0a 60 Block* block = this->queue.get_tail_ref();
f80d18b9
L
61 // Then clean it up
62 if( block->conveyor == this ){
702023f3
MM
63 for(; block->gcodes.size(); ){
64 Gcode* g = block->gcodes.back();
f80d18b9 65 block->gcodes.pop_back();
6476be0a 66 delete g;
f80d18b9
L
67 }
68 }
702023f3 69
f80d18b9
L
70 // Create a new virgin Block in the queue
71 this->queue.push_back(Block());
72 block = this->queue.get_ref( this->queue.size()-1 );
73 while( block == NULL ){
74 block = this->queue.get_ref( this->queue.size()-1 );
75 }
76 block->is_ready = false;
77 block->initial_rate = -2;
78 block->final_rate = -2;
79 block->conveyor = this;
80
81 return block;
82}
83
84// Used by blocks to signal when they are ready to be used by the system
85void Conveyor::new_block_added(){
86 if( this->current_block == NULL ){
87 this->pop_and_process_new_block(33);
88 }
89}
90
91// Process a new block in the queue
92void Conveyor::pop_and_process_new_block(int debug){
93 if( this->looking_for_new_block ){ return; }
94 this->looking_for_new_block = true;
95
96 if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
97
98 // Return if queue is empty
99 if( this->queue.size() == 0 ){
100 this->current_block = NULL;
101 // TODO : ON_QUEUE_EMPTY event
102 this->looking_for_new_block = false;
103 return;
104 }
702023f3 105
f80d18b9
L
106 // Get a new block
107 this->current_block = this->queue.get_ref(0);
108
109 // Tell all modules about it
110 this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
702023f3 111
f80d18b9
L
112 // In case the module was not taken
113 if( this->current_block->times_taken < 1 ){
12800c08 114 Block* temp = this->current_block;
bba74182 115 this->current_block = NULL; // It seems this was missing and adding it fixes things, if something breaks, this may be a suspect
12800c08
AW
116 temp->take();
117 temp->release();
f80d18b9
L
118 }
119
120 this->looking_for_new_block = false;
121
122}
123
addfb453 124void Conveyor::wait_for_queue(int free_blocks)
702023f3 125{
f80d18b9 126 while( this->queue.size() >= this->queue.capacity()-free_blocks ){
f80d18b9 127 this->kernel->call_event(ON_IDLE);
f80d18b9
L
128 }
129}
17c68379 130
a64021a3 131void Conveyor::wait_for_empty_queue(){
7b4c5e05 132 while( this->queue.size() > 0){
17c68379
BG
133 this->kernel->call_event(ON_IDLE);
134 }
135}
136