Skip to content

Commit

Permalink
More '%' modulo opertor removals and some housecleaning.
Browse files Browse the repository at this point in the history
- Serial functions contained quite a few modulo operations that would
be executed with high frequency when streaming. AVR processors are very
slow when operating these. In one test on the Arduino forums, it showed
about a 15x slow down compared to a simple if-then statement. -
Clarified some variable names and types and comments.
  • Loading branch information
chamnit committed Sep 16, 2011
1 parent 4d03c4f commit 110faae
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gcode.c
Expand Up @@ -328,7 +328,7 @@ uint8_t gc_execute_line(char *line) {
}

// Set clockwise/counter-clockwise sign for mc_arc computations
int8_t isclockwise = false;
uint8_t isclockwise = false;
if (gc.motion_mode == MOTION_MODE_CW_ARC) { isclockwise = true; }

// Trace the arc
Expand Down
16 changes: 8 additions & 8 deletions motion_control.c
Expand Up @@ -48,7 +48,7 @@ void mc_dwell(uint32_t milliseconds)
// The arc is approximated by generating a huge number of tiny, linear segments. The length of each
// segment is configured in settings.mm_per_arc_segment.
void mc_arc(double *position, double *target, double *offset, uint8_t axis_0, uint8_t axis_1,
uint8_t axis_linear, double feed_rate, uint8_t invert_feed_rate, double radius, int8_t isclockwise)
uint8_t axis_linear, double feed_rate, uint8_t invert_feed_rate, double radius, uint8_t isclockwise)
{
// int acceleration_manager_was_enabled = plan_is_acceleration_manager_enabled();
// plan_set_acceleration_manager_enabled(false); // disable acceleration management for the duration of the arc
Expand Down Expand Up @@ -106,15 +106,15 @@ void mc_arc(double *position, double *target, double *offset, uint8_t axis_0, ui
double cos_T = 1-0.5*theta_per_segment*theta_per_segment; // Small angle approximation
double sin_T = theta_per_segment;

double trajectory[3];
double arc_target[3];
double sin_Ti;
double cos_Ti;
double r_axisi;
uint16_t i;
int8_t count = 0;

// Initialize the linear axis
trajectory[axis_linear] = position[axis_linear];
arc_target[axis_linear] = position[axis_linear];

for (i = 1; i<segments; i++) { // Increment (segments-1)

Expand All @@ -134,11 +134,11 @@ void mc_arc(double *position, double *target, double *offset, uint8_t axis_0, ui
count = 0;
}

// Update trajectory location
trajectory[axis_0] = center_axis0 + r_axis0;
trajectory[axis_1] = center_axis1 + r_axis1;
trajectory[axis_linear] += linear_per_segment;
plan_buffer_line(trajectory[X_AXIS], trajectory[Y_AXIS], trajectory[Z_AXIS], feed_rate, invert_feed_rate);
// Update arc_target location
arc_target[axis_0] = center_axis0 + r_axis0;
arc_target[axis_1] = center_axis1 + r_axis1;
arc_target[axis_linear] += linear_per_segment;
plan_buffer_line(arc_target[X_AXIS], arc_target[Y_AXIS], arc_target[Z_AXIS], feed_rate, invert_feed_rate);

}
// Ensure last segment arrives at target location.
Expand Down
4 changes: 2 additions & 2 deletions motion_control.h
Expand Up @@ -38,10 +38,10 @@
#ifdef __AVR_ATmega328P__
// Execute an arc in offset mode format. position == current xyz, target == target xyz,
// offset == offset from current xyz, axis_XXX defines circle plane in tool space, axis_linear is
// the direction of helical travel, radius == circle radius, clockwise_sign == -1 or 1. Used
// the direction of helical travel, radius == circle radius, isclockwise boolean. Used
// for vector transformation direction.
void mc_arc(double *position, double *target, double *offset, uint8_t axis_0, uint8_t axis_1,
uint8_t axis_linear, double feed_rate, uint8_t invert_feed_rate, double radius, int8_t isclockwise);
uint8_t axis_linear, double feed_rate, uint8_t invert_feed_rate, double radius, uint8_t isclockwise);
#endif

// Dwell for a couple of time units
Expand Down
11 changes: 7 additions & 4 deletions serial.c
Expand Up @@ -67,7 +67,8 @@ void serial_init(long baud)

void serial_write(uint8_t data) {
// Calculate next head
uint8_t next_head = (tx_buffer_head + 1) % TX_BUFFER_SIZE;
uint8_t next_head = tx_buffer_head + 1;
if (next_head == TX_BUFFER_SIZE) { next_head = 0; }

// Wait until there's a space in the buffer
while (next_head == tx_buffer_tail) { sleep_mode(); };
Expand All @@ -90,7 +91,7 @@ SIGNAL(USART_UDRE_vect) {

// Update tail position
tail ++;
tail %= TX_BUFFER_SIZE;
if (tail == TX_BUFFER_SIZE) { tail = 0; }

// Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
if (tail == tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }
Expand All @@ -104,15 +105,17 @@ uint8_t serial_read()
return SERIAL_NO_DATA;
} else {
uint8_t data = rx_buffer[rx_buffer_tail];
rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE;
rx_buffer_tail++;
if (rx_buffer_tail == RX_BUFFER_SIZE) { rx_buffer_tail = 0; }
return data;
}
}

SIGNAL(USART_RX_vect)
{
uint8_t data = UDR0;
uint8_t next_head = (rx_buffer_head + 1) % RX_BUFFER_SIZE;
uint8_t next_head = rx_buffer_head + 1;
if (next_head == RX_BUFFER_SIZE) { next_head = 0; }

// Write data to buffer unless it is full.
if (next_head != rx_buffer_tail) {
Expand Down
2 changes: 1 addition & 1 deletion settings.c
Expand Up @@ -3,7 +3,7 @@
Part of Grbl
Copyright (c) 2009-2011 Simen Svale Skogsrud
Copyright (c) 2011 Sungeun Jeon
Copyright (c) 2011 Sungeun K. Jeon
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 110faae

Please sign in to comment.