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

Dependencies:   X_NUCLEO_IHM01A1 mbed

Fork of HelloWorld_IHM01A1_2Motors 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 two stepper motors connected to two stacked boards in daisy chain configuration, moving the rotors to specific positions, with given speed values, directions of rotation, etc.

Revision:
3:02d9ec4f88b2
Parent:
2:e12e4df7a486
Child:
5:a0268a435bb1
--- a/main.cpp	Fri Nov 13 12:59:29 2015 +0000
+++ b/main.cpp	Wed Nov 18 18:46:58 2015 +0000
@@ -5,7 +5,7 @@
  * @version V1.0.0
  * @date    October 14th, 2015
  * @brief   mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
- *          Motor Control Expansion Board: control of 1 motor.
+ *          Motor Control Expansion Board: control of 2 motors.
  *          This application makes use of a C++ component architecture obtained
  *          from the C component architecture through the Stm32CubeTOO tool.
  ******************************************************************************
@@ -54,13 +54,14 @@
 /* Definitions ---------------------------------------------------------------*/
 
 /* Number of steps corresponding to one round angle of the motor. */
-#define ROUND_ANGLE_STEPS 1600
+#define ROUND_ANGLE_STEPS 3200
 
 
 /* Variables -----------------------------------------------------------------*/
 
 /* Motor Control Component. */
-L6474 *motor;
+L6474 *motor1;
+L6474 *motor2;
 
 
 /* Main ----------------------------------------------------------------------*/
@@ -71,56 +72,68 @@
     DevSPI dev_spi(D11, D12, D13);
 
     /* Initializing Motor Control Component. */
-    motor = new L6474(D8, D7, D9, D10, dev_spi);
-    if (motor->Init(NULL) != COMPONENT_OK)
+    motor1 = new L6474(D8, D7, D9, D10, dev_spi);
+    if (motor1->Init(NULL) != COMPONENT_OK)
+        return false;
+    motor2 = new L6474(D8, D4, D3, D10, dev_spi);
+    if (motor2->Init(NULL) != COMPONENT_OK)
         return false;
 
     /* Printing to the console. */
-    printf("Motor Control Application Example for 1 Motor\r\n\n");
+    printf("Motor Control Application Example for 2 Motors\r\n\n");
 
     /* Main Loop. */
     while(true)
     {
-        /*----- Moving forward of N steps. -----*/
+        /*----- Moving. -----*/
 
         /* Printing to the console. */
-        printf("--> Moving forward %d steps.\r\n", ROUND_ANGLE_STEPS);
+        printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", ROUND_ANGLE_STEPS >> 1, ROUND_ANGLE_STEPS);
 
         /* Moving N steps in the forward direction. */
-        motor->Move(StepperMotor::CW, ROUND_ANGLE_STEPS);
-        
+        motor1->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS >> 1);
+        motor2->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS);
+
         /* Waiting while the motor is active. */
-        motor->WaitWhileActive();
+        motor1->WaitWhileActive();
+        motor2->WaitWhileActive();
 
         /* Getting current position. */
-        int position = motor->GetPosition();
+        int position1 = motor1->GetPosition();
+        int position2 = motor2->GetPosition();
         
         /* Printing to the console. */
-        printf("    Position: %d.\r\n", position);
+        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
 
         /* Waiting 2 seconds. */
         wait_ms(2000);
 
         
-        /*----- Moving backward N steps. -----*/
+        /*----- Moving. -----*/
         
         /* Printing to the console. */
-        printf("--> Moving backward %d steps.\r\n", ROUND_ANGLE_STEPS);
+        printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", ROUND_ANGLE_STEPS >> 1, ROUND_ANGLE_STEPS);
+
         
         /* Moving N steps in the backward direction. */
-        motor->Move(StepperMotor::CCW, ROUND_ANGLE_STEPS);
+        motor1->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS >> 1);
+        motor2->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS);
         
         /* Waiting while the motor is active. */
-        motor->WaitWhileActive();
+        motor1->WaitWhileActive();
+        motor2->WaitWhileActive();
 
         /* Getting current position. */
-        position = motor->GetPosition();
+        position1 = motor1->GetPosition();
+        position2 = motor2->GetPosition();
         
         /* Printing to the console. */
-        printf("    Position: %d.\r\n", position);
+        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
+        printf("--> Setting Home.\r\n");
 
         /* Setting the current position to be the home position. */
-        motor->SetHome();
+        motor1->SetHome();
+        motor2->SetHome();
 
         /* Waiting 2 seconds. */
         wait_ms(2000);
@@ -129,19 +142,22 @@
         /*----- Going to a specified position. -----*/
 
         /* Printing to the console. */
-        printf("--> Going to position %d.\r\n", ROUND_ANGLE_STEPS >> 1);
+        printf("--> Going to position: M1 %d, M2 %d.\r\n", ROUND_ANGLE_STEPS, ROUND_ANGLE_STEPS >> 1);
         
         /* Requesting to go to a specified position. */
-        motor->GoTo(ROUND_ANGLE_STEPS >> 1);
+        motor1->GoTo(ROUND_ANGLE_STEPS);
+        motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
         
         /* Waiting while the motor is active. */
-        motor->WaitWhileActive();
+        motor1->WaitWhileActive();
+        motor2->WaitWhileActive();
 
         /* Getting current position. */
-        position = motor->GetPosition();
+        position1 = motor1->GetPosition();
+        position2 = motor2->GetPosition();
         
         /* Printing to the console. */
-        printf("    Position: %d.\r\n", position);
+        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
         
         /* Waiting 2 seconds. */
         wait_ms(2000);
@@ -153,38 +169,42 @@
         printf("--> Going Home.\r\n");
         
         /* Requesting to go to home. */
-        motor->GoHome();
+        motor1->GoHome();
+        motor2->GoHome();
         
         /* Waiting while the motor is active. */
-        motor->WaitWhileActive();
+        motor1->WaitWhileActive();
+        motor2->WaitWhileActive();
 
         /* Getting current position. */
-        position = motor->GetPosition();
+        position1 = motor1->GetPosition();
+        position2 = motor2->GetPosition();
 
         /* Printing to the console. */
-        printf("    Position: %d.\r\n", position);
+        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
 
         /* Waiting 2 seconds. */
         wait_ms(2000);
 
 
-        /*----- Moving backward. -----*/
+        /*----- Running. -----*/
 
         /* Printing to the console. */
-        printf("--> Moving backward.\r\n");
+        printf("--> M1 running backward, M2 running forward.\r\n");
 
         /* Requesting to run backward. */
-        motor->Run(StepperMotor::CCW);
+        motor1->Run(StepperMotor::BWD);
+        motor2->Run(StepperMotor::FWD);
 
         /* Waiting until delay has expired. */
         wait_ms(6000);
 
         /* Getting current speed. */
-        int speed = motor->GetSpeed();
+        int speed1 = motor1->GetSpeed();
+        int speed2 = motor2->GetSpeed();
 
         /* Printing to the console. */
-        printf("    Speed: %d.\r\n", speed);
-
+        printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
 
         /*----- Increasing the speed while running. -----*/
 
@@ -192,16 +212,18 @@
         printf("--> Increasing the speed while running.\r\n");
 
         /* Increasing speed to 2400 step/s. */
-        motor->SetMaxSpeed(2400);
+        motor1->SetMaxSpeed(2400);
+        motor2->SetMaxSpeed(2400);
 
         /* Waiting until delay has expired. */
         wait_ms(6000);
 
         /* Getting current speed. */
-        speed = motor->GetSpeed();
+        speed1 = motor1->GetSpeed();
+        speed2 = motor2->GetSpeed();
 
         /* Printing to the console. */
-        printf("    Speed: %d.\r\n", speed);
+        printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
 
 
         /*----- Decreasing the speed while running. -----*/
@@ -210,29 +232,19 @@
         printf("--> Decreasing the speed while running.\r\n");
 
         /* Decreasing speed to 1200 step/s. */
-        motor->SetMaxSpeed(1200);
+        motor1->SetMaxSpeed(1200);
+        motor2->SetMaxSpeed(1200);
 
         /* Waiting until delay has expired. */
         wait_ms(8000);
 
         /* Getting current speed. */
-        speed = motor->GetSpeed();
-
-        /* Printing to the console. */
-        printf("    Speed: %d.\r\n", speed);
-
-
-        /*----- Moving forward. -----*/
+        speed1 = motor1->GetSpeed();
+        speed2 = motor2->GetSpeed();
 
         /* Printing to the console. */
-        printf("--> Moving forward.\r\n");
+        printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
 
-        /* Requesting to run in forward direction. */
-        motor->Run(StepperMotor::CW);
-
-        /* Waiting until delay has expired. */
-        wait_ms(4000);
-        
 
         /*----- Requiring hard-stop while running. -----*/
 
@@ -240,10 +252,12 @@
         printf("--> Requiring hard-stop while running.\r\n");
 
         /* Requesting to immediatly stop. */
-        motor->HardStop();
+        motor1->HardStop();
+        motor2->HardStop();
 
         /* Waiting while the motor is active. */
-        motor->WaitWhileActive();
+        motor1->WaitWhileActive();
+        motor2->WaitWhileActive();
 
         /* Waiting 2 seconds. */
         wait_ms(2000);
@@ -255,22 +269,27 @@
         printf("--> Infinite Loop...\r\n");
 
         /* Setting the current position to be the home position. */
-        motor->SetHome();
+        motor1->SetHome();
+        motor2->SetHome();
 
         /* Infinite Loop. */
         while(1)
         {
             /* Requesting to go to a specified position. */
-            motor->GoTo(- ROUND_ANGLE_STEPS >> 2);
+            motor1->GoTo(ROUND_ANGLE_STEPS >> 1);
+            motor2->GoTo(- (ROUND_ANGLE_STEPS >> 1));
 
             /* Waiting while the motor is active. */
-            motor->WaitWhileActive();
+            motor1->WaitWhileActive();
+            motor2->WaitWhileActive();
 
             /* Requesting to go to a specified position. */
-            motor->GoTo(ROUND_ANGLE_STEPS >> 2);
+            motor1->GoTo(- (ROUND_ANGLE_STEPS >> 1));
+            motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
 
             /* Waiting while the motor is active. */
-            motor->WaitWhileActive();
+            motor1->WaitWhileActive();
+            motor2->WaitWhileActive();
         }
     }
 }