renamed the Player module to Conveyor
[clinton/Smoothieware.git] / src / modules / robot / Player.cpp
index cc1a771..b07a3b3 100644 (file)
@@ -1,8 +1,8 @@
-/*  
+/*
       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)
       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.
       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.
-      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
+      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
 */
 
 using namespace std;
@@ -12,6 +12,8 @@ using namespace std;
 #include "../communication/utils/Gcode.h"
 #include "libs/Module.h"
 #include "libs/Kernel.h"
+#include "Timer.h" // mbed.h lib
+#include "wait_api.h" // mbed.h lib
 #include "Block.h"
 #include "Player.h"
 #include "Planner.h"
@@ -27,17 +29,22 @@ Block* Player::new_block(){
     // Clean up the vector of commands in the block we are about to replace
     // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode
     // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access
-    //Block* block = this->queue.get_ref( this->queue.size()-1 );
+    
+    // Take the next untaken block on the queue ( the one after the last one )
     Block* block = this->queue.get_ref( this->queue.size() );
+    // Then clean it up
     if( block->player == this ){
-        for(short index=0; index<block->gcodes.size(); index++){
-            block->gcodes.pop_back(); 
-        }     
+        for(unsigned int index=0; index<block->gcodes.size(); index++){
+            block->gcodes.pop_back();
+        }
     }
     
-    // Create a new virgin Block in the queue 
+    // Create a new virgin Block in the queue
     this->queue.push_back(Block());
     block = this->queue.get_ref( this->queue.size()-1 );
+    while( block == NULL ){
+        block = this->queue.get_ref( this->queue.size()-1 );
+    }
     block->is_ready = false;
     block->initial_rate = -2;
     block->final_rate = -2;
@@ -60,12 +67,12 @@ void Player::pop_and_process_new_block(int debug){
 
     if( this->current_block != NULL ){ this->looking_for_new_block = false; return; }
 
-    // Return if queue is empty 
+    // Return if queue is empty
     if( this->queue.size() == 0 ){
-        this->current_block = NULL; 
-        // TODO : ON_QUEUE_EMPTY event 
+        this->current_block = NULL;
+        // TODO : ON_QUEUE_EMPTY event
         this->looking_for_new_block = false;
-        return; 
+        return;
     }
     
     // Get a new block
@@ -85,8 +92,9 @@ void Player::pop_and_process_new_block(int debug){
 }
 
 void Player::wait_for_queue(int free_blocks){
+    mbed::Timer t;
     while( this->queue.size() >= this->queue.capacity()-free_blocks ){
-        Timer t;
+        t.reset();
         t.start();
         this->kernel->call_event(ON_IDLE);
         t.stop();