[Solved] - Segway won't balance itself.

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

Moderators: 99jonathan, roger, imaqine

[Solved] - Segway won't balance itself.

Postby Knutern » Fri Oct 28, 2011 10:08 am

Hello,

I am currently testing out my NXT-Segway with an identical build based on the HTWay (http://www.hitechnic.com/blog/gyro-sensor/htway/). I'm still quite new to this and I couldn't find that many sample codes for the new leJos 0.9.0.

So this is the code I've got so far:
Code: Select all
import lejos.nxt.*;
import lejos.robotics.*;
import lejos.nxt.Motor.*;
import lejos.nxt.Button.*;
import lejos.nxt.SensorPort.*;
import lejos.nxt.addon.GyroSensor;
import lejos.robotics.navigation.Segway;

class easyway
{
   public static void main(String[] args) throws Exception
   {
      NXTMotor left = new NXTMotor(MotorPort.C);
      NXTMotor right = new NXTMotor(MotorPort.A);

      GyroSensor gyro = new GyroSensor (SensorPort.S2);

      Segway a = new Segway(left, right, gyro, 4.9);
   }
}


After compiling and transferring the code to the NXT it starts by prompting me to lay Segway flat for gyro calibration, then begins self-balancing thread. It just moves forward or backward, depending on how it is balanced, no matter what and ends it with a faceplant/falling on the back. I've tried balancing it against the wall but it moves forward/backward in the same way. The wheel diameter is written down in cm, but I've tried it with inches but to no avail.

It feels like it won't update itself as fast as it should? I don't know for sure, I'm still quite new to this. Any help? I can attach a video if necessary.
Last edited by Knutern on Fri Nov 04, 2011 8:27 am, edited 1 time in total.
Knutern
New User
 
Posts: 5
Joined: Fri Oct 28, 2011 9:53 am

Re: [Beginner] - Segway won't balance itself.

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

I've never tried the segway, but wouldn't you need to put your code in a while loop?

EG:

Code: Select all
while(!Button.ENTER.isPressed())
{
// your code here
}


Hope this helps,

Rich.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: [Beginner] - Segway won't balance itself.

Postby Knutern » Mon Oct 31, 2011 8:13 am

Thanks for the help, Rich. I'll give it a try later this morning.

It might explain why the Segway only balanced once and then just faceplanted, so putting it in a loop might change things.
Knutern
New User
 
Posts: 5
Joined: Fri Oct 28, 2011 9:53 am

Re: [Beginner] - Segway won't balance itself.

Postby Knutern » Mon Oct 31, 2011 11:47 am

Ok, so I tried this code:

Code: Select all
import lejos.nxt.*;
import lejos.robotics.*;
import lejos.nxt.Motor.*;
import lejos.nxt.Button.*;
import lejos.nxt.SensorPort.*;
import lejos.nxt.addon.GyroSensor;
import lejos.robotics.navigation.Segway;

class easyway
{
   public static void main(String[] args) throws Exception
   {
     while(!Button.ESCAPE.isPressed())
     {
        NXTMotor left = new NXTMotor(MotorPort.C);
        NXTMotor right = new NXTMotor(MotorPort.A);

        GyroSensor gyro = new GyroSensor (SensorPort.S2);

        Segway a = new Segway(left, right, gyro, 4.9);
      }
   }
}


but sadly it didn't work either. I still definately need help on this issue.
Knutern
New User
 
Posts: 5
Joined: Fri Oct 28, 2011 9:53 am

Re: [Beginner] - Segway won't balance itself.

Postby kirkpthompson » Mon Oct 31, 2011 2:38 pm

Hi Knutern and welcome to the world of programming.

What you have done here is continuously create new objects until you run out of memory (if the garbage collector doesn't take care of the discarded object references promptly).

I'm guessing the internals of the Segway class takes care of the balancing in a loop but with no details (it no longer exists in the SVN repository; replaced with Segoway it looks like), I have no real idea. Can you describe what the your code does in plain words? If not, you probably don't understand how it is going to achieve what it is supposed to do which is most of the work in writing effective code. Your code snippet to me "says":
As long as the escape button is not pressed, continuously create 2 new motor objects, a new Gyro sensor object, and a new Segway object using the previous three objects (and a constant 4.9) as parameters to its constructor.
The questions now are: Is this what you wanted to do and, how do you envision it should work?

Since we think in natural language, it makes sense to think through our code in the same way. Try to search through the forum on "segway" (or "balancing", or.. ) to see what other people have done, come up against, etc.

Best,
-K
Leg Godt!
User avatar
kirkpthompson
leJOS Team Member
 
Posts: 304
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: [Beginner] - Segway won't balance itself.

Postby gloomyandy » Mon Oct 31, 2011 5:01 pm

The source or the 0.9 Segway class is here:
http://lejos.svn.sourceforge.net/viewvc ... threv=5716

Which wheels do you have on your robot? The diameter you have specified does not seem to match any of those given in the program source. The diameters from the program source are:
* NXT 1.0 wheels = 5.6 cm
* NXT 2.0 wheels = 4.32 cm
* RCX "motorcycle" wheels = 8.16 cm
If you are still having problems try rotating the gyro so that it points in the other direction.

The Segway is a pretty advanced project, and is probably not the best one to start with when learning robotics and leJOS. If you are still having problems you may want to try a simpler project and come back to this one later...

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

Re: [Beginner] - Segway won't balance itself.

Postby Knutern » Wed Nov 02, 2011 12:20 pm

You pretty much accurately described what I envision the code to do, Kirk. I searched alot on this forum but I only found posts about the pre-0.9.0 leJos. I used the Segway API (http://lejos.sourceforge.net/nxt/nxj/api/lejos/robotics/navigation/Segway.html) in order to come up with the current code.

The wheels I'm using are 49.6x28, so I'm not 100% sure about what kind of tires they are. We tried pointing the gyro in another direction and it helped! With the release of 0.9.0, the Segway class seems really easy to use, to be honest. I'm giving a go at the SegwayPilot now :lol:
Knutern
New User
 
Posts: 5
Joined: Fri Oct 28, 2011 9:53 am

Re: [Beginner] - Segway won't balance itself.

Postby gloomyandy » Wed Nov 02, 2011 1:30 pm

Have you managed to get your segway balancing yet? If not then using the Segway pilot will not help, and will probably make things worse... When it works a Segway should happily stay upright for a very long time, not just a few seconds...

Although using the Segway class may be easy, fixing things if/when it does not work is not... That is why I said it is an advanced project. I'm not sure which wheels you are using but they do not sound like any of the ones that have been used to test the Segway class. Can you post a picture (or a link to a picture), of the wheels you have.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3993
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: [Beginner] - Segway won't balance itself.

Postby kirkpthompson » Wed Nov 02, 2011 1:34 pm

continuously create 2 new motor objects, a new Gyro sensor object, and a new Segway object


I was trying to say [albeit indirectly] that you do not need to continuously create and recreate these objects. And assuming the Segway class manages the control loop, you don't need to use a loop like you did.

Best,
-K
Leg Godt!
User avatar
kirkpthompson
leJOS Team Member
 
Posts: 304
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: [Beginner] - Segway won't balance itself.

Postby Knutern » Wed Nov 02, 2011 2:32 pm

The Segway is balancing and functioning as anticipated now. I started out with the standard Segway-class and moved on to SegwayPilot. I tried out some different commands in SegwayPilot and everything so far seems to be logical and working as envisioned. All I had to do was to turn the Gyrosensor 180 degrees sidewise. I looked into the source code you posted and it was indeed very advanced and it would be difficult to start fixing specific stuff. I can see if I can post a picture tomorrow. The issue I posted in the original post is resolved though.

I removed the loop and it worked in the exact same way, so you're right Kirk.
Knutern
New User
 
Posts: 5
Joined: Fri Oct 28, 2011 9:53 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff