NEHSROBOT / Mbed 2 deprecated Motor_Ble_v1201820223FINAL

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed

Fork of Motor_Ble_v10223 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, A2, &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=1      ;
00150             BWD();
00151 
00152         }
00153         else if (*(params->data)== 0x43)  
00154         { 
00155             actuatedLED=1      ;
00156             RIGHT();
00157 
00158         }
00159         else if (*(params->data)== 0x44)  
00160         { 
00161             actuatedLED=1      ;
00162             LEFT();
00163         }
00164         else if (*(params->data)== 0x45)  
00165         { 
00166             actuatedLED=0      ;
00167             SOFTSTOP();
00168         }
00169 
00170            
00171    
00172     }
00173  
00174 }
00175 
00176 /** 
00177  * This function is called when the ble initialization process has failled 
00178  */ 
00179 int onBleInitError(BLE &ble, ble_error_t error) 
00180 { 
00181     /* Initialization error handling should go here */ 
00182 } 
00183 
00184 /** 
00185  * Callback triggered when the ble initialization process has finished 
00186  */ 
00187 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
00188 {
00189     BLE&        ble   = params->ble;
00190     ble_error_t error = params->error;
00191 
00192     if (error != BLE_ERROR_NONE) {
00193         /* In case of error, forward the error handling to onBleInitError */
00194         onBleInitError(ble, error);
00195         return;
00196     }
00197 
00198     /* Ensure that it is the default instance of BLE */
00199     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00200         return;
00201     }
00202 
00203     ble.gap().onDisconnection(disconnectionCallback);
00204     ble.gattServer().onDataWritten(onDataWrittenCallback);
00205 
00206     bool initialValueForLEDCharacteristic = true;
00207     ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
00208 
00209     /* setup advertising */
00210     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00211     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00212     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00213     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00214     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00215     ble.gap().startAdvertising();
00216  
00217     while (true) {
00218         ble.waitForEvent();
00219     }
00220 }
00221 
00222 int main(void)
00223 {       
00224 
00225     BLE &ble = BLE::Instance();
00226     
00227     ble.init(bleInitComplete); 
00228  
00229 }
00230   
00231 void FWD()
00232 {
00233 
00234     /* Building a list of motor control components. */
00235     L6470 **motors = x_nucleo_ihm02a1->get_components();
00236    //otors[0]->set_home(); 
00237 
00238     /* Getting the current position. */
00239     //t position = motors[0]->get_position();
00240 
00241     /* Preparing each motor to perform a run at a specified speed. */
00242     for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
00243         motors[m]->prepare_run(StepperMotor::FWD, 400);
00244     }
00245 
00246     /* Performing the action on each motor at the same time. */
00247     x_nucleo_ihm02a1->perform_prepared_actions();
00248     
00249     /* Waiting while active. */
00250     motors[0]->wait_while_active();
00251     motors[1]->wait_while_active();
00252 }
00253 
00254 void BWD()
00255 {
00256 
00257     /* Building a list of motor control components. */
00258     L6470 **motors = x_nucleo_ihm02a1->get_components();
00259  // motors[0]->set_home(); 
00260 
00261     /* Getting the current position. */
00262 //  int position = motors[0]->get_position();
00263 
00264     /* Preparing each motor to perform a run at a specified speed. */
00265     for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
00266         motors[m]->prepare_run(StepperMotor::BWD, 400);
00267     }
00268 
00269     /* Performing the action on each motor at the same time. */
00270     x_nucleo_ihm02a1->perform_prepared_actions();
00271     
00272     /* Waiting while active. */
00273     motors[0]->wait_while_active();
00274     motors[1]->wait_while_active();
00275 }
00276 void LEFT()
00277 {
00278 
00279     /* Building a list of motor control components. */
00280     L6470 **motors = x_nucleo_ihm02a1->get_components();
00281  // motors[0]->set_home(); 
00282 
00283     /* Getting the current position. */
00284  // int position = motors[0]->get_position();
00285 
00286         /* Preparing each motor to perform a run at a specified speed. */
00287     motors[0]->prepare_run(StepperMotor::FWD, 400);
00288     motors[1]->prepare_run(StepperMotor::BWD, 400);
00289 
00290     /* Performing the action on each motor at the same time. */
00291     x_nucleo_ihm02a1->perform_prepared_actions();
00292     
00293     /* Waiting while active. */
00294     motors[0]->wait_while_active();
00295     motors[1]->wait_while_active();
00296 }
00297 void RIGHT()
00298 {
00299 
00300     /* Building a list of motor control components. */
00301     L6470 **motors = x_nucleo_ihm02a1->get_components();
00302  // motors[0]->set_home(); 
00303 
00304     /* Getting the current position. */
00305 //  int position = motors[0]->get_position();
00306 
00307         /* Preparing each motor to perform a run at a specified speed. */
00308     motors[0]->prepare_run(StepperMotor::BWD, 400);
00309     motors[1]->prepare_run(StepperMotor::FWD, 400);
00310 
00311     /* Performing the action on each motor at the same time. */
00312     x_nucleo_ihm02a1->perform_prepared_actions();
00313     
00314     /* Waiting while active. */
00315     motors[0]->wait_while_active();
00316     motors[1]->wait_while_active();
00317 }
00318 void SOFTSTOP()
00319 {
00320     
00321     /* Building a list of motor control components. */
00322     L6470 **motors = x_nucleo_ihm02a1->get_components();
00323     /* Preparing each motor to perform a hard stop. */
00324     for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
00325         motors[m]->prepare_soft_stop();
00326     }
00327 
00328     /* Performing the action on each motor at the same time. */
00329     x_nucleo_ihm02a1->perform_prepared_actions();
00330 
00331     }