Bluetooth shifted aquisition

Dear all,

I’m a french student and I would like to introduce you my project based on MPU accelerometer communicating data to my smartphone.

So i’m equipped with one arduino nano, one accelerometer MPU 6050 and one HC-05 bluetooth module.

I adapted a arduino sketch which send all 100ms angle information from MPU with HC-05.

#include <SoftwareSerial.h> //SoftwareSerial Port
#define RxD 10//Pin 10 pour RX, PB2 survotre board, a brancher sur le TX du HC-06
#define TxD 11 //Pin 11 pour TX, PB3 survotre board, a brancher sur le RX du HC-06
SoftwareSerial BTSerie(RxD,TxD);

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 mpu;

#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2  // use pin 2 on Arduino Uno & most boards
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

// packet structure for InvenSense teapot demo
uint8_t teapotPacket[14] = { '

Then I quickly coded with “mit app inventor 2” a mobile application which scan bluetooth module, connect to it and receive data (A screenshot of the scratch program is attached).
The application create a text file and save data from internal phone GPS’s and MPU arduino each 100 ms.
I can start, stop and reset when i want the recording.

The idea is to take this text file and separate easily different data to plot them.
My problem is that I try to control the order of accumulated data in order to easily plot it but after some running time there is a shift of order as you can see below :

   s:0 la:45.20283 lo:5.70243g:11a:11
    s:0 la:45.20283 lo:5.70243g:11a:11
    s:0 la:45.20283 lo:5.70243g:11a:12
    s:0 la:45.20283 lo:5.70243g:12a:15
    s:0 la:45.20283 lo:5.70243g:12a:15
    s:0 la:45.20283 lo:5.70243g:12a:15
    s:0 la:45.20283 lo:5.70243g:16a:14
    s:0 la:45.20283 lo:5.70243g:16a:14
    s:0 la:45.20283 lo:5.70243g:16a:14
    s:0 la:45.20283 lo:5.70243g:17a:9
    s:0 la:45.20283 lo:5.70243g:17a:9
    s:0 la:45.20283 lo:5.70243g:17a:9
    s:0 la:45.20283 lo:5.70243g:18a:15
g
    s:0 la:45.20283 lo:5.70243:18a:15
g:18a:16
    s:0 la:45.20283 lo:5.70243g:18a:20
    s:0 la:45.20283 lo:5.70243g:19a:20
    s:0 la:45.20283 lo:5.70243g:19a:20
    s:0 la:45.20283 lo:5.70243g:18a:22
    s:0 la:45.20283 lo:5.70243g:18a:21
    s:0 la:45.20283 lo:5.70243g:18a:21
    s:0 la:45.20283 lo:5.70243g:15a:19
g
    s:0 la:45.20283 lo:5.70243:15a:19
g:15a:19
    s:0 la:45.20283 lo:5.70243
    s:0 la:45.20283 lo:5.70243g:13a:16
g:13a:16
    s:0 la:45.20283 lo:5.70243g:13a:16
    s:0 la:45.20283 lo:5.70243g:13a:14
    s:0 la:45.20283 lo:5.70243g:13a:14
    s:0 la:45.20283 lo:5.70243g:13a:14
    s:0 la:45.20283 lo:5.70243g:13a:14
    s:0 la:45.20283 lo:5.70243g:13a:14
g:13a:14
    s:0 la:45.20283 lo:5.70243g
    s:0 la:45.20283 lo:5.70243:11a:13
g:11a:13
    s:0 la:45.20283 lo:5.70243g:11a:13
    s:0 la:45.20283 lo:5.70243g:13a:13
    s:0 la:45.20283 lo:5.70243g:13a:13
    s:0 la:45.20283 lo:5.70243g:13a:13
    s:0 la:45.20283 lo:5.70243g:13a:13
    s:0 la:45.20283 lo:5.70243g:13a:13

Data “s:0 la:45.20283 lo:5.70243” are coming from the phone GPS and g:13a:13 are angle coming from MPU via bluetooth protocol.

Is there a easy way to control the order of my data ? Is it a problem of data format from arduino ?

I’m a beginner and I need help and advice ^^.

I’m able to give you more details if it’s not clear.

Thank you :slight_smile:

, 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, ‘\r’, ‘\n’ };

