NEHSROBOT / Mbed 2 deprecated Motor_Ble_v1

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed

Fork of BLE_LED_IDB0XA1_demo by NEHSROBOT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017  /* Includes ------------------------------------------------------------------*/
00018 
00019 
00020 
00021 /* Helper header files. */
00022 #include "DevSPI.h"
00023 void FWD();
00024 void BWD();
00025 void LEFT();
00026 void RIGHT();
00027 void SOFTSTOP();
00028 /* Expansion Board specific header files. */
00029 #include "XNucleoIHM02A1.h"
00030 
00031 #include "mbed.h"
00032 #include "ble/BLE.h"
00033 #include "LEDService.h"
00034 
00035 
00036 /* Definitions ---------------------------------------------------------------*/
00037 
00038 /* Number of movements per revolution. */
00039 #define MPR_1 4
00040 
00041 /* Number of steps. */
00042 #define STEPS_1 (400 * 128)   /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
00043 #define STEPS_2 (STEPS_1 * 2)
00044 
00045 /* Delay in milliseconds. */
00046 #define DELAY_1 1000
00047 #define DELAY_2 2000
00048 #define DELAY_3 5000
00049 
00050 #ifdef TARGET_STM32F401
00051     DevSPI dev_spi(PB_15, PB_14, PB_13);
00052 #else
00053     DevSPI dev_spi(PB_15, PB_14, PB_13);
00054 #endif
00055  
00056 /* Variables -----------------------------------------------------------------*/
00057 
00058 /* Initialization parameters of the motors connected to the expansion board. */
00059 L6470_init_t init[L6470DAISYCHAINSIZE] = {
00060     /* First Motor. */
00061     {
00062         9.0,                           /* Motor supply voltage in V. */
00063         400,                           /* Min number of steps per revolution for the motor. */
00064         1.7,                           /* Max motor phase voltage in A. */
00065         3.06,                          /* Max motor phase voltage in V. */
00066         300.0,                         /* Motor initial speed [step/s]. */
00067         500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
00068         500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
00069         992.0,                         /* Motor maximum speed [step/s]. */
00070         0.0,                           /* Motor minimum speed [step/s]. */
00071         602.7,                         /* Motor full-step speed threshold [step/s]. */
00072         3.06,                          /* Holding kval [V]. */
00073         3.06,                          /* Constant speed kval [V]. */
00074         3.06,                          /* Acceleration starting kval [V]. */
00075         3.06,                          /* Deceleration starting kval [V]. */
00076         61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
00077         392.1569e-6,                   /* Start slope [s/step]. */
00078         643.1372e-6,                   /* Acceleration final slope [s/step]. */
00079         643.1372e-6,                   /* Deceleration final slope [s/step]. */
00080         0,                             /* Thermal compensation factor (range [0, 15]). */
00081         3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
00082         3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
00083         StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
00084         0xFF,                          /* Alarm conditions enable. */
00085         0x2E88                         /* Ic configuration. */
00086     },
00087 
00088     /* Second Motor. */
00089     {
00090         9.0,                           /* Motor supply voltage in V. */
00091         400,                           /* Min number of steps per revolution for the motor. */
00092         1.7,                           /* Max motor phase voltage in A. */
00093         3.06,                          /* Max motor phase voltage in V. */
00094         300.0,                         /* Motor initial speed [step/s]. */
00095         500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
00096         500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
00097         992.0,                         /* Motor maximum speed [step/s]. */
00098         0.0,                           /* Motor minimum speed [step/s]. */
00099         602.7,                         /* Motor full-step speed threshold [step/s]. */
00100         3.06,                          /* Holding kval [V]. */
00101         3.06,                          /* Constant speed kval [V]. */
00102         3.06,                          /* Acceleration starting kval [V]. */
00103         3.06,                          /* Deceleration starting kval [V]. */
00104         61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
00105         392.1569e-6,                   /* Start slope [s/step]. */
00106         643.1372e-6,                   /* Acceleration final slope [s/step]. */
00107         643.1372e-6,                   /* Deceleration final slope [s/step]. */
00108         0,                             /* Thermal compensation factor (range [0, 15]). */
00109         3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
00110         3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
00111         StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
00112         0xFF,                          /* Alarm conditions enable. */
00113         0x2E88                         /* Ic configuration. */
00114     }
00115 };
00116 
00117 
00118 /* Motor Control Expansion Board. */
00119 XNucleoIHM02A1* x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, D10, &dev_spi);
00120 
00121 DigitalOut actuatedLED(LED2);
00122 const static char     DEVICE_NAME[] = "mydevice";    //   CHANGE NAME
00123 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};   // GATT ATTRIBUTE UUID
00124 
00125 LEDService *ledServicePtr;
00126 
00127 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00128 {
00129     (void)params;
00130     BLE::Instance().gap().startAdvertising(); // restart advertising
00131 }
00132 
00133 /**
00134  * This callback allows the LEDService to receive updates to the ledState Characteristic.
00135  *
00136  * @param[in] params
00137  *     Information about the characterisitc being updated.
00138  */
00139 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00140     if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
00141         
00142         if (  *(params->data)== 0x41 ) 
00143         {    
00144             actuatedLED=1      ;
00145             FWD();
00146         }
00147         else if (*(params->data)== 0x42)  
00148         { 
00149             actuatedLED=0      ;
00150             //SOFTSTOP();
00151 
00152         }
00153         else if (*(params->data)== 0x43)  
00154         { 
00155             actuatedLED=1      ;
00156             //FWD();
00157 
00158 
00159         }
00160         else if (*(params->data)== 0x44)  
00161         { 
00162             actuatedLED=0      ;
00163             wait(0.1)          ;
00164             BWD();
00165         }
00166         else if (*(params->data)== 0x01)  
00167         { 
00168             actuatedLED=1      ;
00169             FWD();
00170         }
00171         else if (*(params->data)== 0x02)  {
00172             actuatedLED=1      ;
00173             BWD();
00174         }
00175         else if (*(params->data)== 0x03)  {
00176             actuatedLED=1      ;
00177             RIGHT();
00178         }
00179         else if (*(params->data)== 0x04)  {
00180             actuatedLED=1      ;
00181             LEFT();
00182         }
00183            
00184    
00185     }
00186  
00187 }
00188 
00189 /** 
00190  * This function is called when the ble initialization process has failled 
00191  */ 
00192 int onBleInitError(BLE &ble, ble_error_t error) 
00193 { 
00194     /* Initialization error handling should go here */ 
00195 } 
00196 
00197 /** 
00198  * Callback triggered when the ble initialization process has finished 
00199  */ 
00200 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
00201 {
00202     BLE&        ble   = params->ble;
00203     ble_error_t error = params->error;
00204 
00205     if (error != BLE_ERROR_NONE) {
00206         /* In case of error, forward the error handling to onBleInitError */
00207         onBleInitError(ble, error);
00208         return;
00209     }
00210 
00211     /* Ensure that it is the default instance of BLE */
00212     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00213         return;
00214     }
00215 
00216     ble.gap().onDisconnection(disconnectionCallback);
00217     ble.gattServer().onDataWritten(onDataWrittenCallback);
00218 
00219     bool initialValueForLEDCharacteristic = true;
00220     ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
00221 
00222     /* setup advertising */
00223     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00224     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00225     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00226     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00227     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00228     ble.gap().startAdvertising();
00229  
00230     while (true) {
00231         ble.waitForEvent();
00232     }
00233 }
00234 
00235 int main(void)
00236 {       
00237 
00238     BLE &ble = BLE::Instance();
00239     
00240     ble.init(bleInitComplete); 
00241  
00242 }
00243   
00244 void FWD()
00245 {
00246 
00247     /* Building a list of motor control components. */
00248     L6470 **motors = x_nucleo_ihm02a1->get_components();
00249    //otors[0]->set_home(); 
00250 
00251     /* Getting the current position. */
00252     //t position = motors[0]->get_position();
00253 
00254     /* Preparing each motor to perform a run at a specified speed. */
00255     for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
00256         motors[m]->prepare_run(StepperMotor::FWD, 400);
00257     }
00258 
00259     /* Performing the action on each motor at the same time. */
00260     x_nucleo_ihm02a1->perform_prepared_actions();
00261     
00262     /* Waiting while active. */
00263     motors[0]->wait_while_active();
00264     motors[1]->wait_while_active();
00265 }
00266 
00267 void BWD()
00268 {
00269 
00270     /* Building a list of motor control components. */
00271     L6470 **motors = x_nucleo_ihm02a1->get_components();
00272  // motors[0]->set_home(); 
00273 
00274     /* Getting the current position. */
00275 //  int position = motors[0]->get_position();
00276 
00277         /* Preparing each motor to perform a run at a specified speed. */
00278     for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
00279         motors[m]->prepare_move(StepperMotor::BWD, 25600);
00280     }
00281 
00282     /* Performing the action on each motor at the same time. */
00283     x_nucleo_ihm02a1->perform_prepared_actions();
00284     
00285     /* Waiting while active. */
00286     motors[0]->wait_while_active();
00287     motors[1]->wait_while_active();
00288 }
00289 void LEFT()
00290 {
00291 
00292     /* Building a list of motor control components. */
00293     L6470 **motors = x_nucleo_ihm02a1->get_components();
00294  // motors[0]->set_home(); 
00295 
00296     /* Getting the current position. */
00297  // int position = motors[0]->get_position();
00298 
00299         /* Preparing each motor to perform a run at a specified speed. */
00300     motors[0]->prepare_move(StepperMotor::FWD, 25600);
00301     motors[1]->prepare_move(StepperMotor::BWD, 25600);
00302 
00303     /* Performing the action on each motor at the same time. */
00304     x_nucleo_ihm02a1->perform_prepared_actions();
00305     
00306     /* Waiting while active. */
00307     motors[0]->wait_while_active();
00308     motors[1]->wait_while_active();
00309 }
00310 void RIGHT()
00311 {
00312 
00313     /* Building a list of motor control components. */
00314     L6470 **motors = x_nucleo_ihm02a1->get_components();
00315  // motors[0]->set_home(); 
00316 
00317     /* Getting the current position. */
00318 //  int position = motors[0]->get_position();
00319 
00320         /* Preparing each motor to perform a run at a specified speed. */
00321     motors[1]->prepare_move(StepperMotor::FWD, 25600);
00322     motors[0]->prepare_move(StepperMotor::BWD, 25600);
00323 
00324     /* Performing the action on each motor at the same time. */
00325     x_nucleo_ihm02a1->perform_prepared_actions();
00326     
00327     /* Waiting while active. */
00328     motors[0]->wait_while_active();
00329     motors[1]->wait_while_active();
00330 }
00331 void SOFTSTOP()
00332 {
00333     
00334     /* Building a list of motor control components. */
00335     L6470 **motors = x_nucleo_ihm02a1->get_components();
00336     /* Preparing each motor to perform a hard stop. */
00337     for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
00338         motors[m]->prepare_soft_stop();
00339     }
00340 
00341     /* Performing the action on each motor at the same time. */
00342     x_nucleo_ihm02a1->perform_prepared_actions();
00343 
00344     }