200117StepMotorControl

Dependencies:   X_NUCLEO_IHM02A1 TextLCD

Revision:
18:591a007effc2
Parent:
15:698fe51556c0
Child:
22:e81ccf73bc5d
diff -r 09f408175202 -r 591a007effc2 main.cpp
--- a/main.cpp	Fri Mar 11 16:03:13 2016 +0000
+++ b/main.cpp	Thu Apr 07 16:56:27 2016 +0000
@@ -55,8 +55,8 @@
 #define MPR_1 4
 
 /* Number of steps. */
-#define STEPS_1 100000
-#define STEPS_2 200000
+#define STEPS_1 (400 * 128)   /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
+#define STEPS_2 (STEPS_1 * 2)
 
 /* Delay in milliseconds. */
 #define DELAY_1 1000
@@ -74,58 +74,58 @@
 {
     /* First Motor. */
     {
-        9.0,                /* Motor supply voltage in V. */
-        400,                /* Min number of steps per revolution for the motor. */
-        1.7,                /* Max motor phase voltage in A. */
-        3.06,               /* Max motor phase voltage in V. */
-        300.0,              /* Motor initial speed [step/s]. */
-        500.0,              /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
-        500.0,              /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
-        992.0,              /* Motor maximum speed [step/s]. */
-        0.0,                /* Motor minimum speed [step/s]. */
-        602.7,              /* Motor full-step speed threshold [step/s]. */
-        3.06,               /* Holding kval [V]. */
-        3.06,               /* Constant speed kval [V]. */
-        3.06,               /* Acceleration starting kval [V]. */
-        3.06,               /* Deceleration starting kval [V]. */
-        61.52,              /* Intersect speed for bemf compensation curve slope changing [step/s]. */
-        392.1569e-6,        /* Start slope [s/step]. */
-        643.1372e-6,        /* Acceleration final slope [s/step]. */
-        643.1372e-6,        /* Deceleration final slope [s/step]. */
-        0,                  /* Thermal compensation factor (range [0, 15]). */
-        3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
-        3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
-        MICROSTEP_1_128,    /* Step mode selection. */
-        0xFF,               /* Alarm conditions enable. */
-        0x2E88              /* Ic configuration. */
+        9.0,                           /* Motor supply voltage in V. */
+        400,                           /* Min number of steps per revolution for the motor. */
+        1.7,                           /* Max motor phase voltage in A. */
+        3.06,                          /* Max motor phase voltage in V. */
+        300.0,                         /* Motor initial speed [step/s]. */
+        500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
+        500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
+        992.0,                         /* Motor maximum speed [step/s]. */
+        0.0,                           /* Motor minimum speed [step/s]. */
+        602.7,                         /* Motor full-step speed threshold [step/s]. */
+        3.06,                          /* Holding kval [V]. */
+        3.06,                          /* Constant speed kval [V]. */
+        3.06,                          /* Acceleration starting kval [V]. */
+        3.06,                          /* Deceleration starting kval [V]. */
+        61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
+        392.1569e-6,                   /* Start slope [s/step]. */
+        643.1372e-6,                   /* Acceleration final slope [s/step]. */
+        643.1372e-6,                   /* Deceleration final slope [s/step]. */
+        0,                             /* Thermal compensation factor (range [0, 15]). */
+        3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
+        3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
+        StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
+        0xFF,                          /* Alarm conditions enable. */
+        0x2E88                         /* Ic configuration. */
     },
 
     /* Second Motor. */
     {
-        9.0,                /* Motor supply voltage in V. */
-        400,                /* Min number of steps per revolution for the motor. */
-        1.7,                /* Max motor phase voltage in A. */
-        3.06,               /* Max motor phase voltage in V. */
-        300.0,              /* Motor initial speed [step/s]. */
-        500.0,              /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
-        500.0,              /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
-        992.0,              /* Motor maximum speed [step/s]. */
-        0.0,                /* Motor minimum speed [step/s]. */
-        602.7,              /* Motor full-step speed threshold [step/s]. */
-        3.06,               /* Holding kval [V]. */
-        3.06,               /* Constant speed kval [V]. */
-        3.06,               /* Acceleration starting kval [V]. */
-        3.06,               /* Deceleration starting kval [V]. */
-        61.52,              /* Intersect speed for bemf compensation curve slope changing [step/s]. */
-        392.1569e-6,        /* Start slope [s/step]. */
-        643.1372e-6,        /* Acceleration final slope [s/step]. */
-        643.1372e-6,        /* Deceleration final slope [s/step]. */
-        0,                  /* Thermal compensation factor (range [0, 15]). */
-        3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
-        3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
-        MICROSTEP_1_128,    /* Step mode selection. */
-        0xFF,               /* Alarm conditions enable. */
-        0x2E88              /* Ic configuration. */
+        9.0,                           /* Motor supply voltage in V. */
+        400,                           /* Min number of steps per revolution for the motor. */
+        1.7,                           /* Max motor phase voltage in A. */
+        3.06,                          /* Max motor phase voltage in V. */
+        300.0,                         /* Motor initial speed [step/s]. */
+        500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
+        500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
+        992.0,                         /* Motor maximum speed [step/s]. */
+        0.0,                           /* Motor minimum speed [step/s]. */
+        602.7,                         /* Motor full-step speed threshold [step/s]. */
+        3.06,                          /* Holding kval [V]. */
+        3.06,                          /* Constant speed kval [V]. */
+        3.06,                          /* Acceleration starting kval [V]. */
+        3.06,                          /* Deceleration starting kval [V]. */
+        61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
+        392.1569e-6,                   /* Start slope [s/step]. */
+        643.1372e-6,                   /* Acceleration final slope [s/step]. */
+        643.1372e-6,                   /* Deceleration final slope [s/step]. */
+        0,                             /* Thermal compensation factor (range [0, 15]). */
+        3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
+        3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
+        StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
+        0xFF,                          /* Alarm conditions enable. */
+        0x2E88                         /* Ic configuration. */
     }
 };
 
