Smartphone app control stepper motors via bluetooth low energy(BLE) on stm32F401RE x IHM02A1 x IDB05A1 V2 .

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed

Fork of BLE_LED_IDB0XA1_demo by NEHSROBOT

Revision:
8:ef9b37e2464f
Parent:
7:126b141a8c86
diff -r 126b141a8c86 -r ef9b37e2464f main.cpp
--- a/main.cpp	Thu Feb 01 07:55:42 2018 +0000
+++ b/main.cpp	Sun Feb 04 06:55:45 2018 +0000
@@ -17,10 +17,10 @@
  /* Includes ------------------------------------------------------------------*/
 
 
-
 /* Helper header files. */
 #include "DevSPI.h"
-void call();
+
+
 /* Expansion Board specific header files. */
 #include "XNucleoIHM02A1.h"
 
@@ -28,7 +28,7 @@
 #include "ble/BLE.h"
 #include "LEDService.h"
 #include "BlueNRGDevice.h"
-int       temp,add         ;
+
 /* Definitions ---------------------------------------------------------------*/
 
 /* Number of movements per revolution. */
@@ -42,12 +42,17 @@
 #define DELAY_1 1000
 #define DELAY_2 2000
 #define DELAY_3 5000
+void call3();
 
+#ifdef TARGET_STM32F401
+    DevSPI dev_spi(PB_15, PB_14, PB_13);
+#else
+    DevSPI dev_spi(PB_15, PB_14, PB_13);
+#endif
 
 /* Variables -----------------------------------------------------------------*/
 
 /* Motor Control Expansion Board. */
-XNucleoIHM02A1 *x_nucleo_ihm02a1;
 
 /* Initialization parameters of the motors connected to the expansion board. */
 L6470_init_t init[L6470DAISYCHAINSIZE] = {
@@ -108,6 +113,8 @@
     }
 };
 
+XNucleoIHM02A1 *x_nucleo_ihm02a1= new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);
+
 DigitalOut actuatedLED(LED2);
 const static char     DEVICE_NAME[] = "mydevice";    //   CHANGE NAME
 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};   // GATT ATTRIBUTE UUID
@@ -120,6 +127,7 @@
     BLE::Instance().gap().startAdvertising(); // restart advertising
 }
 
+
 /**
  * This callback allows the LEDService to receive updates to the ledState Characteristic.
  *
@@ -128,27 +136,16 @@
  */
 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
     if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
-        
- /*       switch( *(params->data)){
-            case 0:
-             actuatedLED=0      ;
-            wait(1)            ;
-            actuatedLED=1      ;
-            wait(1)            ;
-            actuatedLED=0      ;
-            break              ;
-            case 1:
-            actuatedLED=1      ;
-            wait(1)            ;
-            actuatedLED=0      ;
-            wait(1)            ;
-            actuatedLED=1      ;
-            break              ;
-            }    */
+ 
         if (  *(params->data)== 0x00 ) 
         {    
             actuatedLED=1     ;
-            call();
+           call3();
+              
+            actuatedLED=0     ;wait(0.5)            ;
+            actuatedLED=1     ;wait(0.5)            ;
+            actuatedLED=0     ;wait(0.5)            ;
+
               
         }
         else if (*(params->data)== 0x01)  
@@ -206,7 +203,7 @@
     if (error != BLE_ERROR_NONE) {
         /* In case of error, forward the error handling to onBleInitError */
         onBleInitError(ble, error);
-        return;
+       return;
     }
 
     /* Ensure that it is the default instance of BLE */
@@ -241,22 +238,9 @@
     ble.init(bleInitComplete); 
  
 }
-  
-void call()
+
+void call3()
 {
-/*----- Initialization. -----*/
-/* Initializing SPI bus. */
- 
-  
-#ifdef TARGET_STM32F401
-    DevSPI dev_spi(PB_15, PB_14, PB_13);
-#else
-    DevSPI dev_spi(PB_15, PB_14, PB_13);
-#endif
-
-    /* Initializing Motor Control Expansion Board. */
-    x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);
-
     /* Building a list of motor control components. */
     L6470 **motors = x_nucleo_ihm02a1->get_components();
    motors[0]->set_home(); 
@@ -266,272 +250,12 @@
     /* Getting the current position. */
     int position = motors[0]->get_position();
 
-    /* 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);
-
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
-    /* Getting the current position. */
-    position = motors[0]->get_position();
-    
-    /* Printing to the console. */
-    printf("--> Getting the current position: %d\r\n", position);
-
-    /* Printing to the console. */
-    printf("--> Marking the current position.\r\n");
-
-    /* Marking the current position. */
-    motors[0]->set_mark();
-
-    /* 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]->wait_while_active();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    position = motors[0]->get_position();
-    
-    /* Printing to the console. */
-    printf("--> Getting the current position: %d\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Going to marked position.\r\n");
-
-    /* Going to marked position. */
-    motors[0]->go_mark();
-    
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
     /* Waiting. */
     wait_ms(DELAY_1);
 
-    /* Getting the current position. */
-    position = motors[0]->get_position();
-    
-    /* Printing to the console. */
-    printf("--> Getting the current position: %d\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Going to home position.\r\n");
-
-    /* Going to home position. */
-    motors[0]->go_home();
-    
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    position = motors[0]->get_position();
-    
-    /* Printing to the console. */
-    printf("--> Getting the current position: %d\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Halving the microsteps.\r\n");
-
-    /* Halving the microsteps. */
-    init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel -  1 : init[0].step_sel);
-    if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel)) {
-        printf("    Step Mode not allowed.\r\n");
-    }
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Setting home position.\r\n");
-
-    /* Setting the home position. */
-    motors[0]->set_home();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    position = motors[0]->get_position();
-    
-    /* 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);
-
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
-    /* Getting the current position. */
-    position = motors[0]->get_position();
     
-    /* Printing to the console. */
-    printf("--> Getting the current position: %d\r\n", position);
-
-    /* Printing to the console. */
-    printf("--> Marking the current position.\r\n");
-
-    /* Marking the current position. */
-    motors[0]->set_mark();
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- Running together for a certain amount of time. -----*/
-
-    /* Printing to the console. */
-    printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_run(StepperMotor::BWD, 400);
     }
-
-    /* Performing the action on each motor at the same time. */
-    x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_3);
-
-
-    /*----- Increasing the speed while running. -----*/
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_get_speed();
-    }
-
-    /* Performing the action on each motor at the same time. */
-    uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* 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 again for %d seconds.\r\n", DELAY_3 / 1000);
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1);
-    }
-
-    /* Performing the action on each motor at the same time. */
-    results = x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_3);
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_get_speed();
-    }
+    
 
-    /* Performing the action on each motor at the same time. */
-    results = x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Printing to the console. */
-    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-
-    /*----- Hard Stop. -----*/
-
-    /* Printing to the console. */
-    printf("--> Hard Stop.\r\n");
-
-    /* Preparing each motor to perform a hard stop. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_hard_stop();
-    }
-
-    /* Performing the action on each motor at the same time. */
-    x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- 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) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1);
-
-            /* Moving. */
-            motors[m]->move(StepperMotor::FWD, steps);
-            
-            /* Waiting while active. */
-            motors[m]->wait_while_active();
-
-            /* 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]->prepare_hard_hiz();
-    }
-
-    /* Performing the action on each motor at the same time. */
-    x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_2);  
-
-   nCS_ = 0        ;
-    }
-