grbl jogging how to?

Discussion about the arduino based g-code interpreter, grbl
Post Reply
Trax
Posts: 18
Joined: Thu Aug 14, 2014 10:52 am

grbl jogging how to?

Post by Trax » Thu Sep 18, 2014 6:49 am

I have some working code, stepping works great, jogging works so so, it has a lag as it just queues new blocks a few times a second to be processed.


How to implement jogging properly such that if the jog button gets released grbl stops as fas ats the max deceleration allows?
I guess it must be done somehow like homing or probing works, but I don't see yet how should i do that properly.

Some advice would be very appreciated

Code: Select all

/*
  remote.h - remote controll
  Part of Grbl v0.9

  Copyright (c) 2012-2014 Trax Xavier

  Grbl 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.

  Grbl 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 Grbl.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "system.h"
#include "remote.h"
#include "twi.h"
#include "report.h"
#include "settings.h"
#include "gcode.h"
#include "planner.h"

#define REMOTE_ADDRESS 0x10 // Note: the remote is always 0x10

volatile int remote_steps_togo[N_AXIS];
volatile int8_t remote_jog_speed[N_AXIS];
volatile uint8_t remote_flags;

#define REMOTE_STATUS_REPORT 	bit(0)
//#define REMOTE_				  bit(1)
//#define REMOTE_ 				bit(2)
//#define REMOTE_ 				bit(3)
#define REMOTE_UNLOCK 				bit(4)
#define REMOTE_HOME		 				bit(5)
#define REMOTE_ZERO 					bit(6)
//#define REMOTE_ 				bit(7)

void onReceiveService(uint8_t* inBytes, int numBytes)
{
	uint8_t axis = N_AXIS;
	
  switch (*inBytes++) {
    case CMD_STATUS_REPORT:	bit_true_atomic(remote_flags, REMOTE_STATUS_REPORT); break;
    case CMD_CYCLE_START:   bit_true_atomic(sys.execute, EXEC_CYCLE_START); break; // Set as true
    case CMD_FEED_HOLD:     bit_true_atomic(sys.execute, EXEC_FEED_HOLD); break; // Set as true
    case CMD_RESET:         mc_reset(); break; // Call motion control reset routine.
    case '$': // command
    {
    	switch (*inBytes++) {
    		case 'X': // unlock
    		case 'x':	bit_true_atomic(remote_flags, REMOTE_UNLOCK); break;
    		case 'H': // home
    		case 'h':	bit_true_atomic(remote_flags, REMOTE_HOME); break;
    		case 'Z': // zero
    		case 'z': bit_true_atomic(remote_flags, REMOTE_ZERO); break;
    	}
    	break;
    }
    case '#': // Step
    	axis = *inBytes++;
    	if(axis >= N_AXIS)
    		break;
    	remote_steps_togo[axis] += (int)(*((int32_t*)inBytes)); inBytes += 4; // 32 bit integers with µm unit - for now limit to 16 bit
    	break;
		case '*': // Jog
    	axis = *inBytes++;
    	if(axis >= N_AXIS)
    		break;
    	remote_jog_speed[axis] = *(int8_t*)(inBytes++); // jogging speed in +/- % max feed rate
    	break; 
		//case '%':
    //	break; 
  }
}

void remote_init()
{
	uint8_t idx;
	for (idx=0; idx<N_AXIS; idx++){
		remote_steps_togo[idx] = 0;
		remote_jog_speed[idx] = 0;
	}
	remote_flags = 0;
	
  twi_attachSlaveRxEvent(onReceiveService);
}

void report_realtime_status_i2c()
{
	uint8_t i2c_buffer[TWI_BUFFER_LENGTH]; // max length 32
	uint8_t* outBytes = i2c_buffer;
	
	uint8_t i;

	*outBytes++ = sys.state;
	*outBytes++ = 0; // reserved
	*outBytes++ = 0; // reserved
	*outBytes++ = 0; // reserved

  for (i=0; i< N_AXIS; i++) {
    float print_position = sys.position[i]/settings.steps_per_mm[i];
    print_position -= gc_state.coord_system[i]+gc_state.coord_offset[i];
    if (i == TOOL_LENGTH_OFFSET_AXIS) { print_position -= gc_state.tool_length_offset; }    

		*((uint32_t*)outBytes) = (uint32_t)(print_position*1000.0); outBytes += 4;
  }
  
  //16 bytes left
	
  twi_writeTo(REMOTE_ADDRESS, i2c_buffer, outBytes - i2c_buffer, 1, true);
}

void remote_execute_runtime()
{
  if (remote_flags & REMOTE_STATUS_REPORT) { 
    report_realtime_status_i2c();
    bit_false_atomic(remote_flags,REMOTE_STATUS_REPORT);
  }
 
  if (remote_flags & REMOTE_UNLOCK) { 
  	// Disable alarm lock [ALARM]
    if (sys.state == STATE_ALARM) { 
      report_feedback_message(MESSAGE_ALARM_UNLOCK);
      sys.state = STATE_IDLE;
      // Don't run startup script. Prevents stored moves in startup from causing accidents.
    } // Otherwise, no effect.
    bit_false_atomic(remote_flags,REMOTE_UNLOCK);
  }
 
	if (remote_flags & REMOTE_HOME) { 
		// Only perform homing if Grbl is idle or lost.
   	if (sys.state == STATE_IDLE || sys.state == STATE_ALARM) { 
   		if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { 
    		mc_homing_cycle(); 
    		//if (!sys.abort) { system_execute_startup(line); } // Execute startup scripts after successful homing.
    	}
    }
    bit_false_atomic(remote_flags,REMOTE_HOME);
  }
 
 uint8_t i;
  
	if (remote_flags & REMOTE_ZERO) { 
	  for (i=0; i< N_AXIS; i++) {
	    float print_position = sys.position[i]/settings.steps_per_mm[i];
	    print_position -= gc_state.coord_system[i];
	    if (i == TOOL_LENGTH_OFFSET_AXIS) { print_position -= gc_state.tool_length_offset; }    
	
			gc_state.coord_offset[i] = print_position;
	  }
    bit_false_atomic(remote_flags,REMOTE_ZERO);
  }
 

  
  float cur_steps_rate = 0.0;
  int cur_steps_togo[N_AXIS];
  
  float cur_jogging_rate = 0.0;
  
  for (i=0; i< N_AXIS; i++) {
  	
  	if(cur_steps_togo[i] = remote_steps_togo[i])
  		cur_steps_rate += settings.max_rate[i];
  		
  	if(remote_jog_speed[i])
 			cur_jogging_rate += settings.max_rate[i] * remote_jog_speed[i] / 100.0;
  }
  
  if(cur_jogging_rate != 0.0 || cur_steps_rate != 0.0){
  	if (!sys.state) { gc_sync_position(); }
  }
  
  // handle stepping - make steps if we have enough place in buffer
  if(cur_steps_rate != 0.0 && plan_get_block_buffer_count() < 4) {
  	
  	// update position
  	for (i=0; i< N_AXIS; i++) {
  		gc_state.position[i] += cur_steps_togo[i]/1000.0;
  		remote_steps_togo[i] -= cur_steps_togo[i];
  	}
  	
  	// plan next steps
	  #ifdef USE_LINE_NUMBERS
	    plan_buffer_line(gc_state.position, cur_steps_rate, false, HOMING_CYCLE_LINE_NUMBER);
	  #else
	    plan_buffer_line(gc_state.position, cur_steps_rate, false);
	  #endif
  }
  
	// handle jogging - check block queue
	if(cur_jogging_rate != 0.0 && plan_get_block_buffer_count() < 4) {
		
  	// update position
  	for (i=0; i< N_AXIS; i++) {
  		float rate = settings.max_rate[i] * abs(remote_jog_speed[i]) / 100.0; // mm/min
  		float togo = rate / 60.0 / 10.0;
  		if(remote_jog_speed[i] > 0)
  			gc_state.position[i] += togo;
  		else if(remote_jog_speed[i] < 0)
  			gc_state.position[i] -= togo;
  	}
  	
  	// plan next steps
	  #ifdef USE_LINE_NUMBERS
	    plan_buffer_line(gc_state.position, cur_jogging_rate, false, HOMING_CYCLE_LINE_NUMBER);
	  #else
	    plan_buffer_line(gc_state.position, cur_jogging_rate, false);
	  #endif
	}
	
	if(cur_jogging_rate != 0.0 || cur_steps_rate != 0.0) {
		// resume/start cycle if its idle
	  if (!sys.state) { sys.state = STATE_QUEUED; }
	  protocol_auto_cycle_start();
  }
}

Trax
Posts: 18
Joined: Thu Aug 14, 2014 10:52 am

Re: grbl jogging how to?

Post by Trax » Sat Nov 08, 2014 4:32 pm

any help would be greatly appriciated

cvoinescu
Posts: 4442
Joined: Thu Jul 19, 2012 6:50 pm
Location: Camberley, UK
Contact:

Re: grbl jogging how to?

Post by cvoinescu » Sat Nov 08, 2014 4:35 pm

Maybe this is not the best place to ask about programming...

As far as I know, Sonny (@chamnit) has jogging support planned for GRBL 1.0.
Proud owner of ShapeOko #709, eShapeOko #0, and of store.amberspyglass.co.uk

Trax
Posts: 18
Joined: Thu Aug 14, 2014 10:52 am

Re: grbl jogging how to?

Post by Trax » Sat Nov 08, 2014 6:08 pm

will grbl 1.0 still fit an arduino uno ir will i need to upgrade to a mega?
fyi I dont use an arduino but a bear chip, so updating to a mega would mean to make a new PCB for my mill ;)

tbfleming
Posts: 89
Joined: Sat May 31, 2014 12:04 pm

Re: grbl jogging how to?

Post by tbfleming » Sat Nov 08, 2014 6:56 pm

It looks like 1.0 might be the last major release for Uno. After that Chamnit wants to switch to Arm to enable a large feature jump.

Post Reply