@@ -162,7 +162,7 @@
 
     /* Getting the current position. */
     int position = motors[0]->GetPosition();
-    
+
     /* Printing to the console. */
     printf("--> Getting the current position: %d\r\n", position);
 
@@ -254,6 +254,59 @@
     printf("--> Getting the current position: %d\r\n", position);
 
     /* Waiting. */
+    wait_ms(DELAY_1);
+
+    /* Printing to the console. */
+    printf("--> Halving the microsteps.\r\n");
+
+    /* Halving the microsteps. */
+    init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel -  1 : init[0].step_sel);
+    if (!motors[0]->SetStepMode((StepperMotor::step_mode_t) init[0].step_sel))
+        printf("    Step Mode not allowed.\r\n");
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
+
+    /* Printing to the console. */
+    printf("--> Setting home position.\r\n");
+
+    /* Setting the home position. */
+    motors[0]->SetHome();
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
+
+    /* Getting the current position. */
+    position = motors[0]->GetPosition();
+    
+    /* Printing to the console. */
+    printf("--> Getting the current position: %d\r\n", position);
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
+
+    /* Printing to the console. */
+    printf("--> Moving forward %d steps.\r\n", STEPS_1);
+
+    /* Moving. */
+    motors[0]->Move(StepperMotor::FWD, STEPS_1);
+
+    /* Waiting while active. */
+    motors[0]->WaitWhileActive();
+
+    /* Getting the current position. */
+    position = motors[0]->GetPosition();
+    
+    /* Printing to the console. */
+    printf("--> Getting the current position: %d\r\n", position);
+
+    /* Printing to the console. */
+    printf("--> Marking the current position.\r\n");
+
+    /* Marking the current position. */
+    motors[0]->SetMark();
+
+    /* Waiting. */
     wait_ms(DELAY_2);
 
 
@@ -286,7 +339,7 @@
     printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
 
     /* Printing to the console. */
-    printf("--> Doublig the speed while running.\r\n");
+    printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
 
     /* Preparing each motor to perform a run at a specified speed. */
     for (int m = 0; m < L6470DAISYCHAINSIZE; m++)