Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Grove - 3-axis Gyro I2C problem


  • Please log in to reply
4 replies to this topic

#1 Alazar

Alazar

    New Member

  • Members
  • Pip
  • 1 posts

Posted 17 April 2012 - 08:54 AM

Hi, everyone!
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 Attached File  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.

#2 teo

teo

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationEdmonton, AB

Posted 17 April 2012 - 08:21 PM

Perhaps the connectors are not firm enough and shaking the device might (for a split second) disconnect the I2C bus and result in an unrecoverable error state. Not sure if this is your case, but it happened to me last time.

#3 teo

teo

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationEdmonton, AB

Posted 18 April 2012 - 01:29 AM

Another thing, I realised that you enabled interrupts:
gyro.WriteToRegister(0x17, 0x01);
Looking at the photograph, the interrupt line is not connected to the netduino. (I don't think that's the source of your problem, though.)

#4 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 18 April 2012 - 04:21 AM

Is anyone here faced such problem? Could you tell me what wrong with it?

Looking t the datasheet there is no mention of the module supplying it's own pull up resistors. It is also not clear from the photograph how the module is connected to the Netduino. So I'd consider investigating this.

regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#5 teo

teo

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationEdmonton, AB

Posted 18 April 2012 - 05:15 PM

Looking t the datasheet there is no mention of the module supplying it's own pull up resistors. It is also not clear from the photograph how the module is connected to the Netduino. So I'd consider investigating this.

regards,
Mark


According to the schematic of the module, there are 10k pullups on the bus, before and after the 3v3/5v level-shifter.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.