Hallo Netduino Community,
I think it is time to post my example here in the Netduino Forum. My first post was in German, but I see many webcalls come from around the world and the google translater is not quite good at understanding my descriptions. Well, my English is very rusty. So, I am sorry for any mistakes I might (and will) make.
Last year, I bought the sensor MPU-6050 and at first I couldn't run it. I saw the code library in C++ and read how to function the sensor. It was not very helpful for me. But it helped me to understand the code from Jeff Rowbergs Librarys. The code is awesome, but unfortunately not for a quick understanding.
First half of the year, I succeeded to run my prorgam with the sensor and posted it on my blog for sharing. Later I wrote a short Version for arduino. It fitted on a page side
Ok, what does my program do.
public class MPU6050Sensor{ // I²C Class for connecting to the sensor private I2CDevice _i2CDevice; // The Contructor initialized the connecting and the sensor public MPU6050Sensor() { // I²C connecting: // The first HEX value 0x68 is the address from the sensor and // the value 100 is for the transmission speed. // Well you can use 400kHz, // if you have a clean cable connection. _i2CDevice = new I2CDevice(new I2CDevice.Configuration(0x68, 100)); // Sensor init // Sleep und Reset execute: // The HEX value 0x6B is for the Power Management 1 // The secound Hex value includes the 'Bit' for reset. StatusMessage(Write(new byte[] { 0x6B, 0x80 })); Thread.Sleep(10); // Stop sleep and Clock setup: // The HEX value 0x6B is for the Power Management 1 // The secound Hex value 0x00 set the 'Clock Select' // to 'Internal 8MHz oscillator' // If you don't need the temperature sens, // you can set the HEX value 0x08. StatusMessage(Write(new byte[] { 0x6B, 0x00 })); // Configuration setup: // This Configuration activates the Low Pass Filter (DLPF). // Setting => Acc=5Hz, Delay=19.0ms, Gyro=5Hz, Delay=18.6ms, Fs=1kHz StatusMessage(Write(new byte[] { 0x1A, 0x06 })); } // Send a byte array to the sensor private int Write(byte[] buffer) { I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(buffer) }; return _i2CDevice.Execute(transactions, 1000); } // Read a byte array the sensor results private int Read(byte[] buffer) { I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(buffer) }; return _i2CDevice.Execute(transactions, 1000); } // Give a status message of result private void StatusMessage(int result) { if (result == 0) { Debug.Print("Status: Fehler beim Senden oder Empfangen"); } else { Debug.Print("Status: OK"); } } // Call the object SensorData with the sensor result public SensorData GetSensorData() { // Byte Array for reading byte[] buffer = new byte[14]; buffer[0] = 0x3B; // The HEX Value set the first byte // of the acceleration axis X Write(new byte[] { 0x3B }); // Read from the sensor Read(buffer); // Give the byte array result to the object, // and convert it to usable values return new SensorData(buffer); }}
The last method shows the SensorData object class and how it Looks.
public class SensorData{ public uint Acceleration_X = 0; public uint Acceleration_Y = 0; public uint Acceleration_Z = 0; public uint Temperatur = 0; public uint Gyroscope_X = 0; public uint Gyroscope_Y = 0; public uint Gyroscope_Z = 0; public SensorData(byte[] buffer) { // Result of the acceleration axis Acceleration_X = (((uint)buffer[0]) << 8) | buffer[1]; Acceleration_Y = (((uint)buffer[2]) << 8) | buffer[3]; Acceleration_Z = (((uint)buffer[4]) << 8) | buffer[5]; // Restult of temperature Temperatur = (((uint)buffer[6]) << 8) | buffer[7]; // Result of the gyroscope axis Gyroscope_X = (((uint)buffer[8]) << 8) | buffer[9]; Gyroscope_Y = (((uint)buffer[10]) << 8) | buffer[11]; Gyroscope_Z = (((uint)buffer[12]) << 8) | buffer[13]; }}
The next part is to connect the hardware, before you can deploy. Netduino to Sensor 3.3V >> VCC GND >> GND AnalogIn 4 >> SDA AnalogIn 5 >> SCL
At last, for the Main() Methode
public static void Main(){ MPU6050Sensor mpu = new MPU6050Sensor(); SensorData data = mpu.GetSensorData(); while (true) { data = mpu.GetSensorData(); Debug.Print( "Acc X: " + data.Acceleration_X + " " + "Acc Y: " + data.Acceleration_Y + " " + "Acc Z: " + data.Acceleration_Z + " " + "Gyro X: " + data.Gyroscope_X + " " + "Gyro Y: " + data.Gyroscope_Y + " " + "Gyro X: " + data.Gyroscope_Z + " " + "Temperatur: " + data.Temperatur); }}
Well, that is all and I hpoe you have fun
Greetings from Germany, have a nice weekend!!
Edit:
My reposting on my blog (Language German)
http://meineweltinme...en-mit-dem.html
Attached Files
Edited by Heroduino, 07 July 2013 - 07:45 PM.