// ================================================================
// ===              INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;    // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
    mpuInterrupt = true;
}

// ================================================================
// ===                      INITIAL SETUP                      ===
// ================================================================

void setup() {
    // join I2C bus (I2Cdev library doesn’t do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
      // Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

// initialize serial communication
 
    Serial.begin(9600);
    while (!Serial); // wait for Leonardo enumeration, others continue immediately

// initialize device
    mpu.initialize();
    pinMode(INTERRUPT_PIN, INPUT);
    devStatus = mpu.dmpInitialize();

// supply your own gyro offsets here, scaled for min sensitivity
    mpu.setXGyroOffset(220);
    mpu.setYGyroOffset(76);
    mpu.setZGyroOffset(-85);
    mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

mpu.setDMPEnabled(true);
        // enable Arduino interrupt detection
        attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        // set our DMP Ready flag so the main loop() function knows it’s okay to use it
        dmpReady = true;
        // get expected DMP packet size for later comparison
      packetSize = mpu.dmpGetFIFOPacketSize();
           
BTSerie.begin(9600);
}

// ================================================================
// ===                    MAIN PROGRAM LOOP                    ===
// ================================================================

void loop() {
    // if programming failed, don’t try to do anything
    if (!dmpReady) return;

// wait for MPU interrupt or extra packet(s) available
    while (!mpuInterrupt && fifoCount < packetSize) {
        if (mpuInterrupt && fifoCount < packetSize) {
          // try to get out of the infinite loop
          fifoCount = mpu.getFIFOCount();
         
        } 
    }

// reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();

// get current FIFO count
    fifoCount = mpu.getFIFOCount();

// check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus & _BV(MPU6050_INTERRUPT_FIFO_OFLOW_BIT)) || fifoCount >= 1024) {
        // reset so we can continue cleanly
        mpu.resetFIFO();
        fifoCount = mpu.getFIFOCount();

// otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus & _BV(MPU6050_INTERRUPT_DMP_INT_BIT)) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

// read a packet from FIFO
        mpu.getFIFOBytes(fifoBuffer, packetSize);
       
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

int git = ypr[1] * 180/M_PI ;
            int as = ypr[2] * 180/M_PI ;

BTSerie.print(" g:");
            BTSerie.print(git);
            BTSerie.print(" a:");
            BTSerie.print(as);
            BTSerie.print(’\r’);

delay(100);
        #endif

}
}


Then I quickly coded with "mit app inventor 2" a mobile application which scan bluetooth module, connect to it and receive data (A screenshot of the scratch program is attached). 
The application create a text file and save data from internal phone GPS's and MPU arduino each 100 ms.
I can start, stop and reset when i want the recording.

The idea is to take this text file and separate easily different data to plot them.
My problem is that I try to control the order of accumulated data in order to easily plot it but after some running time there is a shift of order as you can see below :

§DISCOURSE_HOISTED_CODE_1§


Data "s:0 la:45.20283 lo:5.70243" are coming from the phone GPS and g:13a:13 are angle coming from MPU via bluetooth protocol.

Is there a easy way to control the order of my data ? Is it a problem of data format from arduino ?

I'm a beginner and I need help and advice ^^.

I'm able to give you more details if it's not clear.

Thank you :)

![Capture.JPG|973x660](upload://qntGkMjRnpH3BEUtupHm6DGZamQ.jpeg)
#include <SoftwareSerial.h> //SoftwareSerial Port
#define RxD 1//Pin 10 pour RX, PB2 survotre board, a brancher sur le TX du HC-06
#define TxD 11 //Pin 11 pour TX, PB3 survotre board, a brancher sur le RX du HC-06
SoftwareSerial BTSerie(RxD,TxD);

    Serial.begin(9600);

You can NOT use pin 1 for software serial while using it for hardware serial.

What happens in your code when you try is completely unpredictable. So, don’t even try.

Sorry, It's a "Copy & paste" mistake :

define RxD 10//Pin 10 pour RX, PB2 survotre board, a brancher sur le TX du HC-06

define TxD 11 //Pin 11 pour TX, PB3 survotre board, a brancher sur le RX du HC-06