Simple test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board, built against mbed OS.

Dependencies:   X_NUCLEO_IHM01A1

Fork of HelloWorld_IHM01A1_2Motors by ST

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:
8:cec4c2c03a27
Parent:
7:141f878e9590
Child:
9:a9e51320aee4
--- a/main.cpp	Thu Nov 26 16:02:20 2015 +0000
+++ b/main.cpp	Thu Nov 26 16:17:52 2015 +0000
@@ -66,6 +66,8 @@
 
 int main()
 {
+    /*----- Initialization. -----*/
+
     /* Initializing SPI bus. */
     DevSPI dev_spi(D11, D12, D13);
 
@@ -80,214 +82,211 @@
     /* Printing to the console. */
     printf("Motor Control Application Example for 2 Motors\r\n\n");
 
-    /* Main Loop. */
-    while(true)
-    {
-        /*----- Moving. -----*/
+
+    /*----- Moving. -----*/
+
+    /* Printing to the console. */
+    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. */
+    motor1->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS >> 1);
+    motor2->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS);
+
+    /* Waiting while the motor is active. */
+    motor1->WaitWhileActive();
+    motor2->WaitWhileActive();
+
+    /* Getting current position. */
+    int position1 = motor1->GetPosition();
+    int position2 = motor2->GetPosition();
+    
+    /* Printing to the console. */
+    printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
+
+    /* Waiting 2 seconds. */
+    wait_ms(2000);
+
+    
+    /*----- Moving. -----*/
+    
+    /* Printing to the console. */
+    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. */
+    motor1->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS >> 1);
+    motor2->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS);
+    
+    /* Waiting while the motor is active. */
+    motor1->WaitWhileActive();
+    motor2->WaitWhileActive();
+
+    /* Getting current position. */
+    position1 = motor1->GetPosition();
+    position2 = motor2->GetPosition();
+    
+    /* Printing to the console. */
+    printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
+    printf("--> Setting Home.\r\n");
+
+    /* Setting the current position to be the home position. */
+    motor1->SetHome();
+    motor2->SetHome();
+
+    /* Waiting 2 seconds. */
+    wait_ms(2000);
+
+
+    /*----- Going to a specified position. -----*/
+
+    /* Printing to the console. */
+    printf("--> Going to position: M1 %d, M2 %d.\r\n", ROUND_ANGLE_STEPS, ROUND_ANGLE_STEPS >> 1);
+    
+    /* Requesting to go to a specified position. */
+    motor1->GoTo(ROUND_ANGLE_STEPS);
+    motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
+    
+    /* Waiting while the motor is active. */
+    motor1->WaitWhileActive();
+    motor2->WaitWhileActive();
+
+    /* Getting current position. */
+    position1 = motor1->GetPosition();
+    position2 = motor2->GetPosition();
+    
+    /* Printing to the console. */
+    printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
+    
+    /* Waiting 2 seconds. */
+    wait_ms(2000);
+
+    
+    /*----- Going Home. -----*/
+
+    /* Printing to the console. */
+    printf("--> Going Home.\r\n");
+    
+    /* Requesting to go to home. */
+    motor1->GoHome();
+    motor2->GoHome();
+    
+    /* Waiting while the motor is active. */
+    motor1->WaitWhileActive();
+    motor2->WaitWhileActive();
+
+    /* Getting current position. */
+    position1 = motor1->GetPosition();
+    position2 = motor2->GetPosition();
 
