#include <stdio>
#include <conio>
#if defined(WIN32)
#include <roboard_dll> // use the DLL version of RoBoIO lib
// #include <roboard> // use the static version of RoBoIO lib
#else
#include <roboard>
#endif
const unsigned char acceler_addr = 0x30>>1;
int main()
{
unsigned int acceler_X, acceler_Y, acceler_Z, d;
roboio_SetRBVer(RB_110);
if (i2c_Initialize(I2CIRQ_DISABLE) == false)
{
printf("FALSE!! %s\n", roboio_GetErrMsg());
return -1;
}
i2c0_SetSpeed(I2CMODE_AUTO, 400000L); // set i2c speed:400k
// set acceler
if(i2c0master_StartN(acceler_addr, I2C_WRITE,2) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
i2c0master_WriteN(0x20);
i2c0master_WriteN(0x37);
do
{
//////////////////////////////////////////////////////////////////////// read acceler_X
if(i2c0master_StartN(acceler_addr, I2C_WRITE,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_WriteN(0x29) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_StartN(acceler_addr, I2C_READ,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if((acceler_X = i2c0master_ReadN()) == 0xffff)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
acceler_X = acceler_X << 8;
if(i2c0master_StartN(acceler_addr, I2C_WRITE,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_WriteN(0x28) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_StartN(acceler_addr, I2C_READ,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if((d = i2c0master_ReadN()) == 0xffff)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
acceler_X = acceler_X + d;
//////////////////////////////////////////////////////////////////////// read acceler_Y
if(i2c0master_StartN(acceler_addr, I2C_WRITE,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_WriteN(0x2b) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_StartN(acceler_addr, I2C_READ,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if((acceler_Y = i2c0master_ReadN()) == 0xffff)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
acceler_Y = acceler_Y << 8;
if(i2c0master_StartN(acceler_addr, I2C_WRITE,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_WriteN(0x2a) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_StartN(acceler_addr, I2C_READ,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if((d = i2c0master_ReadN()) == 0xffff)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
acceler_Y = acceler_Y + d;
//////////////////////////////////////////////////////////////////////// read acceler_Z
if(i2c0master_StartN(acceler_addr, I2C_WRITE,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_WriteN(0x2d) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_StartN(acceler_addr, I2C_READ,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if((acceler_Z = i2c0master_ReadN()) == 0xffff)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
acceler_Z = acceler_Z << 8;
if(i2c0master_StartN(acceler_addr, I2C_WRITE,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_WriteN(0x2c) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if(i2c0master_StartN(acceler_addr, I2C_READ,1) == false)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
if((d = i2c0master_ReadN()) == 0xffff)
{
printf("g-sensor error:%s !!\n",roboio_GetErrMsg());
goto END;
}
acceler_Z = acceler_Z + d;
printf("acceler:X-axis:%d Y-azis:%d Z-axis:%d\n\n",acceler_X, acceler_Y, acceler_Z);
delay_ms(10);
}while(_getch() != 27);
END:
i2c_Close();
return 0;
}
RN1AsOf091407 wrote:I saw your other post. I'll try to re-explain:
At rest, when sitting still, you will get some value from X, Y, and Z. To generate + and - values relative to rest position, do this:
RelativeValueX = CurrentX - AtRestX
RelativeValueX will be + when axis is greater than AtRestX, and - when axis is less than AtRestX.
Do the same for Y and Z.
All accelerometers exhibit some value when at rest, as gravity is always affecting them. An axis perfectly perpendicular to the force of gravity and not moving should be zero, but it is unlikely you'll actually see zero at this position due to fabrication, tolerances, etc. It is sufficient in practice to zero-reference by using "at rest" values.
This is similar to how RC gyros work in helicopters and such, and is why you're supposed to leave the gyro alone for a few seconds when powering on - because it is recording its "at rest" position to use as a reference.