I just got this 3 axis gyro
http://www.sparkfun.com/commerce/product_info.php?products_id=9801and a level converter board from sparkfun.
Since the gyro is a lower voltage device than the Nano I used the TX lines of the level converter and a 3.3v reg to form a lower voltage I2C bus.
Here is the code to read and display the gyro registers
Code:
control con %11010010
register var byte
result1 var byte
result2 var byte
result3 var byte
result4 var byte
result5 var byte
temp var word
gyrox var word
gyroy var word
gyroz var word
pause 500
'-------------------------------------------Configure registers----------------------------
register = 22
i2cout p2, p1, control, [register,24]
register = 62
i2cout p2, p1, control, [register,1]
'-----------------------------------Read and Display Registers----------------------------------
serout S_out, i9600, ["Register 21 22 23 26 62", 13]
register = 21
i2cout p2, p1, control, [register]
i2cin p2, p1, ,control, [result1,result2,result3]
register = 26
i2cout p2, p1, control, [register]
i2cin p2, p1, ,control, [result4]
register = 62
i2cout p2, p1, control, [register]
i2cin p2, p1, ,control, [result5]
serout S_out, i9600, [ " ", bin result1\8, " ", bin result2\8, " ", bin result3\8, " ", bin result4\8, " ", bin result5\8, 13, 13]
'---------------------------------Read and Display Sensor Registers------------------------
register = 27
serout S_out, i9600, ["Sensors temp gyro x gyro y gyro z ", 13]
i2cout p2, p1, control, [register]
i2cin p2, p1, ,control, [temp.highbyte, temp.lowbyte, gyrox.highbyte, gyrox.lowbyte, gyroy.highbyte, gyroy.lowbyte, gyroz.highbyte, gyroz.lowbyte]
serout S_out, i9600, ["Binary ", bin temp\16, " ", bin gyrox\16, " ", bin gyroy\16, " ", bin gyroz\16,13]
The output looks like this:
Code:
Register 21 22 23 26 62
00000000 00011111 00000000 00000001 00000010
Sensors temp gyro x gyro y gyro z
Binary 1100010010100000 1111111111110111 0000000000101000 0000000001000010
Now the output of the sensor registers is in 16 bit 2s complement
So I added this code
Code:
if temp.highbit = 1 then
temp = ~temp + 1
endif
if gyrox.highbit = 1 then
gyrox = ~gyrox + 1
endif
if gyroy.highbit = 1 then
gyroy = ~gyroy + 1
endif
if gyroz.highbit = 1 then
gyroz = ~gyroz + 1
endif
serout S_out, i9600, ["Invert ", bin temp\16, " ", bin gyrox\16, " ", bin gyroy\16, " ", bin gyroz\16,13]
That gives this output
Code:
Register 21 22 23 26 62
00000000 00000000 00000000 00000001 00000000
Sensors temp gyro x gyro y gyro z
Binary 1100010001100000 1111111110001011 0000000001100111 0000000101010100
Invert 0011101110100000 0000000001110101 0000000001100111 0000000101010100
To convert gyro to deg/sec I need to divide by 14.375
Code:
gyrox = gyrox/14
gyroy = gyroy/14
gyroz = gyroz/14
serout S_out, i9600, [" "," Gyro X Gyro Y Gyro Z", 13]
serout S_out, i9600, ["Deg/Sec ", dec gyrox, " ", dec gyroy, " ", dec gyroz,13]
Code:
Gyro X Gyro Y Gyro Z
Deg/Sec 1 0 3
This gives Deg/sec but does not specify a direction I need the sign to determin direction
To convert temp I use this(not sure if this is right)
Code:
tempa = (temp - 13200)/ 280
if temp > 13200 then
temp = 35 - tempa
else
temp = 35 + tempa
endif
serout S_out, i9600, [" "," Temp Gyro X Gyro Y Gyro Z", 13]
serout S_out, i9600, ["Deg/Sec ", dec temp, " ", dec gyrox, " ", dec gyroy, " ", dec gyroz,13]
Which yeilds
Code:
Temp Gyro X Gyro Y Gyro Z
Deg/Sec 28 0 0 3
Now a few questins:
What would be the best way to determine direction(negitive or positive) number? Is there a way to convert to sdec format and then set the sign bit? I have seen the bit format for this befor but can't find it again.
The atrest output of the gyro does not sit at zero instead it "drifts" around zero. What is the best way to deal with this? Can I take an average atrest reading and subtract this out?