Facebook Twitter Gplus YouTube RSS
Home » Tutorials » EV3 Programming » Tutorial: Self-Balancing EV3 Robot
formats

Tutorial: Self-Balancing EV3 Robot

Published on July 1, 2014 by in EV3 Programming
BALANC3R (left) and Gyro Boy (right)

BALANC3R (left) and Gyro Boy (right)

This tutorial will show you how to build and program a self-balancing LEGO MINDSTORMS EV3 robot that can drive around a room. You can build and program BALANC3R (left) or Gyro Boy (right). Once your robot is up and running, you’ll be challenged to customize the construction and program to invent your own self-balancing robot.

 

Step 1: Building a robot

To begin, choose the robot you want to build, and follow the respective step-by-step building instructions.

Requirements for BALANC3R:

Click to go to the building instructions

Click to build BALANC3R

Requirements for Gyro Boy:

Step 2: Installing the EV3 Gyro Sensor block

If you’re using the LEGO MINDSTORMS EV3 Home Edition Software, you’ll need to install the EV3 Gyro Sensor block before you can program your robot.

  • Follow the steps in this article to install the block.
  • If you’re using the LEGO MINDSTORMS EV3 Student Edition, this block is already installed.
Intalling the Gyro Sensor block

Installing new Sensor blocks

Step 3: Download the balancing robot project

  • Right-click this link, click “Save Link As,” and save the project file to your computer.
  • Launch the EV3 programming software, and open the downloaded project file.

Before you run the programs, let’s briefly examine how they work. The project includes four example programs, two for each robot:

  • BALANC3R-Basics: Balance in place, turn right, and turn left
  • BALANC3R-RemoteControl: Control the robot with the infrared remote
  • GyroBoy-Basics: Balance in place, turn right, and turn left
  • GyroBoy-AvoidObstacles: Drive around while avoiding obstacles

Each program consists of two configuration blocks, a balance loop, and a drive control loop, as shown in the figure below.

The balancing program consists of a Balance Loop, a Drive Control Loop and configuration blocks.

The balancing program consists of a balance loop, a drive control loop, and configuration blocks.

  • The configuration blocks let you specify what the robot looks like, so that the robot knows how to balance. For example, the second setting of the first setting specifies the diameter of the robot’s wheels. The example programs come preconfigured with the correct settings for BALANC3R and Gyro Boy if you use the LEGO EV3 Gyro Sensor. If you use the NXT HiTechnic Gyro, change the Choose Sensor setting on the Initialize My Block to 1.
  • The balance loop keeps the robot balanced. It measures and calculates the position and speed of the motors, and it determines the robot’s angular velocity (how fast it’s falling), as well as the robot’s angle relative to the ground. In turn, it uses this sensor information to calculate how to drive the motors in order to keep the robot up right. You won’t need to change any setting of the blocks in this loop.
  • The drive control loop controls the speed and steering of the robot as it drives around a room using a simplified move block. This is the part of the program that you can easily customize to create your own program.

Step 4: Running the basic example program

You are now ready to download the example program to your robot.

  • If you’ve built BALANC3R, begin with BALANC3R-Basics.
  • If you’ve built Gyro Boy, begin with GyroBoy-Basics.

To start the program:

  • Hold the robot upright with its wheels on the ground. Do not hold it tightly, but hold it loosely so that it’s just between falling forward and falling backward.
  • Select the program and start it with the center button on the EV3 brick.
  • You’ll first hear one beep. Keep holding the robot in place.
  • You’ll then hear a double beep. Now release the robot and let it balance.

Your robot should now repeatedly balance in place for 7 seconds, turn right for 7 seconds, and turn left for 7 seconds.

Follow these steps if the robot doesn’t balance:

  • If it doesn’t work on the first try, repeat the steps above a few times. After a while, you’ll know which is the correct “upright” starting position.
  • Do not try to “help” the robot balance. Of course you should catch the robot before it falls, but trying to keep it upright with your hands is counter-effective.
  • Verify that the cables have been plugged correctly according to the building instructions:
    • The two Large motors should be connected to ports A and D. (If you’ve accidentally interchanged them, that’s fine. The robot will confuse left and right turns, but balancing is unaffected.)
    • The Gyro Sensor should be connected to input port 2, regardless of which sensor you use.
  • Verify that you’ve mounted the Gyro Sensor correctly according to the building instructions.
  • Verify that the batteries are fresh.
  • Verify that you are using the latest EV3 firmware (1.06H or 1.06E as of this writing).

Step 5: Running the second example program

