Reader small image

You're reading from  Learning Lego Mindstorms EV3

Product typeBook
Published inJan 2015
Publisher
ISBN-139781783985029
Edition1st Edition
Right arrow
Author (1)
Gary Garber
Gary Garber
author image
Gary Garber

Gary Garber teaches physics, math, and engineering at Boston University Academy. Gary is the president of the New England Section of the American Association of Physics Teachers and has led dozens of professional development workshops in education at both the local and national levels. Gary runs the Boston University FIRST Robotics program. He has run and hosted numerous robotics workshops in VEX, Tetrix, and LEGO platforms. He has run dozens of LEGO robotics tournaments and spoken on robotics education at both local and national conferences. His robotics team has worked with Engineers Without Borders, NASA, and the National Science Teachers Association on a variety of engineering and education projects. He is currently an educational consultant, working to develop new software tools for the classroom, at the Tufts Center for Engineering Education and Outreach, which is a pioneer in LEGO Robotics Education. He is the author of Instant LEGO MINDSTORMS EV3, Packt Publishing. He currently resides in Massachusetts, US. When he is not playing with LEGO, robots, or toy trains, he enjoys spending time with his wife, Catalina, and their two children, Alejandro and Leonardo.
Read more about Gary Garber

Right arrow

Chapter 12. Advanced Robot – Gyro Boy

In this chapter, we will analyze one of the impressive programs that come in the Education Edition of the EV3 software. We will look in depth at the program for the model called Gyro Boy. Gyro Boy is a two-wheeled upright balancing robot. Gyro Boy uses a combination of the Motor Rotation sensors working with the Gyro Sensor to balance upright. The robot also responds to commands and feedback from the Color Sensor and the Ultrasonic Sensor. This model is an amazing example of what we can build with the LEGO MINDSTORMS kit. However, the LEGO supplied program is lacking in documentation and a full explanation of the algorithms. LEGO provides the user with complex example models, such as Gyro Boy, as an inspiration of what you can build and design with LEGO MINDSTORMS. My goal in this chapter is to close the gap and explain how this model works. In this chapter, we will:

  • Explain the concept behind a two-wheeled upright balancing robot

  • Introduce the Gyro Boy...

Concept of a balancing robot


The inspiration for Gyro Boy is the Segway robot. The LEGO community has been building LEGO MINDSTORMS two-wheeled balancing robots almost since the introduction of the Segway by Dean Kamen. The real Segway is a two-wheeled motorized personal transport device that uses several Gyro Sensors to detect the pitch and pitch rate of the vehicle. The real Segway uses additional Gyro Sensors for redundancy and to detect roll (turning left and right). The physics model for a Segway is what we call the inverted pendulum problem. A simple pendulum has its pivot point at the top and the bob swings back and forth below the pivot. An inverted pendulum has this pivot point at the bottom with the bob oscillating above the pivot. We can think of a metronome or even a human being as simple inverted pendulums. In the case of the Segway, the goal is to minimize the oscillations and keep the vehicle upright and then provide movement forward, backwards, as well as turn left or/and...

The Gyro Boy model


The EV3 Gyro Boy model developed by LEGO builds on the Segway designs of the numerous LEGO hobbyists. Not only does Gyro Boy balance itself using a Gyro Sensor, but it also takes commands using the Ultrasonic Sensors and the Color Sensors.

The mechanical design for Gyro Boy has many impressive features. The building designs for Gyro Boy can be found embedded in the Educational Edition of the EV3 software. If you have the Home Edition of the software, you can download the building instructions from various websites such as RobotSquare by Laurens Valk at http://robotsquare.com/. The design is well balanced and comes with a color-coded stand to get the robot started and send color commands to the robot. There is also a medium motor in the model that can be used to move the arms of the model. The medium motor uses the Technic Knob Wheel as a type of crown gear to control both arms. One arm has a Color Sensor attached to it, and the other arm has the Ultrasonic motion sensor...

Programming bugbears


The EV3 Programs provided by LEGO with the software are highly impressive, but are nearly impossible to understand. The LEGO tutorials do accomplish their goal of introducing the reader to the blocks and vernacular of EV3 programming. The provided models demonstrate the potential of the kit for the advanced hobbyist. This is a great marketing tool for LEGO when one watches videos of the extremely cool models that can be built with EV3. However, the programs are entirely lacking in any kind of documentation. One of the main reasons I wrote a book for the intermediate level user is this need for explanation of these great programs and to help bridge the gap between the rudimentary tutorials and the advanced models.

There are great advantages to visual programming languages such as LabVIEW, Scratch, and the EV3 LEGO MINDSTORMS software. They can be just as powerful as line code, but examining the main code for the Gyro Boy program in the following screenshot, we can see...

The main program


