BasicMicro - Forums

www.basicmicro.com
It is currently Sat Feb 04, 2012 6:58 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: experimenting with 3axis gyro
PostPosted: Sun Aug 15, 2010 4:10 pm 
Offline
Master

Joined: Sun Oct 05, 2008 9:40 am
Posts: 108
I just got this 3 axis gyro http://www.sparkfun.com/commerce/product_info.php?products_id=9801
and 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?


Top
 Profile  
 
 Post subject: Re: experimenting with 3axis gyro
PostPosted: Sun Aug 15, 2010 7:31 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
Well, first, I'd just make yoiur temp and gyro axis variables SWORD instead of WORD variables. Then they will correctly keep their sign without any work from you.

Then you can just use SDEC modifiers to display their output. Also instead of truncationg the 14.375 to 14 I'd either use floating point math to display them:

serout s_out,i9600,[real TOFLOAT(gyrox)/14.375]

of use fixed point math, which would be a much bigger example to write so I won't do it unless your interested.

Also, SDEC has no "format". SDEC assumes bit31 is the sign bit. Even if you give an SDEC modifier an unsigned variable it will still look at bit31 as a sign bit. It's just outputing human readable conversion of the numbers it's given.

I've played with a couple gyros myself and they all drift a bit no matter how good they are. You have to add other sensors(eg accelerometers) to correct for the drift or just live with it.

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: experimenting with 3axis gyro
PostPosted: Mon Nov 01, 2010 10:23 am 
Offline
Master

Joined: Sun Oct 05, 2008 9:40 am
Posts: 108
http://www.youtube.com/watch?v=4M1GGscp_2Q

This is run on a pro40. The accelrometers I am using are the ADXL78. Not the best choice but what I had.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO