![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MPU6050
Dependencies: BLE_API MPU6050 mbed nRF51822
Fork of NordicDTMviaUART by
Revision 1:91caa6c7f077, committed 2015-09-22
- Comitter:
- DarkerL
- Date:
- Tue Sep 22 04:49:00 2015 +0000
- Parent:
- 0:a2cffc867df4
- Commit message:
- BLE_MPU6050
Changed in this revision
MPU6050.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPU6050.lib Tue Sep 22 04:49:00 2015 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/DarkerL/code/MPU6050/#04a94bea4f74
--- a/main.cpp Wed Jan 14 12:36:59 2015 +0000 +++ b/main.cpp Tue Sep 22 04:49:00 2015 +0000 @@ -1,15 +1,3 @@ -/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - #include "mbed.h" #include <stdint.h> #include <stdbool.h> @@ -17,11 +5,32 @@ #include "nrf51_bitfields.h" #include "ble_dtm.h" #include "nrf_gpio.h" +#include <stdio.h> +#include "MPU6050.h" +#include "BLEDevice.h" + + +// DFUService is already included & automatically advertised by the mbed lib dependancies (currently) + +const static char DEVICE_NAME[] = "Hello_LinkSprite"; + +BLEDevice ble; + +DigitalOut myled(LED1); + +MPU6050 mpu(0x69); +int16_t ax=0, ay=0, az=0; +int16_t gx=0, gy=0, gz=0; +int moyZ=0; //global Z value (mean) +int16_t moy[64]; //array of different measurements + + +//DigitalOut myled(LED2); // Configuration parameters. -#define BITRATE UART_BAUDRATE_BAUDRATE_Baud19200 /**< Serial bitrate on the UART */ -#define TX_PIN_NUMBER USBTX /**< Use proper value for your hardware */ -#define RX_PIN_NUMBER USBRX /**< Use proper value for your hardware */ +#define BITRATE UART_BAUDRATE_BAUDRATE_Baud9600 /**< Serial bitrate on the UART */ +#define TX_PIN_NUMBER 0 /**< Use proper value for your hardware */ +#define RX_PIN_NUMBER 1 /**< Use proper value for your hardware */ #define MAX_ITERATIONS_NEEDED_FOR_NEXT_BYTE 21 /**@brief Function for UART initialization. */ @@ -48,30 +57,88 @@ } -/**@brief Function for splitting UART command bit fields into separate command parameters for the DTM library. -* - * @param[in] command The packed UART command. - * @return result status from dtmlib. - */ -static uint32_t dtm_cmd_put(uint16_t command) + +void moyennage_Z() //calculates the mean value by going through the whole array, sum and divide by the sample size (64) { - dtm_cmd_t command_code = (command >> 14) & 0x03; - dtm_freq_t freq = (command >> 8) & 0x3F; - uint32_t length = (command >> 2) & 0x3F; - dtm_pkt_type_t payload = command & 0x03; + for (int n=0; n<64; n++) + { + moyZ=moyZ+moy[n]; + } + moyZ=(int)moyZ/64; +} + + +const uint8_t MPU6050_service_uuid[] = { + 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 +}; + +const uint8_t MPU6050_Accel_Characteristic_uuid[] = { + 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9 +}; + + +uint8_t accelPayload[sizeof(int16_t)*6] = {0,}; + +void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) +{ + ble.startAdvertising(); // restart advertising +} + +GattCharacteristic accelChar (MPU6050_Accel_Characteristic_uuid, + accelPayload, (sizeof(int16_t) * 6), (sizeof(int16_t) * 6), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + +GattCharacteristic *ControllerChars[] = { &accelChar }; +GattService MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); + + - // Check for Vendor Specific payload. - if (payload == 0x03) { - /* Note that in a HCI adaption layer, as well as in the DTM PDU format, - the value 0x03 is a distinct bit pattern (PRBS15). Even though BLE does not - support PRBS15, this implementation re-maps 0x03 to DTM_PKT_VENDORSPECIFIC, - to avoid the risk of confusion, should the code be extended to greater coverage. - */ - payload = DTM_PKT_VENDORSPECIFIC; - } - return dtm_cmd(command_code, freq, length, payload); +int16_t exchange_position(int16_t temp) +{ + int16_t i = temp ; + + temp = temp>>8; + temp = (i<<8) | temp; + + return temp; } +void updata_values() +{ + //int16_t value_test = 100; + + //value_test = exchange_position(value_test); + + + mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + + + ax = exchange_position(ax); + ay = exchange_position(ay); + az = exchange_position(az); + gx = exchange_position(gx); + gy = exchange_position(gy); + gz = exchange_position(gz); + + //memcpy(accelPayload+sizeof(int16_t)*0, &value_test, sizeof(value_test)); + + memcpy(accelPayload+sizeof(int16_t)*0, &ax, sizeof(ax)); + memcpy(accelPayload+sizeof(int16_t)*1, &ay, sizeof(ay)); + memcpy(accelPayload+sizeof(int16_t)*2, &az, sizeof(az)); + memcpy(accelPayload+sizeof(int16_t)*3, &gx, sizeof(gx)); + memcpy(accelPayload+sizeof(int16_t)*4, &gy, sizeof(gy)); + memcpy(accelPayload+sizeof(int16_t)*5, &gz, sizeof(gz)); + + ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload)); //Mod + + wait(0.1); +} + + + + + + /**@brief Function for application main entry. * * @details This function serves as an adaptation layer between a 2-wire UART interface and the @@ -79,89 +146,93 @@ * dtmlib and events (i.e. results from the command) is reported back through the UART. */ int main(void) -{ - uint32_t current_time; - uint32_t dtm_error_code; - uint32_t msb_time = 0; // Time when MSB of the DTM command was read. Used to catch stray bytes from "misbehaving" testers. - bool is_msb_read = false; // True when MSB of the DTM command has been read and the application is waiting for LSB. - uint16_t dtm_cmd_from_uart = 0; // Packed command containing command_code:freqency:length:payload in 2:6:6:2 bits. - uint8_t rx_byte; // Last byte read from UART. - dtm_event_t result; // Result of a DTM operation. - - uart_init(); - - // Turn default LED on to indicate power - nrf_gpio_cfg_output(LED1); - nrf_gpio_pin_set(LED1); - - dtm_error_code = dtm_init(); - if (dtm_error_code != DTM_SUCCESS) { - // If DTM cannot be correctly initialized, then we just return. - return -1; - } +{ + + int16_t test = 0x6400; + int16_t LinkSprite = 0; + LinkSprite = exchange_position(test); + + ble.init(); + ble.onDisconnection(disconnectionCallback); - for (;;) { - // Will return every timeout, 625 us. - current_time = dtm_wait(); + /* Setup advertising. */ + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); +// ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); + // ble.startAdvertising(); + + + + /* setup advertising */ + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, + (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid)); + //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid)); - if (NRF_UART0->EVENTS_RXDRDY == 0) { - // Nothing read from the UART. - continue; - } - NRF_UART0->EVENTS_RXDRDY = 0; - rx_byte = (uint8_t)NRF_UART0->RXD; - - if (!is_msb_read) { - // This is first byte of two-byte command. - is_msb_read = true; - dtm_cmd_from_uart = ((dtm_cmd_t)rx_byte) << 8; - msb_time = current_time; - - // Go back and wait for 2nd byte of command word. - continue; - } + ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ + ble.startAdvertising(); - // This is the second byte read; combine it with the first and process command - if (current_time > (msb_time + MAX_ITERATIONS_NEEDED_FOR_NEXT_BYTE)) { - // More than ~5mS after msb: Drop old byte, take the new byte as MSB. - // The variable is_msb_read will remains true. - // Go back and wait for 2nd byte of the command word. - dtm_cmd_from_uart = ((dtm_cmd_t)rx_byte) << 8; - msb_time = current_time; - continue; - } - - // 2-byte UART command received. - is_msb_read = false; - dtm_cmd_from_uart |= (dtm_cmd_t)rx_byte; - - if (dtm_cmd_put(dtm_cmd_from_uart) != DTM_SUCCESS) { - // Extended error handling may be put here. - // Default behavior is to return the event on the UART (see below); - // the event report will reflect any lack of success. - } - - // Retrieve result of the operation. This implementation will busy-loop - // for the duration of the byte transmissions on the UART. - if (dtm_event_get(&result)) { - // Report command status on the UART. - // Transmit MSB of the result. - NRF_UART0->TXD = (result >> 8) & 0xFF; - // Wait until MSB is sent. - while (NRF_UART0->EVENTS_TXDRDY != 1) { - // Do nothing. - } - NRF_UART0->EVENTS_TXDRDY = 0; - - // Transmit LSB of the result. - NRF_UART0->TXD = result & 0xFF; - // Wait until LSB is sent. - while (NRF_UART0->EVENTS_TXDRDY != 1) { - // Do nothing. - } - NRF_UART0->EVENTS_TXDRDY = 0; - } - } + ble.addService(MPU6050Service); + + uart_init(); + + // Thread thread(mon_thr); + printf("MPU6050 test\n\n\r"); //procedure to test the connection to the mpu6050, if valid + printf("MPU6050 initialize \n\r"); //the code to execute is embedded in a if{} + mpu.initialize(); + printf("MPU6050 testConnection \n\r"); + bool mpu6050TestResult = mpu.testConnection(); + + if(mpu6050TestResult) + { + + printf("MPU6050 test passed \n\r"); + + while(1) + { + /* + myled = 0; + wait(0.2); + myled = 1; + wait(0.2); + + wait(1); + + */ + printf("hello LinkSprite123 \r\n"); + + printf("LinkSprite test = 0x%x \r\n\n\r", LinkSprite); + + + + + /* Uncomment below if you want to see accel and gyro data */ + /* + for (int n=0; n<64; n++) + { //refreshing the 64-int16 array + mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + moy[n]=az; + wait(0.01); + } + + */ + + updata_values(); + wait(0.1); + + moyennage_Z(); //calculating the mean value by a simple sum and divide + printf("%i\n\r",moyZ+17000); + printf("ax = %d, ay = %d, az = %d, gx = %d, gy = %d, gz = %d\n\r", ax,ay, az, gx, gy, gz); + wait(0.1); + printf("hello LinkSprite456 \r\n"); + + } + } + + else + { + printf("MPU6050 test failed \n\r"); + } } -/// @}