-        /* Printing to the console. */
-        printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", ROUND_ANGLE_STEPS >> 1, ROUND_ANGLE_STEPS);
+    /* Printing to the console. */
+    printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
+
+    /* Waiting 2 seconds. */
+    wait_ms(2000);
+
+
+    /*----- Running. -----*/
+
+    /* Printing to the console. */
+    printf("--> M1 running backward, M2 running forward.\r\n");
+
+    /* Requesting to run backward. */
+    motor1->Run(StepperMotor::BWD);
+    motor2->Run(StepperMotor::FWD);
+
+    /* Waiting until delay has expired. */
+    wait_ms(6000);
+
+    /* Getting current speed. */
+    int speed1 = motor1->GetSpeed();
+    int speed2 = motor2->GetSpeed();
+
+    /* Printing to the console. */
+    printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
+
+    /*----- Increasing the speed while running. -----*/
+
+    /* Printing to the console. */
+    printf("--> Increasing the speed while running.\r\n");
+
+    /* Increasing speed to 2400 step/s. */
+    motor1->SetMaxSpeed(2400);
+    motor2->SetMaxSpeed(2400);
+
+    /* Waiting until delay has expired. */
+    wait_ms(6000);
+
+    /* Getting current speed. */
+    speed1 = motor1->GetSpeed();
+    speed2 = motor2->GetSpeed();
+
+    /* Printing to the console. */
+    printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
+
+
+    /*----- Decreasing the speed while running. -----*/
 
-        /* Moving N steps in the forward direction. */
-        motor1->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS >> 1);
-        motor2->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS);
+    /* Printing to the console. */
+    printf("--> Decreasing the speed while running.\r\n");
+
+    /* Decreasing speed to 1200 step/s. */
+    motor1->SetMaxSpeed(1200);
+    motor2->SetMaxSpeed(1200);
+
+    /* Waiting until delay has expired. */
+    wait_ms(8000);
+
+    /* Getting current speed. */
+    speed1 = motor1->GetSpeed();
+    speed2 = motor2->GetSpeed();
+
+    /* Printing to the console. */
+    printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
+
+
+    /*----- Requiring hard-stop while running. -----*/
+
+    /* Printing to the console. */
+    printf("--> Requiring hard-stop while running.\r\n");
+
+    /* Requesting to immediatly stop. */
+    motor1->HardStop();
+    motor2->HardStop();
+
+    /* Waiting while the motor is active. */
+    motor1->WaitWhileActive();
+    motor2->WaitWhileActive();
+
+    /* Waiting 2 seconds. */
+    wait_ms(2000);
+
+
+    /*----- Infinite Loop. -----*/
+
+    /* Printing to the console. */
+    printf("--> Infinite Loop...\r\n");
+
+    /* Setting the current position to be the home position. */
+    motor1->SetHome();
+    motor2->SetHome();
+
+    /* Infinite Loop. */
+    while(1)
+    {
+        /* Requesting to go to a specified position. */
+        motor1->GoTo(ROUND_ANGLE_STEPS >> 1);
+        motor2->GoTo(- (ROUND_ANGLE_STEPS >> 1));
 
         /* Waiting while the motor is active. */
         motor1->WaitWhileActive();
         motor2->WaitWhileActive();
 
-        /* Getting current position. */
-        int position1 = motor1->GetPosition();
-        int position2 = motor2->GetPosition();
-        
-        /* Printing to the console. */
-        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
-
-        /* Waiting 2 seconds. */
-        wait_ms(2000);
-
-        
-        /*----- Moving. -----*/
-        
-        /* Printing to the console. */
-        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. */
-        motor1->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS >> 1);
-        motor2->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS);
-        
-        /* Waiting while the motor is active. */
-        motor1->WaitWhileActive();
-        motor2->WaitWhileActive();
-
-        /* Getting current position. */
-        position1 = motor1->GetPosition();
-        position2 = motor2->GetPosition();
-        
-        /* Printing to the console. */
-        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
-        printf("--> Setting Home.\r\n");
-
-        /* Setting the current position to be the home position. */
-        motor1->SetHome();
-        motor2->SetHome();
-
-        /* Waiting 2 seconds. */
-        wait_ms(2000);
-
-
-        /*----- Going to a specified position. -----*/
-
-        /* Printing to the console. */
-        printf("--> Going to position: M1 %d, M2 %d.\r\n", ROUND_ANGLE_STEPS, ROUND_ANGLE_STEPS >> 1);
-        
         /* Requesting to go to a specified position. */
-        motor1->GoTo(ROUND_ANGLE_STEPS);
+        motor1->GoTo(- (ROUND_ANGLE_STEPS >> 1));
         motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
-        
-        /* Waiting while the motor is active. */
-        motor1->WaitWhileActive();
-        motor2->WaitWhileActive();
-
-        /* Getting current position. */
-        position1 = motor1->GetPosition();
-        position2 = motor2->GetPosition();
-        
-        /* Printing to the console. */
-        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
-        
-        /* Waiting 2 seconds. */
-        wait_ms(2000);
-
-        
-        /*----- Going Home. -----*/
-
-        /* Printing to the console. */
-        printf("--> Going Home.\r\n");
-        
-        /* Requesting to go to home. */
-        motor1->GoHome();
-        motor2->GoHome();
-        
-        /* Waiting while the motor is active. */
-        motor1->WaitWhileActive();
-        motor2->WaitWhileActive();
-
-        /* Getting current position. */
-        position1 = motor1->GetPosition();
-        position2 = motor2->GetPosition();
-
-        /* Printing to the console. */
-        printf("    Position: M1 %d, M2 %d.\r\n", position1, position2);
-
-        /* Waiting 2 seconds. */
-        wait_ms(2000);
-
-
-        /*----- Running. -----*/
-
-        /* Printing to the console. */
-        printf("--> M1 running backward, M2 running forward.\r\n");
-
-        /* Requesting to run backward. */
-        motor1->Run(StepperMotor::BWD);
-        motor2->Run(StepperMotor::FWD);
-
-        /* Waiting until delay has expired. */
-        wait_ms(6000);
-
-        /* Getting current speed. */
-        int speed1 = motor1->GetSpeed();
-        int speed2 = motor2->GetSpeed();
-
-        /* Printing to the console. */
-        printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
-
-        /*----- Increasing the speed while running. -----*/
-
-        /* Printing to the console. */
-        printf("--> Increasing the speed while running.\r\n");
-
-        /* Increasing speed to 2400 step/s. */
-        motor1->SetMaxSpeed(2400);
-        motor2->SetMaxSpeed(2400);
-
-        /* Waiting until delay has expired. */
-        wait_ms(6000);
-
-        /* Getting current speed. */
-        speed1 = motor1->GetSpeed();
-        speed2 = motor2->GetSpeed();
-
-        /* Printing to the console. */
-        printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
-
-
-        /*----- Decreasing the speed while running. -----*/
-
-        /* Printing to the console. */
-        printf("--> Decreasing the speed while running.\r\n");
-
-        /* Decreasing speed to 1200 step/s. */
-        motor1->SetMaxSpeed(1200);
-        motor2->SetMaxSpeed(1200);
-
-        /* Waiting until delay has expired. */
-        wait_ms(8000);
-
-        /* Getting current speed. */
-        speed1 = motor1->GetSpeed();
-        speed2 = motor2->GetSpeed();
-
-        /* Printing to the console. */
-        printf("    Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
-
-
-        /*----- Requiring hard-stop while running. -----*/
-
-        /* Printing to the console. */
-        printf("--> Requiring hard-stop while running.\r\n");
-
-        /* Requesting to immediatly stop. */
-        motor1->HardStop();
-        motor2->HardStop();
 
         /* Waiting while the motor is active. */
         motor1->WaitWhileActive();
         motor2->WaitWhileActive();
-
-        /* Waiting 2 seconds. */
-        wait_ms(2000);
-
-
-        /*----- Infinite Loop. -----*/
-
-        /* Printing to the console. */
-        printf("--> Infinite Loop...\r\n");
-
-        /* Setting the current position to be the home position. */
-        motor1->SetHome();
-        motor2->SetHome();
-
-        /* Infinite Loop. */
-        while(1)
-        {
-            /* Requesting to go to a specified position. */
-            motor1->GoTo(ROUND_ANGLE_STEPS >> 1);
-            motor2->GoTo(- (ROUND_ANGLE_STEPS >> 1));
-
-            /* Waiting while the motor is active. */
-            motor1->WaitWhileActive();
-            motor2->WaitWhileActive();
-
-            /* Requesting to go to a specified position. */
-            motor1->GoTo(- (ROUND_ANGLE_STEPS >> 1));
-            motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
-
-            /* Waiting while the motor is active. */
-            motor1->WaitWhileActive();
-            motor2->WaitWhileActive();
-        }
     }
 }