20180223 ULTIMATE FINAL

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed

Fork of Motor_Ble_v1201820223FINAL by NEHSROBOT

Committer:
yong304
Date:
Sun Feb 04 08:49:24 2018 +0000
Revision:
8:66938f196868
Parent:
7:126b141a8c86
smart phone app control stepper motor via bluetooth low energy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apalmieri 0:aae2d6c2a9eb 1 /* mbed Microcontroller Library
apalmieri 0:aae2d6c2a9eb 2 * Copyright (c) 2006-2013 ARM Limited
apalmieri 0:aae2d6c2a9eb 3 *
apalmieri 0:aae2d6c2a9eb 4 * Licensed under the Apache License, Version 2.0 (the "License");
apalmieri 0:aae2d6c2a9eb 5 * you may not use this file except in compliance with the License.
apalmieri 0:aae2d6c2a9eb 6 * You may obtain a copy of the License at
apalmieri 0:aae2d6c2a9eb 7 *
apalmieri 0:aae2d6c2a9eb 8 * http://www.apache.org/licenses/LICENSE-2.0
apalmieri 0:aae2d6c2a9eb 9 *
apalmieri 0:aae2d6c2a9eb 10 * Unless required by applicable law or agreed to in writing, software
apalmieri 0:aae2d6c2a9eb 11 * distributed under the License is distributed on an "AS IS" BASIS,
apalmieri 0:aae2d6c2a9eb 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
apalmieri 0:aae2d6c2a9eb 13 * See the License for the specific language governing permissions and
apalmieri 0:aae2d6c2a9eb 14 * limitations under the License.
apalmieri 0:aae2d6c2a9eb 15 */
barry210110 7:126b141a8c86 16
barry210110 7:126b141a8c86 17 /* Includes ------------------------------------------------------------------*/
barry210110 7:126b141a8c86 18
barry210110 7:126b141a8c86 19
barry210110 7:126b141a8c86 20
barry210110 7:126b141a8c86 21 /* Helper header files. */
barry210110 7:126b141a8c86 22 #include "DevSPI.h"
yong304 8:66938f196868 23 void FWD();
yong304 8:66938f196868 24 void BWD();
yong304 8:66938f196868 25 void LEFT();
yong304 8:66938f196868 26 void RIGHT();
barry210110 7:126b141a8c86 27 /* Expansion Board specific header files. */
barry210110 7:126b141a8c86 28 #include "XNucleoIHM02A1.h"
apalmieri 0:aae2d6c2a9eb 29
apalmieri 0:aae2d6c2a9eb 30 #include "mbed.h"
apalmieri 0:aae2d6c2a9eb 31 #include "ble/BLE.h"
apalmieri 0:aae2d6c2a9eb 32 #include "LEDService.h"
yong304 8:66938f196868 33
yong304 8:66938f196868 34
barry210110 7:126b141a8c86 35 /* Definitions ---------------------------------------------------------------*/
apalmieri 0:aae2d6c2a9eb 36
barry210110 7:126b141a8c86 37 /* Number of movements per revolution. */
barry210110 7:126b141a8c86 38 #define MPR_1 4
barry210110 7:126b141a8c86 39
barry210110 7:126b141a8c86 40 /* Number of steps. */
barry210110 7:126b141a8c86 41 #define STEPS_1 (400 * 128) /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
barry210110 7:126b141a8c86 42 #define STEPS_2 (STEPS_1 * 2)
barry210110 7:126b141a8c86 43
barry210110 7:126b141a8c86 44 /* Delay in milliseconds. */
barry210110 7:126b141a8c86 45 #define DELAY_1 1000
barry210110 7:126b141a8c86 46 #define DELAY_2 2000
barry210110 7:126b141a8c86 47 #define DELAY_3 5000
barry210110 7:126b141a8c86 48
yong304 8:66938f196868 49 #ifdef TARGET_STM32F401
yong304 8:66938f196868 50 DevSPI dev_spi(PB_15, PB_14, PB_13);
yong304 8:66938f196868 51 #else
yong304 8:66938f196868 52 DevSPI dev_spi(PB_15, PB_14, PB_13);
yong304 8:66938f196868 53 #endif
yong304 8:66938f196868 54
barry210110 7:126b141a8c86 55 /* Variables -----------------------------------------------------------------*/
barry210110 7:126b141a8c86 56
barry210110 7:126b141a8c86 57 /* Initialization parameters of the motors connected to the expansion board. */
barry210110 7:126b141a8c86 58 L6470_init_t init[L6470DAISYCHAINSIZE] = {
barry210110 7:126b141a8c86 59 /* First Motor. */
barry210110 7:126b141a8c86 60 {
barry210110 7:126b141a8c86 61 9.0, /* Motor supply voltage in V. */
barry210110 7:126b141a8c86 62 400, /* Min number of steps per revolution for the motor. */
barry210110 7:126b141a8c86 63 1.7, /* Max motor phase voltage in A. */
barry210110 7:126b141a8c86 64 3.06, /* Max motor phase voltage in V. */
barry210110 7:126b141a8c86 65 300.0, /* Motor initial speed [step/s]. */
barry210110 7:126b141a8c86 66 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
barry210110 7:126b141a8c86 67 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
barry210110 7:126b141a8c86 68 992.0, /* Motor maximum speed [step/s]. */
barry210110 7:126b141a8c86 69 0.0, /* Motor minimum speed [step/s]. */
barry210110 7:126b141a8c86 70 602.7, /* Motor full-step speed threshold [step/s]. */
barry210110 7:126b141a8c86 71 3.06, /* Holding kval [V]. */
barry210110 7:126b141a8c86 72 3.06, /* Constant speed kval [V]. */
barry210110 7:126b141a8c86 73 3.06, /* Acceleration starting kval [V]. */
barry210110 7:126b141a8c86 74 3.06, /* Deceleration starting kval [V]. */
barry210110 7:126b141a8c86 75 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
barry210110 7:126b141a8c86 76 392.1569e-6, /* Start slope [s/step]. */
barry210110 7:126b141a8c86 77 643.1372e-6, /* Acceleration final slope [s/step]. */
barry210110 7:126b141a8c86 78 643.1372e-6, /* Deceleration final slope [s/step]. */
barry210110 7:126b141a8c86 79 0, /* Thermal compensation factor (range [0, 15]). */
barry210110 7:126b141a8c86 80 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
barry210110 7:126b141a8c86 81 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
barry210110 7:126b141a8c86 82 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
barry210110 7:126b141a8c86 83 0xFF, /* Alarm conditions enable. */
barry210110 7:126b141a8c86 84 0x2E88 /* Ic configuration. */
barry210110 7:126b141a8c86 85 },
barry210110 7:126b141a8c86 86
barry210110 7:126b141a8c86 87 /* Second Motor. */
barry210110 7:126b141a8c86 88 {
barry210110 7:126b141a8c86 89 9.0, /* Motor supply voltage in V. */
barry210110 7:126b141a8c86 90 400, /* Min number of steps per revolution for the motor. */
barry210110 7:126b141a8c86 91 1.7, /* Max motor phase voltage in A. */
barry210110 7:126b141a8c86 92 3.06, /* Max motor phase voltage in V. */
barry210110 7:126b141a8c86 93 300.0, /* Motor initial speed [step/s]. */
barry210110 7:126b141a8c86 94 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
barry210110 7:126b141a8c86 95 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
barry210110 7:126b141a8c86 96 992.0, /* Motor maximum speed [step/s]. */
barry210110 7:126b141a8c86 97 0.0, /* Motor minimum speed [step/s]. */
barry210110 7:126b141a8c86 98 602.7, /* Motor full-step speed threshold [step/s]. */
barry210110 7:126b141a8c86 99 3.06, /* Holding kval [V]. */
barry210110 7:126b141a8c86 100 3.06, /* Constant speed kval [V]. */
barry210110 7:126b141a8c86 101 3.06, /* Acceleration starting kval [V]. */
barry210110 7:126b141a8c86 102 3.06, /* Deceleration starting kval [V]. */
barry210110 7:126b141a8c86 103 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
barry210110 7:126b141a8c86 104 392.1569e-6, /* Start slope [s/step]. */
barry210110 7:126b141a8c86 105 643.1372e-6, /* Acceleration final slope [s/step]. */
barry210110 7:126b141a8c86 106 643.1372e-6, /* Deceleration final slope [s/step]. */
barry210110 7:126b141a8c86 107 0, /* Thermal compensation factor (range [0, 15]). */
barry210110 7:126b141a8c86 108 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
barry210110 7:126b141a8c86 109 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
barry210110 7:126b141a8c86 110 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
barry210110 7:126b141a8c86 111 0xFF, /* Alarm conditions enable. */
barry210110 7:126b141a8c86 112 0x2E88 /* Ic configuration. */
barry210110 7:126b141a8c86 113 }
barry210110 7:126b141a8c86 114 };
barry210110 7:126b141a8c86 115
yong304 8:66938f196868 116
yong304 8:66938f196868 117 /* Motor Control Expansion Board. */
yong304 8:66938f196868 118 XNucleoIHM02A1* x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);
yong304 8:66938f196868 119
barry210110 7:126b141a8c86 120 DigitalOut actuatedLED(LED2);
barry210110 7:126b141a8c86 121 const static char DEVICE_NAME[] = "mydevice"; // CHANGE NAME
barry210110 7:126b141a8c86 122 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; // GATT ATTRIBUTE UUID
apalmieri 0:aae2d6c2a9eb 123
apalmieri 0:aae2d6c2a9eb 124 LEDService *ledServicePtr;
apalmieri 0:aae2d6c2a9eb 125
apalmieri 0:aae2d6c2a9eb 126 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
apalmieri 0:aae2d6c2a9eb 127 {
apalmieri 0:aae2d6c2a9eb 128 (void)params;
apalmieri 3:e0efdb741bd4 129 BLE::Instance().gap().startAdvertising(); // restart advertising
apalmieri 0:aae2d6c2a9eb 130 }
apalmieri 0:aae2d6c2a9eb 131
apalmieri 0:aae2d6c2a9eb 132 /**
apalmieri 0:aae2d6c2a9eb 133 * This callback allows the LEDService to receive updates to the ledState Characteristic.
apalmieri 0:aae2d6c2a9eb 134 *
apalmieri 0:aae2d6c2a9eb 135 * @param[in] params
apalmieri 0:aae2d6c2a9eb 136 * Information about the characterisitc being updated.
apalmieri 0:aae2d6c2a9eb 137 */
apalmieri 0:aae2d6c2a9eb 138 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
apalmieri 0:aae2d6c2a9eb 139 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
barry210110 7:126b141a8c86 140
barry210110 7:126b141a8c86 141 if ( *(params->data)== 0x00 )
barry210110 7:126b141a8c86 142 {
yong304 8:66938f196868 143 actuatedLED=0 ;
yong304 8:66938f196868 144 wait(0.5) ;
yong304 8:66938f196868 145 actuatedLED=1 ;
yong304 8:66938f196868 146 wait(0.5) ;
barry210110 7:126b141a8c86 147 actuatedLED=0 ;
barry210110 7:126b141a8c86 148 wait(0.5) ;
barry210110 7:126b141a8c86 149 actuatedLED=1 ;
barry210110 7:126b141a8c86 150 wait(0.5) ;
barry210110 7:126b141a8c86 151 actuatedLED=0 ;
barry210110 7:126b141a8c86 152 wait(0.5) ;
barry210110 7:126b141a8c86 153 actuatedLED=1 ;
barry210110 7:126b141a8c86 154 wait(0.5) ;
barry210110 7:126b141a8c86 155 actuatedLED=0 ;
yong304 8:66938f196868 156 }
yong304 8:66938f196868 157 else if (*(params->data)== 0x01)
yong304 8:66938f196868 158 {
yong304 8:66938f196868 159 actuatedLED=1 ;
yong304 8:66938f196868 160 FWD();
barry210110 7:126b141a8c86 161 }
barry210110 7:126b141a8c86 162 else if (*(params->data)== 0x02) {
barry210110 7:126b141a8c86 163 actuatedLED=1 ;
yong304 8:66938f196868 164 BWD();
yong304 8:66938f196868 165 }
yong304 8:66938f196868 166 else if (*(params->data)== 0x03) {
barry210110 7:126b141a8c86 167 actuatedLED=1 ;
yong304 8:66938f196868 168 RIGHT();
barry210110 7:126b141a8c86 169 }
yong304 8:66938f196868 170 else if (*(params->data)== 0x04) {
barry210110 7:126b141a8c86 171 actuatedLED=1 ;
yong304 8:66938f196868 172 LEFT();
barry210110 7:126b141a8c86 173 }
yong304 8:66938f196868 174
barry210110 7:126b141a8c86 175
apalmieri 0:aae2d6c2a9eb 176 }
barry210110 7:126b141a8c86 177
apalmieri 0:aae2d6c2a9eb 178 }
apalmieri 0:aae2d6c2a9eb 179
apalmieri 0:aae2d6c2a9eb 180 /**
apalmieri 0:aae2d6c2a9eb 181 * This function is called when the ble initialization process has failled
apalmieri 0:aae2d6c2a9eb 182 */
barry210110 7:126b141a8c86 183 int onBleInitError(BLE &ble, ble_error_t error)
apalmieri 0:aae2d6c2a9eb 184 {
apalmieri 0:aae2d6c2a9eb 185 /* Initialization error handling should go here */
apalmieri 0:aae2d6c2a9eb 186 }
apalmieri 0:aae2d6c2a9eb 187
apalmieri 0:aae2d6c2a9eb 188 /**
apalmieri 0:aae2d6c2a9eb 189 * Callback triggered when the ble initialization process has finished
apalmieri 0:aae2d6c2a9eb 190 */
apalmieri 0:aae2d6c2a9eb 191 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
apalmieri 0:aae2d6c2a9eb 192 {
apalmieri 0:aae2d6c2a9eb 193 BLE& ble = params->ble;
apalmieri 0:aae2d6c2a9eb 194 ble_error_t error = params->error;
apalmieri 0:aae2d6c2a9eb 195
apalmieri 0:aae2d6c2a9eb 196 if (error != BLE_ERROR_NONE) {
apalmieri 0:aae2d6c2a9eb 197 /* In case of error, forward the error handling to onBleInitError */
apalmieri 0:aae2d6c2a9eb 198 onBleInitError(ble, error);
apalmieri 0:aae2d6c2a9eb 199 return;
apalmieri 0:aae2d6c2a9eb 200 }
apalmieri 0:aae2d6c2a9eb 201
apalmieri 0:aae2d6c2a9eb 202 /* Ensure that it is the default instance of BLE */
apalmieri 0:aae2d6c2a9eb 203 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
apalmieri 0:aae2d6c2a9eb 204 return;
apalmieri 0:aae2d6c2a9eb 205 }
apalmieri 0:aae2d6c2a9eb 206
apalmieri 0:aae2d6c2a9eb 207 ble.gap().onDisconnection(disconnectionCallback);
apalmieri 0:aae2d6c2a9eb 208 ble.gattServer().onDataWritten(onDataWrittenCallback);
apalmieri 0:aae2d6c2a9eb 209
apalmieri 0:aae2d6c2a9eb 210 bool initialValueForLEDCharacteristic = true;
apalmieri 3:e0efdb741bd4 211 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
apalmieri 0:aae2d6c2a9eb 212
apalmieri 0:aae2d6c2a9eb 213 /* setup advertising */
apalmieri 0:aae2d6c2a9eb 214 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
apalmieri 0:aae2d6c2a9eb 215 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
apalmieri 0:aae2d6c2a9eb 216 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
apalmieri 0:aae2d6c2a9eb 217 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
apalmieri 0:aae2d6c2a9eb 218 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
apalmieri 0:aae2d6c2a9eb 219 ble.gap().startAdvertising();
barry210110 7:126b141a8c86 220
apalmieri 0:aae2d6c2a9eb 221 while (true) {
apalmieri 0:aae2d6c2a9eb 222 ble.waitForEvent();
apalmieri 0:aae2d6c2a9eb 223 }
apalmieri 0:aae2d6c2a9eb 224 }
apalmieri 0:aae2d6c2a9eb 225
apalmieri 0:aae2d6c2a9eb 226 int main(void)
barry210110 7:126b141a8c86 227 {
barry210110 7:126b141a8c86 228
apalmieri 0:aae2d6c2a9eb 229 BLE &ble = BLE::Instance();
apalmieri 0:aae2d6c2a9eb 230
barry210110 7:126b141a8c86 231 ble.init(bleInitComplete);
barry210110 7:126b141a8c86 232
apalmieri 0:aae2d6c2a9eb 233 }
barry210110 7:126b141a8c86 234
yong304 8:66938f196868 235 void FWD()
barry210110 7:126b141a8c86 236 {
barry210110 7:126b141a8c86 237
barry210110 7:126b141a8c86 238 /* Building a list of motor control components. */
barry210110 7:126b141a8c86 239 L6470 **motors = x_nucleo_ihm02a1->get_components();
yong304 8:66938f196868 240 motors[0]->set_home();
yong304 8:66938f196868 241
yong304 8:66938f196868 242 /* Getting the current position. */
yong304 8:66938f196868 243 int position = motors[0]->get_position();
yong304 8:66938f196868 244
yong304 8:66938f196868 245 /* Preparing each motor to perform a run at a specified speed. */
yong304 8:66938f196868 246 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
yong304 8:66938f196868 247 motors[m]->prepare_move(StepperMotor::FWD, 25600);
yong304 8:66938f196868 248 }
yong304 8:66938f196868 249
yong304 8:66938f196868 250 /* Performing the action on each motor at the same time. */
yong304 8:66938f196868 251 x_nucleo_ihm02a1->perform_prepared_actions();
yong304 8:66938f196868 252
yong304 8:66938f196868 253 /* Waiting while active. */
yong304 8:66938f196868 254 motors[0]->wait_while_active();
yong304 8:66938f196868 255 }
yong304 8:66938f196868 256
yong304 8:66938f196868 257 void BWD()
yong304 8:66938f196868 258 {
yong304 8:66938f196868 259
yong304 8:66938f196868 260 /* Building a list of motor control components. */
yong304 8:66938f196868 261 L6470 **motors = x_nucleo_ihm02a1->get_components();
yong304 8:66938f196868 262 motors[0]->set_home();
barry210110 7:126b141a8c86 263
barry210110 7:126b141a8c86 264 /* Getting the current position. */
barry210110 7:126b141a8c86 265 int position = motors[0]->get_position();
barry210110 7:126b141a8c86 266
yong304 8:66938f196868 267 /* Preparing each motor to perform a run at a specified speed. */
barry210110 7:126b141a8c86 268 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
yong304 8:66938f196868 269 motors[m]->prepare_move(StepperMotor::BWD, 25600);
barry210110 7:126b141a8c86 270 }
barry210110 7:126b141a8c86 271
barry210110 7:126b141a8c86 272 /* Performing the action on each motor at the same time. */
barry210110 7:126b141a8c86 273 x_nucleo_ihm02a1->perform_prepared_actions();
yong304 8:66938f196868 274
yong304 8:66938f196868 275 /* Waiting while active. */
yong304 8:66938f196868 276 motors[0]->wait_while_active();
yong304 8:66938f196868 277 }
yong304 8:66938f196868 278 void LEFT()
yong304 8:66938f196868 279 {
barry210110 7:126b141a8c86 280
yong304 8:66938f196868 281 /* Building a list of motor control components. */
yong304 8:66938f196868 282 L6470 **motors = x_nucleo_ihm02a1->get_components();
yong304 8:66938f196868 283 motors[0]->set_home();
barry210110 7:126b141a8c86 284
yong304 8:66938f196868 285 /* Getting the current position. */
yong304 8:66938f196868 286 int position = motors[0]->get_position();
barry210110 7:126b141a8c86 287
yong304 8:66938f196868 288 /* Preparing each motor to perform a run at a specified speed. */
yong304 8:66938f196868 289 motors[0]->prepare_move(StepperMotor::FWD, 25600);
yong304 8:66938f196868 290 motors[1]->prepare_move(StepperMotor::BWD, 25600);
barry210110 7:126b141a8c86 291
barry210110 7:126b141a8c86 292 /* Performing the action on each motor at the same time. */
barry210110 7:126b141a8c86 293 x_nucleo_ihm02a1->perform_prepared_actions();
yong304 8:66938f196868 294
yong304 8:66938f196868 295 /* Waiting while active. */
yong304 8:66938f196868 296 motors[0]->wait_while_active();
yong304 8:66938f196868 297 }
yong304 8:66938f196868 298 void RIGHT()
yong304 8:66938f196868 299 {
barry210110 7:126b141a8c86 300
yong304 8:66938f196868 301 /* Building a list of motor control components. */
yong304 8:66938f196868 302 L6470 **motors = x_nucleo_ihm02a1->get_components();
yong304 8:66938f196868 303 motors[0]->set_home();
barry210110 7:126b141a8c86 304
yong304 8:66938f196868 305 /* Getting the current position. */
yong304 8:66938f196868 306 int position = motors[0]->get_position();
barry210110 7:126b141a8c86 307
yong304 8:66938f196868 308 /* Preparing each motor to perform a run at a specified speed. */
yong304 8:66938f196868 309 motors[1]->prepare_move(StepperMotor::FWD, 25600);
yong304 8:66938f196868 310 motors[0]->prepare_move(StepperMotor::BWD, 25600);
barry210110 7:126b141a8c86 311
barry210110 7:126b141a8c86 312 /* Performing the action on each motor at the same time. */
barry210110 7:126b141a8c86 313 x_nucleo_ihm02a1->perform_prepared_actions();
yong304 8:66938f196868 314
yong304 8:66938f196868 315 /* Waiting while active. */
yong304 8:66938f196868 316 motors[0]->wait_while_active();
yong304 8:66938f196868 317 }