If you’ve successfully programmed your robot in the previous step, it’s easy to try out the other example program for your robot. The robot balances in exactly the same way, but the robot’s movements are a little more interesting:

  • BALANC3R-RemoteControl lets you control BALANC3R with the infrared remote, as shown in the video above. Just press the buttons on the remote to make the robot drive forward, backward, and turn. (You’ll figure out the controls quickly.) If you don’t press any buttons, the robot just balances in the same place.
  • GyroBoy-AvoidObstacles makes Gyro Boy drive around a room while backing up from obstacles, as shown in the video above. Before you run the program, make sure that the white beams of both the robot’s arms point downward. The program relies on this starting position to make sure the Ultrasonic Sensor doesn’t detect the floor as an ‘obstacle’ when the robot’s left arm points downward.

Step 6: Customizing the program

As you’ve learned earlier, the balance loop keeps the robot balanced while the drive control loop controls the robot’s speed and steering. The two loops run simultaneously, or at the same time. In the drive control loop, you use the Move My Block to specify the robot’s speed and steering, as shown below.

movemyblock-ref

The Move block makes the robot drive and steer. In this configuration, the robot drives forward (30) while turning to the left (-15).

The robot keeps driving or turning at the specified rate until you run the block again with different values for speed and steering. The figure below shows the Move My Block in action in the basic example program you ran in step 4. The first Move block sets both steering and speed to 0, which makes the robot balance in place without turning. Next, a Wait block pauses the loop for 7 seconds, keeping the robot in the same place. Then, a second Move block sets the steering value to 20, making the robot turn to the right. After another 7 second wait, the robot starts tuning left by setting the steering value to -20.

Controlling the robot with Move My Blocks in the drive control loop.

Controlling the robot with Move My Blocks in the drive control loop

Now that you’ve learned how to control the robot, it’s time to put your skills to the test with programming challenges. To solve these challenges, you can use the techniques from the LEGO MINDSTORMS EV3 Discovery Book, presented throughout chapters 1-9. Instead of using Move Steering blocks in On mode as in the example programs, you use Move My Blocks as shown above.

Challenges for BALANC3R:

  • Make your robot drive around a room while avoiding obstacles with the Infrared Sensor in Proximity mode.
  • Make your robot follow the Infrared Beacon. As you move the beacon around, the robot should follow you.
  • Attach the Color Sensor in front of the robot’s wheels, and make the robot follow a line. You can print line following tracks for your robot from here.

Challenges for Gyro Boy:

  • Make your robot drive in different directions based on the color it detects with the Color Sensor. To accomplish this, make the robot wait for the sensor to see either a yellow, red, green, or blue object. Then, make it drive in a certain direction for 3 seconds based on the detected color, before waiting for a new color signal.
  • Make your robot show different types of faces/eyes on the screen as you interact with its sensors. Show an angry face if you press the Touch Sensor, show a happy face when you trigger the Color Sensor, and so on.

Step 7: Making your own balancing robot

In the previous steps, you’ve made BALANC3R or Gyro Boy balance on two wheels, and you learned to control it with the Move My Block. Now that you’ve got the essential components working, you’re ready to customize both your robot and your program. For example, you can turn BALANC3R into a life-like humanoid with arms, and make it talk to you. Or, go crazy and make any EV3 vehicle balance on its rear wheels. What about a self-balancing F1 style race car? Whatever you make, let others know in the comments below. Happy building!

Step 8: Further reading

In order to make this tutorial accessible for everyone with an EV3 set, I didn’t cover the details of the balancing algorithm. Rather, the design of this program makes it possible to control the robot even if you don’t know exactly how the balancing mechanism works.

However, many papers have been written about self-balancing robots, and I encourage you to read more on the subject as you explore the details of the EV3 program provided on this page. The balancing algorithm in this program is based on a Bachelor thesis written by Steven Witzand, which provides a good overview of the subject, along with Java source code that implements the balancing algorithm. In turn, this paper relies on the design and algorithm used in the NXTway-GS by Yorihisa Yamamoto, which you can study for further detail.

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
31 Comments  comments 

