I trying to make a quadrotor (just like many of us ) so, i bought a Grove - 3-axis Gyro http://www.seeedstud...is_Gyro#Support
I connected it to Netduino via I2C, everything works fine for several minutes or until i shake it or rotate rapidly(not always). Then I2CDevice.Execute() return zero, and i can`t connect again to device, until power of netduino is switched off. I'm tried many I2C drivers, but problem still remain.
The one that works more stable is listed below:
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace I2CDev { public class Program { public static void Main() { I2CPlug gyro = new I2CPlug(0x68); gyro.WriteToRegister(0x3e, 0x80); Thread.Sleep(100); gyro.WriteToRegister(0x15, 0x00); Thread.Sleep(100); gyro.WriteToRegister(0x16, 0x19); Thread.Sleep(100); gyro.WriteToRegister(0x17, 0x01); Thread.Sleep(100); byte[] readBuffer = new byte[1] { 0 }; int i = 0; for (; ; ) { gyro.ReadFromRegister(0x1a, readBuffer); if ( (byte)(readBuffer[0] & 0x01) == 0x01) { Debug.Print("Iteration : " + i.ToString() + "\n"); gyro.ReadFromRegister(0x00, readBuffer); Debug.Print("ID : " + readBuffer[0].ToString() + "\n"); gyro.ReadFromRegister(0x1d, readBuffer); Debug.Print(readBuffer[0].ToString() + "\n"); gyro.ReadFromRegister(0x1e, readBuffer); Debug.Print(readBuffer[0].ToString() + "\n"); gyro.ReadFromRegister(0x1f, readBuffer); Debug.Print(readBuffer[0].ToString() + "\n"); gyro.ReadFromRegister(0x20, readBuffer); Debug.Print(readBuffer[0].ToString() + "\n"); gyro.ReadFromRegister(0x21, readBuffer); Debug.Print(readBuffer[0].ToString() + "\n"); gyro.ReadFromRegister(0x22, readBuffer); Debug.Print(readBuffer[0].ToString() + "\n"); } i++; } } } }
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; namespace I2CDev { public class I2CPlug { private const int DefaultClockRate = 400; private const int TransactionTimeout = 1000; private I2CDevice.Configuration i2cConfig; private I2CDevice i2cDevice; public byte Address { get; private set; } public I2CPlug(byte address, int clockRateKhz) { this.Address = address; this.i2cConfig = new I2CDevice.Configuration(this.Address, clockRateKhz); this.i2cDevice = new I2CDevice(this.i2cConfig); } public I2CPlug(byte address) : this(address, DefaultClockRate) { } private void Write(byte[] writeBuffer) { // create a write transaction containing the bytes to be written to the device I2CDevice.I2CTransaction[] writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; // write the data to the device int written = this.i2cDevice.Execute(writeTransaction, TransactionTimeout); while (written < writeBuffer.Length) { byte[] newBuffer = new byte[writeBuffer.Length - written]; Array.Copy(writeBuffer, written, newBuffer, 0, newBuffer.Length); writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(newBuffer) }; written += this.i2cDevice.Execute(writeTransaction, TransactionTimeout); } // make sure the data was sent if (written != writeBuffer.Length) { throw new Exception("Could not write to device."); } } private void Read(byte[] readBuffer) { // create a read transaction I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(readBuffer) }; // read data from the device int read = this.i2cDevice.Execute(readTransaction, TransactionTimeout); // make sure the data was read if (read != readBuffer.Length) { throw new Exception("Could not read from device."); } } public void WriteToRegister(byte register, byte value) { this.Write(new byte[] { register, value }); } protected void WriteToRegister(byte register, byte[] values) { // create a single buffer, so register and values can be send in a single transaction byte[] writeBuffer = new byte[values.Length + 1]; writeBuffer[0] = register; Array.Copy(values, 0, writeBuffer, 1, values.Length); this.Write(writeBuffer); } public void ReadFromRegister(byte register, byte[] readBuffer) { this.Write(new byte[] { register }); this.Read(readBuffer); } } }
Device connection look like this WP_000094.jpg 96.45KB 28 downloads
Is anyone here faced such problem? Could you tell me what wrong with it?
P.S. Sorry for my english.