The Gyro Boy project does contain several My Blocks in addition to the main program. I will explain these My Blocks later in the chapter. In the project, the main program is called 001. There are two separate parts of the 001 program. There is the main program, Loop M, which controls the robot, and the Control Loop, BHV, which looks for feedback from the sensors.

We will first examine the main program Loop. In the following screenshot, we can see an Infinite Loop block titled M. Inside the loop, several My Blocks are called to execute the various subroutines. Instead of one long horizontal line of blocks as shown in the previous screenshot, I have rearranged the Loop block M into several rows. In this way, we can view the entire code on one screen.

The main program loop M repeats each time the robot is set up upright to begin running. The RST My Block resets several variables that will be used in the program to zero, and also resets the Gyro Sensor, the motor shaft encoders...

The control program


The second part of the program, the control loop BHV, has its own Start block, which allows this part of the program to run in parallel to the main programming loop. At the beginning, the st Variable block sets the robot state to 0 or resting. The program then enters the BHV Loop block or the robot behavior loop.

A numeric switch block examines which of the three robot states the robot is in, with 0 for resting state, 1 for waking state, and 2 for the active state. The resting state (state 0) is the default case of the switch block. In this state, the variables blocks Cdrv and Cstr are set to 0. The Cdrv variables block (Control drive) is used to control the speed at which the robot drives either forward or backwards. The Cstr variable block (Control steering) is used to control in which direction the robot steers. Setting both to 0 will result in no motion by the robot.

After the Gyro Sensor has been calibrated by the gOS My Block in the main program, the control state...

The RST My Block


The RST My block resets all of the variables to their starting values and resets the readings on all of the Gyro and Motor Rotation sensors. The main programming loop starts each and every time the robot is set upright. Since the RST My Block is inside of the main programming loop, it is not merely an initialization block that is only run at the beginning of the entire program, but every time that the robot is set upright it goes to enter the balancing loop. The My Block begins by resetting the values for the shaft encoders on both of the large motors. Next, the subroutine resets the value for the Gyro Sensor. When you look at the Timer Sensor block, you should note the ID on the Timer block, which indicates that this is the second Timer block, not the first Timer block. This timer is used to detect when the robot has fallen over. Since you cannot name timers, a comment in the code would have been really helpful here. Numerous numeric variable blocks are set to zero. There...

The gOS My Block


All gyros drift over time. The gOS My Block measures this drift and the resulting bias error in the rate of the Gyro Sensor. As a FIRST LEGO League tournament host, one of the largest complaints about the EV3 Gyro Sensor I hear about is the drift in the sensor readings. If you look at the HiTechnic sensor block, you may notice an input for a bias offset. Even after using the Gyro Sensor reset block, the Gyro Sensor may still not read 0. If you change the mode of the Gyro Sensor while it is perfectly still, it will reset to 0, but that is not done in the Gyro Boy program. The gOS My Block compensates for this bias. You can think of this as a DC offset that you may see in electrical signals.

The gOS My Block measures the Gyro offset and then subtracts this value from the calculations used later to keep Gyro Boy balanced. The robot needs to be completely stationary while the offset is determined. This could be fixed by adding a one-time delay at the start of the program. A...

The GT My Block


The GT My Block uses a Timer block to measure the duration of a single loop iteration. The timer value is used to determine the value of the tInt variable block, which is the average period of time of one iteration of the balance loop, over which the Gyro rates are integrated to determine the Gyro position. tInt is also used to calculate the derivative of the motor position to determine the motor rate. Because the rate at which the program executes may vary, the integration time will vary. Thus, the GT My Block allows us to compensate for any affect on calculations in other parts of our program.

The variable block cLo is a loop counter variable. Whenever the main programming loop runs, the loop counter will be set to zero in the RST My Block. Thus, the compare block and the logic switch block will set the tInt variable block value to 0.014 seconds and reset timer 1 with the reset timer block. After the switch block, the index of the loop counter cLo will be increased by...

The GG My Block


The GG My Block gets Gyro Sensor feedback and calculates values to store to the Gyro rate and angular position variables. The variable block gSpd stores the current rate of rotation of the Gyro Sensor after accounting for the offset bias. The calculation is not quite as simple as subtracting the bias as we can see from the detailed equation inside the Block Text Field of the Advanced Math block. A new Gyro offset is actually calculated from 99.95 percent of the old gOS variable block and 0.05 percent of the current Gyro rate. This is to account for increased bias over time. It is this new Gyro offset that is subtracted from the current Gyro Sensor block value to return the value for the gSpd variable block.

As opposed to measuring the angle value of the Gyro Sensor directly, the value of the gAng variable block is calculated by integrating the value of the Gyro rate. The value of the gSpd variable block is multiplied by the unit of time over which it is integrated, is equivalent...

The GM My Block


The GM My Block gets motor shaft encoder sensor feedback and calculates values to store to the motor position and motor speed variables. The GM My Block does not measure the motor speed directly in the same way the GG My Block does. Instead, it calculates the derivative of the motor positions, or how much the values of the motor positions have changed over the time of the balance loop iterations. The motor position is almost a direct read from the shaft encoders with a slight tweak to account for historical variations between the two motors due to the turning of the robot. Having the current motor positions and speeds will become important when trying to calculate the needed accelerations to the motors to maintain an upright position.

The motor speed calculation begins by recalling the sum of the two motor shaft encoder values from the previous iteration using the mSUM variable block. Although the mSUM variable block is written with this sum, if you trace the data wires,...

The EQ My Block


The EQ My Block provides an equation that controls the power of the motors. This equation determines if the robot needs to accelerate to stay upright. We are solving the inverted pendulum problem with this calculation. By the end of the EQ My Block, a new value is generated for the pwr variable block (the power variable).

A series of three Advanced Math blocks are used to calculate the value of the power variable, which in turn leads to the control of the motors. Although the Cdrv variable block affects how we control whether the robot drives forward or backwards, it is the value of the pwr variable block that sets the level of motor speed to keep the robot upright. Essentially, we are trying to determine whether the robot needs to accelerate to stay upright. If the Advanced Math block could handle more inputs, then only one Advanced Math block would be needed. The value of the power variable is based on input from several numeric variable blocks, including the Cdrv, time...

The cntrl My Block


The cntrl My Block is used to control the left and right motors and to update the mPos variable block. This updating of mPos is identical to what was done in the EQ My Block, where the motor position is calculated by integrating the speed. The Math block multiplies the tInt variable block by the Cdrv variable block. This factor is subtracted from the previous value of the mPos variable block to return a new value for the motor position. This new value for mPos is used for the next run of the balance loop.

The program takes the settings from the pwr variable block as the base power level for each motor. The code then uses the Math blocks to either increase or decrease the power to each motor so that the robot can turn. Even for turning, you increase the power of one motor and decrease the power of the other. The multiply Math block multiplies the Cstr variable block, the control steering variable by 0.1, so it has a lesser influence when it is added (or subtracted) to the...

The CHK My Block


The CHK My Block is used to make a check to see if the robot is upright or has fallen down. If the pwr variable block has an absolute value that is less than 100, timer 2 is reset. When the timer switch block measures a reading less than 1, then the state of the ok variable block is not touched and the balance loop keeps running. The initial state of the ok variable block was false. If the pwr variable block has an absolute value that is equal to 100 (we know it cannot be greater), timer 2 is not affected (thus having a larger value), and the ok variable writes a true statement, which causes the balance loop to end.

Looking in more detail, if the logic switch block receives a true value, then Timer Sensor block 2 is reset. Lastly, you saw a Timer Sensor block 2 during the RST My Block. So timer 2 was reset at the beginning of the main programming loop. The compare switch block will change the logic variable block ok to a true condition if timer 2 is greater than 1. If you...

Summary


In this chapter, we examined an impressive program provided by LEGO, the Gyro Boy. You saw many examples of some programming shortcomings and why there is a need for order and hierarchy in visual programming along with sufficient documentation. The Gyro Boy program shows the potential of what can be done with the LEGO EV3 kit, particularly with advanced programming techniques.

Although you might not think that the EV3 would change much over time, I have seen the evolution of this kit since I first played with the prerelease alpha versions of the EV3 in the summer of 2012. Some of those early pieces were made on a 3D printer and the people from LEGO Education showed me the trick of calibrating the Gyro by physically unplugging it. We now know you can calibrate the Gyro by changing the Gyro modes with sensor blocks. You will not find this trick in the Gyro Boy program, which leads me to think that this was a later development. There have been subtle unannounced hardware changes. For...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Lego Mindstorms EV3
Published in: Jan 2015Publisher: ISBN-13: 9781783985029
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gary Garber

Gary Garber teaches physics, math, and engineering at Boston University Academy. Gary is the president of the New England Section of the American Association of Physics Teachers and has led dozens of professional development workshops in education at both the local and national levels. Gary runs the Boston University FIRST Robotics program. He has run and hosted numerous robotics workshops in VEX, Tetrix, and LEGO platforms. He has run dozens of LEGO robotics tournaments and spoken on robotics education at both local and national conferences. His robotics team has worked with Engineers Without Borders, NASA, and the National Science Teachers Association on a variety of engineering and education projects. He is currently an educational consultant, working to develop new software tools for the classroom, at the Tufts Center for Engineering Education and Outreach, which is a pioneer in LEGO Robotics Education. He is the author of Instant LEGO MINDSTORMS EV3, Packt Publishing. He currently resides in Massachusetts, US. When he is not playing with LEGO, robots, or toy trains, he enjoys spending time with his wife, Catalina, and their two children, Alejandro and Leonardo.
Read more about Gary Garber