31 Responses

  1. Cevinius

    Thank you for posting this! :D

  2. Tycho Van Malderghem

    where to put ultrasonic sensor giro boy (port)

    • I could tell you that, but why not check out the building instructions or the program? :)

      How is the port setting of the Ultrasonic Sensor block in the example program configured?

  3. David Wierwille

    Hey, Ian realy glad to read this.
    Tank you very much for this nice work!

  4. Yeochoon Park

    Thank you very much!
    Everyday, in everyway, You’re getting better and better~

  5. Bert

    THANKS Laurens.
    A bit of confusion though. In the ReadGyro block there is part of the program unconnected. Is that part redundant or should it be connected somewhere?
    Could not paste an example picture here to make it visible :-(

    • Ah, you’re right, thanks for spotting that.

      It must have been something I experimented with but I forgot to delete it in the end. Since it’s not connected it shouldn’t affect your program, but you can delete it if you like.

  6. Yeochoon Park

    Could you share your project file? ( .ev3p -> .ev3s )

  7. yongyuth phasukyued

    Thanks Laurens! If you have times and is not too much troubles. Could you please do another tutorial on Configuration Block and Balance Loop? I am new too this and really want to learn more on this.

  8. PODZIMEk Petr

    Hello,
    Great project I think.
    Program is quite easy to read.

    Well done.

    Thank you.

  9. Robin Newman

    Thanks for this Laurens
    A great project, and the balancing model works very well with the Lego Gyro Sensor. Now I must wrap a wet towel round my head and spend some time understanding the program. Thanks for the references to help with this. Did you find it difficult determining the kd ki and kp parameters for the pid?

    • Thanks Robin. The papers I referenced provide a way to calculate the constants, but I determined them by trial and error. However, I’ve been using these values since four years ago when I first wrote the program for the NXT. I’ve been tuning it since, and eventually I ‘translated’ it to EV3 to create this program.

      Note that the other four constants in the configuration block are also very important. They determine by how much each sensor value contributes to the overall motor control.

  10. Al

    Hello,

    Followed the building instruction for Banlanc3r, updated my firmware to 1.06E, downloaded your code into EV3.. checked everything twice!

    When I start both program for Balanc3r, I hear the single beep, then the two ones and then; the motor connected to port A goes -100% (backward)power continuous while the other is idle.

    Do you have any hint on what can be wrong?

    ———————-

    Hi,

    Forget about my previous mail…
    My gyro was dead :( , tried with a new one and all works now.

    No I need to tune the parameters…

    BR

    Al

    • The Gyro isn’t necessarily broken. It may just need to be recalibrated. To do this, unplug the sensor from the brick, and plug it back in. For the best results, this should be done while the robot lays flat on the ground or table, so that it doesn’t move during the calibration.

      I’ll add this to the troubleshooting section.

      The program does account for this by doing its own calibration, but the LEGO Gyro Sensor can be way off when it’s moving too much when you plug it in.

  11. Ronald

    I did not want to tear my car down so I used sensors and motors from my NXT kit. To make the Balanc3r work, I had to rearrange a few items in your building instructions. When I executed the program, got the one beep, then 2 beeps and then the Robot started to balance, waited for 7 secs and then went straight forward at high speed, fell down and then stopped with “ERROR” showing on the screen.
    Before delving into your program for learning,I will try your program using the formul3r car as the robot. If this fails, then I will finish reading your book and try building the robot according to your instructions.

  12. Ethan

    Thanks for posting this, I’m rather new to mindstorms. I’ve ben working on translating the HiTechnic balancing bot program(NXT-G) to (EV3-G). I’ll keep working on it and compare it to your program (which works great!!) and see if one is better than the other. Thanks again!!!!!!!!!!!!!! P.S I tried your program with a bot like the Educator bot made from #31313, and a different bot of my own design and it still worked!!!! Is it okay if I post the link to here on Mindstorms Community and pics of your bot?

  13. cobalt

    Great post. Thanks for your sharing.

  14. barry

    Has anyone successfully gotten the Gyro Boy to follow the IR beacon?

  15. cobalt

    As I have only one large motor is available for now, I made a single motor but 2 wheels robot base on BALANCE3R with small change on robot structure and program. It is able to keep stand stable and move forward/backward, but it cannot steer (of course it cannot). It is my first self-balance robot. Thanks Laurens!

    One question, the ReadEncoder block contains one parameter 57.3. What this value means? Does it depending on robot structure? i.e. the robot is higher and bigger than BALANCE3R do I need to change this value?

    • Cool to hear you made a modified robot.

      57.3 is 180 / PI. I use it to convert between degrees and radians. Whith the angles measured in radians, it is easier to calculate traveled distance and speed. (distance = turn angle * radius and speed = turn rate * radius.)

  16. Jimmy

    Hi
    Is there any source code for ev3 self balancing in.Net or Java?

  17. […] Laurens Valk’s article┬áI made my first self-balancing robot with using LEGO Mindstorms EV3. I made some small change on […]

  18. Chris

    What does the ERROR message mean on the Gyro Boy Program? Reply quick. Pls.
    My robot is not balancing and is always showing the error message 5 seconds after I start the program. I start it, it moves a bit then falls down and gives the error message.

  19. bob

    mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

  20. Devansh

    Hi Laurens,

    This is a great project !

    I’ve been trying to make sense of the algorithm and implement it in a Matlab/Simulink environment. I just have a small doubt about the algorithm:

    Where is the variable ‘old_steering’ defined ? I see that its being read in the ‘SetMotorPower’ block but can’t find where its been written.

    Thanks,
    Devansh

  21. rhiannon

    how do you make it spin and then move forward

  22. Pmb

    Hi Laurens, I ported most of your basic balanc3r code to RobotC and posted it over in the RobotC forum, thank you for a great job, it worked almost out of the box after porting it… :)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>