|
squiggy
Rookie
Joined: Fri Apr 20, 2007 3:22 am Posts: 36
|
 Hitechnic accelerometer help needed.
Hello,
I’m new to the forums, RobotC, and I2C messaging.
I’m trying to use the Hitechnic Accelerometer Sensor and I’m not really sure how to get the data from the sensor.
I tried writing a simple program to learn how to do it, but I’m not really having much luck.
Here’s the program.
task main()
{
int xUpper,xLower;
int yUpper,yLower;
int zUpper,zLower;
int xVal,yVal,zVal;
typedef struct{
byte nMsgSize;
byte nDeviceAddress;
byte nLocationPtr;
byte nData;
} TI2C_Output;
SensorType[S1] = sensorI2CCustom;
wait10Msec(5);
nI2CBytesReady[S1]=0;
while(nI2CStatus[S1]== STAT_COMM_PENDING)
wait1Msec(2);
byte replyMessage[6];
TI2C_Output sOutput;
sOutput.nMsgSize = 6;
sOutput.nDeviceAddress = 0x02;
sOutput.nLocationPtr = 0x42;
while(true)
{
nI2CBytesReady[S1]=0;
sendI2CMsg(S1,sOutput.nMsgSize ,6);
while (nI2CStatus[S1]==STAT_COMM_PENDING)
wait1Msec(2);
if (nI2CStatus[S1]==NO_ERR) {
readI2CReply(S1, replyMessage[0], 6);
xUpper = (int)replyMessage[0];
yUpper = (int)replyMessage[1];
zUpper = (int)replyMessage[2];
xLower = (int)replyMessage[3];
yLower = (int)replyMessage[4];
zLower = (int)replyMessage[5];
xVal = (xUpper<<4)+xLower;
yVal = (yUpper<<4)+yLower;
zVal = (zUpper<<4)+zLower;
if(xVal > 511)
xVal -= 1024;
if(yVal > 511)
yVal -= 1024;
if(zVal > 511)
zVal -= 1024;
nxtDisplayTextLine(4, "x-val %d", xVal);
nxtDisplayTextLine(5, "y-val %d", yVal);
nxtDisplayTextLine(6, "z-val %d", zVal);
} else
{
nxtDisplayTextLine(3, "i2c err %d", nI2CStatus[S1]);
}
}
}
I put this together by looking at the robotC compass driver and a RobotC accelerometer program written for another sensor.
I’m not sure if the sensor needs to be initialized or not, there’s no information on Hitechnic’s site or elsewhere that I’ve been able to find.
I’ve tried initializing with:
sOutput.nMsgSize = 6;
sOutput.nDeviceAddress = 0x02;
sOutput.nLocationPtr = 0x42;
sOutput.nData = 0x00;
SensorType[S1] = sensorI2CCustomStd9V;
wait10Msec(5);
nI2CBytesReady[S1]=0;
sendI2CMsg(S1, sOutput.nMsgSize, 0);
while(nI2CStatus[S1]== STAT_COMM_PENDING)
wait1Msec(2);
I’m also not sure that I have the right device address.
I’m sure there are other problems here as well.
Any help I can get would be appreciated.
-Scott
|