Hello everyone,
I am very new to arduino and mpu 6050 module.I am working on a project "Walking pattern analysis" My job is to read accelerometer and gyroscope raw data. I've no idea how to set sampling rate for the mpu 6050. The code i m using is
// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include "MPU6050.h"
#include "I2Cdev.h"
//#include "MPU6050_6Axis_MotionApps20.h"
#include <elapsedMillis.h>
#include <Wire.h>
#include <SoftwareSerial.h>
elapsedMillis timeElapsed;
SoftwareSerial Bluetooth(0, 1); // RX, TX
const int MPU_addr=0x68; // I2C address of the MPU-6050
MPU6050 mpu;
int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
/*#define MPU6050_ACCEL_FS_2 0x00
#define MPU6050_ACCEL_FS_4 0x01
#define MPU6050_ACCEL_FS_8 0x02
#define MPU6050_ACCEL_FS_16 0x03
#define MPU6050_GYRO_FS_250 0x00
#define MPU6050_GYRO_FS_500 0x01
#define MPU6050_GYRO_FS_1000 0x02
#define MPU6050_GYRO_FS_2000 0x03*/
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
//unsigned long startTime;
//unsigned long elapsedTime;
void setup(){
Bluetooth.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
mpu.initialize();
// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
// mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
// mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_4);
mpu.setFullScaleGyroRange(3); //set the gyro range to 2000
mpu.setFullScaleAccelRange(2); //set the accelerometer sensibilty to 8g
Serial.println("Full scale gyro range : ");
Serial.println(mpu.getFullScaleGyroRange());
Serial.println(mpu.getFullScaleAccelRange());
//mpu.setRate(4);
// Serial.println("Sampling rate: ");
//Serial.println(mpu.getRate());
}
void loop(){
Wire.begin();
// Wire.setClock(400000L);
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
//startTime = millis();
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
//elapsedTime = (millis() - startTime)/1000;
//Serial.print("Time elapsed in ms:");
Serial.print("Time");
Serial.print("\t");
Serial.print(timeElapsed);
Serial.print("\t");
Serial.print("Ax,Ay,Az,Gx,Gy,Gz");
Serial.print("\t");
Serial.print(AcX);
Serial.print("\t");
//Serial.print("AcY = ");
Serial.print(AcY);
Serial.print("\t");
// Serial.print("AcZ = ");
Serial.print(AcZ);
Serial.print("\t");
//Serial.print("GyX = ");
Serial.print(GyX);
Serial.print("\t");
//Serial.print("GyY = ");
Serial.print(GyY);
Serial.print("\t");
//Serial.print("GyZ = ");
Serial.println(GyZ);
delay(333);
}
Is this code good for my job and how to set sampling rate?
I'll be so grateful if someone helps me out for the problem.