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

Dependencies:   X_NUCLEO_IHM01A1

Fork of MotorControl_IHM01A1 by ST

Motor Control with the X-NUCLEO-IHM01A1 Expansion Board

This application provides a more complex 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, with an example of ISR and error handler, as well as an almost complete coverage of the available APIs, e.g.:

  • moving the rotor to a specific position;
  • moving the rotor for a certain amount of time;
  • setting the speed value;
  • setting the direction of rotation;
  • changing the stepper motor mode;
  • soft/hard stopping the rotor;
  • powering off the power bridge.
Revision:
20:bec1eba352a5
Parent:
15:4a1684b7252b
Child:
22:ce8a202d67ac
--- a/main.cpp	Fri Mar 11 16:13:44 2016 +0000
+++ b/main.cpp	Thu Apr 07 16:47:30 2016 +0000
@@ -52,6 +52,34 @@
 
 /* Variables -----------------------------------------------------------------*/
 
+/* Initialization parameters. */
+L6474_Init_t init =
+{
+    160,                              /* Acceleration rate in step/s2. Range: (0..+inf). */
+    160,                              /* Deceleration rate in step/s2. Range: (0..+inf). */
+    1600,                             /* Maximum speed in step/s. Range: (30..10000]. */
+    800,                              /* Minimum speed in step/s. Range: [30..10000). */
+    250,                              /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
+    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_16,              /* 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. */
+    3,                                /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
+    21,                               /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
+    L6474_CONFIG_TOFF_044us,          /* Target Swicthing Period (field TOFF of CONFIG register). */
+    L6474_CONFIG_SR_320V_us,          /* Slew rate (POW_SR field of CONFIG register). */
+    L6474_CONFIG_INT_16MHZ,           /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
+    L6474_ALARM_EN_OVERCURRENT |
+    L6474_ALARM_EN_THERMAL_SHUTDOWN |
+    L6474_ALARM_EN_THERMAL_WARNING |
+    L6474_ALARM_EN_UNDERVOLTAGE |
+    L6474_ALARM_EN_SW_TURN_ON |
+    L6474_ALARM_EN_WRONG_NPERF_CMD    /* Alarm (ALARM_EN register). */
+};
+
 /* Motor Control Component. */
 L6474 *motor;
 
@@ -143,12 +171,9 @@
 
     /* Initializing Motor Control Component. */
     motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
-    if (motor->Init() != COMPONENT_OK)
+    if (motor->Init(&init) != COMPONENT_OK)
         exit(EXIT_FAILURE);
 
-    /* Setting 1/16 Microstepping mode. */
-    motor->SetStepMode(STEP_MODE_1_16);
-
     /* Printing to the console. */
     printf("Motor Control Application Example for 1 Motor\r\n\n");
 
@@ -441,7 +466,7 @@
     motor->WaitWhileActive();
 
     /* Waiting 2 seconds. */
-    wait_ms(2000);  
+    wait_ms(2000);
 
 
     /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
@@ -466,7 +491,8 @@
     printf("--> Changing step mode to full step mode.\r\n");
 
     /* Selecting full step mode. */
-    motor->SetStepMode(STEP_MODE_FULL);
+    if (!motor->SetStepMode((StepperMotor::step_mode_t) STEP_MODE_FULL))
+        printf("    Step Mode not allowed.\r\n");
 
     /* Setting speed and acceleration to be consistent with full step mode. */
     motor->SetMaxSpeed(100);
@@ -496,7 +522,8 @@
     printf("--> Restoring 1/16 microstepping mode.\r\n");
 
     /* Resetting to 1/16 microstepping mode */
-    motor->SetStepMode(STEP_MODE_1_16);    
+    if (!motor->SetStepMode((StepperMotor::step_mode_t) STEP_MODE_1_16))
+        printf("    Step Mode not allowed.\r\n");
 
     /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
     motor->SetMaxSpeed(1600);