From 4143351780cf1504f3da3a7a16d192eec05609d5 Mon Sep 17 00:00:00 2001 From: Anders Sandstrom Date: Mon, 17 Jan 2022 17:13:38 +0100 Subject: [PATCH] WIP --- .gitignore | 4 +- build/.gitignore | 4 -- grbl/coolant_control.c | 1 - grbl/eeprom.c | 11 ++-- grbl/grbl.h | 1 + grbl/limits.c | 4 +- grbl/motion_control.c | 1 - grbl/nuts_bolts.c | 32 +++++++++--- grbl/nuts_bolts.h | 1 + grbl/print.c | 6 +-- grbl/settings.c | 2 +- grbl/spindle_control.c | 5 +- grbl/stepper.c | 112 ++++++++++++++++++++--------------------- 13 files changed, 99 insertions(+), 85 deletions(-) delete mode 100644 build/.gitignore diff --git a/.gitignore b/.gitignore index b61325d..d1ccd2c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ *.elf *.DS_Store *.d - -README.md +O.* +*.Makefile diff --git a/build/.gitignore b/build/.gitignore deleted file mode 100644 index 86d0cb2..0000000 --- a/build/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# Ignore everything in this directory -* -# Except this file -!.gitignore \ No newline at end of file diff --git a/grbl/coolant_control.c b/grbl/coolant_control.c index 034e7b1..73c2d61 100644 --- a/grbl/coolant_control.c +++ b/grbl/coolant_control.c @@ -20,7 +20,6 @@ #include "grbl.h" - void coolant_init() { printf("%s:%s:%d Not supported yet..\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/grbl/eeprom.c b/grbl/eeprom.c index e6da28d..0807c3c 100644 --- a/grbl/eeprom.c +++ b/grbl/eeprom.c @@ -28,7 +28,8 @@ // ecmc added #include #include -#define EEPROM_DUMMY_FILE ./ecmc_grbl_eeprom.txt +#include +#define EEPROM_DUMMY_FILE "./ecmc_grbl_eeprom.txt" #define EEPROM_MEM_SIZE 512 char buffer[EEPROM_MEM_SIZE]; @@ -46,7 +47,7 @@ char buffer[EEPROM_MEM_SIZE]; /* Define to reduce code size. */ //#define EEPROM_IGNORE_SELFPROG //!< Remove SPM flag polling. - +unsigned char ecmc_mem_to_file(); // Init file void ecmc_init_file() { printf("%s:%s:%d EEPROM simulated by file..\n",__FILE__,__FUNCTION__,__LINE__); @@ -67,7 +68,7 @@ unsigned char ecmc_file_to_mem() return 1; } unsigned char c = 0; - for (int i = 0, i < EEPROM_MEM_SIZE ; i++) { + for (int i = 0; i < EEPROM_MEM_SIZE ; i++) { // Get the characters buffer[i] = fgetc(fh); } @@ -77,7 +78,7 @@ unsigned char ecmc_file_to_mem() } // Write buffer[] to file -unsigned char void ecmc_mem_to_file() +unsigned char ecmc_mem_to_file() { printf("%s:%s:%d EEPROM simulated by file..\n",__FILE__,__FUNCTION__,__LINE__); @@ -88,7 +89,7 @@ unsigned char void ecmc_mem_to_file() printf("something went wrong and file could not be opened"); return 1; } - for (int i = 0, i < EEPROM_MEM_SIZE ; i++) { + for (int i = 0; i < EEPROM_MEM_SIZE ; i++) { // Get the characters fputc (buffer[i], fh); } diff --git a/grbl/grbl.h b/grbl/grbl.h index 2af4d40..309898c 100644 --- a/grbl/grbl.h +++ b/grbl/grbl.h @@ -37,6 +37,7 @@ #include #include #include +#include // Define the Grbl system include files. NOTE: Do not alter organization. #include "config.h" diff --git a/grbl/limits.c b/grbl/limits.c index 931f430..7c3de1a 100644 --- a/grbl/limits.c +++ b/grbl/limits.c @@ -20,7 +20,7 @@ */ #include "grbl.h" -#include "ecmcMotion.h" +//#include "ecmcMotion.h" // Homing axis search distance multiplier. Computed by this value times the cycle travel. #ifndef HOMING_AXIS_SEARCH_SCALAR @@ -413,7 +413,7 @@ void limits_go_home(uint8_t cycle_mask) // } // } // sys.step_control = STEP_CONTROL_NORMAL_OP; // Return step control to normal operation. -//} +} // Performs a soft limit check. Called from mc_line() only. Assumes the machine has been homed, diff --git a/grbl/motion_control.c b/grbl/motion_control.c index 2544803..919e414 100644 --- a/grbl/motion_control.c +++ b/grbl/motion_control.c @@ -21,7 +21,6 @@ #include "grbl.h" - // Execute linear motion in absolute millimeter coordinates. Feed rate given in millimeters/second // unless invert_feed_rate is true. Then the feed_rate means that the motion should be completed in // (1 minute)/feed_rate time. diff --git a/grbl/nuts_bolts.c b/grbl/nuts_bolts.c index 6df70f4..7e9b48d 100644 --- a/grbl/nuts_bolts.c +++ b/grbl/nuts_bolts.c @@ -26,8 +26,6 @@ //added for ecmc #include -CLOCK_MONOTONIC - // Extracts a floating point value from a string. The following code is based loosely on // the avr-libc strtod() function by Michael Stumpf and Dmitry Xmelkov and many freely @@ -115,9 +113,13 @@ uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr) // Non-blocking delay function used for general operation and suspend features. void delay_sec(float seconds, uint8_t mode) { - - clock_nanosleep(CLOCK_MONOTONIC,0,1E9); - + struct timespec deadline; + clock_gettime(CLOCK_MONOTONIC,&deadline); + deadline.tv_sec+=1*seconds; + clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME, &deadline,NULL); + + // NOTE!!!: mode not used!!!! + //uint16_t i = ceil(1000/DWELL_TIME_STEP*seconds); //while (i-- > 0) { // if (sys.abort) { return; } @@ -137,7 +139,15 @@ void delay_sec(float seconds, uint8_t mode) // which only accepts constants in future compiler releases. void delay_ms(uint16_t ms) { - clock_nanosleep(CLOCK_MONOTONIC,0,1E6); + struct timespec deadline; + clock_gettime(CLOCK_MONOTONIC,&deadline); + deadline.tv_nsec+=1E6*ms; + if(deadline.tv_nsec>=1E9) { + deadline.tv_nsec-=1E6; + deadline.tv_sec++; + } + clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME, &deadline,NULL); + //while ( ms-- ) { _delay_ms(1); } } @@ -147,7 +157,15 @@ void delay_ms(uint16_t ms) // efficiently with larger delays, as the counter adds parasitic time in each iteration. void delay_us(uint32_t us) { - clock_nanosleep(CLOCK_MONOTONIC,0,1E3); + struct timespec deadline; + clock_gettime(CLOCK_MONOTONIC,&deadline); + deadline.tv_nsec+=1E3*us; + if(deadline.tv_nsec>=1E9) { + deadline.tv_nsec-=1E6; + deadline.tv_sec++; + } + clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME, &deadline,NULL); + //while (us) { // if (us < 10) { diff --git a/grbl/nuts_bolts.h b/grbl/nuts_bolts.h index c936f2b..58f3193 100644 --- a/grbl/nuts_bolts.h +++ b/grbl/nuts_bolts.h @@ -42,6 +42,7 @@ #endif // Conversions +#define F_CPU (16000000) #define MM_PER_INCH (25.40) #define INCH_PER_MM (0.0393701) #define TICKS_PER_MICROSECOND (F_CPU/1000000) diff --git a/grbl/print.c b/grbl/print.c index 61e28aa..6acbcf8 100644 --- a/grbl/print.c +++ b/grbl/print.c @@ -34,7 +34,7 @@ void printString(const char *s) // Print a string stored in PGM-memory void printPgmString(const char *s) { - printf("%s:%s:%d: Not supported\n",__FILE__,__FUNCTION__,__LINE__,s); + printf("%s:%s:%d: %s\n",__FILE__,__FUNCTION__,__LINE__,s); // char c; // while ((c = pgm_read_byte_near(s++))) // serial_write(c); @@ -88,7 +88,7 @@ void print_uint8_base10(uint8_t n) void print_uint8_base2_ndigit(uint8_t n, uint8_t digits) { - printf("%s:%s:%d:\n",__FILE__,__FUNCTION__,__LINE__,n); + printf("%s:%s:%d:%d\n",__FILE__,__FUNCTION__,__LINE__,n); unsigned char buf[digits]; uint8_t i = 0; @@ -130,7 +130,7 @@ void print_uint32_base10(uint32_t n) void printInteger(long n) { - printf("%s:%s:%d:%d\n",__FILE__,__FUNCTION__,__LINE__,n); + printf("%s:%s:%d:%ld\n",__FILE__,__FUNCTION__,__LINE__,n); //if (n < 0) { // serial_write('-'); diff --git a/grbl/settings.c b/grbl/settings.c index a9c830e..6e34cc8 100644 --- a/grbl/settings.c +++ b/grbl/settings.c @@ -23,7 +23,7 @@ settings_t settings; -const __flash settings_t defaults = {\ +const settings_t defaults = {\ .pulse_microseconds = DEFAULT_STEP_PULSE_MICROSECONDS, .stepper_idle_lock_time = DEFAULT_STEPPER_IDLE_LOCK_TIME, .step_invert_mask = DEFAULT_STEPPING_INVERT_MASK, diff --git a/grbl/spindle_control.c b/grbl/spindle_control.c index 8b1525d..ca24afa 100644 --- a/grbl/spindle_control.c +++ b/grbl/spindle_control.c @@ -21,7 +21,6 @@ #include "grbl.h" - #ifdef VARIABLE_SPINDLE static float pwm_gradient; // Precalulated value to speed up rpm to PWM conversions. #endif @@ -149,9 +148,9 @@ void spindle_stop() // } else { // SPINDLE_TCCRA_REGISTER |= (1< +#include // Some useful constants. #define DT_SEGMENT (1.0/(ACCELERATION_TICKS_PER_SECOND*60.0)) // min/segment @@ -223,28 +224,27 @@ static bool stepperInterruptEnable = 0; void ecmc_grbl_main_thread(); pthread_t tid; -void ecmc_dummy_thread() { +void *ecmc_dummy_thread(void *ptr) { while (stepperInterruptEnable) { for(int i=0; i< 30;i++) { ecmc_grbl_main_thread(); } - printf("%s:%s:%d Positions(x,y,x)=%lf,%lf,%lf..\n",__FILE__,__FUNCTION__,__LINE__,sys_position[X_AXIS], sys_position[Y_AXIS],sys_position[Z_AXIS] ); + printf("%s:%s:%d Positions(x,y,x)=%d,%d,%d..\n",__FILE__,__FUNCTION__,__LINE__,sys_position[X_AXIS], sys_position[Y_AXIS],sys_position[Z_AXIS] ); sleep(0.001); } } - void ecmc_start_dummy_thread() { int i = 0; int err; - err = pthread_create(&(tid), NULL, &ecmc_dummy_thread, NULL); + err = pthread_create(&(tid), NULL, *ecmc_dummy_thread, NULL); if (err != 0) printf("\ncan't create thread :[%s]", strerror(err)); else printf("\n Thread created successfully\n"); i++; - return 0; + return; } // Stepper state initialization. Cycle should only start if the st.cycle_start flag is @@ -252,22 +252,22 @@ void ecmc_start_dummy_thread() void st_wake_up() { // Enable stepper drivers. - if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { STEPPERS_DISABLE_PORT |= (1<> 3); - // Set delay between direction pin write and step command. - OCR0A = -(((settings.pulse_microseconds)*TICKS_PER_MICROSECOND) >> 3); - #else // Normal operation - // Set step pulse time. Ad hoc computation from oscilloscope. Uses two's complement. - st.step_pulse_time = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND) >> 3); - #endif +// st.step_outbits = step_port_invert_mask; +// +// // Initialize step pulse timing from settings. Here to ensure updating after re-writing. +// #ifdef STEP_PULSE_DELAY +// // Set total step pulse time after direction pin set. Ad hoc computation from oscilloscope. +// st.step_pulse_time = -(((settings.pulse_microseconds+STEP_PULSE_DELAY-2)*TICKS_PER_MICROSECOND) >> 3); +// // Set delay between direction pin write and step command. +// //OCR0A = -(((settings.pulse_microseconds)*TICKS_PER_MICROSECOND) >> 3); +// #else // Normal operation +// // Set step pulse time. Ad hoc computation from oscilloscope. Uses two's complement. +// st.step_pulse_time = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND) >> 3); +// #endif // Enable Stepper Driver Interrupt //TIMSK1 |= (1<