Jegway – Lego NXT Segway

Engscope is home of the Jegway, a Segway like device made from Legos! Actually, I am by no means the first to make this out of Legos Mindstorm NXT set. That credit would go to Steve Hassenplug’s Legway, the world-famous self-balancing LEGO robot. The Legway uses an optical detector to maintain a certain distance from the sensor to the ground. Next, there’s the NXTway, by Philo. It uses the optical sensor that comes with the NXT set. Lastly, the inspiration for the Jegway comes from the NXTway-G by Ryo Watanabe. His implementation involves a gyroscope and displays outstanding stability.

Of course, I can just leave it at that. A real Segway uses accelerometers and gyroscopes. The reason for the use of both types of sensors comes down to the estimation of tilt angle, and I will discuss more on the subject in each of the Jegway segments.

I have made two implementation, using just the gyroscope, similar to Ryo Watanabe’s NXTway-G, and another that combines a gyroscope and accelerometer to get a more accurate measure of the tilt angle. This idea comes from both the original Segway, and also Trevor Blackwell’s balancing scooter.

Jegway Rev0 – Implemented using the Hitechnic gyroscope.

Jegway Rev2 – Implemented using the Hitechnic gyroscope, Hitechnic accelerometer and the Mindsensor PS2 controller.

Enjoy, you’ll find documentation as well as code for both versions. The code is implemented in RobotC. Originally it was written in NXC, but because NXC does not support floating point arithmetic, I eventually broke from the pope and converted to RobotC.

