Brownian motion with Lego Mindstorm EV3¶
We chose to treat the Brownian motion as an interdisciplinary argument because it allows us to connect topics such as probability and statistics to physical phenomena ( thermodynamics). Not less important are the “ Random Walk’’ applications to the field of biology, economics and social sciences.The aim of our didactic activity is to implement two programs in Python code using Lego Mindstorm EV3 robot, which simulate one and two-dimensional motion of a gas molecule.
Didactic target
The students chosen as the reference group for this experimentation are those of the fourth year of the scientific high school , with age between 17 and 18 years. The activity was carried out with 40 students from two different classes.
Prerequisite¶
What students should know before
Basic knowledge of the LEGO® Mindstorms EV3 Education kit, Gyrosensor.
Basic knowledge of the LEGO® Mindstorms EV3 Home Edition Software.
Basic knowledge of Python.
Basic knowledge of random numbers and binomial distribution of probability.
Basic knowledge of thermodynamics and statistical mechanics.
Time constraints:
10 hrs (5 meeting)
Methodology
The methodology adopted for the realization of the project was that of learning based on problem solving and learning: “challenge-based” . At the beginning of each lesson a problem or challenge was proposed that the students have to solve by working in a team; which also involves collaborative learning among the strategies chosen during the design phase of the activity.
Activity guidelines
The first two meeting
Meeting three
You can start right away with a problem:
Implement a program, using Python code, that using a random sequence of a fixed number of iterations makes the robot move along a straight line, and determine the average distance from the initial position. On the contrary, write a random walk program that allows the robot to reach a certain point along a straight line and determine the average value of the necessary steps.
Meeting four
Implement a program, using Python code, that using a random sequence of a fixed numbers of iterations makes the robot move along a broken line in the plain, and determine the average distance from the initial position, increase the number of steps, change the robot speed and the average path between to virtual collisions.
Meeting Five
During the last work session, students must propose a technological application of the random walk, and write a program also using the sensors available in LEGO® MINDSTORMS® Education Core Set ( Example cleaning robot).
Preparing For This Tutorial:¶
The LEGO Mindstorm EV3 Robot that coincides with this tutorial comes from building specific sections found in the LEGO® MINDSTORMS® Education-Core set.
You will need to build the main body for the robot (I’ll refer to use the Driving Base), plus a gyro sensor.
Effects¶
All students involved have achieved the proposed objectives.
Exercises¶
1. Implement a program, using Python code, that using a random sequence of a fixed number of iterations makes the robot move along a straight line, and determine the average distance from the initial position. On the contrary, write a random walk program that allows the robot to reach a certain point along a straight line and determine the average value of the necessary steps.
2. Implement a program, using Python code, that using a random sequence of a fixed numbers of iterations makes the robot move along a broken line in the plain, and determine the average distance from the initial position, increase the number of steps, change the robot speed and the average path between to virtual collisions.
Example solutions¶
ONE-DIMENSIONAL RANDOM WALK (1)
PYTHON PROGRAM
#!/usr/bin/env python3
from ev3dev2.motor import MoveTank, OUTPUT_B,OUTPUT_C, speed
from time import sleep
from random import random
L= 184.5 # Total length of the space
λ= 36.9 # Length of single step
Ns= 5 # N\ :sub:`s` = L/λ Steps number
# The time of the single step is t = λ / speed, 100 → 1050°/s , d = 5.6
cm ( r = 2.8 cm)
# speed =36 →378 °/s, S = r \* θ = 2,8*2,1*2*3.14= 18,45 cm
nby = int(0) # number of steps beyond
nba = int(0) # number of steps back
deltan = int(0) # difference between step beyond and step back
Nt = int(0)
for i in range(100):
nr= random()
tank_pair = MoveTank(OUTPUT_B,OUTPUT_C)
if nr>0.5:
tank_pair .on_for_seconds(36,36,,2)
nby+=1
else:
tank_pair.on_for_seconds(-36,-36,2)
nba+=1
sleep(1)
deltan = abs(nby-nba)
Nt=nby+nba
if deltan = Nt:
print(Nt)
tank_pair.off()
ONE-DIMENSIONAL RANDOM WALK (2)
PYTHON
#!/usr/bin/env python3
from ev3dev2.motor import MoveTank, OUTPUT_B,OUTPUT_C, speed
from time import sleep
from random import random
L= 184.5 # Total length of the space
λ= 36.9 # Length of single step
Ns= 5 # N\ :sub:`s` = L/λ Steps number
# The time of the single step is t = λ / speed, 100 → 1050°/s , d = 5.6
cm ( r = 2.8 cm)
# speed =36 →378 °/s, S = r \* θ = 2,8*2,1*2*3.14= 18,45 cm
nby = int(0) # number of steps beyond
nba = int(0) # number of steps back
deltan = int(0) # difference between step beyond and step back
Nt = int(0)
for i in range(100):
nr= random()
tank_pair = MoveTank(OUTPUT_B,OUTPUT_C)
if nr>0.5:
tank_pair .on_for_seconds(36,36,,2)
nby+=1
else:
tank_pair.on_for_seconds(-36,-36,2)
nba+=1
sleep(1)
deltan = abs(nby-nba)
d= λ*deltan
print(d) # d is the distance from the starting point
tank_pair.off()
2-DIMENSIONAL RANDOM WALK
#!/usr/bin/env python3
from ev3dev2.motor import (MoveTank, OUTPUT_B,OUTPUT_C)
from ev3dev2.sensor import GYRO,OUTPUT_A
from time import sleep
from random import random,randint
gy=GyroSensor(OUTPUT_A)
gy.mode='GYRO-ANG' #Put Gyro Sensor into “GYRO-ANG”
td=int() # time of desplacement
nr_anglerot = int() # angle of scattering
rand_speed = int() # speed of the random motion
for i in range(100):
nr_anglerot =randint(-180,180)
rand_speed= randint(10,50)
td= randint(1,4)
tank_pair = MoveTank(OUTPUT_B,OUTPUT_C)
tank_pair.on_for_seconds(rand_speed,rand_speed,td)
sleep(1)
angle=gy
angle=0
if nr_anglerot >0:
while angle-nr_anglerot<=0:
tank_pair.on_forever(-20,20)
else:
while angle-nr_anglerot>=0:
tank_pair.on_forever(20,-20)
tank_pair.off()
Random Numbers Pre-Quiz
What is computer software?
_______________________________________________________________________________
What does the word “random” mean?
_______________________________________________________________________________
Give an example of a random relationship and a non-random relationship.
_______________________________________________________________________________
What is a random number generator?
_______________________________________________________________________________
Give an example of how you could use a mathematical concept to solve an engineering problem.
_______________________________________________________________________________
Random Numbers Pre-Quiz Answer Key
What is computer software?
Computer software is a set of instructions that a computer can understand and follow. It directs the computer processor to perform specific tasks, such as opening a file, editing a text document or displaying a video.
What does the word “random” mean?
A random event is one that happens without conscious decision or pattern. Random events do not have “order.”
Give an example of a random relationship and a non-random relationship.
The relationship between a person’s weight and birth date is most likely random. The relationship between a person’s weight and clothing size is non-random.
What is a random number generator?
A random number generator is a computer program or calculation, or mechanical system or device, that produces random numbers, which are numbers that are not connected by any decision or intended pattern.
Give an example of how you could use a mathematical concept to solve an engineering problem.
Engineers could use the shape of a rectangle in order to construct strong walls of a building that fit together easily.
Random Numbers Post-Quiz
If you were designing a robot vacuum cleaner, how would you make sure the robot cleans the entire kitchen floor?
_______________________________________________________________________________
_______________________________________________________________________________
What is “computer software”?
_______________________________________________________________________________
_______________________________________________________________________________
What is a programming language?
_______________________________________________________________________________
_______________________________________________________________________________
What does “random” mean?
_______________________________________________________________________________
_______________________________________________________________________________
Give an example of a random relationship and a non-random relationship.
_______________________________________________________________________________
_______________________________________________________________________________
What is a random number generator?
_______________________________________________________________________________
_______________________________________________________________________________
For what kind of mathematical/engineering problem might a random number generator be useful?
_______________________________________________________________________________
_______________________________________________________________________________
WORKSHEET
for students
ONE-DIMENSIONAL RANDOM WALK ( 1 )
Draw a line of length L ( for example 184.5cm ) on the floor.
L = 184.5 cm
Divide L into n steps of λ length, λ = L/n ( for example n = 5, λ = 36.9 )
Implement a Python program that reproduce the one-dimensional random walk of a gas molecule using the lego minstorm ev3 as a simulator of a random motion.
If n is the number of step that the robot must perform in the same direction, determines the average number of iterations that the program must do.
N = ______________________
WORKSHEET
for students
ONE-DIMENSIONAL RANDOM WALK ( 2 )
Draw a line of length L .
Sets the length of the single step λ
Implement a Python program that reproduce the one-dimensional random walk of a gas molecule using the lego mindstorm ev3 as a simulator of a random motion, calculate at what average distance the robot will be after a fixed number of iteration n = 100
< d > = ______________________
WORKSHEET
for students
TWO-DIMENSIONAL RANDOM WALK ( 3 )
Connect a pencil to the robot
Implement a Python program that reproduces the two- dimensional random walk of a gas molecule using the lego mindstorm ev3 as a simulator of a random motion, calculate at what average distance the robot will be after a fixed number of iteration n = 100. Measures the average distance between the starting and the ending points.
In the first case, set the speed and length of the single step constant
In the second case, set only the speed constant
In the third case the speed, length and direction are totally random