Using the HiTechnic gyroscope sensor to measure angles

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Wed Jul 04, 2012 1:28 pm

Hey i'm trying to measure how much the robot turns with the gyroscope sensor i tried to use

Code: Select all
System.out.println(gyro.readValue());


but that just give me 595 the whole time en when i move the sensor it goes to like 600 but then right back to 595 ?
is there a way i can measure angles with it ?
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby skoehler » Wed Jul 04, 2012 2:00 pm

How is gyro declared? What have you assinged to it? To which SensorPort is the sensor connected?

Edit: Andy is right. You're expecting an angle, but a gyro rather return a rate (angle per second). In order to get an angle, you need to integrate the samples obtained from the gyro.
skoehler
leJOS Team Member
 
Posts: 1422
Joined: Thu Oct 30, 2008 4:54 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby gloomyandy » Wed Jul 04, 2012 2:01 pm

a gyro measures the rate of change of rotation, this is the angular velocity. You must ensure you have the sensor mounted in the correct orientation. The sensor typically has an offset that it will read even when it is not being rotated. You need to measure this and subtract it from and readings. To get rotation from angular velocity you will need to integrate the velocity readings. But this may generate a build up of errors and it will always be a relative value not an absolute one. To find out more try searching the web for information on gyros and integration...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4084
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Wed Jul 04, 2012 4:21 pm

im confused?
so i set the offset to 595 so when it is in the right position it is 0 and when i rotate it it changes the angle
but it doesn't give me ilk 90 or -90 it jumps right back to 0
could someone explain me a lil more on how i would make it so it measures the angles and i hooked it up to sensor port 1
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby gloomyandy » Wed Jul 04, 2012 4:31 pm

Read what I posted above. A gyro does not measure an angle it measures the speed of rotation. So when you stop rotating it the speed will go back to zero. As I said google a few things do a bit of reading on gyros and how integration will let you turn angular velocity into the amount of rotation.

What is it you are trying to use this sensor for. It may not be the best sensor for the job...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4084
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Wed Jul 04, 2012 5:32 pm

I'm trying to move a motor the same amount of degrees as the gyroscope moves
Well i googled it but i couldn't really find anything that explained well how to turn angular into the amount of degrees it has turned
i know I'm asking a lot and i may seem dumb but i never used anything like this with degrees and such
so could someone here on this forum give me a link where it gets explained or explain me it yourself
if you can thank you soooo much!! i'm trying to do this 2 full days and its driving me crazy ;(
sorry for all the questions!!
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby skoehler » Wed Jul 04, 2012 5:44 pm

I think you should ask somebody who's more of any expert in that area, so he can explain it to you properly. It's not hard after all. Don't you know Integral from school? Same thing, but you only have discrete samples and not a continuous function.
Did you attend physics lessons in school? Cause the integral of speed (meters/second) gives you distance (meters) while in this case you need to integrate degrees/second into degrees.

Here's a rough sketch: you need a thread that continuously (every 10ms or so) obtains samples from the gyro. Then, multiple each sample with time that has passed between the current and the previous sample and then sum up the results.
skoehler
leJOS Team Member
 
Posts: 1422
Joined: Thu Oct 30, 2008 4:54 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Wed Jul 04, 2012 6:14 pm

something like this ??

Code: Select all
public void timedOut() {
   float first = (float) (gyro.getAngularVelocity()* 0.01);
   float second = first;
   int third =(int) (+ first+second);
  System.out.println(third);
 }
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby skoehler » Wed Jul 04, 2012 6:26 pm

That code doesn't look quote right. It doesn't even contain a loop or the code necessary to measure the time that has passed inbetween two samples.
skoehler
leJOS Team Member
 
Posts: 1422
Joined: Thu Oct 30, 2008 4:54 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Wed Jul 04, 2012 6:29 pm

okay I'm trying to get this working but I'm too stupid i think i can't get it to work could someone (i NEVER EVER ask this) give me like a little code snippet that converts the angular velocity into real angles?
like how can i know how much degrees the robot has turned ? it would really help !
I'm trying to do this for 2 days straight but i can't get it to work ;(
THANK you SO MUCH if you can help me :)
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby TechnoX » Thu Jul 05, 2012 12:54 am

You should ask your math teacher to explain integration for you! You'll need it many times in robotics ;)

What about this code? ASK if you do not understand it :)
Code: Select all
UpdatePeriod = 10ms

loop(forever)
{
   degreesPerSecond = getVelocity()

   // Integration
   angle += degreesPerSecond * UpdatePeriod

   sleep(UpdatePeriod)
}
TechnoX
Novice
 
Posts: 52
Joined: Tue May 03, 2011 5:57 pm
Location: Sweden

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Thu Jul 05, 2012 11:36 am

wow.. i will test it if it works I'm dumb haha thank you so much man !
and i would ask it my math teacher but its summer :p no school for 9 weeks :)
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby janvos2506 » Thu Jul 05, 2012 12:16 pm

okay i got this now

Code: Select all
import java.io.*;
import lejos.nxt.*;
import lejos.nxt.addon.GyroSensor;
import lejos.nxt.comm.*;
import lejos.util.Timer;
import lejos.util.TimerListener;

public class Angle {
     static GyroSensor gyro;
     static float degreesPerSecond;
     static int angle;
     static int UpdatePeriod = 10;
      
  public static void main(String [] args) throws Exception {
     gyro = new GyroSensor(SensorPort.S1,595);
    
     while(true)
     {
        degreesPerSecond = gyro.getAngularVelocity();

        angle += degreesPerSecond * UpdatePeriod;
        System.out.println(degreesPerSecond);

        Thread.sleep(UpdatePeriod);
        
     }
  }
    
}


but it is just giving me an increment number ? :)
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby TechnoX » Thu Jul 05, 2012 2:24 pm

You have to retrieve the velocity from the gyro-sensor. Obviously you have an offset on the value (595?) that you need to subtract :)

Try to understand the code. Read about Integrals: http://simple.wikipedia.org/wiki/Integral
TechnoX
Novice
 
Posts: 52
Joined: Tue May 03, 2011 5:57 pm
Location: Sweden

Re: Using the HiTechnic gyroscope sensor to measure angles

Postby gloomyandy » Thu Jul 05, 2012 2:45 pm

If you are using getAngularVelocity then you really need to make a call to recalibrateOffset before you start to read values. If you do that there is no need to pass the offset value into the constructor. See the doc for details:
http://lejos.sourceforge.net/nxt/nxj/ap ... rVelocity()
http://lejos.sourceforge.net/nxt/nxj/ap ... ateOffset()

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4084
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Next

Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff