Adam Resnick / StepperMotors

Dependents:   CNCAirbrushCode

Revision:
1:cf60b60a1b5b
Parent:
0:3058939fa37c
--- a/LinearMotion.cpp	Sat Apr 28 21:08:58 2012 +0000
+++ b/LinearMotion.cpp	Fri May 04 06:25:41 2012 +0000
@@ -1,8 +1,5 @@
 #include  "LinearMotion.h"
-#define STEPS_PER_PIXEL 600
-#define Z_STEPS_PER_PIXEL 960
-#define STEP_BUFFER 4 // Make a gap of STEPS_PER_PIXEL/STEP_BUFFER  between each pixel
-#define INITIAL_DELAY 1200 // in us
+
 
 void LinearMotion::setStepMotors(Stepper * s1, Stepper * s2, Stepper * s3, DigitalOut * solenoid, bool * pause, bool * stop) {
     _sol = solenoid;
@@ -93,12 +90,12 @@
                      Stepper ** stepMotor, int maxSpeed, bool solenoidOn) {
     int fxy,fxz;
     int rowLoc = 0;
-    int delay = INITIAL_DELAY;
+    int delay = _initial_delay;
     int delayPos = 0;
     Vector lastRow = {-nextRow.x, -nextRow.y, -nextRow.z};
     Vector dir = {1, 1, 1};
     long stepsPerPixel;
-    _z_slantways ? stepsPerPixel = Z_STEPS_PER_PIXEL : stepsPerPixel = STEPS_PER_PIXEL;
+    _z_slantways ? stepsPerPixel = _z_steps_per_pixel : stepsPerPixel = _steps_per_pixel;
     
     if(delta.x < 0)
         dir.x = 0; //towards motor
@@ -160,11 +157,11 @@
 void LinearMotion::doLinear(Vector delta, Stepper** stepMotor, int maxSpeed, bool solenoidOn) {
     int fxy,fxz;
     int pixelLoc = 0;
-    int delay = INITIAL_DELAY;
+    int delay = _initial_delay;
     int farthestDelay = 0;
     Vector dir = {1, 1, 1};
     long stepsPerPixel;
-    _z ? stepsPerPixel = Z_STEPS_PER_PIXEL : stepsPerPixel = STEPS_PER_PIXEL;
+    _z ? stepsPerPixel = _z_steps_per_pixel : stepsPerPixel = _steps_per_pixel;
     
     if(delta.x < 0)
         dir.x = 0; //towards motor
@@ -184,11 +181,11 @@
     curPos.z = 0;
     while (!(*_stopped) && (curPos.x<=delta.x)&&(curPos.y<=delta.y)&&(curPos.z<=delta.z)){
         if (solenoidOn) {
-            if ((reversing && ((delta.x - curPos.x - stepsPerPixel / STEP_BUFFER) % stepsPerPixel == 0)) ||
-               (!reversing && ((curPos.x + stepsPerPixel / STEP_BUFFER) % stepsPerPixel == 0)))
+            if ((reversing && ((delta.x - curPos.x - stepsPerPixel / _step_buffer) % stepsPerPixel == 0)) ||
+               (!reversing && ((curPos.x + stepsPerPixel / _step_buffer) % stepsPerPixel == 0)))
                 *_sol = _bmp.isPixel(pixelLoc++, reversing);
-            else if ((reversing &&  ((delta.x - curPos.x + stepsPerPixel / STEP_BUFFER) % stepsPerPixel == 0)) ||
-                    (!reversing &&  ((curPos.x - stepsPerPixel / STEP_BUFFER) % stepsPerPixel == 0)) )
+            else if ((reversing &&  ((delta.x - curPos.x + stepsPerPixel / _step_buffer) % stepsPerPixel == 0)) ||
+                    (!reversing &&  ((curPos.x - stepsPerPixel / _step_buffer) % stepsPerPixel == 0)) )
                 *_sol = 0;
         }
         ++curPos.x;
@@ -218,11 +215,20 @@
         wait_us(delay);  //Implement linear accelleration!
         if (*_paused) {
             enableSteppers(false);
+            *_sol = 0;
             while(*_paused){printf("Pausing!\n\r");}
             enableSteppers(true);
         }
     }
+    *_sol = 0;
 }
+
+void LinearMotion::updateSettings(long steps_per_pixel, long initial_delay, long step_buffer) {
+    _steps_per_pixel = steps_per_pixel;
+    _z_steps_per_pixel = (steps_per_pixel * 8) / 5;
+    _step_buffer = step_buffer; // Make a gap of _steps_per_pixel/_step_buffer  between each pixel
+    _initial_delay = initial_delay; // in us
+} 
       
 int LinearMotion::delayTime (int oldDelay, int stepNumber) {
     return oldDelay - (2*oldDelay)/(4*stepNumber + 1);