smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

For documentation, license, ..., please check http://smoothieware.org/

This version has been tested with a 3 axis machine

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?

UserRevisionLine numberNew 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).
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 #include <string>
scachat 0:31e91bb0ef3c 9 using std::string;
scachat 0:31e91bb0ef3c 10 #include "libs/Module.h"
scachat 0:31e91bb0ef3c 11 #include "libs/Kernel.h"
scachat 0:31e91bb0ef3c 12 #include "utils/Gcode.h"
scachat 0:31e91bb0ef3c 13 #include "libs/nuts_bolts.h"
scachat 0:31e91bb0ef3c 14 #include "GcodeDispatch.h"
scachat 0:31e91bb0ef3c 15 #include "modules/robot/Player.h"
scachat 0:31e91bb0ef3c 16 #include "libs/SerialMessage.h"
scachat 0:31e91bb0ef3c 17 #include "libs/StreamOutput.h"
scachat 0:31e91bb0ef3c 18
scachat 0:31e91bb0ef3c 19 GcodeDispatch::GcodeDispatch(){}
scachat 0:31e91bb0ef3c 20
scachat 0:31e91bb0ef3c 21 // Called when the module has just been loaded
scachat 0:31e91bb0ef3c 22 void GcodeDispatch::on_module_loaded() {
scachat 0:31e91bb0ef3c 23 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
scachat 0:31e91bb0ef3c 24 currentline = -1;
scachat 0:31e91bb0ef3c 25 }
scachat 0:31e91bb0ef3c 26
scachat 0:31e91bb0ef3c 27 // When a command is received, if it is a Gcode, dispatch it as an object via an event
scachat 0:31e91bb0ef3c 28 void GcodeDispatch::on_console_line_received(void * line){
scachat 0:31e91bb0ef3c 29 SerialMessage new_message = *static_cast<SerialMessage*>(line);
scachat 0:31e91bb0ef3c 30 string possible_command = new_message.message;
scachat 0:31e91bb0ef3c 31
scachat 0:31e91bb0ef3c 32 char first_char = possible_command[0];
scachat 0:31e91bb0ef3c 33 int ln = 0;
scachat 0:31e91bb0ef3c 34 int cs = 0;
scachat 0:31e91bb0ef3c 35 if( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ){
scachat 0:31e91bb0ef3c 36
scachat 0:31e91bb0ef3c 37 //Get linenumber
scachat 0:31e91bb0ef3c 38 if( first_char == 'N' ){
scachat 0:31e91bb0ef3c 39 Gcode full_line = Gcode();
scachat 0:31e91bb0ef3c 40 full_line.command = possible_command;
scachat 0:31e91bb0ef3c 41 full_line.stream = new_message.stream;
scachat 0:31e91bb0ef3c 42 ln = (int) full_line.get_value('N');
scachat 0:31e91bb0ef3c 43 int chksum = (int) full_line.get_value('*');
scachat 0:31e91bb0ef3c 44
scachat 0:31e91bb0ef3c 45 //Catch message if it is M110: Set Current Line Number
scachat 0:31e91bb0ef3c 46 if( full_line.has_letter('M') ){
scachat 0:31e91bb0ef3c 47 if( ((int) full_line.get_value('M')) == 110 ){
scachat 0:31e91bb0ef3c 48 currentline = ln;
scachat 0:31e91bb0ef3c 49 new_message.stream->printf("ok\r\n");
scachat 0:31e91bb0ef3c 50 return;
scachat 0:31e91bb0ef3c 51 }
scachat 0:31e91bb0ef3c 52 }
scachat 0:31e91bb0ef3c 53
scachat 0:31e91bb0ef3c 54 //Strip checksum value from possible_command
scachat 0:31e91bb0ef3c 55 size_t chkpos = possible_command.find_first_of("*");
scachat 0:31e91bb0ef3c 56 possible_command = possible_command.substr(0, chkpos);
scachat 0:31e91bb0ef3c 57 //Calculate checksum
scachat 0:31e91bb0ef3c 58 if( chkpos != string::npos ){
scachat 0:31e91bb0ef3c 59 for(int i = 0; possible_command[i] != '*' && possible_command[i] != NULL; i++)
scachat 0:31e91bb0ef3c 60 cs = cs ^ possible_command[i];
scachat 0:31e91bb0ef3c 61 cs &= 0xff; // Defensive programming...
scachat 0:31e91bb0ef3c 62 cs -= chksum;
scachat 0:31e91bb0ef3c 63 }
scachat 0:31e91bb0ef3c 64 //Strip line number value from possible_command
scachat 0:31e91bb0ef3c 65 size_t lnsize = possible_command.find_first_of(" ") + 1;
scachat 0:31e91bb0ef3c 66 possible_command = possible_command.substr(lnsize);
scachat 0:31e91bb0ef3c 67
scachat 0:31e91bb0ef3c 68 }else{
scachat 0:31e91bb0ef3c 69 //Assume checks succeeded
scachat 0:31e91bb0ef3c 70 cs = 0x00;
scachat 0:31e91bb0ef3c 71 ln = currentline + 1;
scachat 0:31e91bb0ef3c 72 }
scachat 0:31e91bb0ef3c 73
scachat 0:31e91bb0ef3c 74 //Remove comments
scachat 0:31e91bb0ef3c 75 size_t comment = possible_command.find_first_of(";");
scachat 0:31e91bb0ef3c 76 if( comment != string::npos ){ possible_command = possible_command.substr(0, comment); }
scachat 0:31e91bb0ef3c 77
scachat 0:31e91bb0ef3c 78 //If checksum passes then process message, else request resend
scachat 0:31e91bb0ef3c 79 int nextline = currentline + 1;
scachat 0:31e91bb0ef3c 80 if( cs == 0x00 && ln == nextline ){
scachat 0:31e91bb0ef3c 81 if( first_char == 'N' ) {
scachat 0:31e91bb0ef3c 82 currentline = nextline;
scachat 0:31e91bb0ef3c 83 }
scachat 0:31e91bb0ef3c 84
scachat 0:31e91bb0ef3c 85 while(possible_command.size() > 0) {
scachat 0:31e91bb0ef3c 86 size_t nextcmd = possible_command.find_first_of("GMT", possible_command.find_first_of("GMT")+1);
scachat 0:31e91bb0ef3c 87 string single_command;
scachat 0:31e91bb0ef3c 88 if(nextcmd == string::npos) {
scachat 0:31e91bb0ef3c 89 single_command = possible_command;
scachat 0:31e91bb0ef3c 90 possible_command = "";
scachat 0:31e91bb0ef3c 91 }
scachat 0:31e91bb0ef3c 92 else {
scachat 0:31e91bb0ef3c 93 single_command = possible_command.substr(0,nextcmd);
scachat 0:31e91bb0ef3c 94 possible_command = possible_command.substr(nextcmd);
scachat 0:31e91bb0ef3c 95 }
scachat 0:31e91bb0ef3c 96 //Prepare gcode for dispatch
scachat 0:31e91bb0ef3c 97 Gcode gcode = Gcode();
scachat 0:31e91bb0ef3c 98 gcode.command = single_command;
scachat 0:31e91bb0ef3c 99 gcode.stream = new_message.stream;
scachat 0:31e91bb0ef3c 100
scachat 0:31e91bb0ef3c 101 //Dispatch message!
scachat 0:31e91bb0ef3c 102 this->kernel->call_event(ON_GCODE_RECEIVED, &gcode );
scachat 0:31e91bb0ef3c 103 new_message.stream->printf("ok\r\n");
scachat 0:31e91bb0ef3c 104 }
scachat 0:31e91bb0ef3c 105 }else{
scachat 0:31e91bb0ef3c 106 //Request resend
scachat 0:31e91bb0ef3c 107 new_message.stream->printf("rs N%d\r\n", nextline);
scachat 0:31e91bb0ef3c 108 }
scachat 0:31e91bb0ef3c 109
scachat 0:31e91bb0ef3c 110 // Ignore comments and blank lines
scachat 0:31e91bb0ef3c 111 }else if( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ){
scachat 0:31e91bb0ef3c 112 new_message.stream->printf("ok\r\n");
scachat 0:31e91bb0ef3c 113 }
scachat 0:31e91bb0ef3c 114 }
scachat 0:31e91bb0ef3c 115