Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
modules/robot/Player.cpp@0:31e91bb0ef3c, 2012-07-31 (annotated)
- Committer:
- scachat
- Date:
- Tue Jul 31 21:11:18 2012 +0000
- Revision:
- 0:31e91bb0ef3c
smoothie port to mbed online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | /* |
scachat | 0:31e91bb0ef3c | 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) |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | */ |
scachat | 0:31e91bb0ef3c | 7 | |
scachat | 0:31e91bb0ef3c | 8 | using namespace std; |
scachat | 0:31e91bb0ef3c | 9 | #include <vector> |
scachat | 0:31e91bb0ef3c | 10 | #include "libs/nuts_bolts.h" |
scachat | 0:31e91bb0ef3c | 11 | #include "libs/RingBuffer.h" |
scachat | 0:31e91bb0ef3c | 12 | #include "../communication/utils/Gcode.h" |
scachat | 0:31e91bb0ef3c | 13 | #include "libs/Module.h" |
scachat | 0:31e91bb0ef3c | 14 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 15 | #include "Timer.h" // mbed.h lib |
scachat | 0:31e91bb0ef3c | 16 | #include "wait_api.h" // mbed.h lib |
scachat | 0:31e91bb0ef3c | 17 | #include "Block.h" |
scachat | 0:31e91bb0ef3c | 18 | #include "Player.h" |
scachat | 0:31e91bb0ef3c | 19 | #include "Planner.h" |
scachat | 0:31e91bb0ef3c | 20 | |
scachat | 0:31e91bb0ef3c | 21 | Player::Player(){ |
scachat | 0:31e91bb0ef3c | 22 | this->current_block = NULL; |
scachat | 0:31e91bb0ef3c | 23 | this->looking_for_new_block = false; |
scachat | 0:31e91bb0ef3c | 24 | } |
scachat | 0:31e91bb0ef3c | 25 | |
scachat | 0:31e91bb0ef3c | 26 | // Append a block to the list |
scachat | 0:31e91bb0ef3c | 27 | Block* Player::new_block(){ |
scachat | 0:31e91bb0ef3c | 28 | |
scachat | 0:31e91bb0ef3c | 29 | // Clean up the vector of commands in the block we are about to replace |
scachat | 0:31e91bb0ef3c | 30 | // It is quite strange to do this here, we really should do it inside Block->pop_and_execute_gcode |
scachat | 0:31e91bb0ef3c | 31 | // but that function is called inside an interrupt and thus can break everything if the interrupt was trigerred during a memory access |
scachat | 0:31e91bb0ef3c | 32 | //Block* block = this->queue.get_ref( this->queue.size()-1 ); |
scachat | 0:31e91bb0ef3c | 33 | Block* block = this->queue.get_ref( this->queue.size() ); |
scachat | 0:31e91bb0ef3c | 34 | if( block->player == this ){ |
scachat | 0:31e91bb0ef3c | 35 | for(short index=0; index<block->gcodes.size(); index++){ |
scachat | 0:31e91bb0ef3c | 36 | block->gcodes.pop_back(); |
scachat | 0:31e91bb0ef3c | 37 | } |
scachat | 0:31e91bb0ef3c | 38 | } |
scachat | 0:31e91bb0ef3c | 39 | |
scachat | 0:31e91bb0ef3c | 40 | // Create a new virgin Block in the queue |
scachat | 0:31e91bb0ef3c | 41 | this->queue.push_back(Block()); |
scachat | 0:31e91bb0ef3c | 42 | block = this->queue.get_ref( this->queue.size()-1 ); |
scachat | 0:31e91bb0ef3c | 43 | block->is_ready = false; |
scachat | 0:31e91bb0ef3c | 44 | block->initial_rate = -2; |
scachat | 0:31e91bb0ef3c | 45 | block->final_rate = -2; |
scachat | 0:31e91bb0ef3c | 46 | block->player = this; |
scachat | 0:31e91bb0ef3c | 47 | |
scachat | 0:31e91bb0ef3c | 48 | return block; |
scachat | 0:31e91bb0ef3c | 49 | } |
scachat | 0:31e91bb0ef3c | 50 | |
scachat | 0:31e91bb0ef3c | 51 | // Used by blocks to signal when they are ready to be used by the system |
scachat | 0:31e91bb0ef3c | 52 | void Player::new_block_added(){ |
scachat | 0:31e91bb0ef3c | 53 | if( this->current_block == NULL ){ |
scachat | 0:31e91bb0ef3c | 54 | this->pop_and_process_new_block(33); |
scachat | 0:31e91bb0ef3c | 55 | } |
scachat | 0:31e91bb0ef3c | 56 | } |
scachat | 0:31e91bb0ef3c | 57 | |
scachat | 0:31e91bb0ef3c | 58 | // Process a new block in the queue |
scachat | 0:31e91bb0ef3c | 59 | void Player::pop_and_process_new_block(int debug){ |
scachat | 0:31e91bb0ef3c | 60 | if( this->looking_for_new_block ){ return; } |
scachat | 0:31e91bb0ef3c | 61 | this->looking_for_new_block = true; |
scachat | 0:31e91bb0ef3c | 62 | |
scachat | 0:31e91bb0ef3c | 63 | if( this->current_block != NULL ){ this->looking_for_new_block = false; return; } |
scachat | 0:31e91bb0ef3c | 64 | |
scachat | 0:31e91bb0ef3c | 65 | // Return if queue is empty |
scachat | 0:31e91bb0ef3c | 66 | if( this->queue.size() == 0 ){ |
scachat | 0:31e91bb0ef3c | 67 | this->current_block = NULL; |
scachat | 0:31e91bb0ef3c | 68 | // TODO : ON_QUEUE_EMPTY event |
scachat | 0:31e91bb0ef3c | 69 | this->looking_for_new_block = false; |
scachat | 0:31e91bb0ef3c | 70 | return; |
scachat | 0:31e91bb0ef3c | 71 | } |
scachat | 0:31e91bb0ef3c | 72 | |
scachat | 0:31e91bb0ef3c | 73 | // Get a new block |
scachat | 0:31e91bb0ef3c | 74 | this->current_block = this->queue.get_ref(0); |
scachat | 0:31e91bb0ef3c | 75 | |
scachat | 0:31e91bb0ef3c | 76 | // Tell all modules about it |
scachat | 0:31e91bb0ef3c | 77 | this->kernel->call_event(ON_BLOCK_BEGIN, this->current_block); |
scachat | 0:31e91bb0ef3c | 78 | |
scachat | 0:31e91bb0ef3c | 79 | // In case the module was not taken |
scachat | 0:31e91bb0ef3c | 80 | if( this->current_block->times_taken < 1 ){ |
scachat | 0:31e91bb0ef3c | 81 | this->looking_for_new_block = false; |
scachat | 0:31e91bb0ef3c | 82 | this->current_block->release(); |
scachat | 0:31e91bb0ef3c | 83 | } |
scachat | 0:31e91bb0ef3c | 84 | |
scachat | 0:31e91bb0ef3c | 85 | this->looking_for_new_block = false; |
scachat | 0:31e91bb0ef3c | 86 | |
scachat | 0:31e91bb0ef3c | 87 | } |
scachat | 0:31e91bb0ef3c | 88 | |
scachat | 0:31e91bb0ef3c | 89 | void Player::wait_for_queue(int free_blocks){ |
scachat | 0:31e91bb0ef3c | 90 | mbed::Timer t; |
scachat | 0:31e91bb0ef3c | 91 | while( this->queue.size() >= this->queue.capacity()-free_blocks ){ |
scachat | 0:31e91bb0ef3c | 92 | t.reset(); |
scachat | 0:31e91bb0ef3c | 93 | t.start(); |
scachat | 0:31e91bb0ef3c | 94 | this->kernel->call_event(ON_IDLE); |
scachat | 0:31e91bb0ef3c | 95 | t.stop(); |
scachat | 0:31e91bb0ef3c | 96 | if(t.read_us() < 500) |
scachat | 0:31e91bb0ef3c | 97 | wait_us(500 - t.read_us()); |
scachat | 0:31e91bb0ef3c | 98 | } |
scachat | 0:31e91bb0ef3c | 99 | } |