Lego Segway Balancing Problem

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc

Moderators: roger, 99jonathan, imaqine

Lego Segway Balancing Problem

Postby janvos2506 » Fri Oct 28, 2011 11:31 am

Hey!
I'm making a set way and everything works but one thing.
the robot won't glance itself good enough :(
i have no idea why?
i have the motors at the right speed got the code nd everything but why ?

Thanks this is my code :

Code: Select all
import lejos.nxt.*;
import lejos.util.Delay;

public class Segway {
   
    public static void Balance() {
       ColorSensor light = new ColorSensor(SensorPort.S1);
       int lightVal = 0;
         lightVal = light.getLightValue();
         
         Motor.A.setSpeed(990);
         Motor.B.setSpeed(990);
      while(true) {

         while(lightVal < light.getLightValue()) {
            Motor.A.forward();
            Motor.B.forward();
     }
         
         while(lightVal > light.getLightValue()) {
            Motor.A.backward();
            Motor.B.backward();
     }
    }
    }
   
  public static void main(String[] args) throws Exception {
     Button.waitForPress();
        Sound.beep();
        Delay.msDelay(1500);
        Sound.beep();
        Delay.msDelay(1500);
        Sound.beep();
        Delay.msDelay(1500);
        Sound.twoBeeps();
        Balance();
        Button.waitForPress();
       
  }
  }
   


EDIT:: would an admin please move this to software? i accidentally putted in the hardware topics
Last edited by janvos2506 on Fri Oct 28, 2011 12:58 pm, edited 1 time in total.
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Lego Segway Balancing Problem

Postby Rickz2020 » Fri Oct 28, 2011 12:46 pm

Try this:

Code: Select all
    public static void Balance() {

       ColorSensor light = new ColorSensor(SensorPort.S1);
       int lightVal = 0;                 
        Motor.A.setSpeed(990);
        Motor.B.setSpeed(990);

      while(true) {

              lightVal = light.getLightValue();

                        if(lightVal < light.getLightValue()) {
                            Motor.A.forward();
                            Motor.B.forward();
                       }
         
                       if(lightVal > light.getLightValue()) {
                          Motor.A.backward();
                          Motor.B.backward();
                    }
               }
    }
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Lego Segway Balancing Problem

Postby janvos2506 » Fri Oct 28, 2011 12:56 pm

But you put the lightVal = thing in the while(true) thing but wouldn't that reset the desired position every time?
so it doesn't know with position it must be
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Lego Segway Balancing Problem

Postby Rickz2020 » Fri Oct 28, 2011 2:55 pm

Yes, you're right, sorry. I didn't mean to put it there - put it outside the while statement and try it.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Lego Segway Balancing Problem

Postby janvos2506 » Fri Oct 28, 2011 2:57 pm

lol that would be the same as i have now :p

EDIT:: tried your code... no improvement :(
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Lego Segway Balancing Problem

Postby Rickz2020 » Fri Oct 28, 2011 3:33 pm

Hmm, you are using a gyro, right? I've not made a segway myself, but every one of them I've looked at seems to use a gyro sensor from hi-technic to maintain balance.

GloomyAndy, a leJos team member, created a segway. Here is his post: viewtopic.php?t=866

He provides a link to his source code, too.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Lego Segway Balancing Problem

Postby TechnoX » Fri Oct 28, 2011 4:03 pm

I have done a segway that uses the shadow of the robot to determine the angle.
But with this approach it is really hard to make the robot balance for more than a few seconds.
The floor has to be just one color, and you must have the correct lightnings around the robot.

Try to build the robot in such a way that a small change in the angle make a great change in shadow over the colorsensor.
TechnoX
Novice
 
Posts: 51
Joined: Tue May 03, 2011 5:57 pm
Location: Sweden

Re: Lego Segway Balancing Problem

Postby janvos2506 » Fri Oct 28, 2011 4:45 pm

Okay tried it with a white flop enough lightning everywhere but still no luck.
i use the segway model form nxt projects with one did you use?
can you explain a bit more about using the shadow ? (:S)
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm

Re: Lego Segway Balancing Problem

Postby TechnoX » Fri Oct 28, 2011 5:25 pm

I built one myself, and I don't think I have any photos of it :/
But I know which one you have ;)

Do you know how it should work?
The lightsensor measure the amount of light and can calculate how close it is to the ground, and by that determine the angle.

So you need to have darker lightvalues when the robot is leaning (forward) and lighter when it's standing up (or leaning backwards). So you can test that first.

You shouldn't have lightning everywhere, you need a shadow under the lightsensor.

Do not have too high expectations on a segway with lightsensor, it's not really the right sensor for this task. But as I said, I have done it, and many people have used the one you build, so you can make it work! Just try at different floors and with different light conditions. Maybe you can try to mount the lightsensor on an lever arm to make it move further at every degree ;)


Just for debuging, test to print the lightvalue to the screen. So you can see if you have good conditions ;)
TechnoX
Novice
 
Posts: 51
Joined: Tue May 03, 2011 5:57 pm
Location: Sweden

Re: Lego Segway Balancing Problem

Postby janvos2506 » Fri Oct 28, 2011 8:15 pm

Allright thank you!
i will try it at a white VERY well lit floor Thanks!
And yes i know the color sensor is pretty rubbish for this but i can't afford a 50$ gyro
But is my code all right??
janvos2506
Novice
 
Posts: 34
Joined: Mon Oct 24, 2011 8:58 pm


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff