Lego robots in physical experiments¶
Introduction¶
Presented task is one of many problems solved during realization of the project PROBOT at III LO in Chorzów, Poland. The main task undertaken by the physicist employed at this school was to use Lego robots for execution measurements and calculations in physics. The review of all achievements in this field is included in the movie:
Most of the experiments need nonstandard sensors measuring different physical quantities like: force, temperature, magnetic field, acceleration and angle. Because these sensors are not in widespread use and may be difficult to purchase, from among all these experiments it was chosen as the simplest one, using the most common nonstandard sensor: thermometer produced by Lego. If your school isn’t equipped with it, it is easy to replace the thermometer with an ultrasonic sensor. In that case the task will look a little bit strange, but still will teach the same mechanisms and programming solutions like the original version with a temperature sensor.
The main task of this lesson is to teach how to control the angular speed of a fan, position of a valve, sound emitted by the brick and LEDs colors, according to the temperature measured by the thermometer (or distance measured by ultrasound sensor).
Example solution is presented in another YouTube movie:
Prerequisites¶
What students should know before
Using Lego bricks with Python environment on SD cards
Syntax of loops and conditional instructions in Python
General knowledge of objects and methods
General knowledge of declaring motors and sensors
How to define a linear function.
Time constraints:
It is a simple task. Two 45-min. a lesson should be enough.
Effects¶
What students will learn:
Declaring nonstandard sensors
Writing nested instructions
Writing formulas with linear functions
Controlling the motor speed
Controlling the brick sound
Controlling the LEDs color
Exercise¶
Equipment needed:
Intelligent EV3 brick with SD card including Python environment.
Medium servomotor (quicker) for driving the fan.
Large servomotor (stronger) for driving the valve.
Two touch sensors for checking the valve position.
Temperature sensor. If your school has no access to temperature sensor, ultrasonic sensor may be used as a substitution.
Cables.
Set of bricks, including propellers.
Construct and program a machine consisting of a fan, valve, two touch sensors and one temperature sensor. In case of lack of a temperature sensor, an ultrasonic sensor may be used instead. The behavior of fan, brick and valve should depend on the temperature interval as follows:
T < 20°C. The fan is motionless, safety valve is closed, brick is silent and LEDs shine green.
20°C <= T < 30°C. Valve is still closed, brick is silent, LEDs shine orange. The angular speed of the fan rises by 1% with every 0,1°C over 20°C.
T >= 30°C. The fan spins around with maximum power. Safety valve opens. LEDs blink red. Sound alarm is emitted.
All of it should work as long as no brick button is pressed. After pressing any button, both motors should stop and LEDs color should be set green. The brick should verbally confirm that the program is terminated.
If an ultrasonic sensor is used as a replacement, it should measure the distance d of the “enemy”. Try to use the general object Sensor() in the program instead of UltrasonicSensor(). The behavior of the machine should be defined by analogy, treating the smaller distance as the higher temperature, ie:
d > 30 cm. The fan is motionless, safety valve is closed, brick is silent and LEDs shine green.
30 cm >= d > 20 cm. Valve is still closed, brick is silent, LEDs shine orange. The angular speed of the fan rises by 1% with every 1 mm below 30 cm.
d <= 20 cm. The fan spins around with maximum power. Safety valve opens. LEDs blink red. Sound alarm is emitted.
Example solution¶
Example construction of the robot:
Most cables should be fastened or hidden.
The temperature sensor should be left free (not fixed), so that it can be easily put into hot/ cold water for quick change of temperature.
The temperature sensor is connected to port in1. The touch sensors are plugged into ports in3 and in4.
The valve has a common axle with a lever, which presses one touch sensor when the valve is open and the second touch sensor, when the valve is closed.
The entire program with comments:
#!/usr/bin/env python3
from ev3dev.ev3 import \*
# Initialising the program
Sound.speak('Hello') # Confirmation that program started
motor_fan = MediumMotor('outA')
motor_valve = LargeMotor('outC')
btn = Button()
ts1 = TouchSensor('in3') # Checks if valve is closed
ts2 = TouchSensor('in4') # Checks if valve is open
temps = Sensor('in1') # Temeperature sensor
# Main program working until any brick button is pressed
while not btn.any():
temp = temps.value() # Reading the temperature
if temp < 200: # T < 20*C
motor_fan.stop(stop_action='brake')
while ts1.value() == 0: # Close valve if open
motor_valve.run_forever(speed_sp=100)
motor_valve.stop(stop_action="hold")
Leds.set_color(Leds.LEFT, Leds.GREEN)
Leds.set_color(Leds.RIGHT, Leds.GREEN)
elif temp < 300: # 20*C <= T < 30*C
while ts1.value() == 0: # Close valve if open
motor_valve.run_forever(speed_sp=100)
motor_valve.stop(stop_action="hold")
rotspeed = (temp-200)*10
motor_fan.run_forever(speed_sp=rotspeed)
Leds.set_color(Leds.LEFT, Leds.ORANGE)
Leds.set_color(Leds.RIGHT, Leds.ORANGE)
else: # T >= 30*C
while ts2.value() == 0: # Open valve if closed
motor_valve.run_forever(speed_sp=-100)
motor_valve.stop(stop_action="hold")
motor_fan.run_forever(speed_sp=1000)
Leds.set_color(Leds.LEFT, Leds.RED)
Leds.set_color(Leds.RIGHT, Leds.RED)
Sound.beep() # Alarm!
# Finalising the program when any brick button is pressed
Sound.speak('This is the end')
motor_fan.stop(stop_action='brake')
motor_valve.stop(stop_action='brake')
Leds.set_color(Leds.LEFT, Leds.GREEN)
Leds.set_color(Leds.RIGHT, Leds.GREEN)
If ultrasound sensor is used as a substitution of the thermometer, the following fragments of the code should be replaced:
Thermometer version |
Ultrasonic version |
---|---|
temps = Sensor(‘in1’) |
us = Sensor(‘in1’) |
temp = temps.value() |
dist = us.value() |
if temp < 200: |
if dist > 300: |
elif temp < 300: |
elif dist > 200: |
rotspeed = (temp-200)*10 |
rotspeed = (300-dist)*10 |