Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   C++ I2C Accelerometer - Can't send power (http://www.chiefdelphi.com/forums/showthread.php?t=83931)

nedrobotics 06-03-2010 21:46

C++ I2C Accelerometer - Can't send power
 
We have recently installed the 2010 Accelerometer (ADXL345) on our robot and have the new ADXL345 class code in Windriver. But whenever we try to read values from it, they all read 0. We believe this is because according to the FIRST Sensor Manual, “The ADXL345 starts in a power saving mode. You must turn it on by writing 0x08 to POWER_CTL (0X2D) before it will do anything interesting.”

We are not sure how to write any values to the I2C class to turn on the accelerometer. We tried accessing the write function from the I2C class through the ADXL345_I2C class and using it within the main robot class, but then the code would not compile. What syntax would we use to write the value "0x08" to the POWER_CTL and turn on the accelerometer?

mikets 07-03-2010 04:46

Re: C++ I2C Accelerometer - Can't send power
 
If you are using the ADXL345_I2C class, you should not have to deal with I2C. The class will take care of it for you. It will do the intialization for you provided that you give it the correct slot of your digital I/O module and correctly wire the accelerometer to the I2C port on the digital side car. Assuming your digital I/O module is in the default slot 4, your code should look something like this:
Code:

private:
    ADXL345_I2C *m_accel;
    ....

    ...
    // In your robot constructor.
    m_accel = new ADXL345_I2C(SensorBase::GetDefaultDigitalModule());
    ...

    // To read the accelerometer, you either read one of the axes or all axes at the same time.
    AllAxes accelData = m_accel->GetAccelerations();
    // or
    double xAxis = m_accel->GetAcceleration(kAxis_X);

Here is the source code for the ADXL345_I2C constructor. You can see that it is already doing the correct initialization for you.
Code:

ADXL345_I2C::ADXL345_I2C(UINT32 slot, ADXL345_I2C::DataFormat_Range range)
        : m_i2c (NULL)
{
        DigitalModule *module = DigitalModule::GetInstance(slot);
        m_i2c = module->GetI2C(kAddress);

        // Turn on the measurements
        // This is the line writing the value 0x8 to the PowerCtlRegister.
        m_i2c->Write(kPowerCtlRegister, kPowerCtl_Measure);
        // Specify the data format to read
        m_i2c->Write(kDataFormatRegister, kDataFormat_FullRes | (UINT8)range);
}



All times are GMT -5. The time now is 13:04.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi