Preforming the Polygon Shuffle with a EV3 Gyroscope

EV3-G    Send article as PDF   

A polygon is any 2-dimensional shape formed with straight lines. Triangles, quadrilaterals, pentagons, and hexagons are all examples of polygons. The name tells you how many sides the shape has. A polygon is any shape made up of straight lines that can be drawn on a flat surface, like a piece of paper. Such shapes include squares, rectangles, triangles and pentagons, but not circles or any other shape that includes a curve.

There are two main types of polygon – regular and irregular. A regular polygon has equal length sides with equal angles between each side. Any other polygon is an irregular polygon, which by definition has unequal length sides and unequal angles between sides.


For the purposes of this article we will be only working with Regular Polygons.

To get a Robot to trace out the shape of any Regular Polygon we need to be able to calculate the Interior Angles of the said Polygon. An Interior Angle is an angle inside a shape, as shown below.

Definition: The angles on the inside of a Polygon formed by each pair of adjacent sides.

The interior angles of a polygon are those angles at each vertex that are on the inside of the polygon. There is one per vertex. So for a polygon with N sides, there are N vertices and N interior angles. For a Regular Polygon, by definition, all the interior angles are the same.

Sum of Interior Angles:

The interior angles of any polygon always add up to a constant value, which depends only on the number of sides. For example, the interior angles of a pentagon always add up to 540°, no matter if it regular or irregular, convex or concave, or what size and shape it is. The sum of the interior angles of a polygon is given by the formula: where ,  is the number of sides.

So for example:

  • A square     has 4 sides,     so interior angles add up to 360°
  • A pentagon     has 5 sides,     so interior angles add up to 540°
  • A hexagon     has 6 sides,     so interior angles add up to 720°
  • … etc

In Regular Polygons:

For a regular polygon, the total described above is spread evenly among all the interior angles, since they all have the same values. So for example the interior angles of a pentagon always add up to 540°, so in a regular pentagon (5 sides), each one is one fifth of that, or 108°. Or, as a formula, each interior angle of a regular polygon is given by: where n  is the number of sides: Interior Angle = ((n-2)*180/n)

A rule of polygons is that the sum of the interior angles always equals 180°, thus the Interior Angle = 180°-((n-2)*180°/n).

Lets prove this for a regular octagon (8-sides):

First we must figure out what each of the interior angles equal. To do this we use the formula:

((n-2)*180)/n  where n is the number of sides of the polygon. In our case n=8 for an octagon, so we get:

((8-2)*180)/8 => (6*180)/8 => 1080/8 = 135 degrees. This means that each interior angle of the regular octagon is equal to 135 degrees.

Each exterior angle is the supplementary angle to the interior angle at the vertex of the polygon, so in this case each exterior angle is equal to 45 degrees. (180 – 135 = 45). Remember that supplementary angles add up to 180 degrees.

And since there are 8 exterior angles, we multiply 45 degrees * 8 and we get 360 degrees.

This technique works for every Polygon, as long as you are asked to take one exterior angle per vertex, n.

Thus the Robot’s Turn Angle to make a Polygon of ‘n’ Sides = 180°-((n-2)*180°/n).

Now we have the Mathematics sorted, lets put this knowledge into EV3-G Code. Shown below is the “Trace Out Polygon” program which is written with LEGO’s EV3-G Programming Environment for the LEGO Mindstorms EV3 & NXT platforms.

“Trace Out Polygon” ProgramTrace Out Polygons Program

To simplify the programming I have divided it up using the following three MyBlocks which do all the work, and the Menu System shown above.

“Go Straight” MyBlock Go Straight MyBlock

The “Go StraightMyBlock, uses π (PI) to calculate the Circumference of the Robot’s Wheels, given that the Diameter is printed on the wall of the tyre. Circumference = π X Diameter, or C=πD. The Code uses two Variable Blocks labeled Wheel Diameter and Travel Distance. The Wheel Diameter is entered into a Multiplication Block to calculate the wheel circumference as stated above.

The second Variable Blocks labeled Travel Distance is used to calculate the number of Wheel Rotations required to travel the desired distance. Wheel Rotations = Travel Distance / Wheel Circumference. The output of the division is fed into the Rotations Tab of the Move Tank Block to have the Robot move the desired distance.

“Gyroscopic Turn” MyBlock Gyroscopic Turn MyBlock

The “Gyroscopic TurnMyBlock uses the LEGO Mindstorms Gyroscopic Sensor to allow more accurate and reliable turning of the Robot. The code uses the Variable Block labeld TurnAngle to hold the desired angle of the turn. This value can be a positive or negative value. A Negative value will cause the Robot to Turn Left, and a positive value causes the Robot to Turn Right. The next Block is the Gyroscope Reset Block which Zeros all of the Gyroscopes Values output values. A Wait Block follows, which allows the Gyroscope to Zero Correctly without the Robot Moving.

Next is a Move Steering Block which starts the Robot Turning. Following this Block is the Wait “Gyro Sensor” Block set to monitor the Change of Angle. This Block Waits until the Gyroscope has registered that the Robot has turned through the TurnAngle which is input to the Amount Tab of the Block. When the Robot has turned through the alloted angle, a Move Tank Block is set to stop the B+C Motors.

“Polygon Pattern” MyBlock Polygon Pattern MyBlock

The “Polygon PatternMyBlock uses the  Interior Angle = 180°-((n-2)*180°/n) formula mentioned at the tope of the article to get the Robot to Trace Out an Requested Polygon on the floor.

A Variable Block labeled, “Number of Sides” which holds the number of sides of the requested Polygon. Also a Constant Block is used to hold the value for the Length of a Sides for the polygon, and is passed to the “Go Straight” MyBlock. The value in the “Number of Sides” Variable Block is passed to a Math Block in ‘Advanced Mode’. The Math Block calculates the Interior Angle for the Polygon requested, and passes the value to the “Gyroscopic Turn” MyBlock.

Both the “Go Straight” MyBlock and the “Gyroscopic Turn” MyBlock are placed in a Loop Block which repeats their function the same number of times that there are sides to the Polygon.  The Variable Block labeled, “Number of Sides“, is connected to the Count Tab of the Loop Block to allow the Loop’s contents to be repeated the correct number of times.

Menu System of the “Trace Out Polygons” Program Trace Out Polygons Menu

Getting the Menu System of the “Trace Out Polygons” Program working proved quite a task, and took me about four hours in the end. The prim function of the Menu System is to allow the uses to select what shape of a Polygon to trace out on the floor. Basically you use the Up and Down Buttons on the EV3 Programmable Brick to increase or degrease the number of sides for a Polygon. The Minimum Number of sides for a Polygon is 3, as it the case for a Triangle. In this case I have set the Maximum Number of sides to 8, which equates to a Octagon.

A Wait Block is used wait for a Button Press on the EV3 Programmable Brick. Primarily we are only interested in the UP, Down and Select Buttons. The output of the Wait Block is fed into a Switch Block which allows the Number of Sides for the Polygon to be increase, or decreased, and choose to Trace Out the selected Polygon. The values of the  EV3 Programmable Brick’s Buttons are as Follows:

  • 0‘ = NO Buttons Pressed
  • 1‘ = LEFT Button Pressed
  • 2‘ = MIDDLE Button Pressed
  • 3‘ = RIGHT Button Pressed
  • 4‘ = UP Button Pressed
  • 5‘ = DOWN Button Pressed

For our purposes, we only need to monitor the 2, 4, and 5 Buttons Status which determines what branch of the Switch Block is taken. Branch ‘2’ is the preform Polygon Trace option. Branch ‘4’ is used to Increase the number of sides of the Polygon, whilst Branch ‘5’ Decreases the number of sides.There is also Error Correction included in Branches ‘4’ & ‘5’. The code also corrects for an attempt to input less than three sides, or greater than eight sides.

Audio Visual Feedback for the “Trace Out Polygons” Program Trace Out Polygons A/V Feedback

Audio Feedback for the “Trace Out Polygons” Program makes navigating the Menu System easier without the need to view the Text Menu on the Mindstorms Programmable Brick. Depending on the Value of the Variable Block labeled, “Number of Sides” to what part of the Switch Block is executed. Each part of the Switch Block contains A Text Block showing the name of the Polygon and and a Sound Block which produce an audible announcement of the Number of Sides the Polygon has.


Download a copy of the “Trace Out Polygons” EV3-G Program:

Download a copy of the EV3-G My Blocks:


Previous Post

Powering the LEGO Mindstorms Brick from a 9v Plugpack

I have been asked a number of times too how I power my Stationary and Development Robots from a 9 Volt, 5 Amp D.C. Plugpack connected to the Mains Power ... Read more

Next Post
Animal Allies Logo

FLL Animal Allies: Climbing the Bio-mimicry Wall with Robot

After setting my new 2017 group of FIRST LEGO Leage (FLL) Students the "Bio-mimicry Wall Climbing Task" from the 2016 Animal Allies Challenge, I thought I should prove that it is possible ... Read more

Short URL:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!