From 9dd1d14707622b5fab4c4df58e377b5f5a94e25d Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Sun, 11 Aug 2013 05:00:55 +0200 Subject: [PATCH 1/4] Added better manual movement code (untested because i don't have access to the hardware right now) --- laser/LaosMenu/LaosMenu.cpp | 23 +---- laser/LaosMotion/LaosMotion.cpp | 178 ++++++++++++++++++++++++++++++++ laser/LaosMotion/LaosMotion.h | 5 +- laser/global.cpp | 2 + laser/global.h | 3 +- 5 files changed, 188 insertions(+), 23 deletions(-) diff --git a/laser/LaosMenu/LaosMenu.cpp b/laser/LaosMenu/LaosMenu.cpp index 182d4b3..c09c749 100755 --- a/laser/LaosMenu/LaosMenu.cpp +++ b/laser/LaosMenu/LaosMenu.cpp @@ -253,27 +253,8 @@ void LaosMenu::Handle() { break; case MOVE: // pos xy - mot->getPosition(&x, &y, &z); - xt = x; yt= y; - switch ( c ) { - case K_DOWN: y+=100*speed; break; - case K_UP: y-=100*speed; break; - case K_LEFT: x-=100*speed; break; - case K_RIGHT: x+=100*speed; break; - case K_OK: case K_CANCEL: screen=MAIN; waitup=1; break; - case K_FUP: screen=FOCUS; break; - case K_FDOWN: screen=FOCUS; break; - case K_ORIGIN: screen=ORIGIN; break; - } - if ((mot->queue() < 5) && ( (x!=xt) || (y != yt) )) { - mot->moveTo(x, y, z, speed/2); - printf("Move: %d %d %d %d\n", x,y,z, speed); - } else { - // if (! mot->ready()) - // printf("Buffer vol\n"); - } - args[0]=x-xoff; - args[1]=y-yoff; + mot->manualMove(); + screen=MAIN; break; case FOCUS: // focus diff --git a/laser/LaosMotion/LaosMotion.cpp b/laser/LaosMotion/LaosMotion.cpp index 4a7d4d6..00e7bca 100755 --- a/laser/LaosMotion/LaosMotion.cpp +++ b/laser/LaosMotion/LaosMotion.cpp @@ -33,6 +33,10 @@ // status leds extern DigitalOut led1,led2,led3,led4; +// bool endstopreached=false; + +static Ticker timer; // the periodic timer used to step + // Inputs; DigitalIn xhome(p8); DigitalIn yhome(p17); @@ -64,6 +68,9 @@ DigitalIn cover(p19); // globals int step=0, command=0; int mark_speed = 100; // 100 [mm/sec] +int counter; +int steps; +int interrupt_busy=0; // next planner action to enqueue tActionRequest action; @@ -406,6 +413,177 @@ void LaosMotion::setOrigin(int x, int y, int z) ofsz = z; } +/** +*** Timers for manual movement +**/ + +void timerMoveY() +{ + if(!interrupt_busy){ + interrupt_busy=1; + ystep = !ystep; +// if(endstopReachedTest()) endstopreached=true; + if(ystep){ + steps++; + } + counter++; + interrupt_busy=0; + } +} + +void timerMoveX() +{ + if(!interrupt_busy){ + interrupt_busy=1; + xstep = !xstep; +// if(endstopReachedTest()) endstopreached=true; + if(xstep){ + steps++; + } + counter++; + interrupt_busy=0; + } +} + +/** +*** Manual movement code +**/ + +void LaosMotion::manualMove() +{ + LaosDisplay *dsp; + int c; +// endstopreached=false; + int countupto = 50; + int speed; + int x,y,z; + int args[5]; + getPosition(&x,&y,&z); + args[0]=x/1000.0; + args[1]=y/1000.0; + dsp->ShowScreen("X: +6543210 mm " "Y: +6543210 mm ", args, NULL); + while(1){ +// if(cover==0 || endstopReached()) return; + c=dsp->read(); + counter=0; + switch(c){ + case K_CANCEL: + return; + case K_UP: + case K_DOWN: + speed=cfg->manualspeed*4; + if(cfg->yscale>0){ + (c==K_UP) ? ydir = 1 : ydir = 0; + }else{ + (c==K_UP) ? ydir = 0 : ydir = 1; + } + timer.attach_us(&timerMoveY,speed); + while(1){ +/* if(endstopreached){ + timer.detach(); + return; + }*/ + c = dsp->read(); + if((c!=K_UP && c!=K_DOWN) || cover==0){ + counter=0; + while(speedmanualspeed*2){ + wait_ms(1); // this "fixes" a (timing?) bug.... doesn't work without this... + if(counter>=countupto){ + speed=speed*1.05; + counter=0; + timer.attach_us(&timerMoveY,speed); + } + } + timer.detach(); + ystep=0; + if(ydir){ + y=y+(steps/(cfg->yscale/1000000.0)); + }else{ + y=y-(steps/(cfg->yscale/1000000.0)); + } + args[0]=x/1000.0; + args[1]=y/1000.0; + dsp->ShowScreen("X: +6543210 mm " "Y: +6543210 mm ", args, NULL); + steps=0; + setPosition(x,y,z); + break; + } + if(counter>=countupto){ + if(cfg->manualspeedmanualspeed) speed=cfg->manualspeed; + timer.attach_us(&timerMoveY,speed); + } + args[0]=x/1000.0; + if(ydir){ + args[1]=y/1000.0+(steps/(cfg->yscale/1000)); + }else{ + args[1]=y/1000.0-(steps/(cfg->yscale/1000)); + } + dsp->ShowScreen("X: +6543210 mm " "Y: +6543210 mm ", args, NULL); + counter=0; + } + } + break; + case K_LEFT: + case K_RIGHT: + speed=cfg->manualspeed*4; + if(cfg->xscale>0){ + (c==K_RIGHT) ? xdir = 1 : xdir = 0; + }else{ + (c==K_RIGHT) ? xdir = 0 : xdir = 1; + } + timer.attach_us(&timerMoveX,speed); + while(1){ +/* if(endstopreached){ + timer.detach(); + return; + }*/ + c = dsp->read(); + if((c!=K_LEFT && c!=K_RIGHT) || cover==0){ + counter=0; + while(speedmanualspeed*2){ + wait_ms(1); // this "fixes" a (timing?) bug.... doesn't work without this... + if(counter>=countupto){ + speed=speed*1.05; + counter=0; + timer.attach_us(&timerMoveX,speed); + } + } + timer.detach(); + xstep=0; + if(xdir){ + x=x+(steps/(cfg->xscale/1000000.0)); + }else{ + x=x-(steps/(cfg->xscale/1000000.0)); + } + args[0]=x/1000.0; + args[1]=y/1000.0; + dsp->ShowScreen("X: +6543210 mm " "Y: +6543210 mm ", args, NULL); + steps=0; + setPosition(x,y,z); + break; + } + if(counter>=countupto){ + if(cfg->manualspeedmanualspeed) speed=cfg->manualspeed; + timer.attach_us(&timerMoveX,speed); + } + args[1]=y/1000.0; + if(ydir){ + args[0]=x/1000.0+(steps/(cfg->xscale/1000)); + }else{ + args[0]=x/1000.0-(steps/(cfg->xscale/1000)); + } + dsp->ShowScreen("X: +6543210 mm " "Y: +6543210 mm ", args, NULL); + counter=0; + } + } + break; + } + } +} diff --git a/laser/LaosMotion/LaosMotion.h b/laser/LaosMotion/LaosMotion.h index 9050496..12e0f16 100644 --- a/laser/LaosMotion/LaosMotion.h +++ b/laser/LaosMotion/LaosMotion.h @@ -55,11 +55,14 @@ class LaosMotion { void setOrigin(int x, int y, int z); // set the origin to this absolute position [micron] void moveTo(int x, int y, int z); // move (jog) to a specific position [microns] void moveTo(int x, int y, int z, int speed); // move (jog) to a specific position [microns] + void manualMove(); // manual move from menu int queue(); // queued items private: }; - +// Timers for manual movement +void timerMoveX(); +void timerMoveY(); #endif diff --git a/laser/global.cpp b/laser/global.cpp index afa4041..0c03359 100644 --- a/laser/global.cpp +++ b/laser/global.cpp @@ -147,5 +147,7 @@ GlobalConfig::GlobalConfig(char *filename) cfg.Value("motion.accel", &accel, 100); // accelleration [mm/sec2] cfg.Value("motion.enable", &enable, 0); // enable output polarity [0/1] cfg.Value("motion.tolerance", &tolerance, 50); // cornering tolerance [1/1000 units] + + cfg.Value("motion.manualspeed", &manualspeed, 10); // speed during manual movement [usec/step / 2] } diff --git a/laser/global.h b/laser/global.h index 97f39e0..e245001 100644 --- a/laser/global.h +++ b/laser/global.h @@ -54,10 +54,11 @@ class GlobalConfig int zscale; // steps per meter int escale; // steps per meter int lenable, lon, pwmmin, pwmmax, pwmfreq; // laser enable, laser on and pwm min/max [%] and frequency [Hz]; + int manualspeed; // speed used for homing [usec/step / 2] GlobalConfig(char *filename); }; extern GlobalConfig *cfg; #define VERSION_STRING "\033LAOS v0.3" __DATE__ " " __TIME__ -#endif \ No newline at end of file +#endif From 8546fef218b456db9f70f0696c2dd4bfa3caaa1e Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Sun, 11 Aug 2013 05:31:45 +0200 Subject: [PATCH 2/4] added cancel-function to RUNNING-mode --- laser/LaosMenu/LaosMenu.cpp | 37 ++++++++++++++++++++++++------- laser/LaosMenu/LaosMenu.h | 13 ++++++++++- laser/LaosMotion/LaosMotion.cpp | 11 +++++++-- laser/LaosMotion/LaosMotion.h | 1 + laser/LaosMotion/grbl/planner.cpp | 4 ++++ laser/LaosMotion/grbl/planner.h | 3 +++ laser/LaosMotion/grbl/stepper.cpp | 8 +++++-- laser/LaosMotion/grbl/stepper.h | 4 ++++ 8 files changed, 68 insertions(+), 13 deletions(-) diff --git a/laser/LaosMenu/LaosMenu.cpp b/laser/LaosMenu/LaosMenu.cpp index c09c749..a2b6c5b 100755 --- a/laser/LaosMenu/LaosMenu.cpp +++ b/laser/LaosMenu/LaosMenu.cpp @@ -175,6 +175,27 @@ void LaosMenu::SetScreen(char *msg) { Handle(); } +/** +*** Check if cancel button is pressed (should only be used while running jobs!) +**/ +void LaosMenu::checkCancel() { + c = dsp->read(); + if(c==K_CANCEL || !mot->isStart()/* || mot->endstopReached()*/){ + fclose(runfile); + runfile = NULL; + screen = MAIN; + canceled=1; + mot->clearBuffer(); + mot->reset(); + mot->isHome=false; + printf("cancel pressed!\r\n"); + } + if(c==K_FUP){ + skipped=1; + } +} + + /** *** Handle menu system *** Read keys, and plan next action on the screen, output screen if @@ -374,12 +395,6 @@ void LaosMenu::Handle() { case RUNNING: // Screen while running switch ( c ) { - /* case K_CANCEL: - while (mot->queue()); - mot->reset(); - if (runfile != NULL) fclose(runfile); - runfile=NULL; screen=MAIN; menu=MAIN; - break; */ default: if (runfile == NULL) { runfile = sd.openfile(jobname, "rb"); @@ -388,15 +403,21 @@ void LaosMenu::Handle() { else mot->reset(); } else { + canceled=0; #ifdef READ_FILE_DEBUG printf("Parsing file: \n"); #endif - while ((!feof(runfile)) && mot->ready()) + while (!canceled && ((!feof(runfile)) && mot->ready())){ + checkCancel(); mot->write(readint(runfile)); + } + while(!canceled && mot->queue()>0){ + checkCancel(); + } #ifdef READ_FILE_DEBUG printf("File parsed \n"); #endif - if (feof(runfile) && mot->ready()) { + if (!canceled && feof(runfile) && mot->ready()) { fclose(runfile); runfile = NULL; mot->moveTo(cfg->xrest, cfg->yrest, cfg->zrest); diff --git a/laser/LaosMenu/LaosMenu.h b/laser/LaosMenu/LaosMenu.h index a26bd80..747805b 100644 --- a/laser/LaosMenu/LaosMenu.h +++ b/laser/LaosMenu/LaosMenu.h @@ -44,6 +44,8 @@ extern "C" void mbed_reset(); extern void plan_get_current_position_xyz(float *x, float *y, float *z); +extern LaosMotion *mot; + class LaosMenu { public: /** Make new LaosMenu object. @@ -58,7 +60,8 @@ class LaosMenu { void SetScreen(int screen); void SetScreen(char *s); void SetFileName(char * name); - + void checkCancel(); + private: // LaosDisplay *display; int args[5]; @@ -67,6 +70,14 @@ class LaosMenu { int speed; char jobname[MAXFILESIZE]; + // button input character + int c; + + // cancel/skip markers + int canceled=0; + int skipped=0; + + // menu states int screen, prevscreen, lastscreen; unsigned char menu, ipfield, iofield; diff --git a/laser/LaosMotion/LaosMotion.cpp b/laser/LaosMotion/LaosMotion.cpp index 00e7bca..1421c5d 100755 --- a/laser/LaosMotion/LaosMotion.cpp +++ b/laser/LaosMotion/LaosMotion.cpp @@ -190,8 +190,15 @@ int LaosMotion::queue() return plan_queue_items(); } - - +/** +*** clearBuffer() +*** clears the grbl buffer +**/ +void LaosMotion::clearBuffer() +{ + plan_clear_buffer(); + clear_current_block(); +} /** *** MoveTo() diff --git a/laser/LaosMotion/LaosMotion.h b/laser/LaosMotion/LaosMotion.h index 12e0f16..7061c16 100644 --- a/laser/LaosMotion/LaosMotion.h +++ b/laser/LaosMotion/LaosMotion.h @@ -57,6 +57,7 @@ class LaosMotion { void moveTo(int x, int y, int z, int speed); // move (jog) to a specific position [microns] void manualMove(); // manual move from menu int queue(); // queued items + void clearBuffer(); private: }; diff --git a/laser/LaosMotion/grbl/planner.cpp b/laser/LaosMotion/grbl/planner.cpp index a020970..fc77064 100755 --- a/laser/LaosMotion/grbl/planner.cpp +++ b/laser/LaosMotion/grbl/planner.cpp @@ -336,6 +336,10 @@ void plan_set_acceleration_manager_enabled(uint8_t enabled) { } } +void plan_clear_buffer(){ + while(plan_get_current_block()!=NULL) plan_discard_current_block(); +} + int plan_is_acceleration_manager_enabled() { return(acceleration_manager_enabled); } diff --git a/laser/LaosMotion/grbl/planner.h b/laser/LaosMotion/grbl/planner.h index 0efb9c1..5d98c8e 100644 --- a/laser/LaosMotion/grbl/planner.h +++ b/laser/LaosMotion/grbl/planner.h @@ -114,6 +114,9 @@ block_t *plan_get_current_block(); // Enables or disables acceleration-management for upcoming blocks void plan_set_acceleration_manager_enabled(uint8_t enabled); +// Clear Buffers +void plan_clear_buffer(); + // Is acceleration-management currently enabled? int plan_is_acceleration_manager_enabled(); diff --git a/laser/LaosMotion/grbl/stepper.cpp b/laser/LaosMotion/grbl/stepper.cpp index e1cba36..2540ec2 100755 --- a/laser/LaosMotion/grbl/stepper.cpp +++ b/laser/LaosMotion/grbl/stepper.cpp @@ -52,7 +52,6 @@ typedef enum {RAMP_UP, RAMP_MAX, RAMP_DOWN} tRamp; // Prototypes static void st_interrupt (); static void set_step_timer (uint32_t cycles); -static void st_go_idle(); // Globals volatile unsigned char busy = 0; @@ -194,9 +193,14 @@ void st_wake_up() } } +// Remove current buffer-block +void clear_current_block(){ + current_block = NULL; +} + // When not stepping, go to idle mode. Steppers can be switched off, or set to reduced current // (some delay might have to be implemented). Currently no motor switchoff is done. -static void st_go_idle() +void st_go_idle() { timer.detach(); running = 0; diff --git a/laser/LaosMotion/grbl/stepper.h b/laser/LaosMotion/grbl/stepper.h index 4899328..b33f14f 100755 --- a/laser/LaosMotion/grbl/stepper.h +++ b/laser/LaosMotion/grbl/stepper.h @@ -104,6 +104,10 @@ void st_init(); // Block until all buffered steps are executed void st_synchronize(); +// Execute to kill everything that moves +void st_go_idle(); +void clear_current_block(); + // Execute the homing cycle void st_go_home(); From 16e616a87d635ddf7d968923f27228166cb7a9c9 Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Sun, 11 Aug 2013 05:36:12 +0200 Subject: [PATCH 3/4] Added cancel function to HOMING-mode --- laser/LaosMotion/LaosMotion.cpp | 36 +++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/laser/LaosMotion/LaosMotion.cpp b/laser/LaosMotion/LaosMotion.cpp index 1421c5d..20467e3 100755 --- a/laser/LaosMotion/LaosMotion.cpp +++ b/laser/LaosMotion/LaosMotion.cpp @@ -599,7 +599,12 @@ void LaosMotion::manualMove() **/ void LaosMotion::home(int x, int y, int z) { + LaosDisplay *dsp; int i=0; + int canceled = 0; + int c = 0; + int counter = 0; + int countupto = (cfg->xscale/1000)*5; // check cancel button state every 5mm (maybe) printf("Homing %d,%d, %d with speed %d\n", x, y, z, cfg->homespeed); xdir = cfg->xhomedir; ydir = cfg->yhomedir; @@ -608,16 +613,36 @@ void LaosMotion::home(int x, int y, int z) isHome = false; printf("Home Z...\n\r"); if (cfg->autozhome) { - while ((zmin ^ cfg->zpol) && (zmax ^ cfg->zpol)) { - zstep = 0; + while ((zmin ^ cfg->zpol) && (zmax ^ cfg->zpol) && !canceled) { + if(counter==countupto){ + c = dsp->read(); + if(c==K_CANCEL || cover==0){ + isHome = false; + return; + } + counter=0; + }else{ + counter++; + } + zstep = 0; wait(cfg->homespeed/1E6); zstep = 1; wait(cfg->homespeed/1E6); } } - printf("Home XY...\n\r"); - while ( 1 ) + printf("Home XY...\r\n"); + while ( 1 && !canceled ) { + if(counter==countupto){ + c = dsp->read(); + if(c==K_CANCEL || cover==0){ + isHome = false; + return; + } + counter=0; + }else{ + counter++; + } xstep = ystep = 0; wait(cfg->homespeed/1E6); xstep = xhome ^ cfg->xpol; @@ -632,11 +657,10 @@ void LaosMotion::home(int x, int y, int z) setPosition(x,y,z); moveTo(x,y,z); isHome = true; - printf("Home done.\n\r"); + printf("Home done.\r\n"); return; } } - } From 18a4502332408674345889e2c9b9c17201fcbe49 Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Sun, 11 Aug 2013 18:15:52 +0200 Subject: [PATCH 4/4] changed OPTIMIZATION flag because of weird lags --- laser/makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/laser/makefile b/laser/makefile index 0f90aa5..24af4c2 100644 --- a/laser/makefile +++ b/laser/makefile @@ -16,5 +16,7 @@ GCC4MBED_DIR=../../gcc4mbed LIBS_PREFIX= LIBS_SUFFIX= +OPTIMIZATION=1 + include ../../gcc4mbed/build/gcc4mbed.mk #include gcc4mbed.mk