Beispiel #1
0
        /// <summary>
        /// Creates a new <see cref="ADXL362"/> with the specified <see cref="SPI.Port">Port</see>
        /// and <see cref="AccelerometerRange">Range</see>.
        /// </summary>
        /// <param name="port">The SPI Port the accelerometer is connected to.</param>
        /// <param name="range">The range that the accelerometer will measure.</param>
        public ADXL362(SPI.Port port, AccelerometerRange range)
        {
            m_spi = new SPI(port);
            m_spi.SetClockRate(3000000);
            m_spi.SetMSBFirst();
            m_spi.SetSampleDataOnRising();
            m_spi.SetClockActiveHigh();
            m_spi.SetChipSelectActiveLow();

            byte[] commands = new byte[]
            {
                RegRead,
                PartIdRegister,
                0,
            };

            m_spi.Transaction(commands, commands, 3);

            if (commands[2] != 0xF2)
            {
                DriverStation.ReportError("Could not find ADXL362", false);
                m_gsPerLSB = 0.0;
                return;
            }

            AccelerometerRange = range;

            commands[0] = RegWrite;
            commands[1] = PowerCtlRegister;
            commands[2] = PowerCtlRegister | PowerCtl_UltraLowNoise;
            m_spi.Write(commands, 3);

            Report(ResourceType.kResourceType_ADXL362, (byte)port);
            LiveWindow.LiveWindow.AddSensor("ADXL362", port.ToString(), this);
        }
Beispiel #2
0
        private ushort ReadRegister(byte reg)
        {
            uint cmd = 0x80000000 | (((uint)reg) << 17);

            if (!CalcParity(cmd))
            {
                cmd |= 1u;
            }

            // big endian   102
            byte[] buf = new byte[]
            {
                (byte)((cmd >> 24) & 0xff),
                (byte)((cmd >> 16) & 0xff),
                (byte)((cmd >> 8) & 0xff),
                (byte)(cmd & 0xff)
            };

            m_spi.Write(buf, 4);
            m_spi.Read(false, buf, 4);

            if ((buf[0] & 0xe0) == 0)
            {
                return(0);                       // error, return 0
            }
            return((ushort)((BytesToIntBE(buf) >> 5) & 0xffff));
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="port">The SPI port the accelerometer is attached to</param>
 /// <param name="range">The range (+ or -) that the accelerometer will measure.</param>
 public ADXL345_SPI(SPI.Port port, AccelerometerRange range)
 {
     m_spi = new SPI(port);
     m_spi.SetClockRate(500000);
     m_spi.SetSampleDataOnFalling();
     m_spi.SetClockActiveLow();
     m_spi.SetChipSelectActiveHigh();
     byte[] commands = new byte[2];
     commands[0] = PowerCtlRegister;
     commands[1] = (byte)PowerCtl.Measure;
     m_spi.Write(commands, 2);
     AccelerometerRange = range;
     HAL.Report(ResourceType.kResourceType_ADXL345, Instances.kADXL345_SPI);
     LiveWindow.AddSensor("ADXL345_SPI", (byte)port, this);
 }
Beispiel #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="port">The SPI port the accelerometer is attached to</param>
 /// <param name="range">The range (+ or -) that the accelerometer will measure.</param>
 public ADXL345_SPI(SPI.Port port, AccelerometerRange range)
 {
     m_spi = new SPI(port);
     m_spi.SetClockRate(500000);
     m_spi.SetSampleDataOnFalling();
     m_spi.SetClockActiveLow();
     m_spi.SetChipSelectActiveHigh();
     byte[] commands = new byte[2];
     commands[0] = PowerCtlRegister;
     commands[1] = (byte)PowerCtl.Measure;
     m_spi.Write(commands, 2);
     AccelerometerRange = range;
     HAL.Base.HAL.Report(ResourceType.kResourceType_ADXL345, Instances.kADXL345_SPI);
     LiveWindow.LiveWindow.AddSensor("ADXL345_SPI", (byte)port, this);
 }
Beispiel #5
0
        /// <summary>
        /// Creates a new <see cref="ADXL362"/> with the specified <see cref="SPI.Port">Port</see> 
        /// and <see cref="AccelerometerRange">Range</see>.
        /// </summary>
        /// <param name="port">The SPI Port the accelerometer is connected to.</param>
        /// <param name="range">The range that the accelerometer will measure.</param>
        public ADXL362(SPI.Port port, AccelerometerRange range)
        {
            m_spi = new SPI(port);
            m_spi.SetClockRate(3000000);
            m_spi.SetMSBFirst();
            m_spi.SetSampleDataOnRising();
            m_spi.SetClockActiveHigh();
            m_spi.SetChipSelectActiveLow();

            byte[] commands = new byte[]
            {
                RegRead,
                PartIdRegister,
                0,
            };

            m_spi.Transaction(commands, commands, 3);

            if (commands[2] != 0xF2)
            {
                DriverStation.ReportError("Could not find ADXL362", false);
                m_gsPerLSB = 0.0;
                return;
            }

            AccelerometerRange = range;

            commands[0] = RegWrite;
            commands[1] = PowerCtlRegister;
            commands[2] = PowerCtlRegister | PowerCtl_UltraLowNoise;
            m_spi.Write(commands, 3);

            Report(ResourceType.kResourceType_ADXL362, (byte)port);
            LiveWindow.LiveWindow.AddSensor("ADXL362", port.ToString(), this);
        }
Beispiel #6
0
 /// <summary>
 /// Writes the range to the specified interface.
 /// </summary>
 /// <param name="value">The Range to write.</param>
 protected override void WriteRange(byte value)
 {
     byte[] commands = new byte[] { DataFormatRegister, (byte)((byte)DataFormat.FullRes | value) };
     m_spi.Write(commands, commands.Length);
 }