major bug fixes, way too many to enumerate
[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 "mbed.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/RingBuffer.h"
13 #include "../communication/utils/Gcode.h"
14 #include "libs/Module.h"
15 #include "libs/Kernel.h"
16 #include "Block.h"
17 #include "Player.h"
18 #include "Planner.h"
19
20 Player::Player(){
21 this->current_block = NULL;
22 this->looking_for_new_block = false;
23 }
24
25 // Append a block to the list
26 Block* Player::new_block(){
27
28 // Clean up the vector of commands in the block we are about to replace
29 // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
30 // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
31 //Block* block = this->queue.get_ref( this->queue.size()-1 );
32 Block* block = this->queue.get_ref( this->queue.size() );
33 if( block->player == this ){
34 for(short index=0; index<block->gcodes.size(); index++){
35 block->gcodes.pop_back();
36 }
37 //for(short index=0; index<block->commands.size(); index++){
38 // block->commands.pop_back();
39 // block->travel_distances.pop_back();
40 //}
41 }
42
43 // Create a new virgin Block in the queue
44 this->queue.push_back(Block());
45 block = this->queue.get_ref( this->queue.size()-1 );
46 block->is_ready = false;
47 block->initial_rate = -2;
48 block->final_rate = -2;
49 block->player = this;
50
51 return block;
52 }
53
54 // Used by blocks to signal when they are ready to be used by the system
55 void Player::new_block_added(){
56 //this->kernel->serial->printf("new block: %p\r\n", this->current_block);
57
58 //if( this->current_block == 0x00 || this->queue.size() == 0 ){
59 //this->kernel->serial->printf("f %p %d\r\n", this->current_block, this->queue.size() );
60 //}
61
62
63 if( this->current_block == NULL ){
64 this->pop_and_process_new_block(33);
65 }
66 }
67
68 // Process a new block in the queue
69 void Player::pop_and_process_new_block(int debug){
70 if( this->looking_for_new_block ){ return; }
71 this->looking_for_new_block = true;
72
73 if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
74
75 // Return if queue is empty
76 if( this->queue.size() == 0 ){
77 this->current_block = NULL;
78 // TODO : ON_QUEUE_EMPTY event
79 this->looking_for_new_block = false;
80 return;
81 }
82
83 // Get a new block
84 this->current_block = this->queue.get_ref(0);
85
86 // Tell all modules about it
87 this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block);
88
89 // In case the module was not taken
90 if( this->current_block->times_taken < 1 ){
91 //this->kernel->serial->printf("e %p %d %d %d\r\n", this->current_block, this->queue.size(), this->current_block == 0x00, debug );
92 //wait(0.1);
93 this->looking_for_new_block = false;
94 this->current_block->release();
95 }
96
97 this->looking_for_new_block = false;
98
99 }