Пример #1
0
 /// <summary>
 /// Get the acceleration of one axis in Gs.
 /// </summary>
 /// <param name="axis">The axis to read from.</param>
 /// <returns>Acceleration of the ADXL345 in Gs.</returns>
 public override double GetAcceleration(Axes axis)
 {
     byte[] transferBuffer = new byte[3];
     transferBuffer[0] = (byte)((AddressRead | AddressMultiByte | DataRegister) + (byte)axis);
     m_spi.Transaction(transferBuffer, transferBuffer, 3);
     return(BitConverter.ToInt16(transferBuffer, 1) * GsPerLSB);
 }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
0
        /// <summary>
        /// Get the acceleration of one axis in Gs.
        /// </summary>
        /// <param name="axis">The axis to read from</param>
        /// <returns>Acceleration of the ADXL362 in Gs.</returns>
        public double GetAcceleration(Axes axis)
        {
            if (m_gsPerLSB == 0.0)
            {
                return(0.0);
            }

            byte[] buffer  = new byte[4];
            byte[] command = new byte[] { 0, 0, 0, 0 };
            command[0] = RegRead;
            command[1] = (byte)(DataRegister + (byte)axis);
            m_spi.Transaction(command, buffer, 4);

            short rawAccel = BitConverter.ToInt16(buffer, 2);

            return(rawAccel * m_gsPerLSB);
        }