TinyCircuits Forum

General Category => User Projects / Code Examples => Topic started by: kiwilead on March 01, 2018, 01:23:29 PM

Title: TinyCircuits NRF8001 and MPU9250 help with sending continuous notification packe
Post by: kiwilead on March 01, 2018, 01:23:29 PM
Sorry for the nube question, but I am really struggling to try and get my IMU data from my MPU9250, to send constant notifications through BLE using the NRF8001. The hardware that I am using is the TinyShield Nordic board  from tinycircuits.

I feel like this should be easy, but when I connect BLElite I only get 0 as the notification.

Is there anyone out there who could help me? This is the thing that is currently holding me back, and I would love to get it working soon.

Thanks for all your help, bellow is the code that I am trying to use in order to read and send the data from the IMU through BLE.

Code: [Select]
#include <Wire.h>

#define    MPU9250_ADDRESS            0x69
#define    MAG_ADDRESS                0x0C

#define    GYRO_FULL_SCALE_250_DPS    0x00 
#define    GYRO_FULL_SCALE_500_DPS    0x08
#define    GYRO_FULL_SCALE_1000_DPS   0x10
#define    GYRO_FULL_SCALE_2000_DPS   0x18

#define    ACC_FULL_SCALE_2_G        0x00 
#define    ACC_FULL_SCALE_4_G        0x08
#define    ACC_FULL_SCALE_8_G        0x10
#define    ACC_FULL_SCALE_16_G       0x18



#define BLE_DEBUG 1

#include "SPI.h"
#include "lib_aci.h"
#include "aci_setup.h"
#include "uart_over_ble.h"
#include "services.h"


uint8_t ble_rx_buffer[21];
uint8_t ble_rx_buffer_len = 0;

//when using this project in the Arduino IDE, delete the following include and rename UART.h to UART.ino
#include "UART.h"

// This function read Nbytes bytes from I2C device at address Address.
// Put read bytes starting at register Register in the Data array.
void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data)
{
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.endTransmission();
 
  // Read Nbytes
  Wire.requestFrom(Address, Nbytes);
  uint8_t index=0;
  while (Wire.available())
    Data[index++]=Wire.read();
}


// Write a byte (Data) in device (Address) at register (Register)
void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data)
{
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.write(Data);
  Wire.endTransmission();
}

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  // Main loop, read and display data
  BLEsetup();
 
  // Set accelerometers low pass filter at 5Hz
  I2CwriteByte(MPU9250_ADDRESS,29,0x06);
  // Set gyroscope low pass filter at 5Hz
  I2CwriteByte(MPU9250_ADDRESS,26,0x06);
 
 
  // Configure gyroscope range
  I2CwriteByte(MPU9250_ADDRESS,27,GYRO_FULL_SCALE_1000_DPS);
  // Configure accelerometers range
  I2CwriteByte(MPU9250_ADDRESS,28,ACC_FULL_SCALE_4_G);
  // Set by pass mode for the magnetometers
  I2CwriteByte(MPU9250_ADDRESS,0x37,0x02);
 
  // Request continuous magnetometer measurements in 16 bits
  I2CwriteByte(MAG_ADDRESS,0x0A,0x16);


}






void loop() {

  aci_loop();//Process any ACI commands or events
 
   
  // ____________________________________
  // :::  accelerometer and gyroscope :::

// Read accelerometer and gyroscope
uint8_t Buf[14];
  I2Cread(MPU9250_ADDRESS,0x3B,14,Buf);
 
  // _____________________
  // :::  Magnetometer :::

 
  // Read register Status 1 and wait for the DRDY: Data Ready
 
  uint8_t ST1;
  do
  {
    I2Cread(MAG_ADDRESS,0x02,1,&ST1);
  }
  while (!(ST1&0x01));

  // Read magnetometer data 
uint8_t Mag[7]; 
I2Cread(MAG_ADDRESS,0x03,7,Mag);
 
 
// Create 16 bits values from 8 bits data
 
  // Accelerometer
  int16_t ax=-(Buf[0]<<8 | Buf[1]);
  int16_t ay=-(Buf[2]<<8 | Buf[3]);
  int16_t az=Buf[4]<<8 | Buf[5];

  // Gyroscope
  int16_t gx=-(Buf[8]<<8 | Buf[9]);
  int16_t gy=-(Buf[10]<<8 | Buf[11]);
  int16_t gz=Buf[12]<<8 | Buf[13];
 
  // Magnetometer
  int16_t mx=-(Mag[3]<<8 | Mag[2]);
  int16_t my=-(Mag[1]<<8 | Mag[0]);
  int16_t mz=-(Mag[5]<<8 | Mag[4]);
 
//  Serial.println(msg);

if(ble_rx_buffer_len){


   
    Serial.println(ble_rx_buffer_len);
    Serial.println((char*)ble_rx_buffer);
   
        // Display values
 
  // Accelerometer
String msg = String(ax,DEC);
msg += ("\t");
  msg += String(ay,DEC);
  msg += ("\t");
  msg += String(az,DEC); 
  msg += ("\t");

  // Gyroscope
  msg += String(gx,DEC);
  msg += ("\t");
  msg += String(gy,DEC);
  msg += ("\t");
  msg += String(gz,DEC); 
  msg += ("\t");
   
    // Magnetometer
  msg += String(mx+200,DEC);
  msg += ("\t");
  msg += String(my-70,DEC);
  msg += ("\t");
  msg += String(mz-700,DEC); 
  msg += ("\t");

   
    uint8_t sendBuffer[15];
    uint8_t length = 15;
   
    msg.getBytes(sendBuffer, 15);
   
    lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, sendBuffer, length);
   
    length=0;
   
  }
 
//  delay(250);//We'll make sure we're over the 64ms update time set on the BMA250

}