Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "I2Cdev.h"
- #include "MPU6050.h"
- #include <Wire.h>
- #include <SD.h>
- int pinCS = 10;
- File myFile ;
- // FIND MEMORY SPACE ! ! !
- /////////////////////////////////// CONFIGURATION /////////////////////////////
- //Change this 3 variables if you want to fine tune the skecth to your needs.
- int buffersize = 1000; //Amount of readings used to average, make it higher to get more precision but sketch will be slower (default:1000)
- int acel_deadzone = 8; //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge (default:8)
- int giro_deadzone = 1; //Giro error allowed, make it lower to get more precision, but sketch may not converge (default:1)
- // 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;
- MPU6050 accelgyro(0x68); // <-- use for AD0 high int cnt = 0; // for counting up to 100 , then reading the SD card.
- int cnt=0;
- int cnt2 =0; // for counting the number of calibration steps.
- int16_t ax, ay, az, gx, gy, gz;
- int mean_ax, mean_ay, mean_az, mean_gx, mean_gy, mean_gz, state = 0;
- int ax_offset, ay_offset, az_offset, gx_offset, gy_offset, gz_offset;
- int ready = 0;
- long accelX, accelY, accelZ;
- float gForceX, gForceY, gForceZ;
- long gyroX, gyroY, gyroZ;
- float rotX, rotY, rotZ;
- void setup() {
- pinMode(pinCS, OUTPUT);
- Serial.begin(9600);
- if (SD.begin())
- {
- Serial.println("SD card is ready to use.");
- } else
- {
- Serial.println("SD card initialization failed");
- return;
- }
- // join I2C bus (I2Cdev library doesn't do this automatically)
- Wire.begin();
- // start message
- Serial.println("Starting 5 seconds timer to give time for the new program to upload: ");
- delay(5000);
- Serial.println("\nMPU6050 Calibration Sketch");
- delay(2000);
- Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
- delay(3000);
- // verify connection
- Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
- delay(1000);
- // reset offsets
- accelgyro.setXAccelOffset(0);
- accelgyro.setYAccelOffset(0);
- accelgyro.setZAccelOffset(0);
- accelgyro.setXGyroOffset(0);
- accelgyro.setYGyroOffset(0);
- accelgyro.setZGyroOffset(0);
- setupMPU();
- if (state == 0) {
- Serial.println(F("\nReading sensors for first time..."));
- meansensors();// why is this function called 2 times ? TODO ! ! ! <-----------------------------------------------------------------------------////////////////////////////////
- state++;
- delay(1000);
- }
- if (state == 1) {
- Serial.println(F("\nCalculating offsets..."));
- calibration();
- state++;
- delay(1000);
- }
- if (state == 2) {
- meansensors();
- Serial.println(F("\nFINISHED!"));
- Serial.println(F("Number of calibration steps: \t ")); Serial.print(cnt2);
- Serial.print(F("\nSensor readings with offsets:\t"));
- Serial.print(mean_ax);
- Serial.print("\t");
- Serial.print(mean_ay);
- Serial.print("\t");
- Serial.print(mean_az);
- Serial.print("\t");
- Serial.print(mean_gx);
- Serial.print("\t");
- Serial.print(mean_gy);
- Serial.print("\t");
- Serial.print(mean_gz);
- Serial.print(F("Your offsets:\t"));
- Serial.print(ax_offset);
- Serial.print("\t");
- Serial.print(ay_offset);
- Serial.print("\t");
- Serial.print(az_offset);
- Serial.print("\t");
- Serial.print(gx_offset);
- Serial.print("\t");
- Serial.print(gy_offset);
- Serial.print("\t");
- Serial.println(gz_offset);
- Serial.println(F("\nData is printed as: acelX acelY acelZ giroX giroY giroZ"));
- Serial.println(F("Check that your sensor readings are close to 0 0 16384 0 0 0"));
- Serial.println(F(" --------------------------------- "));
- myFile = SD.open("test.txt", FILE_WRITE);
- //Serial.println(F("If this value is 0, you've fucked something up :D")); always gives 0 ?
- Serial.print(myFile);
- if (myFile) {
- Serial.println("Yaaay, we are writing to the file :D");
- myFile.print("The offsets are: \n ");
- myFile.print(ax_offset); myFile.print("\t");
- myFile.println(ay_offset); myFile.print("\t");
- myFile.println(az_offset); myFile.print("\t");
- myFile.close(); // close the file
- }
- else {
- Serial.println(F("error opening test.txt"));
- }
- delay(1000);
- }
- }
- void loop() {
- // the offsets were in the loop func
- // myFile = SD.open("test.txt", FILE_WRITE);
- // //Serial.println(F("If this value is 0, you've fucked something up :D")); always gives 0 ?
- // Serial.print(myFile);
- // if (myFile) {
- // Serial.println("Yaaay, we are writing to the file :D");
- // myFile.println(" are ");
- // myFile.print(ax_offset); myFile.print("\t");
- //
- // myFile.println(ay_offset); myFile.print("\t");
- //
- // myFile.println(az_offset); myFile.print("\t");
- //
- //
- // myFile.close(); // close the file
- // }
- // if the file didn't open, print an error:
- // else {
- // Serial.println(F("error opening test.txt"));
- // }
- // delay(1000);
- recordAccelRegisters();
- recordGyroRegisters();
- printData();
- Serial.println(F("After this sentence, it should start writing the Gforces on the SD card, so yeah, we did something: \t"));
- myFile = SD.open("test.txt", FILE_WRITE);
- {
- // new line , today, 17 . 07
- myFile.print(gForceX); myFile.print("\t"); // change to printLN ? 17.07
- myFile.print(gForceY); myFile.print("\t");
- myFile.print(gForceZ); myFile.print("\t");
- cnt++; // increase the counter after writing X times to the file
- myFile.close();
- }
- delay(1000);
- Serial.println(F("This is just for checking the counter status ___ "));
- Serial.print(cnt);
- if (cnt == 10) // increase the counter number for larger data
- {
- myFile = SD.open("test.txt");
- while (myFile.available())
- {
- Serial.write(myFile.read());
- }
- Serial.println(F("File was closed, exiting"));
- myFile.close();
- delay(50000);
- }
- }
- void meansensors() {
- long i = 0, buff_ax = 0, buff_ay = 0, buff_az = 0, buff_gx = 0, buff_gy = 0, buff_gz = 0;
- while (i < (buffersize + 101)) {
- // read raw accel/gyro measurements from device
- accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
- if (i > 100 && i <= (buffersize + 100)) { //First 100 measures are discarded
- buff_ax = buff_ax + ax;
- buff_ay = buff_ay + ay;
- buff_az = buff_az + az;
- buff_gx = buff_gx + gx;
- buff_gy = buff_gy + gy;
- buff_gz = buff_gz + gz;
- }
- if (i == (buffersize + 100)) {
- mean_ax = buff_ax / buffersize;
- mean_ay = buff_ay / buffersize;
- mean_az = buff_az / buffersize;
- mean_gx = buff_gx / buffersize;
- mean_gy = buff_gy / buffersize;
- mean_gz = buff_gz / buffersize;
- }
- i++;
- delay(2); //Needed so we don't get repeated measures
- }
- }
- void calibration() {
- ax_offset = -mean_ax / 8;
- ay_offset = -mean_ay / 8;
- az_offset = (8192 - mean_az) / 8;
- gx_offset = -mean_gx / 4;
- gy_offset = -mean_gy / 4;
- gz_offset = -mean_gz / 4;
- while (ready < 6) {
- int ready = 0;
- accelgyro.setXAccelOffset(ax_offset);
- accelgyro.setYAccelOffset(ay_offset);
- accelgyro.setZAccelOffset(az_offset);
- accelgyro.setXGyroOffset(gx_offset);
- accelgyro.setYGyroOffset(gy_offset);
- accelgyro.setZGyroOffset(gz_offset);
- meansensors();
- Serial.println("...");
- cnt2++;
- if (abs(mean_ax) <= acel_deadzone) ready++;
- else ax_offset = ax_offset - mean_ax / acel_deadzone;
- if (abs(mean_ay) <= acel_deadzone) ready++;
- else ay_offset = ay_offset - mean_ay / acel_deadzone;
- if (abs(8192 - mean_az) <= acel_deadzone) ready++;
- else az_offset = az_offset + (8192 - mean_az) / acel_deadzone;
- if (abs(mean_gx) <= giro_deadzone) ready++;
- else gx_offset = gx_offset - mean_gx / (giro_deadzone + 1);
- if (abs(mean_gy) <= giro_deadzone) ready++;
- else gy_offset = gy_offset - mean_gy / (giro_deadzone + 1);
- if (abs(mean_gz) <= giro_deadzone) ready++;
- else gz_offset = gz_offset - mean_gz / (giro_deadzone + 1);
- if (ready == 6) break;
- }
- }
- void setupMPU() {
- Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2), we use the LOW , a.k.a. ADO is connected to GND .
- 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(0b00001000); //Setting the accel to +/- 4g
- Wire.endTransmission();
- }
- void recordAccelRegisters() {
- Wire.beginTransmission(0b1101000); //I2C address of the MPU
- Wire.write(0x3B); //Starting register for Accel Readings
- Wire.endTransmission();
- Wire.requestFrom(0b1101000, 6); //Request Accel Registers (3B - 40)
- while (Wire.available() < 6);
- accelX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
- accelY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
- accelZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
- processAccelData();
- }
- void processAccelData() {
- gForceX = accelX / 8192.0;
- gForceY = accelY / 8192.0;
- gForceZ = accelZ / 8192.0;
- }
- 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;
- }
- void printData() {
- Serial.println(F("Gyro (deg)"));
- Serial.println(F(" X="));
- Serial.print(rotX);
- // if (rotX >= 100) { // <<<<<<<<<<<<<<--------- DELETE THIS FUCKERS, WE DON'T HAVE MEMORY !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!, NVM FOUND IT :D
- // blinkLED();
- // }
- // if (rotX <= -90) {
- // blinkLED();
- // }
- // Serial.print(" Y=");
- // Serial.print(rotY);
- // if (rotY >= 90) {
- // blinkLED();
- // }
- // if (rotY <= -90) {
- // blinkLED();
- // }
- Serial.println(F(" Z="));
- Serial.print(rotZ);
- Serial.println(F(" Accel (g)"));
- Serial.print(F(" X="));
- Serial.print(gForceX);
- // if (gForceX >= 0.75) {
- // blinkLED();
- // }
- // if (gForceX <= -0.75) {
- // blinkLED();
- // }
- Serial.print(F(" Y="));
- Serial.print(gForceY);
- // if (gForceY >= 0.75) {
- // blinkLED();
- // }s
- // if (gForceY <= -0.75) {
- // blinkLED();
- // }
- // Serial.print(" Z=");
- // Serial.println(gForceZ);
- // if (gForceZ >= 3.5) {
- // blinkLED();
- // }
- //}
- //void blinkLED() {
- // digitalWrite(LED_BUILTIN, HIGH);
- // delay(5000);
- // digitalWrite(LED_BUILTIN, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement