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 no ruler. After reflection, it turns out that it is a difficult and multistage task for the robot to do.
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 and add a gyro sensor and a pen to draw a line  photo no. 3 movie:

 In the photos no 1 and 2 we present our own construction movie:

Photo 1
Photo2
Photo 3
Time constraints:
34 hrs
Exercise¶
How to program a robot to draw triangles?
Theory¶
Introduction¶
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 date¶
We can use the display and buttons of the Intelligent Brick to input variable values representing sides.. We start with value equals 5 and using left or right buttons we are able to increase or decrease this value.Of course you could input the length of the side using the input function and read it from the keyboard.
#!/usr/bin/env python3
# import
from ev3dev.ev3 import \*
from time import sleep
import ev3dev.auto as ev3
import os
os.system('setfont Lat15TerminusBold14')
# 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=a1
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:
side lengths ought to be positive numbers.
the sum of the length of two sides ought to be greater than the length of the third side.
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 measure of a triangle’s interior angles, we can use the area formula of the triangle.
\(F = \frac{1}{2}ab\ sin(x)\)
after the transformations of the formula
after transforming the above 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.
# Calculating the angle of the triangle from the formula to the 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=180a1 # robot rotation angle
Short help on programming¶
Commands/functions needed for the exercise
Commands necessary to move.
#!/usr/bin/env python3
mr = LargeMotor('outB')
Create a new instance of the class  Plug a large motor on port B. Next turn through 360° at speed 900 and optionally apply a ‘hold’ or ‘break’.
#!/usr/bin/env python3
m.run_to_rel_pos(position_sp=360, speed_sp=900, stop_action="hold")
m.run_to_rel_pos(position_sp=360, speed_sp=900, stop_action="break")
Properties from Button class
#!/usr/bin/env python3
bt=Button()
bt.enter  gives the information if central button is pressed
bt.left (bt.right) gives the information if left (right) button is pressed
Connect gyro sensor to any sensor ports
#!/usr/bin/env python3
gy = GyroSensor()
Put the gyro sensor into ANGLE mode.
#!/usr/bin/env python3
gy.mode='GYROANG'
Read the value from gyro sensor
#!/usr/bin/env python3
gy.value()