NEHSROBOT / Mbed 2 deprecated BLE_LED_IDB0XA1_demo

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed

Fork of BLE_LED_IDB0XA1 by ST

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