Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed
Fork of Motor_Ble_v1 by
main.cpp
- Committer:
- barry210110
- Date:
- 2018-02-04
- Revision:
- 9:ef9b37e2464f
- Parent:
- 7:126b141a8c86
- Child:
- 10:21eecb227c05
File content as of revision 9:ef9b37e2464f:
/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 /* Includes ------------------------------------------------------------------*/
/* Helper header files. */
#include "DevSPI.h"
/* Expansion Board specific header files. */
#include "XNucleoIHM02A1.h"
#include "mbed.h"
#include "ble/BLE.h"
#include "LEDService.h"
#include "BlueNRGDevice.h"
/* Definitions ---------------------------------------------------------------*/
/* Number of movements per revolution. */
#define MPR_1 4
/* Number of steps. */
#define STEPS_1 (400 * 128)   /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
#define STEPS_2 (STEPS_1 * 2)
/* Delay in milliseconds. */
#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. */
/* Initialization parameters of the motors connected to the expansion board. */
L6470_init_t init[L6470DAISYCHAINSIZE] = {
    /* First Motor. */
    {
        9.0,                           /* Motor supply voltage in V. */
        400,                           /* Min number of steps per revolution for the motor. */
        1.7,                           /* Max motor phase voltage in A. */
        3.06,                          /* Max motor phase voltage in V. */
        300.0,                         /* Motor initial speed [step/s]. */
        500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
        500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
        992.0,                         /* Motor maximum speed [step/s]. */
        0.0,                           /* Motor minimum speed [step/s]. */
        602.7,                         /* Motor full-step speed threshold [step/s]. */
        3.06,                          /* Holding kval [V]. */
        3.06,                          /* Constant speed kval [V]. */
        3.06,                          /* Acceleration starting kval [V]. */
        3.06,                          /* Deceleration starting kval [V]. */
        61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
        392.1569e-6,                   /* Start slope [s/step]. */
        643.1372e-6,                   /* Acceleration final slope [s/step]. */
        643.1372e-6,                   /* Deceleration final slope [s/step]. */
        0,                             /* Thermal compensation factor (range [0, 15]). */
        3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
        3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
        StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
        0xFF,                          /* Alarm conditions enable. */
        0x2E88                         /* Ic configuration. */
    },
    /* Second Motor. */
    {
        9.0,                           /* Motor supply voltage in V. */
        400,                           /* Min number of steps per revolution for the motor. */
        1.7,                           /* Max motor phase voltage in A. */
        3.06,                          /* Max motor phase voltage in V. */
        300.0,                         /* Motor initial speed [step/s]. */
        500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
        500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
        992.0,                         /* Motor maximum speed [step/s]. */
        0.0,                           /* Motor minimum speed [step/s]. */
        602.7,                         /* Motor full-step speed threshold [step/s]. */
        3.06,                          /* Holding kval [V]. */
        3.06,                          /* Constant speed kval [V]. */
        3.06,                          /* Acceleration starting kval [V]. */
        3.06,                          /* Deceleration starting kval [V]. */
        61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
        392.1569e-6,                   /* Start slope [s/step]. */
        643.1372e-6,                   /* Acceleration final slope [s/step]. */
        643.1372e-6,                   /* Deceleration final slope [s/step]. */
        0,                             /* Thermal compensation factor (range [0, 15]). */
        3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
        3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
        StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
        0xFF,                          /* Alarm conditions enable. */
        0x2E88                         /* Ic configuration. */
    }
};
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
LEDService *ledServicePtr;
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    (void)params;
    BLE::Instance().gap().startAdvertising(); // restart advertising
}
/**
 * This callback allows the LEDService to receive updates to the ledState Characteristic.
 *
 * @param[in] params
 *     Information about the characterisitc being updated.
 */
void onDataWrittenCallback(const GattWriteCallbackParams *params) {
    if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
 
        if (  *(params->data)== 0x00 ) 
        {    
            actuatedLED=1     ;
           call3();
              
            actuatedLED=0     ;wait(0.5)            ;
            actuatedLED=1     ;wait(0.5)            ;
            actuatedLED=0     ;wait(0.5)            ;
              
        }
        else if (*(params->data)== 0x01)  
        { 
            actuatedLED=0      ;
            wait(0.5)            ;
            actuatedLED=1      ;
            wait(0.5)            ;
            actuatedLED=0      ;
            wait(0.5)            ;
            actuatedLED=1      ;
            wait(0.5)            ;
            actuatedLED=0      ;
            
        }
        else if (*(params->data)== 0x02)  {
            actuatedLED=1      ;
            wait(0.5)            ;
            actuatedLED=0      ;
            wait(0.5)            ;
            actuatedLED=1      ;
        }
          else if (*(params->data)== 0x03)  {
           actuatedLED=1      ;
            wait(0.5)            ;
            actuatedLED=0      ;
            wait(0.5)            ;
            actuatedLED=1      ;
            wait(0.5)            ;
            actuatedLED=0      ;
            wait(0.5)            ;
            actuatedLED=1      ;
        }
   
    }
 
}
/** 
 * This function is called when the ble initialization process has failled 
 */ 
int onBleInitError(BLE &ble, ble_error_t error) 
{ 
    /* Initialization error handling should go here */ 
} 
/** 
 * Callback triggered when the ble initialization process has finished 
 */ 
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
{
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;
    if (error != BLE_ERROR_NONE) {
        /* In case of error, forward the error handling to onBleInitError */
        onBleInitError(ble, error);
       return;
    }
    /* Ensure that it is the default instance of BLE */
    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }
    ble.gap().onDisconnection(disconnectionCallback);
    ble.gattServer().onDataWritten(onDataWrittenCallback);
    bool initialValueForLEDCharacteristic = true;
    ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
    /* setup advertising */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
    ble.gap().startAdvertising();
 
    while (true) {
        ble.waitForEvent();
    }
}
int main(void)
{       
    BLE &ble = BLE::Instance();
    
    ble.init(bleInitComplete); 
 
}
void call3()
{
    /* Building a list of motor control components. */
    L6470 **motors = x_nucleo_ihm02a1->get_components();
   motors[0]->set_home(); 
    /* Waiting. */
    wait_ms(DELAY_1);
    /* Getting the current position. */
    int position = motors[0]->get_position();
    /* Waiting. */
    wait_ms(DELAY_1);
    /* Moving. */
    motors[0]->move(StepperMotor::FWD, STEPS_1);
    
    }
    
            
    