# how can you use a gyro in autonomous mode

Hi! i just have a question since i am very new programmer. Okay i know how to create an autonomous mode but how can you use gyro’s in autonomous mode. please show me a code so i can have a look at it and learn. thanks by the way my e-mail address is jigarjuhi@yahoo.ca. thanks guys for your time and co-operation.

I shouldn’t even be writing this, but i felt like i needed to. The second topic in this forum asks the exact same thing (found here:http://www.chiefdelphi.com/forums/showthread.php?t=23268) and i gave them the following response:

I cannot say it enough, search, search, search!

As i just realized though, that doesn’t give autonomous code. All a gyro can really do is tell you which way you’re pointing, nothing more. So there really is no way to give sample code except for the following which just integrates the gyro output to give you one number which shows how far you’ve turned.

``````
int theta=0;  //in global variables

// from here on in autonomous section or what have you

signed char gyro = (GYRO-127)*G_ADJUST; //GYRO being the macro for wherever your gyro is attached

if (theta+gyro>=360)  //rolling from 359 to 0 and around again
theta+=gyro-360;
else if (theta+gryo<0)  //rolling from 0 to 359 and around again
theta+=gyro+360;
else
theta+=gyro;

``````

I cannot say enough that posting “I need code to use the gyro” or “I need autonomous code” is akin to saying “I need a design for an arm for my robot” in the mechanical forum. In order to get a useful answer you need to make your question more specific. What should the code do? Should the code simply read the value of the gyro and discard it while driving forward for 5 seconds? Should the code make sure the robot drives straight? Should the code drive straight ahead, turn 90 degrees, then drive straight ahead again? In engineering requirements are key. We cannot help you if we don’t know what you need help with.

Mike

I know what the best bennifit of a gyro is (in my mind)… I consider it a backup device… using it with your counter you can make a very sound auton. program. I am searching through my programs last year to find an example of what I am talking about but my programs are on my old computer so when I backed them up on CD’s they got thrown all over the place.

Basically it would work like this…

you could use a go-sub like this

``````

Gyrocounter:

if sensor1 > 130 then Gyro1:             'Gyro Sensor Clockwise

if sensor1 < 128 then Gyro2:             'Gyro Sensor Counterclockwise

Goto Gyro3:

Gyro1:

cont = cont + sensor1 - 129

Goto Gyro3:

Gyro2:

cont = cont + sensor1 - 129

Gyro3:

Pass:

Return

``````

Then incorperate your code like this…

``````         Right_Gyro:

gosub gyrocounter

if cont < 32000 then

relay1_fwd = 1 		'Two Wheel Drive On

relay1_rev = 0 		'Four Wheel Drive Off

endif

On indc goto RGyro, RGyro1, RGyro2, RGyro3, RGyro4

RGyro:			'Half Circle

p1_y = 175		'Right Front Wheel Forward

p2_x = 175		'Right Rear Wheel Forward

p2_y = 250		'Left Front Wheel Forward

p3_x = 250		'Left Rear Wheel Forward

If cont > 37800 then RGyro1

Goto Main:

RGyro1:			'Half Circle

indc = 1

relay1_fwd = 0 		'Two Wheel Drive Off

relay1_rev = 1 		'Four Wheel Drive On

p1_y = 175		'Right Front Wheel Forward

p2_x = 175		'Right Rear Wheel Forward

p2_y = 250		'Left Front Wheel Forward

p3_x = 250		'Left Rear Wheel Forward

If cont > 38200 then cycle = 0

If cont > 38200 then goto RGyro2

Goto Main:

RGyro2:			'Straight Forward

indc = 2

p1_y = 253		'Right Front Wheel Forward

p2_x = 253		'Right Rear Wheel Forward

p2_y = 253		'Left Front Wheel Forward

p3_x = 253		'Left Rear Wheel Forward

If cycle >= 130 then RGyro3

Goto Main:

RGyro3:

indc = 3

p1_y = 127		'Right Front Wheel Stop

p2_x = 127		'Right Rear Wheel Stop

p2_y = 127		'Left Front Wheel Stop

p3_x = 127		'Left Rear Wheel Stop

If cycle >= 140 then RGyro4

``````

You can get the basic idea on how to program a gyro (id the variabales)

REMEMBER
This is pbasic coding you will need to convert it into C for this years controller… on Innovation they have the “pamphlet” they created to show you how to do many of these functions… I will work on making some C programming for a gyro but I have many other things to work on for my team so I do not know how productive I will be…

If you need help with the coding give me something to work with… a general idea is hard to show you what you want but if you give me a very spacific idea I would be more than happy to help you out ne way I can.

If you need the help contact me…
AOL IM- GregTheGreat04 (no spaces)
E-mail- g.ullstam@comcast.net

Good Luck,

-Greg The Great