Hi everyone,
I've been working on a project using Arduino Uno, MPU6050 sensor and Load Cell to capture angle in degrees, angular velocity in dps, and force in Newtons combined in just one code. As of now, I have it all working together. I'm currently capturing my angle, angular velocity and Force. However, I'm having a problem in the speed for data output even though I change the delay time to delay(0.00001) and still not outputting the data any faster. Is there anyway to speed the data output up? Is this going to be a code problem or the Arduino Uno processing power? The Code below is the one that I'm using right now. Any help from this would be greatly appreciated. Thank you!
//
#include "HX711.h"
#define calibration_factor -10555.65 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT 4
#define CLK 3
HX711 scale(DOUT, CLK);
//Angle And Angular Velocity
#include "Wire.h"
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define LED_PIN 13
bool blinkState = false;
// accelerometer values
int accel_reading;
int accel_corrected;
int accel_offset = 200;
float accel_angle;
float accel_scale = 1; // set to 0.01
// gyro values
int gyro_offset = 151; // 151
int gyro_corrected;
int gyro_reading;
float gyro_rate;
float gyro_scale = 0.02; // 0.02 by default - tweak as required
float gyro_angle;
float loop_time = 0.05; // 50ms loop
float angle = 0.00; // value to hold final calculated gyro angle
// time stamp variables
int last_update;
int cycle_time;
long last_cycle = 0;
void setup() {
Serial.begin(9600);
Serial.println("HX711 scale demo");
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println("Readings:");
// Angle And Angular Velocity
Wire.begin();
// initialize serial communication
setupMPU();
Serial.begin(9600);
Serial.println("CLEARDATA");
Serial.println("LABEL,Angle-°,X-dps,Y-dps,Z-dps,ForceN");
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
}
void loop() {
Serial.println("Readings:");
Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
Serial.print(" N"); //You can change this to kg but you'll need to refactor the calibration_factor
Serial.println();
// read raw accel/gyro measurements from device
recordGyroRegisters();
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// accelerometer_X_Axis angle calc
accel_reading = ax;
accel_corrected = accel_reading - accel_offset;
accel_corrected = map(accel_corrected, -16800, 16800, -90, 90);
accel_corrected = constrain(accel_corrected, -90, 90);
accel_angle = (float)(accel_corrected * accel_scale);
Serial.print(accel_angle);
Serial.println("\t");
// gyro_Y_Axis angle calc
gyro_reading = gy;
gyro_corrected = (float)((gyro_reading/131) - gyro_offset); // 131 is sensivity of gyro from data sheet
gyro_rate = (gyro_corrected * gyro_scale) * -loop_time; // loop_time = 0.05 ie 50ms
gyro_angle = angle + gyro_rate;
// print values to serial monitor for checking
Serial.println( (String) "DATA," + accel_angle + "," + rotX + "," + rotY + "," + rotZ + "," + scale.get_units ( ));
Serial.print(accel_angle);
Serial.println("\t");
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
//timestamp
time_stamp();
delay(0.00001);
}
void time_stamp(){
while ((millis() - last_cycle) < 50){
}
// once loop cycle reaches 50ms, reset timer value and continue
cycle_time = millis() - last_cycle;
last_cycle = millis();
}
void setupMPU(){
Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00000000); //Setting the accel to +/- 2g
Wire.endTransmission();
}
void recordGyroRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x43); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
while(Wire.available() < 6);
gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processGyroData();
}
void processGyroData() {
rotX = gyroX / 131.0;
rotY = gyroY / 131.0;
rotZ = gyroZ / 131.0;
}