Exemple #1
0
        protected byte Read(byte address, byte[] buffer)
        {
            I2CDevice.I2CWriteTransaction write           = I2CDevice.CreateWriteTransaction(new Byte[] { address });
            I2CDevice.I2CReadTransaction  read            = I2CDevice.CreateReadTransaction(buffer);
            I2CDevice.I2CTransaction[]    readTransaction = new I2CDevice.I2CTransaction[] { write, read };

            var result = I2CBus.Execute(Config, readTransaction, 1000);

            return(buffer[0]);
        }
        public int RequestAngularRate()
        {
            byte[] bufferXhi = new byte[1];
            byte[] bufferYhi = new byte[1];
            byte[] bufferZhi = new byte[1];
            byte[] bufferXlo = new byte[1];
            byte[] bufferYlo = new byte[1];
            byte[] bufferZlo = new byte[1];
            I2CDevice.I2CTransaction[]    readTransaction = new I2CDevice.I2CTransaction[12];
            I2CDevice.I2CWriteTransaction write;
            I2CDevice.I2CReadTransaction  read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_X_H_A });
            read  = I2CDevice.CreateReadTransaction(bufferXhi);
            readTransaction[0] = write;
            readTransaction[1] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_X_L_A });
            read  = I2CDevice.CreateReadTransaction(bufferXlo);
            readTransaction[2] = write;
            readTransaction[3] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Y_H_A });
            read  = I2CDevice.CreateReadTransaction(bufferYhi);
            readTransaction[4] = write;
            readTransaction[5] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Y_L_A });
            read  = I2CDevice.CreateReadTransaction(bufferYlo);
            readTransaction[6] = write;
            readTransaction[7] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Z_H_A });
            read  = I2CDevice.CreateReadTransaction(bufferZhi);
            readTransaction[8] = write;
            readTransaction[9] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Z_L_A });
            read  = I2CDevice.CreateReadTransaction(bufferZlo);
            readTransaction[10] = write;
            readTransaction[11] = read;

            var result = I2CBus.Execute(Config, readTransaction, 1000);

            AngularRate.X = -CombineBytes(bufferXhi[0], bufferXlo[0]) * _scale + calXoffSet;
            AngularRate.Y = -CombineBytes(bufferYhi[0], bufferYlo[0]) * _scale + calYoffSet;
            AngularRate.Z = CombineBytes(bufferZhi[0], bufferZlo[0]) * _scale + calZoffSet;

            return(result);
        }
Exemple #3
0
        protected void Write(byte address, byte value)
        {
            var bytes = new byte[] { address, value };

            I2CBus.Write(Config, bytes, 1000);
        }
Exemple #4
0
        public int RequestMagValue()
        {
            float x, y, z;

            byte[] bufferXhi = new byte[1];
            byte[] bufferYhi = new byte[1];
            byte[] bufferZhi = new byte[1];
            byte[] bufferXlo = new byte[1];
            byte[] bufferYlo = new byte[1];
            byte[] bufferZlo = new byte[1];
            I2CDevice.I2CTransaction[]    readTransaction = new I2CDevice.I2CTransaction[12];
            I2CDevice.I2CWriteTransaction write;
            I2CDevice.I2CReadTransaction  read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_X_H_M });
            read  = I2CDevice.CreateReadTransaction(bufferXhi);
            readTransaction[0] = write;
            readTransaction[1] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_X_L_M });
            read  = I2CDevice.CreateReadTransaction(bufferXlo);
            readTransaction[2] = write;
            readTransaction[3] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Y_H_M });
            read  = I2CDevice.CreateReadTransaction(bufferYhi);
            readTransaction[4] = write;
            readTransaction[5] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Y_L_M });
            read  = I2CDevice.CreateReadTransaction(bufferYlo);
            readTransaction[6] = write;
            readTransaction[7] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Z_H_M });
            read  = I2CDevice.CreateReadTransaction(bufferZhi);
            readTransaction[8] = write;
            readTransaction[9] = read;

            write = I2CDevice.CreateWriteTransaction(new Byte[] { Register.OUT_Z_L_M });
            read  = I2CDevice.CreateReadTransaction(bufferZlo);
            readTransaction[10] = write;
            readTransaction[11] = read;

            var result = I2CBus.Execute(Config, readTransaction, 1000);

            x = CombineBytes(bufferXhi[0], bufferXlo[0]);
            z = CombineBytes(bufferYhi[0], bufferYlo[0]); // Y <--> Z Exchanged
            y = CombineBytes(bufferZhi[0], bufferZlo[0]); // Z <--> Y Exchanged

            if (_calibrationmode)
            {
                if (x > maxX)
                {
                    maxX = x;
                }
                if (x < minX)
                {
                    minX = x;
                }
                if (y > maxY)
                {
                    maxY = y;
                }
                if (y < minY)
                {
                    minY = y;
                }
                if (z > maxZ)
                {
                    maxZ = z;
                }
                if (z < minZ)
                {
                    minZ = z;
                }
            }

            MagValue.X = -(x - CalXoffSet) * CalXscale;
            MagValue.Z = (z - CalZoffSet) * CalZscale;  // Y <--> Z Exchanged
            MagValue.Y = -(y - CalYoffSet) * CalYscale; // Z <--> Y Exchanged

            return(result);
        }