ADXL345_I2C Help

Hello everyone! I have been trying to get the ADXL345 accelerometer working. I have it connected to the I2C spare outputs. Here is my code:

#include "WPILib.h"

class MyRobot : public IterativeRobot {
	ADXL345_I2C myAccel;
	DriverStationLCD *driverStation;
public:
	MyRobot():
		myAccel(4, ADXL345_I2C::kRange_2G)
	{
		driverStation = DriverStationLCD::GetInstance();
	}
	void TeleopInit() {
		GetWatchdog().SetEnabled(true);
		driverStation->Clear();		
	}
	void TeleopPeriodic() {
		GetWatchdog().Feed();
		
		char xAccelChar[100];
		char yAccelChar[100];

		sprintf(xAccelChar, "Accel X: %f", myAccel.GetAcceleration(ADXL345_I2C::kAxis_X));
		sprintf(yAccelChar, "Accel Y: %f", myAccel.GetAcceleration(ADXL345_I2C::kAxis_Y));
		
		driverStation->Clear();
		driverStation->PrintfLine(DriverStationLCD::kUser_Line1, xAccelChar);
		driverStation->PrintfLine(DriverStationLCD::kUser_Line2, yAccelChar);
		driverStation->UpdateLCD();
		
	}
};
START_ROBOT_CLASS(MyRobot);

When I run this program, the display on the driver station reads:
Accel X: 0.00000
Accel Y: 0.00000

No matter how much the accelerometer is moved around, these values do not change. Does anyone have any ideas as to what I can do to make the accelerometer work?

Are you getting any errors on the diagnostics tab of the driver station or on the Console?

Are you sure of your wiring? Make sure clk and data are not swapped.

Nothing; although the I2C class has many assertions, none seem to be failing (or at least communicating a failure). The ADXL345_I2C class has no assertions besides the ones from I2C calls.

Quite positive, the board and the I2C spare outputs on the digital sidecar are labelled. An accelerometer example VI also depicts a wiring diagram.

I would start by putting a scope on the I2C pins to see if they’re being diddled.

While they are labeled, I want to verify that you are connected to the 4 pins closest to the NXT RJ connector. The 4 pins farther from it are low-speed digital output lines (that are not supported by WPILib).

-Joe

They are.

They are.[/quote]

I believe Michael is correct, we have also tried different digital sidecars, including the one from our 2011 kit. Tomorrow we will post our full source code (updated), software versions, and a picture of our wiring setup.

I’m not quite sure what to tell you. I tried both the program above and a program I had laying around, and both worked just fine. It makes me tend to think that you either really do have a wiring/hardware issue or perhaps you have some software that is not updated.

Here’s the other application I tried which also works on my system:


#include "WPILib.h"

class TestADXL345_I2C : public SimpleRobot
{
	ADXL345_I2C acc;

public:
	TestADXL345_I2C(void)
		: acc (4, ADXL345_I2C::kRange_2G)
	{
	}

	void RobotMain(void)
	{
		while (true)
		{
			printf("x=%f  y=%f  z=%f
",
				acc.GetAcceleration(ADXL345_I2C::kAxis_X),
				acc.GetAcceleration(ADXL345_I2C::kAxis_Y),
				acc.GetAcceleration(ADXL345_I2C::kAxis_Z));
			Wait(1.0);
		}
	}
};

START_ROBOT_CLASS(TestADXL345_I2C);

http://www.chiefdelphi.com/forums/attachment.php?attachmentid=9717&stc=1&d=1295297716

Apologies for the poor quality of the image.

WindRiver Version: 3.0.1
WPILib Version: rev2242

2011-01-17 15.47.58.jpg


2011-01-17 15.47.58.jpg

I am definitely a beginner programmer but I thought that a file I read said that the accelerometer would start in power saving mode unless you wrote 0x08 to POWER_CTL (0x2D) and it would be unresponsive unless this was implemented
here’s the file link:

http://usfirst.org/uploadedFiles/Robotics_Programs/FRC/Game_and_Season__Info/2011_Assets/Kit_of_Parts/2011_Sensor_Manual.pdf

sorry if im just imputting random info again I am really new to the coding world

Great attention to detail, however the WPI Library already does this.

Have you tried looking at the lines with a logic analyzer or a scope yet?

Not yet, I haven’t had a chance to bring in my USB logic analyzer yet.

We had last years ADXL345, so we wired that up and it works fine. However, we encounter another problem when having the HiTechnic Compass plugged into the NXT I2C port at the same time the accelerometer is plugged into the spare I2C port. They both work and display values when they are plugged in alone, however when they are both plugged in together, neither will update values and display 0.000000. We do not get any errors, they simply do not work. We also checked the compass and accelerometer source code to make sure that the compass and accelerometer had different addresses which they do (Accelerometer is 0x3A, and compass is 0x02). Here is the code with the Compass added.


#include "WPILib.h"

class MyRobot : public IterativeRobot {
	
	HiTechnicCompass myCompass;
	ADXL345_I2C myAccel;
	DriverStationLCD *driverStation;

	double xAccel, yAccel;
        double compassAngle;

public:
	MyRobot():
		myCompass(4),
		myAccel(4, ADXL345_I2C::kRange_2G)
	{
		driverStation = DriverStationLCD::GetInstance();

	}

	void TeleopInit() {
		GetWatchdog().SetEnabled(true);
		GetWatchdog().SetExpiration(0.5);
		driverStation->Clear();	
	}

	void TeleopPeriodic() {
		
		while(IsOperatorControl() && IsEnabled()) {
			GetWatchdog().Feed();
			
			char xAccelChar[100];
			char yAccelChar[100];
			char compassChar[100];
			char testChar[100];
			
			xAccel =  myAccel.GetAcceleration(ADXL345_I2C::kAxis_X);
			yAccel =  myAccel.GetAcceleration(ADXL345_I2C::kAxis_Y);
			
			compassAngle = myCompass.GetAngle();
			
			sprintf(xAccelChar, "Accel X: %f", xAccel);
			sprintf(yAccelChar, "Accel Y: %f", yAccel);
			sprintf(compassChar, "Compass: %f", compassAngle);
			
			driverStation->Clear();
			driverStation->PrintfLine(DriverStationLCD::kUser_Line1, xAccelChar);
			driverStation->PrintfLine(DriverStationLCD::kUser_Line2, yAccelChar);
			driverStation->PrintfLine(DriverStationLCD::kUser_Line3, compassChar);
			driverStation->UpdateLCD();
		}
	}
};
START_ROBOT_CLASS(MyRobot);

We would appreciate any advice that could fix this issue.

Did you ever resolve this issue?

TIA,

Mike

bump. We’re having the same issue. Here’s ou code:

#include <WPILib.h>
class DefaultRobot : public SimpleRobot
{	
	ADXL345_I2C *acc;                       
public:
	DefaultRobot(void)
	{	
		acc = new ADXL345_I2C (4, ADXL345_I2C::kRange_2G);
	}
	void Autonomous(void)
	{
			while(IsAutonomous())
			{
			}				
	}
	void OperatorControl(void)
	{
		DriverStationLCD *dsLCD = DriverStationLCD::GetInstance();
		while (IsOperatorControl())
		{	
			while (true)
			{
				float x = acc->GetAcceleration(ADXL345_I2C::kAxis_X);
				float y = acc->GetAcceleration(ADXL345_I2C::kAxis_Y);
				float z = acc->GetAcceleration(ADXL345_I2C::kAxis_Z);
				dsLCD->PrintfLine((DriverStationLCD::Line) 1, "x=%f  y=%f  z=%f", x, y, z);
				dsLCD->UpdateLCD();
				Wait(1.0);
			}
		}
	}
};

START_ROBOT_CLASS(DefaultRobot);


We’re not experts. This is our first year programming in C++, and were a little short on time, so there wasn’t much time for testing… If anyone happens to know how to fix it (without any extra effort), we’d love to hear about it. Thanks!

We’ve been using the ADX345_SPI class, and we tried to use ADXL345_I2C without any luck.

however, I was reading though the part datasheet tonight, and I think the issue is that you have to hold the Chip Select line high to select I2C mode.

See page 18 of the part datasheet:

We’ll probably stick to the SPI mode because we’ve got it working. Though we’re considering modifying the class slightly from what WPI provides. (We’d like to add a calibration function, as described in the User’s Guide).

If you get it to work in I2C mode, I’d be interested in hearing about it.