62 Responses to “Jegway – Lego NXT Segway”
  1. Alex says:

    I am attempting to build my own NXT segway and have so far (about 3 weeks) been unable to overcome the problem of the gyro drift. I have attempted to use the gyro alone and the gyro accelerometer and both have failed. I was wondering if you could send me a copy of your code (I am also using RobotC) or if you could post it here and notify me. Since I’m only a senior in high school I don’t have the math skills that Ryo Watanabe used so I cant follow his documentation.

    Thanks,
    Alex

  2. jliu83 says:

    Give me a few days. I’m trying to find where I put the codes. It’s on one of my computers, but I can’t remember which one.

    -J

  3. Alex says:

    Awesome, you have no idea how big a help it will be to have some working code to look at. I just rebuilt my chassis last night to be similar to yours and modified my code a little to track the position and that helped a lot. I shattered my previous record time for it to balance. I had it up for a little over 11 minutes before I pushed it and it fell. However I have since been unable to repeat that feat, so I’m back to square one

  4. jliu83 says:

    Alex, the only way a segway with only a gyroscope can stay upright and compensate for the drift is by keeping track of position and compensating for it. I think the easiest way to implement a segway has to be with at least a gyroscope and an accelerometer. Rev 0 of the segway only had a gyroscope, and it was a pain to test and implement.

    -J

  5. Alex says:

    I have both a gyroscope and an accelerometer mounted on the robot, and am currently using both. However I’ve found that the accelerometer simply isn’t sensitive enough to know when the robot really is balanced and it is too easily affected by the rocking motion mine currently makes. But I have almost exactly the same setup as you did currently. I have my gyro in almost the same spot and my motors are in the same orientation and position. Basically the only thing I cannot do with mine is the PS2 controller, but I can do the same thing through RobotC using a USB controller (I’ve been using a flight joystick so far on other robots)

  6. Alex says:

    Thank you sooo much for getting your code up. I already have my robot balancing beautifully. And I’m already working on modifying the Rev0 code to give me joystick control via bluetooth from my computer. Again, thank you so much for the help, I think I could’ve gotten it to work eventually but it would’ve taken me a few more weeks at least.

    -Alex

  7. Carol says:

    Myself and my friend are trying to build a robot using the gyro and light sensors. We have been unsuccessful so far, and we need this robot to work in time for the Botball competition which is on March 14th. We are running out of time and desperate. Please help us in any way, shape or form. We would prefer programming though. Please respond. Thanks.

  8. jliu83 says:

    Hmmm.. Well I would like to help, but I think I’ll be quite busy. I don’t know what this Botball competition is but, it sounds like fun. However, if the deadline is March 14, I think you guys are in trouble somewhat. Besides, I would prefer to build the bot from scratch for a competition. I think Legos are kinda flimsy somewhat. You shouldn’t give up though. Even though you were unsuccessful until now, at least you learned some stuff! You can’t expect the program to compile the first time, much less expect a program to do exactly what you want.

    You guys are welcome to ask me questions on any particular subjects, but doing the programing is half the fun. So I’d like to help, but only from perhaps a technical consultation point of view.

    -J

  9. Carol says:

    Thanks soooo much for the advice! We have made a breakthrough though. It works on our tables! We are trying to make it work on the carpet, but it has so many colors! Also, we lost our gyro sensor so we worked with a light sensor and that is the one we made the breakthrough with. It actually balances! Also, the Botball website is http://www.botball.org so go ahead and check it out!

    - Carol.

  10. Carol says:

    Wow wow wow! Sooooo much progress! I think that we are ready for Botball! Thank you God! Also, still waiting for a reply.

    - Carol.

  11. jliu83 says:

    Hmm.. I didn’t see a question on your Feb 20th comment, or are you waiting for a reply from god? For the color sensors, I don’t think it would work on anything but a consistent surface. You’ll need to use something like an accelerometer and/or gyroscope. The laser/diode on computer mice works on the principle of phase changes between the outgoing light and the reflected light, but I don’t think they make a sensor like that for the mindstorm. Besides, the distance between the light sensor and the surface must remain constant, which is another factor that makes the light sensor difficult to implement.

    I don’t know if this would help but, in physics, the equation intensity of light is governed by a exponential decay (ie, the intensity of light of something twice as far is exponentially less intense). This makes sense since light is hitting an area, so you have to divide the wattage of the light by the area on which it is directed. I don’t know how large this effect is, but it might make your balancing robot unstable.

    -J

  12. Carol says:

    Okay. Let me try to explain the concept of how our light sensor works. See, we take readings of the white gameboard one leaning back one leaning forward and add those two readings up. Then, we divide that number by two and the number that is left is our setting for the light sensor. If it ends in a decimal point, then we just round it and hope for the best. It has worked all the time, but you’re prediction is right. It is kind of unsteady. We are now trying to use an ultrasonic sensor, but we are still in the beginning stages. please reply and thanks.

    - Carol.

  13. Carol says:

    Okay. Let me try and explain how our light sensor technique works. See, we take readings of the white gameboard one leaning forward, one leaning back, and add those two numbers together. Then, we divide that number by two and that leaves us with our setting for the light sensor. If it ends in a decimal point, we just round it and hope for the best. So far our technique has worked, but you;re right. The segway is unsteady. At this time, we are trying an ultrasonic sensor, but we are still in the beginning stages. Wish us luck! And thanks so much for helping us. Please reply.

    - Carol.

  14. jliu83 says:

    No, ultrasonic will not work, it’s not precise enough. I’ve tried it already. I told you, gyroscope + accelerometer.

    -J

  15. Carol says:

    Okay I will try to find an accelerometer.

  16. Alex says:

    You’ll still need more than an accelerometer. Accelerometers will only give a decent angle reading when they are stationary. However, when used in conjunction with a gyro they can make an extremely steady balancing robot.

  17. jliu83 says:

    Yes, Alex is correct in his statement. The reason is because when your robot is falling down the accelerometer would read “zero” (according to Newtonian Relativity, the point of reference is equal to gravity, therefore, measuring it should read zero. To think of it another way, if gravity accelerates us down, and we accelerate with gravity, the force felt is zero since the two forces cancel each other). Therefore, your accelerometer’s reading is only valid when the robot is not moving very much. The gyroscope on the other hand is good at reading changes in orientation, however, when it is stationary, it tends to drift. You need to use the gyro in conjunction with the accelerometer to get the robot to work. Use the gyro to correct the accelerometer when it is moving, and use the accelerometer to correct the gyro when it is stationary. Read the Jegway Rev2 write up for more info.

  18. Carol says:

    Okay, I will try and get both again. See, we used to have a gyro but my friend lost it. And before I went onto this website, I never even knew that there was something called an accelerometer before! See, I’m new to the whole robotics thing, but I’m getting along quite well. And thanks to both of you!

    - Carol.

  19. Alex says:

    I also have another alternative to using a gyro and/or an accelerometer. If you have a little money to spend you may want to look into investing in a tilt sensor. Unlike the gyro which measures rotational velocity, and the accelerometer which measures force and acceleration, a tilt sensor measures actual angular position. It’s essentially an electronic bubble level. However I think they may be a bit pricey and possibly somewhat slow to respond to a change in angle. So it may be best if you could also use it in conjunction with either an accelerometer or a gyro and just use the tilt sensor to help correct the error in the other sensor’s readings. I’m just beginning to plan how to scale up my lego segway to a full size riding one using exactly this setup.

  20. Carol says:

    Okay, we are ordering a tilt sensor today. But what I don’t get is the fact that both the gyro and the accelerometer sensors both go into port 3.How am I supposed to put them both together to function properly?

    - Carol.

  21. Carol says:

    Hello this is Carols friend Jesus. We finally got the Gyroscope and Accelerometer. I need help looking for the accelerometer & Gyroscope programing though. I was wondering if you can help me and Carol find the programing blocks. Maybe if you send us something that could help us or whatever then tell us then leave a reply.

  22. Alex says:

    What programming language are you using? If you’re using NXT-G then you’ll have a very difficult time getting a robot to balance, the program simply doesn’t run fast enough on the robot. However, RobotC is fast enough (that’s what I used) and is still very simple to learn (I started with it and skipped NXT-G completely.) Either way, you should be able to set which port the accelerometer and gyro read from so you can plug them into separate ports.

  23. jliu83 says:

    Yeah…. You know. I think, before going out and ordering stuff and wondering if something will work or not, you guys should learn some programming first. NXT-G is just not going to cut it. It’s basically made for the average layman to put together simple programs. It’s not very efficient because the compiler has to jump loops before assembler jumps through loops, before finally loading in the bit code. It can’t do any special functions neither. Please just uninstall it from your computer and save yourself a lot of trouble.

    Robot C is probably the way to go. But this is NOT something that we can teach you. You need to teach yourself how to program. When you set out do a project, make sure you know and understand the scope of the project before proceeding. If you are unsure of what you can and cannot do as an engineer, then you’ll probably need to sort that out before trying to satisfy the requirements of the project itself.

  24. Alex says:

    He’s right. Teach yourselves enough to be familiar with RobotC first. It’s not hard. What you’re trying to do should be possible with the beginner user level. Most of the stuff you need to learn is just syntax, and that becomes automatic after a little practice.

  25. Carol says:

    Well….. okay. I don’t exactly know what you mean, but how do you switch NXT-G to RobotC programming? We are still looking for a solution, but I think that we will figure it out in time and with your help.

    - Carol.

  26. Alex says:

    you’ll need a copy of RobotC for starters. Then you’ll need to update your NXT firmware to the RobotC version. From there it’s all the same, just better and in text instead of graphical programming.

  27. Carol says:

    Well, we found a CD for RobotC programming, but we can’t get it working on our Macs. So, we called this guy with Lego Mindstorms, and he told us step-by-step how to get it working. We haven’t been able to test it yet. We finally got it to work on a Friday. At the end of class. So, we get to test it on Monday. Wish us luck!

    -Carol.

  28. Alex says:

    Yeah, I run robotc on my mac by running windows. It’s about all I use windows for nowadays. If robotc ever supports mac I can finally remove windows from my computer :)

  29. Carol says:

    Well, let me think. Oh yeah, it WAS on a Mac, but using Windows. Sucks! I thought that we had done something special! Okay then, we’ll keep working on it, but I don’t think Jesus and John will get RobotC progamming. Which leaves it all up to me and our teacher.

  30. Carol says:

    Okay, we finally got a program on our robot using RobotC! Yay! So happy! This is a major breakthrough for us. But we still need help. Please respond.

    -Carol.

  31. Alex says:

    Awesome, how’s it working? It’s highly unlikely it’ll work first try. I must have gone through at least 25 or 30 variations before it balanced perfectly. But you’ve made a big step!

  32. Carol says:

    I know, right? And guess what? John and Jesus kinda get the programming! But you’re right. Our first try didn’t balance perfectly.

    -Carol.

  33. Alex says:

    If it really balanced at all, then I’m impressed. Mine fell over immediately after I started it. But yeah, you’ll find robotc to be a massively superior programming environment to nxt-g. And it really isn’t that much more difficult than nxt-g to understand.

  34. Carol says:

    Yeah well we are still searching for a program, because we don;t exactly know how to use RobotC yet. We will keep trying, but for now, we will continue.

    -Carol.

  35. Carol says:

    We are still looking on the Internet for a program and we don’t know what else to do. Please help us. We really need it.

    - Carol’s friend, Jesus.

  36. Carol says:

    We are still looking on the Internet for a program and we don’t know what else to do. Please help us. We really need it.

    - Carol’s friend, Jesus.

  37. Alex says:

    First of all, no matter what you’re going to have do some coding of your own. Whether it be to give the exact ability set you want or just because no two balancing robots take exactly the same code. Anyway, there’s always the two versions of code on this site. You can look at those and possibly use them as a baseline for your own code. If you want I can also show you some of my code, though it is significantly more complex as I added a bunch more functionality to mine such as autonomous, a menu system on the bot, the list goes on. But if you want, I can do more help for you if you want to use e-mail. If so you can e-mail me at beattieboy@comcast.net. Any other info you can give on what you’ve already got would also help, such as robot pics, code you already have, etc.

  38. Carol says:

    Well then, that’s a big discovery. And I don’t mean that sarcastically. Wow. We would still have to do some coding of our own. By the way,expect an e-mail soon.

  39. Rade says:

    Hi,

    Have you tried version 0 with PSP-Nx remote. I wonder if it will balance without the accel, like Ryo’s did.

    Also, could you pls post some closeup pictures of the sensors-gyro and accel. I am puzzled as to why is your gyro turned up side down (connector pointing upward), or is it?

    Thanks for posting the code.

    rgds — Rade

  40. jliu83 says:

    Yes, rev 0 of the jegway works without an accelerator, but an algorithm has to be implemented to compensate for the drift. A gyroscope measures the change in angle, so really it doesn’t matter which angle you mount. As long as the axis is aligned with the “falling axis” you can still use it to measure differential angle. I have seen Ryo’s inverted pendulum, it seems to be very well implemented. However, his pendulum is implemented with a lot more optimization (it’s his day job). My day job on the other hand does not involve optimizing fun little robots, so the code is a lot more crude.

    -J

  41. Alex says:

    And unfortunately Ryo doesn’t post his code or respond to e-mail (trust me, I’ve tried)

  42. Rade says:

    I know that Ryo doesn’t share his code and that is OK. He has lot of his hours spent in it.

    I am close to make rev0 code work. The problem is that my NXTway motors are not responsive enough to angle change, so when I tilt it the motors do’nt move fast enough to make it balance. What parameter should I play with to improve responsiveness to angle change. I am using brand new batteries.

    thanks a lot — Rade

  43. jliu83 says:

    Always play with P first. Then gradually start increasing I, and lastly D.

    -J

  44. Alex says:

    I actually didn’t know that. I’ve been playing mostly with D and I, and trying to make P as small as possible. Now that I think about it though it makes more sense to start with P.

  45. Carol says:

    Well, we made a gyro test which we called gyro pain, because it really was a pain in the butt. But the best part is, it works! We are one, actually two steps closer to programming! Why two, you may ask? Well, we got a gyro block on our computer, and an accelerometer block also! Thanks, Mrs. Reynolds. Well, got to go. Have a good night!

    -Carol.

    P.S. What is rev0?

  46. Alex says:

    Ok, I actually have a question for jliu since you do still seem to know more about this than I do. I’ve got a version of lego segway built meant to simulate a full scale one. Therefore I actually tilt the controller to simulate the change in cg of a person leaning. It works fairly well but what’s got me confused is that I changed the wheels on it from the large skinny ones, to the same ones you used and it still balances. I thought the lego segways stopped working if the wheel sizes differed due to the change in correction speed (at least that’s what my research said). So why on earth does it still balance?

  47. Carol says:

    Well, I don’t know about you, but we actually have TWO segways. Both are different sizes, though. Taylor, our most recently built segway, is short and fat. She uses regular Lego wheels. Katy, the original segway, is tall and skinny. We have made a few moderations to make her shorter. She uses the large skinny wheels. Which segway should we pay more attention to? And which one do you think will work better?

  48. Alex says:

    Honestly I’m not sure which will work better. But I can tell you that with proper tuning the one with larger wheels will be faster but jerkier. The smaller the wheels the slower the segway, also meaning the less disturbance it can compensate for, but also the finer the control, meaning the smoother the response will be. The NXT motors have a little extra play in them, meaning they can be turned a tiny amount without turning the motor itself. This movement becomes more pronounced the larger the wheel you use. It’s not really a problem, but the small wheel ones definitely appear to balance better.

  49. jliu83 says:

    Alex, try to think of it as stability (in the control theory sense) rather than whether it works or not. Stability is often more of a gray area than just if it balances or if it falls over. Let’s say that you have a system that balances. Well, if you graph the response of the system with respect to time, it means that the response is stable after a certain amount of time. Let’s take a car suspension for example. It has two components, and a spring and hydraulic damper. Each counteracts the other so that when you are hit a rock, the spring gives, and the hydraulic damp counteracts the spring. The result is: you return to a smooth ride as fast as possible. If a larger rocks comes by, the spring gives a bit more, and the hydraulic damp needs to do more work to stop the car from bouncing, but eventually the ride will be smooth again. So in this case the response time is longer, but the system is still stable. Think about how the system changes if we use a very stiff spring, or if we use a very strong damper. If we use a strong spring, it causes the car to bounce a lot, and very fast, so the car will oscillate quite a few times before it returns to a stable state. If we use a strong damper, the result is a slow bounce, but again, the car is bouncing for a longer time, despite being a stable system. The return to stability takes a longer time, but the system is still stable. This is the equivalent of changing weights or changing parts on the segway. The art of optimizing the control system is to choose the strength of the spring and damper so that the car stop bouncing in the shortest amount of time, for the most sizes of rocks (and I do mean that control system optimization is an art). This is why Ryo’s segway is so good. He is able to graph his system’s response, and change values/parts so that the stability is achieved very quickly.

    However, consider the following situation. If a series of rocks are on the road, then what happens? Do we still have a stable system? Well it depends. A spring and a damper will have a specific harmonic resonance. If the rocks are coming to the wheels at exactly that frequency, the car will bounce higher and higher, and presumably, will eventually land and damage a part. This is known as a unstable system, and is the equivalent of your segway falling over. So, as you can see, there are many situation where stability is still achieved, but not optimized, and many more situations where stability is not achieved. If your control system response is fast enough such that the changing of a wheel does not cause instability, then it’ll still balance.

  50. Carol says:

    Okay……… I’m going to have to sleep on that one. As i said, I’m new to the whole robotics thing, so I’m not exactly an expert like you two. Well, right now I’m on spring break, so it’ll be a week more before I see my teacher. But I’ll try and comprehend all of this information by myself. Wish me luck!

    -Carol.

    P.S. No one has told me what is rev0 yet.

  51.  
Leave a Reply