Converting G values into floats from accelerometer
i having issue trying change g values accelerator int float. when change variable type int float, it's serial output looks while sitting still:
while when it's plain int variable type, looks normal:
however, require float version of g values can accurate calculation of mph/s.
here full arduino code using adxl345 accelerometer:
code: [select]
-26844 -17153 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-26844 -17153 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
while when it's plain int variable type, looks normal:
code: [select]
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
however, require float version of g values can accurate calculation of mph/s.
here full arduino code using adxl345 accelerometer:
code: [select]
#include <wire.h>
#define device (0x1d) //adxl345 device address
#define to_read (6) //num of bytes going read each time (two bytes each axis)
byte buff[to_read] ; //6 bytes buffer saving data read device
char str[512]; //string buffer transform data before sending serial port
void setup()
{
wire.begin(); // join i2c bus (address optional master)
serial.begin(9600); // start serial output
//turning on adxl345
writeto(device, 0x2d, 0);
writeto(device, 0x2d, 16);
writeto(device, 0x2d, 8);
}
void loop()
{
int regaddress = 0x32; //first axis-acceleration-data register on adxl345
int x, y, z;
float xg, yg, zg;
float mphs;
readfrom(device, regaddress, to_read, buff); //read acceleration data adxl345
//each axis reading comes in 10 bit resolution, ie 2 bytes. least significat byte first!
//thus, converting both bytes in 1 int.
x = ((((int)buff[1]) << 8) | buff[0]);
y = ((((int)buff[3]) << 8) | buff[2]);
z = ((((int)buff[5]) << 8) | buff[4]);
// convert raw xyz values g values.
xg = x * 0.0078;
yg = y * 0.0078;
zg = z * 0.0078;
// convert forward axis (y) g value mph/s (miles per hour per second).
mphs = yg * 21.93685129;
//we send x y z values string serial port
sprintf(str, "%d %d %d", xg, yg, zg);
serial.print(str);
serial.write(10);
serial.print("mph/s: ");
serial.println(mphs);
//it appears delay needed in order not clog port
delay(15);
}
void writeto(int device, byte address, byte val) {
wire.begintransmission(device); //start transmission device
wire.write(address); // send register address
wire.write(val); // send value write
wire.endtransmission(); //end transmission
}
void readfrom(int device, byte address, int num, byte buff[]) {
wire.begintransmission(device); //start transmission device
wire.write(address); //sends address read from
wire.endtransmission(); //end transmission
wire.begintransmission(device); //start transmission device (initiate again)
wire.requestfrom(device, num); // request 6 bytes device
int = 0;
while(wire.available()) //device may send less requested (abnormal)
{
buff[i] = wire.read(); // receive byte
i++;
}
wire.endtransmission(); //end transmission
}
code: [select]
sprintf(str, "%d %d %d", xg, yg, zg);
xg, yg, , zg not ints. using integer format specifier floats not work. nor using %f specifier, since not supported on arduino.
on other hand, whole function call unnecessary. print variables 1 @ time.
the mphs value looks ok. float value truncated int matches. value not expect says more expectations.
Arduino Forum > Using Arduino > Programming Questions > Converting G values into floats from accelerometer
arduino
Comments
Post a Comment