Merge branch 'edge' into wait4q
[clinton/Smoothieware.git] / src / modules / robot / Player.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 "Block.h"
16 #include "Player.h"
17 #include "Planner.h"
18
19 Player::Player(){
20 this->current_block = NULL;
21 this->looking_for_new_block = false;
22 }
23
24 // Append a block to the list
25 Block* Player::new_block(){
26
27 // Clean up the vector of commands in the block we are about to replace
28 // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
29 // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
30 //Block* block = this->queue.get_ref( this->queue.size()-1 );
31 Block* block = this->queue.get_ref( this->queue.size() );
32 if( block->player == this ){
33 for(short index=0; index<block->gcodes.size(); index++){
34 block->gcodes.pop_back();
35 }
36 }
37
38 // Create a new virgin Block in the queue
39 this->queue.push_back(Block());
40 block = this->queue.get_ref( this->queue.size()-1 );
41 block->is_ready = false;
42 block->initial_rate = -2;
43 block->final_rate = -2;
44 block->player = this;
45
46 return block;
47 }
48
49 // Used by blocks to signal when they are ready to be used by the system
50 void Player::new_block_added(){
51 if( this->current_block == NULL ){
52 this->pop_and_process_new_block(33);
53 }
54 }
55
56 // Process a new block in the queue
57 void Player::pop_and_process_new_block(int debug){
58 if( this->looking_for_new_block ){ return; }
59 this->looking_for_new_block = true;
60
61 if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
62
63 // Return if queue is empty
64 if( this->queue.size() == 0 ){
65 this->current_block = NULL;
66 // TODO : ON_QUEUE_EMPTY event
67 this->looking_for_new_block = false;
68 return;
69 }
70
71 // Get a new block
72 this->current_block = this->queue.get_ref(0);
73
74 // Tell all modules about it
75 this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
76
77 // In case the module was not taken
78 if( this->current_block->times_taken < 1 ){
79 this->looking_for_new_block = false;
80 this->current_block->release();
81 }
82
83 this->looking_for_new_block = false;
84
85 }
86
87 void Player::wait_for_queue(int free_blocks){
88 Timer t;
89 while( this->queue.size() >= this->queue.capacity()-free_blocks ){
90 t.reset();
91 t.start();
92 this->kernel->call_event(ON_IDLE);
93 t.stop();
94 if(t.read_us() < 500)
95 wait_us(500 - t.read_us());
96 }
97 }