Thursday, 5 March 2015

Output of serial data

Today after managing to write some simple code to be able to get the arduino to output the raw values for the acceleration and rotation in all 3 axis over the serial port I decided to try and do something with this data. As it currently stands it is very difficult to visualise the data from a stream of numbers.

The plan is to use Octave to read the serial data, and then plot simple graphs of the value for each Degree of Freedom against time, so that the data can be visualised and tested.

It will also be desirable to look more closely at how the MPU is processing the data, setting the clock to make it as accurate as possible and various other optimization tasks. These are all possible relatively simply by changing the values for the various different registries as denoted in the registry map. The basic syntax for this is to write 2 bytes across the I2C bus, the first being the address of the registry (normally in hex) and the second being the value to be written (often in binary for ease).

The code currently stands like so :


#include <Wire.h> //include the Wire library for comunication with the MPU 6050 over the I2C bus.
//The adress of the MPU6050 is 0x68 with the ADO pin not connected or put to grund. //For connection to the DUE make sure it goes into pins 20 and 21 for the SDA and SCL as they have
 internal pull up resistors. If using the TWI2 pins, it will not work with out pull up resistors.     int AcX; //declare variables     int AcY;     int AcZ;     int Tmp;     int GyX;     int GyY;     int GyZ;          byte address; void setup() // run once, when the sketch starts {   Serial.begin(9600); // set up Serial library at 9600 bps   Wire.begin();      Wire.beginTransmission(0x68);//reset mpu   Wire.write(0x6B);   Wire.write(0x40);   Wire.endTransmission(true);      Wire.beginTransmission(0x68);//wake device (clear sleep bit)   Wire.write(0x6B);   Wire.write(0x00);   Wire.endTransmission(true);      Wire.beginTransmission(0x68); //who am i   Wire.write(0x75);   Wire.endTransmission(true);   Wire.requestFrom(0x68,1);       address=Wire.read();   Serial.print("MPU6050: ");     Serial.print("MPU I2C Adress: 0x");     Serial.println(address,HEX);      Serial.println("Start");    //  Wire.beginTransmission(0x68);  //  Wire.write(0x35);             //FIFO Enable //  Wire.write(B11111000); //  Wire.endTransmission(true); //   } void loop() // run over and over again {           Wire.beginTransmission(0x68);   Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)   Wire.endTransmission(false);   Wire.requestFrom(0x68,14,true); // request a total of 14 registers   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)   Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_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)   Serial.print("AcX = "); Serial.print(AcX);   Serial.print(" | AcY = "); Serial.print(AcY);   Serial.print(" | AcZ = "); Serial.print(AcZ);   Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  
//equation for temperature in degrees C from datasheet   Serial.print(" | GyX = "); Serial.print(GyX);   Serial.print(" | GyY = "); Serial.print(GyY);   Serial.print(" | GyZ = "); Serial.println(GyZ);        Serial.print("Temp1: ");   Serial.println(analogRead(0));   Serial.print("Tempt2: ");   Serial.println(analogRead(1));      Serial.println();              delay(1000);                                      }

No comments:

Post a Comment