Simple test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.

Dependencies:   X_NUCLEO_IHM01A1 mbed

Fork of HelloWorld_IHM01A1 by ST Expansion SW Team

Motor Control with the X-NUCLEO-IHM01A1 Expansion Board

This application provides a simple example of usage of the X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.
It shows how to use one stepper motor connected to the board, moving the rotor to a specific position, with a given speed value, direction of rotation, etc.

Revision:
26:b0203c2265e5
Parent:
25:3c863b420ac5
Child:
27:e09aa231c16d
--- a/main.cpp	Fri Mar 11 16:12:03 2016 +0000
+++ b/main.cpp	Thu Apr 07 16:33:42 2016 +0000
@@ -51,8 +51,18 @@
 
 /* Definitions ---------------------------------------------------------------*/
 
-/* Number of steps to move. */
-#define STEPS 3200
+/* Number of steps. */
+#define STEPS_1 (400 * 8)   /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */
+
+/* Delay in milliseconds. */
+#define DELAY_1 1000
+#define DELAY_2 2000
+#define DELAY_3 6000
+#define DELAY_4 8000
+
+/* Speed in step/s. */
+#define SPEED_1 2400
+#define SPEED_2 1200
 
 
 /* Variables -----------------------------------------------------------------*/
@@ -68,7 +78,7 @@
     L6474_OCD_TH_750mA,               /* Overcurrent threshold (OCD_TH register). */
     L6474_CONFIG_OC_SD_ENABLE,        /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
     L6474_CONFIG_EN_TQREG_TVAL_USED,  /* Torque regulation method (EN_TQREG field of CONFIG register). */
-    L6474_STEP_SEL_1_4,               /* Step selection (STEP_SEL field of STEP_MODE register). */
+    L6474_STEP_SEL_1_8,               /* Step selection (STEP_SEL field of STEP_MODE register). */
     L6474_SYNC_SEL_1_2,               /* Sync selection (SYNC_SEL field of STEP_MODE register). */
     L6474_FAST_STEP_12us,             /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
     L6474_TOFF_FAST_8us,              /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
@@ -144,10 +154,10 @@
     /*----- Moving. -----*/
 
     /* Printing to the console. */
-    printf("--> Moving forward %d steps.\r\n", STEPS);
+    printf("--> Moving forward %d steps.\r\n", STEPS_1);
 
     /* Moving N steps in the forward direction. */
-    motor->Move(StepperMotor::FWD, STEPS);
+    motor->Move(StepperMotor::FWD, STEPS_1);
 
     /* Waiting while the motor is active. */
     motor->WaitWhileActive();
@@ -158,35 +168,36 @@
     /* Printing to the console. */
     printf("    Position: %d.\r\n", position);
 
-    /* Waiting 2 seconds. */
-    wait_ms(2000);
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
 
-    /*----- Changing motor setting. -----*/
+    /*----- Changing the motor setting. -----*/
 
     /* Printing to the console. */
-    printf("--> Changing Step Mode and Torque.\r\n");
-
-    /* Setting High Impedance State to update L6474's registers. */
-    motor->SoftHiZ();
+    printf("--> Doubling the microsteps.\r\n");
 
-    /* Changing step mode. */
-    motor->SetParameter(L6474_STEP_MODE, (unsigned int) L6474_STEP_SEL_1_16 | (unsigned int) L6474_SYNC_SEL_1_2);
+    /* Doubling the microsteps. */
+    if (!motor->SetStepMode((StepperMotor::step_mode_t) STEP_MODE_1_16))
+        printf("    Step Mode not allowed.\r\n");
 
-    /* Increasing the torque regulation current to 500mA. */
+    /* Printing to the console. */
+    printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
+
+    /* Increasing the torque regulation current to 500[mA]. */
     motor->SetParameter(L6474_TVAL, 500);
 
-    /* Waiting 1 second. */
-    wait_ms(1000);
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
 
     /*----- Moving. -----*/
     
     /* Printing to the console. */
-    printf("--> Moving backward %d steps.\r\n", STEPS);
+    printf("--> Moving backward %d steps.\r\n", STEPS_1);
 
     /* Moving N steps in the backward direction. */
-    motor->Move(StepperMotor::BWD, STEPS);
+    motor->Move(StepperMotor::BWD, STEPS_1);
     
     /* Waiting while the motor is active. */
     motor->WaitWhileActive();
