Robot drawn triangles

(with given side lengths)

The aim of the lesson is to show the correlation between IT (programming) and geometry. Students practise drawing triangles already in primary school. They need a ruler and a compass to do it. Their task is to program a robot that would draw triangles? The robot has no compass nor ruler. After reflection, it turns out that it is a difficult and multi-stage task for the robot to do.

image0

Prerequisite

What students should know before

  • mathematical formulas for the areas of triangles (e.g. Heron’s formula),

  • properties of triangles (triangle inequality),

  • trigonometric functions,

  • basics of programming EV3 robots in Python,

  • law of cosine (it is not necessary).

Time constraints:

  • 3-4 hrs

Preparing For This Tutorial:

  • The LEGO Mindstorm EV3 Robot that coincides with this tutorial comes from building specific sections found in the LEGO Mindstorm Education Core Set building instructions. You will need to build the main body for the robot (I’ll refer to use the Driving Base), plus a gyro sensor.

Effects

Mathematic

After these lessons students are able to:

  • use of mathematical formulas in programming,

  • search for analogies in the program code,

  • test and search for errors in the program’s code,

  • do modification of algorithms,

  • remove programming and algorithmic errors.

Computer science - This lesson will tell students how to create the program using a gyro sensor.

Exercise

How to program a robot to draw triangles?

Example solution

We need to determine the length unit for the robot. The motor rotation at a specific speed (unit of length) will serve to measure the length. Instead of using a compass, we calculate the interior angles of the triangle. The robot will rotate with the supplementary angles.

Input dateimage1

How do I input the side lengths into the EV3 Robot?

We can use the display and buttons of the Intelligent Brick.

Below is an example of input variable values.

#!/usr/bin/env python3

# import

from ev3dev.ev3 import \*

from time import sleep

import ev3dev.auto as ev3

import os

os.system('setfont Lat15-TerminusBold14')

# variable: side of the triangle, button, screen.

a = 5.

bt=Button()

sc=Screen()

# Display of variable a on the screen and its modification by means of
the buttons.

# Warning. Can variable a be less than 1?

print('a =',a)

while bt.enter==False:

  sleep(.2)

if bt.left==True:

  a=a-1

  print(' a =',a)

if bt.right==True:

  a=a+1

print(' a =',a)

sleep(1)

Comments

When entering the length of the sides, remember that:

  1. side lengths ought to be positive numbers,

  2. the sum of the length of two sides ought to be greater than the length of the third side.

We can restrict the second condition to the introduction of the third variable c.

Drawing the sides of a triangle

The robot will move at a constant forward speed. In this way it will measure the right side’s length.

Example

#!/usr/bin/env python3

# length side

a=5

# unit of measure (motor rotation)

r=360

# drawing the first side of a triangle

mr.run_to_rel_pos(position_sp=r*a, speed_sp=500, stop_action="brake")

ml.run_to_rel_pos(position_sp=r*a, speed_sp=500, stop_action="brake")

sleep(a)

Calculation of interior angles of a triangle

To calculate the interior angles of the triangle, we can use the formula for the triangle area..

\(F = \frac{1}{2}\ a\ b\ sin(x)\)

after the transformations of the formula we get

\(sin(x) = \frac{2F}{\text{ab}}\), we get \(x = arcsin\left( \frac{2F}{\text{ab}} \right)\).

We will calculate the area from Heron’s formula.

#!/usr/bin/env python3

#Calculating the circumference and field using the Heron's formula.

p = (a+b+c)/2

F = math.sqrt(p*(p-a)*(p-b)*(p-c))

print('perimeters =',2*p)

print('area =',round(F,2))

# Calculating the angle of the triangle from the formula of triangle area.

# Better this angle to calculate from the law of cosines. (Why?)

a1 = math.degrees(math.asin((2*F)/(a*b))) # calculated triangle angle

rra=180-a1 # robot rotation angle

The above method uses basic formulas, but it is not correct for obtuse angles. We can modify the program code to check if the angle is an obtuse angle. Then we would calculate the correct angle value.

The second way is to calculate the angle from the law of cosine.

\(c^{2} = a^{2} + b^{2} + 2abcos(x)\)

after the transformations of the pattern

\(cos(x) = \frac{c^{2} - a^{2} - b^{2}}{2ab}\), we get \(x = arccos\left( \frac{c^{2} - a^{2} - b^{2}}{2ab} \right)\).

In this way, we get the correct solution.

a1 = math.degrees(math.acos((c^2-a^2-b^2)/(2*a*b))) # calculated triangle angle

rra=180-a1 # robot rotation angle

Lesson with students

(for 3-4 hours)

The above lesson can be carried out in three different ways.

For students with good programming skills, giving them only minor suggestions.

We expect from the student:

  • independent and creative programming skills,

  • using and modifying known mathematical formulas,

  • solving algorithmic problems.

For students who like programming challenges, giving them only snippets, not all program code.

We expect from the student:

  • searching the analogy,

  • doing the modifications in writing the program code,

  • using mathematical formulas.

  1. Giving the pupils auxiliary issues and the full program code. We expect the student to test the program using different lengths of side.

Students should point out problems when entering numbers:

  • negative,

  • not satisfying the triangle inequality,

  • which are lengths of the sides of the obtuse triangle.

Students try to remove errors from the above code on their own.

Next Section - Lego robots in physical experiments