200117StepMotorControl

Dependencies:   X_NUCLEO_IHM02A1 TextLCD

Revision:
1:9f1974b0960d
Parent:
0:5148e9486cf2
Child:
2:41eeee48951b
--- a/main.cpp	Fri Nov 20 18:10:04 2015 +0000
+++ b/main.cpp	Wed Nov 25 12:00:34 2015 +0000
@@ -4,10 +4,8 @@
  * @author  Davide Aliprandi / AST
  * @version V1.0.0
  * @date    November 4th, 2015
- * @brief   mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
- *          Motor Control Expansion Board: control of 1 motor.
- *          This application makes use of a C++ component architecture obtained
- *          from the C component architecture through the Stm32CubeTOO tool.
+ * @brief   mbed test application for the STMicrolectronics X-NUCLEO-IHM02A1
+ *          Motor Control Expansion Board: control of 2 motors.
  ******************************************************************************
  * @attention
  *
@@ -55,10 +53,13 @@
 
 /* Number of movements per revolution. */
 #define MPR_1 4
-#define MPR_2 8
+
+/* Number of steps. */
+#define STEPS_1 100000
+#define STEPS_2 200000
 
 /* Delay in milliseconds. */
-#define DELAY_1 500
+#define DELAY_1 1000
 #define DELAY_2 2000
 #define DELAY_3 5000
 
@@ -73,132 +74,239 @@
 
 int main()
 {
-    /* Led. */
-    DigitalOut led(LED1);
+    /*----- Initialization. -----*/
+
 
     /* Initializing SPI bus. */
     DevSPI dev_spi(D11, D12, D3);
 
     /* Initializing Motor Control Expansion Board. */
-    x_nucleo_ihm02a1 = X_NUCLEO_IHM02A1::Instance(A4, D4, A2, &dev_spi);
+    x_nucleo_ihm02a1 = X_NUCLEO_IHM02A1::Instance(A4, A5, D4, A2, &dev_spi);
 
-    /* Building a list of motors. */
-    L6470 *motors[L6470DAISYCHAINSIZE] = {x_nucleo_ihm02a1->l6470_0, x_nucleo_ihm02a1->l6470_1};
+    /* Building a list of motor control components. */
+    L6470 **motors = x_nucleo_ihm02a1->GetComponents();
 
     /* Printing to the console. */
     printf("Motor Control Application Example for 2 Motors\r\n\n");
 
-    /* Main Loop. */
-    while(true)
-    {
-        /*----- Doing a full revolution on each motor, one after the other. -----*/
+
+    /*----- Setting home and marke positions, getting positions, and going to positions. -----*/
+
+    /* Printing to the console. */
+    printf("--> Setting home position.\r\n");
+
+    /* Setting the home position. */
+    motors[0]->SetHome();
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
-        /* Printing to the console. */
-        printf("--> Doing a full revolution on each motor, one after the other.\r\n");
+    /* Getting the current position. */
+    int 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);
 
-        /* Doing a full revolution on each motor, one after the other. */
-        for (int m = 0; m < 2; m++)
-            for (int i = 0; i < MPR_1; i++)
-            {
-                /* Computing the number of steps. */
-                int steps = (int) (((int) X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].fullstepsperrevolution * pow(2.0f, X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].step_sel)) / MPR_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");
 
-                /* Moving. */
-                motors[m]->Move(StepperMotor::FWD, steps);
-                
-                /* Waiting while active. */
-                motors[m]->WaitWhileActive();
+    /* Marking the current position. */
+    motors[0]->SetMark();
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
+
+    /* Printing to the console. */
+    printf("--> Moving backward %d steps.\r\n", STEPS_2);
+
+    /* Moving. */
+    motors[0]->Move(StepperMotor::BWD, STEPS_2);
+
+    /* Waiting while active. */
+    motors[0]->WaitWhileActive();
 
-                /* Waiting. */
-                wait_ms(DELAY_1);
-            }
+    /* 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);
 
-        /* Waiting. */
-        wait_ms(DELAY_2);
+    /* Printing to the console. */
+    printf("--> Going to marked position.\r\n");
 
+    /* Going to marked position. */
+    motors[0]->GoMark();
+    
+    /* Waiting while active. */
+    motors[0]->WaitWhileActive();
 
-        /*----- Running together for a certain amount of time. -----*/
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
-        /* Printing to the console. */
-        printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
+    /* Getting the current position. */
+    position = motors[0]->GetPosition();
+    
+    /* Printing to the console. */
+    printf("--> Getting the current position: %d\r\n", position);
 
-        /* Preparing each motor to perform a run at a specified speed. */
-        for (int m = 0; m < 2; m++)
-            motors[m]->PrepareRun(StepperMotor::BWD, 10000);
+    /* Waiting. */
+    wait_ms(DELAY_1);
+
+    /* Printing to the console. */
+    printf("--> Going to home position.\r\n");
 
-        /* Performing the action on each motor at the same time. */
-        for (int m = 0; m < 2; m++)
-            motors[m]->PerformAction();
+    /* Going to home position. */
+    motors[0]->GoHome();
+    
+    /* Waiting while active. */
+    motors[0]->WaitWhileActive();
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
-        /* Waiting. */
-        wait_ms(DELAY_3);
+    /* 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_2);
 
 
-        /*----- Hard Stop. -----*/
+    /*----- Running together for a certain amount of time. -----*/
+
+    /* Printing to the console. */
+    printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
 
-        /* Printing to the console. */
-        printf("--> Hard Stop.\r\n");
+    /* Preparing each motor to perform a run at a specified speed. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        motors[m]->PrepareRun(StepperMotor::BWD, 400);
 
-        /* Preparing each motor to perform a hard stop. */
-        for (int m = 0; m < 2; m++)
-            motors[m]->PrepareHardStop();
+    /* Performing the action on each motor at the same time. */
+    x_nucleo_ihm02a1->PerformPreparedActions();
 
-        /* Performing the action on each motor at the same time. */
-        for (int m = 0; m < 2; m++)
-            motors[m]->PerformAction();
+    /* Waiting. */
+    wait_ms(DELAY_3);
+
+
+    /*----- Increasing the speed while running. -----*/
 
-        /* Waiting. */
-        wait_ms(DELAY_2);
-        
+    /* Preparing each motor to perform a run at a specified speed. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        motors[m]->PrepareGetSpeed();
 
-        /*----- Doing a full revolution on each motor, one after the other. -----*/
+    /* Performing the action on each motor at the same time. */
+    uint32_t* results = x_nucleo_ihm02a1->PerformPreparedActions();
 
-        /* Printing to the console. */
-        printf("--> Doing a full revolution on each motor, one after the other.\r\n");
+    /* Printing to the console. */
+    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
+
+    /* Printing to the console. */
+    printf("--> Doublig the speed while running.\r\n");
 
-        /* Doing a full revolution on each motor, one after the other. */
-        for (int m = 0; m < 2; m++)
-            for (int i = 0; i < MPR_2; i++)
-            {
-                /* Computing the number of steps. */
-                int steps = (int) (((int) X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].fullstepsperrevolution * pow(2.0f, X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].step_sel)) / MPR_2);
+    /* Preparing each motor to perform a run at a specified speed. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        motors[m]->PrepareRun(StepperMotor::BWD, results[m] << 1);
+
+    /* Performing the action on each motor at the same time. */
+    results = x_nucleo_ihm02a1->PerformPreparedActions();
 
-                /* Moving. */
-                motors[m]->Move(StepperMotor::FWD, steps);
+    /* Waiting. */
+    wait_ms(DELAY_3);
 
-                /* Waiting while active. */
-                motors[m]->WaitWhileActive();
+    /* Preparing each motor to perform a run at a specified speed. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        motors[m]->PrepareGetSpeed();
 
-                /* Waiting. */
-                wait_ms(DELAY_1);
-            }
+    /* Performing the action on each motor at the same time. */
+    results = x_nucleo_ihm02a1->PerformPreparedActions();
 
-        /* Waiting. */
-        wait_ms(DELAY_2);
+    /* Printing to the console. */
+    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
+
+    /* Waiting. */
+    wait_ms(DELAY_1);
 
 
-        /*----- High Impedance State. -----*/
+    /*----- Hard Stop. -----*/
 
-        /* Printing to the console. */
-        printf("--> High Impedance State.\r\n");
+    /* Printing to the console. */
+    printf("--> Hard Stop.\r\n");
 
-        /* Preparing each motor to set High Impedance State. */
-        for (int m = 0; m < 2; m++)
-            motors[m]->PrepareHardHiZ();
+    /* Preparing each motor to perform a hard stop. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        motors[m]->PrepareHardStop();
 
-        /* Performing the action on each motor at the same time. */
-        for (int m = 0; m < 2; m++)
-            motors[m]->PerformAction();
+    /* Performing the action on each motor at the same time. */
+    x_nucleo_ihm02a1->PerformPreparedActions();
 
-        /* Waiting. */
-        wait_ms(DELAY_2);
+    /* Waiting. */
+    wait_ms(DELAY_2);
 
 
-        /*----- Led Blinking. -----*/
-        
-        /* Led Blinking. */
-        led = 1;
-        wait_ms(DELAY_2);
-        led = 0;
-    }
+    /*----- Doing a full revolution on each motor, one after the other. -----*/
+
+    /* Printing to the console. */
+    printf("--> Doing a full revolution on each motor, one after the other.\r\n");
+
+    /* Doing a full revolution on each motor, one after the other. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        for (int i = 0; i < MPR_1; i++)
+        {
+            /* Computing the number of steps. */
+            int steps = (int) (((int) X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].fullstepsperrevolution * pow(2.0f, X_NUCLEO_IHM02A1::MotorParameterInitData[0][m].step_sel)) / MPR_1);
+
+            /* Moving. */
+            motors[m]->Move(StepperMotor::FWD, steps);
+            
+            /* Waiting while active. */
+            motors[m]->WaitWhileActive();
+
+            /* Waiting. */
+            wait_ms(DELAY_1);
+        }
+
+    /* Waiting. */
+    wait_ms(DELAY_2);
+
+
+    /*----- High Impedance State. -----*/
+
+    /* Printing to the console. */
+    printf("--> High Impedance State.\r\n");
+
+    /* Preparing each motor to set High Impedance State. */
+    for (int m = 0; m < L6470DAISYCHAINSIZE; m++)
+        motors[m]->PrepareHardHiZ();
+
+    /* Performing the action on each motor at the same time. */
+    x_nucleo_ihm02a1->PerformPreparedActions();
+
+    /* Waiting. */
+    wait_ms(DELAY_2);
 }
\ No newline at end of file