@@ -201,17 +212,17 @@
     /* Setting the current position to be the home position. */
     motor->SetHome();
 
-    /* Waiting 2 seconds. */
-    wait_ms(2000);
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
 
     /*----- Going to a specified position. -----*/
 
     /* Printing to the console. */
-    printf("--> Going to position %d.\r\n", STEPS);
+    printf("--> Going to position %d.\r\n", STEPS_1);
     
     /* Requesting to go to a specified position. */
-    motor->GoTo(STEPS);
+    motor->GoTo(STEPS_1);
     
     /* Waiting while the motor is active. */
     motor->WaitWhileActive();
@@ -222,8 +233,8 @@
     /* Printing to the console. */
     printf("    Position: %d.\r\n", position);
     
-    /* Waiting 2 seconds. */
-    wait_ms(2000);
+    /* Waiting. */
+    wait_ms(DELAY_2);
 
     
     /*----- Going Home. -----*/
@@ -243,20 +254,20 @@
     /* Printing to the console. */
     printf("    Position: %d.\r\n", position);
 
-    /* Waiting 2 seconds. */
-    wait_ms(2000);
+    /* Waiting. */
+    wait_ms(DELAY_2);
 
 
     /*----- Running. -----*/
 
     /* Printing to the console. */
-    printf("--> Running backward.\r\n");
+    printf("--> Running backward for %d seconds.\r\n", DELAY_3 / 1000);
 
     /* Requesting to run backward. */
     motor->Run(StepperMotor::BWD);
 
-    /* Waiting until delay has expired. */
-    wait_ms(6000);
+    /* Waiting. */
+    wait_ms(DELAY_3);
 
     /* Getting current speed. */
     int speed = motor->GetSpeed();
@@ -267,13 +278,13 @@
     /*----- Increasing the speed while running. -----*/
 
     /* Printing to the console. */
-    printf("--> Increasing the speed while running.\r\n");
+    printf("--> Increasing the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
 
-    /* Increasing speed to 2400 step/s. */
-    motor->SetMaxSpeed(2400);
+    /* Increasing the speed. */
+    motor->SetMaxSpeed(SPEED_1);
 
-    /* Waiting until delay has expired. */
-    wait_ms(6000);
+    /* Waiting. */
+    wait_ms(DELAY_3);
 
     /* Getting current speed. */
     speed = motor->GetSpeed();
@@ -285,13 +296,13 @@
     /*----- Decreasing the speed while running. -----*/
 
     /* Printing to the console. */
-    printf("--> Decreasing the speed while running.\r\n");
+    printf("--> Decreasing the speed while running again for %d seconds.\r\n", DELAY_4 / 1000);
 
-    /* Decreasing speed to 1200 step/s. */
-    motor->SetMaxSpeed(1200);
+    /* Decreasing the speed. */
+    motor->SetMaxSpeed(SPEED_2);
 
-    /* Waiting until delay has expired. */
-    wait_ms(8000);
+    /* Waiting. */
+    wait_ms(DELAY_4);
 
     /* Getting current speed. */
     speed = motor->GetSpeed();
@@ -300,10 +311,10 @@
     printf("    Speed: %d.\r\n", speed);
 
 
-    /*----- Requiring hard-stop while running. -----*/
+    /*----- Hard Stop. -----*/
 
     /* Printing to the console. */
-    printf("--> Requiring hard-stop while running.\r\n");
+    printf("--> Hard Stop.\r\n");
 
     /* Requesting to immediatly stop. */
     motor->HardStop();
@@ -311,8 +322,8 @@
     /* Waiting while the motor is active. */
     motor->WaitWhileActive();
 
-    /* Waiting 2 seconds. */
-    wait_ms(2000);
+    /* Waiting. */
+    wait_ms(DELAY_2);
 
 
     /*----- Infinite Loop. -----*/
@@ -327,13 +338,13 @@
     while (1)
     {
         /* Requesting to go to a specified position. */
-        motor->GoTo(STEPS >> 1);
+        motor->GoTo(STEPS_1 >> 1);
 
         /* Waiting while the motor is active. */
         motor->WaitWhileActive();
 
         /* Requesting to go to a specified position. */
-        motor->GoTo(- (STEPS >> 1));
+        motor->GoTo(- (STEPS_1 >> 1));
 
         /* Waiting while the motor is active. */
         motor->WaitWhileActive();