AForge.Robotics.Lego.NXTBrick.ReadHiTechnicAccelerationTiltSensor C# (CSharp) Method

ReadHiTechnicAccelerationTiltSensor() public method

Read data from HiTechnic acceleration/tilt sensor. The HiTechnic accelerometer/tilt sensor measures acceleration in three axes. It measures also tilt along each axis. Using the sensor, you can measure the acceleration of your robot in the range of -2g to 2g.

The method retrieves the acceleration in three directions of a HiTechnic acceleration/tilt sensor by communicating with I2C device (writing to and reading from low speed bus). The method first sends { 0x02, 0x42 } command to the specified device using LsWrite( Sensor, byte[], int ) method. Then it waits until there is something available to read using LsGetStatus method. Finally it reads sensor's value using LsRead device. See this page for details.

Before using this method it is required to use SetSensorMode( Sensor, SensorType, SensorMode, bool ) method to set sensor's type to SensorType.Lowspeed mode. It should be done once after NXT brick is powered onq If sensor's type is not set properly, the method will generate an exception. Also after setting sensor's type application may need to wait a bit to give device some time to initialize.

The acceleration sensor can also be used to measure tilt in three axes This is possible because gravity is perceived as acceleration. When the sensor is stationary and in the normal horizontal position, the x and y axis will be near zero, because they are horizontal, while the z axis will be near 200, which represents g. If you tilt the sensor then gravity will also be detected on the other axis and the value for the z axis will go down. Since gravity is distributed among the three component vectors, the tilt of the sensor can be determined.

NXT Firmware version 1.05 or later must be loaded in the NXT for the acceleration/tilt sensor and other digital I2C sensors to operate correctly. You can check the firmware version using the GetVersion method.

public ReadHiTechnicAccelerationTiltSensor ( NXTBrick sensor, int &xAceeleration, int &yAceeleration, int &zAceeleration ) : bool
sensor NXTBrick Sensor to read from.
xAceeleration int Acceleration in X direction, with a scaling of approximately 200 counts per g.
yAceeleration int Acceleration in Y direction, with a scaling of approximately 200 counts per g.
zAceeleration int Acceleration in Z direction, with a scaling of approximately 200 counts per g.
return bool
        public bool ReadHiTechnicAccelerationTiltSensor( NXTBrick.Sensor sensor, ref int xAceeleration, ref int yAceeleration, ref int zAceeleration )
        {
            byte[] command = { 0x02, 0x42 };
            byte[] readBuffer = new byte[6];

            int intReady;
            int bytesRead;

            LsWrite( sensor, command, readBuffer.Length );
            LsGetStatus( sensor, out intReady );
            LsRead( sensor, readBuffer, out bytesRead );

            if ( bytesRead == readBuffer.Length )
            {
                xAceeleration = readBuffer[0] > 127 ? ( readBuffer[0] - 256 ) * 4 + readBuffer[3] : readBuffer[0] * 4 + readBuffer[3];
                yAceeleration = readBuffer[1] > 127 ? ( readBuffer[1] - 256 ) * 4 + readBuffer[4] : readBuffer[1] * 4 + readBuffer[4];
                zAceeleration = readBuffer[2] > 127 ? ( readBuffer[2] - 256 ) * 4 + readBuffer[5] : readBuffer[2] * 4 + readBuffer[5];

                return true;
            }

            return false;
        }
    }