Arduino, Gyroscope and Processing by otaviousp
Hi guys, this is my first attempt to post a project here.

This is an instructable on how to read a gyro sensor and plot the data using processing software at your desktop. I am using gyroscope model XV81-000 and an arduino. The device is a rough prototype of what will eventually become a self balance robot, this is the first part of the hole thing (read accelerometer and control a motor to self balance).

 
Remove these adsRemove these ads by Signing Up
1-40 of 45Next »
ontreus says: Feb 10, 2012. 9:07 PM
Blz Otávio
o Codigo do arduino_gyro.pde
é o codigo do processing e não do arduino
Vc poderia corigir.
Agradeço pelo tutorial.
otaviousp (author) says: Feb 11, 2012. 6:53 AM
Realmente esta errado, vou corrigir assim que possivel.
ontreus says: Aug 21, 2011. 8:39 PM
Olá Otávio, me chamo ontreus e também me encontro em São Paulo.
Na busca por informações sobre Giroscópio, cheguei no seu tutorial.
Bem, possuo os seguintes componentes:
- Giroscopio0 XV-3500CB PROTOTYPE PCB
- Arduino Duemilanove
- Servo Motor
Estou tentando fazer o mesmo desse link que segue:
http://www.youtube.com/watch?v=xkVRZC0e8Vk
Depois de testes verifiquei que este meu sensor possui um sinal muito baixo na porta analógica
Denominada de ANA0 e verifiquei que você usou dois capacitores para amplificar o sinal.
Meu sensor possui já uma saída de sinal digital compatível com l2c pois assim o sinal fica melhor
Pergunto que capacitores você usou em seu projeto e conectado aonde?
otaviousp (author) says: Aug 22, 2011. 10:29 AM
Para este seu sensor voce nao precisa dos capacitores.

O melhor seria usar o I2C para nao ter problemas de interferencia.
vivekchopra says: Jan 28, 2011. 10:37 PM
i need algorithm of above code for my LABVIEW programming... so plz post it here...
paolo.mosna says: Nov 19, 2010. 12:30 AM
Dear otaviousp, sorry if I bother you, but I'm trying to reproduce your realization using an Arduino Mega BT (Blue Tooth) 368 and what I get as output visualization is a very unstable angle indicator also with gyro device laying on the table without movements.

To verify A2D (arduino Analog to Digital) converter I connected analog input 0 to ground. With this configuration the A/D converter is giving me strange values ranging (randomly) from a minimum of 0 to a maximum of 64.
Is that possible? Do you know something about poor A/D conversion with arduino.
What kind of dynamic shows your realization?

Thanks for any comment.
otaviousp (author) says: Nov 22, 2010. 5:54 AM
What Gyro are you using?

Are you using the same code without modifications?
paolo.mosna says: Nov 22, 2010. 6:54 AM
Currently I'm using a LPR510AL on a micro board which provides an output voltage which represents the angular acceleration.

I fixed the A/D conversion problem on arduiono using a resistive partition on AREF pin on arduino board. AREF is now connected to GND with a 10microF, and with a 10K resistor to Vcc (5V). This give me a reference voltage for A/D of 3.77 V.

Yes, I'm using your sample code.
But I'm still having problem with angular value computation.

The angular value is slowly drifting apart when gyro is kept steady
(no force applied).

But I have just found out that in your code you have this constraint:

if(teta>-1 && teta<1) teta=0; //avoid drift error
teta = teta + ( valor * time ) / 1000;

to avoid drifting.
I would like to change it to:

deltaTeta = ( valor * time ) / 1000; // Angle infinitesimal increment

if(deltaTeta>-1 && deltaTeta<1) {
deltaTeta=0; //avoid drift error
}

teta = teta + deltaTeta;

Probably this would avoid spurious variation is steady state.
But I haven't yet tried this solution.
I would let you know as soon I get tested with new code.

Thanks.

otaviousp (author) says: Nov 22, 2010. 7:33 AM
Have you change de offset value?

Make a simple code to return the value read from your analog port with your gyro steady. The value show often is yout offset. Just replace this value on the code.

This can help improve the steady problem.
paolo.mosna says: Nov 23, 2010. 12:11 AM
Yes correct.
This is what I'm doing. I'm reading teta, the A/D output (from 0 to 1023) which is the gyro output voltage read with the arduino A/D converter.

The gyro output from the A/D has a range from 328 to 330 [count] when gyro is in a steady state ( I do not know if this variability is normal in a gyro or not). So I did use value 329 as offset inside the equation used to compute teta.

This produce, anyway, a drifting value for teta which keeps increasing or decreasing (depending on the value i choose for offset).
Also tuning the offset value using 0.1 resolution (let's suppose 329.4 instead of 329.0) can improve performance in the sense that I reduce the speed with which teta is increasing (decreasing) when gyro is in steady state but i?m not able to stop it.

I do not want to bother you too much with this problem.
Probably I would try with a different gyro with a I2C interface with integrated A/D conversion.

Thanks for you help.
Paolo.


paolo.mosna says: Dec 15, 2010. 2:56 AM
OK I got the solution to my problem.
The problem was related to the fact that I was querying the arduino's A/D converter to fast, a was not giving enough delay in between two consecutive readAnalog() calls.

Introducing a delay of at least 50 ms definitively solved my problem.
By the way I did find a solution to increase speed in A/D conversion on arduino.
Here are few lines of "code":

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#define FASTDAC 1

#if FASTDAC
#define DELAY 10 // delay in ms
#else
#define DELAY 50
#endif

so you can use less than 10 ms as delay time in between A/D calls.
I tested this with 5 ms delay and A/D conversion worked grate.

Thanks.
perfo says: Jul 17, 2010. 5:41 AM
Hello, I'm interested in finding out more about these solid state gyros. My question is:- If you had the device on the worktop and it read say 20 degrees. If you pick it up and drive around the block then put it back on the table in exactely the same place would it still show 20 degrees? what kind of accuracy can you get with these? Lastely where can I get chep ones in the UK to play with? Thanks, and sorry for so many questions...
otaviousp (author) says: Jul 25, 2010. 1:12 PM
Here is a nice tutorial about this gyros and accelerometers.

http://www.instructables.com/id/Accelerometer-Gyro-Tutorial/
just_watching says: May 6, 2010. 8:18 AM
ok so this is the code i would need just to get the angle



float sens= 0.512;
float offset= 316 ;
float count= 0;
float valor =0;
float first_time, time;
float teta=0;


//####################################################################//
// //
// Gyro //
// Scale Factor 2.5mV/ �/s = 0.512 counts/ �/s //
// Offset 316 counts = 1562mV = 1.562V //
// ADC 4.88 mV/count // 0.2048 count/mV //
// //
//####################################################################//

count =0;
for(int i=0;i<20;i++){

count = count + arduino.analogRead(0);

}

count = count /20;

valor = (count - offset ) / sens;

time=millis()-first_time;

first_time=millis();

teta=teta+valor*time/1000;

if(teta>-1 && teta<1) teta=0; //avoid drift error

otaviousp (author) says: May 6, 2010. 9:32 AM
Thats right.

cya
just_watching says: May 6, 2010. 9:48 AM
...ok just one other thing if I use a gyro with a accelerometer I would need this:

Angle=HPF*(teta) + LPF*(arctg( Ay / sqrt( Ax^2 + Az^2 ))

(from your other instructable)
otaviousp (author) says: May 6, 2010. 10:16 AM
Yes, but if you gonna use a 6DOF sensor, i think it is better to use the angle from only one direction. That way you have the rotation in on direction off the acellerometer plus the rotation from your gyro. 3 times this configuration.

Sorry my bad english and worst explanation.
just_watching says: May 17, 2010. 9:14 AM
how did you got the          float sens= 0.512; ?
otaviousp (author) says: May 17, 2010. 11:31 AM
The Scale factor is 2.5mV / º/s.
From the ADC 5V / 1024 = 4.8mV / count(raw value)

( 2.5mV / (º/s)  ) / (4.8mV / count) = 0.512 count(raw value) / (º/s)

so, if you read from the adc channel a value like 2, this means 3.9 º/s

2 / 0.512 = 3.9
just_watching says: May 18, 2010. 9:49 AM
so for this sensor (http://www.sparkfun.com/datasheets/Sensors/IMU/lpr530al.pdf)it would be (with 4x amplified)

3.3mV / (º/s)  ) / (4.8mV / count) = 0.512 count(raw value) / (º/s)

and if you would wire up 3.3v to Aref pin it would be

From the ADC 3.3V / 1024 = ...
otaviousp (author) says: May 19, 2010. 1:43 PM
From the ADC 3.3V / 1024 = 3.222mV / count (raw value)

3.33mV / (º/s) / (3.222mV / count)  = 1.0333 count (raw value) / (º/s)
just_watching says: May 20, 2010. 5:08 AM
ok thanks ...just one question why is the offet=
316
otaviousp (author) says: May 20, 2010. 6:13 AM
When you leave your gyro on a stationary position, probably you wont read a ZERO value.

You will have something like 1.23V. Or 381 counts.

This is your offset.

So, if you extract the value you are reading from the offset you will have ZERO.


just_watching says: May 20, 2010. 1:21 PM
ok thanks a lot this really helped me
just_watching says: May 20, 2010. 4:53 AM
ok thanks

just_watching says: May 7, 2010. 8:05 AM
I know what you mean ... so Ill get a tetax,and tetay, and anglex, angley.

Thanks this realy helped a lot because there isnt much about gyros or 6DOF on arduino.cc



just_watching says: May 6, 2010. 9:44 AM
ok thanks for that fast reply
im gonna use this for a quadrocoper with a razor 6dof from Sparkfun
tdragovich says: Dec 15, 2009. 8:31 PM

Can you provide the manufacturer of the breakout board or the retailer from who you got it? I have only been able to find the Gyro in SMD w/o breakout board. Thanks

otaviousp (author) says: Dec 16, 2009. 5:34 AM
the sensor is manufactured by epson toycom.
i bought that board on ebay, but I didn't found the seller anymore.

cya
WillTheRescue says: Dec 14, 2009. 4:13 PM
 Is there anyway I can use Processing to send a command to my Arduino?

Right now I have it set so if I type "T" into my serial monitor, it sends the command out digital pin 9, through my relay, and "presses" a button on my TV controller, thus turning it on.

Can I do something so that pressing a button in my Processing sketch would be like typing "T"?
otaviousp (author) says: Dec 14, 2009. 5:39 PM
Somithing like this should work:

color fillVal = color(126);
import processing.serial.*;

void draw() {
  fill(fillVal);
  rect(25, 25, 50, 50);
}

void keyReleased() {
  Serial myPort;
    myPort = new Serial(this, Serial.list()[0], 9600);
    if (keyCode == 'T') {
      println("T key pressed");
      myPort.write("T");
    }
 
}
WillTheRescue says: Dec 14, 2009. 6:38 PM
 Ah, I see!
Thank you very much.

I'm pretty new to Processing, so I'm not too sure of all it's capabilities.
otaviousp (author) says: Dec 15, 2009. 3:59 AM
NP, xD
rokag3 says: Nov 13, 2009. 6:43 PM
andrew  this type of gyroscope use the stability of a blade in vibration.
1)take a cork and insert in the center a long steel stick
2)put your stick in vibration vertical and hold the cork under your palm
3)turn the cork the axe of the stick stay vertical and you feel a force under your palm

in this type of chips they use a double (A,A')=x=(B,B') lyre the extremity (A and A')is put in vibration by a solenoid the other extremity (B and B') will vibrate then generate a current in a receptive solenoid the difference between the current generate by B and B' with a constant vibration  A and A' will mesure the anti couple
Sarah_mu says: Jul 3, 2009. 1:40 PM
Please explain, why you add 270 here: line(200,200,50*cos(radians(teta+270))+200,50*sin(radians(teta+270))+200);
otaviousp (author) says: Jul 3, 2009. 2:29 PM
This is just an offset for the angle, when add 270º to the teta angle, the line who indicates the angle stay on vertical.
Sarah_mu says: Jul 4, 2009. 1:32 AM
Thank you! I'm still confused ;)
Would you please explain it mathematically? For I see here:
if(teta>-1 && teta<1) teta=0; //avoid drift error
That the absolute value of teta is less than 1, then teta could not be in degree...right? next I don't see any conversion to degree, and then it is added to 270 and used as a degree. The program works with my IMU and it means that it is correct. I anyway can't understand the math behind this angle offset! do you see where my problem in understanding is? ;)
otaviousp (author) says: Jul 4, 2009. 3:54 PM
probably your IMU measure the angle rate in º/s like mine. so you have the angle already in degrees.
if(teta>-1 && teta<1) teta=0; //avoid drift error <- when you integrate the signal from the IMU if the IMU doesn't stay still, an error will be summed to the angle, so, this line try to prevent this.
the line() function on processing works in rad, so we have to make a conversion, that's why I use the function radians(270+teta); i really don't remember why i choose this value of 270, but i know it's work fine for me, xD
if you need some other explanation, feel free to ask.
Sarah_mu says: Jul 5, 2009. 10:52 AM
Thank you for the answer. Well then I will go for trial and error, to find out what that 270 does there! Your software is anyway nice! It didn't need any alteration when I uploaded to my Arduino, and worked fine with my IMU. I am doing the same thing, i.e. balancing a robot, toward DIY Segway. What I have now, is good tools for Kalman filtering the balancing process. I am seeking complementary filter code and information. It would be nice if we could share info ;)
otaviousp (author) says: Jul 5, 2009. 6:48 PM
Nice, I almost finished my DIY Segway, but it doesn't stay on the vertical, xD.
Soon I will post some information about it. http://web.mit.edu/first/segway/ Here you can found some information about sensor merging and filtering. My hole project is based on this. I'm using an accelerometer from a nunchuk to have the acceleration and the angle, that way I have two angles, and avoid the drift error from gyro. I have some documentation but it is on portuguese.
Plz send a PM with your email and I cans send it, and more informations for you.
Good luck
1-40 of 45Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!