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 educational progression designed for a course of 5 meetings (10 hours) is as follows:
Meeting 1- Introduction to robotics, construction of the robot
Meeting 2- First programming tests (engine ignition, gyrosensor use test to measure angles)
Match 3 – One-dimensional random walk algorithm
Meeting 4 – Two-dimensional random walk algorithm
Meeting 5 - Proposal for a technological application of the random walk model.

The first two meeting

In the first meeting it is important:
- Forming work groups;
- Define and share project objectives with students;
- Contextualize the Brownian motion and random walk. ? When is it used?
The contextualization serves to make students understand that they will have to try to implement a real algorithm, not something exclusively educational.
After having built the robot and done the first tests about “random numbers” and “ binomial distribution of probability” we continue with the didactic progression.

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

Expected results
In this case we can talk about results achieved.
The results are closely related to the objectives previously presented:
- To construct a simple mathematical model with respect to a posed problem .
- Use basic programming elements (Random sequence, cyclic and instruction-based execution).
- Understanding the algorithms used and the link with physical systems (thermodynamics process).
Regarding these first 3 objectives, we can say that:
- 100% of the students involved managed to solve the “basic” problem, that is, to create a simple algorithm simulating the one-dimensional and the two – dimensional random walk.
- 25% ( only two groups ) of the students involved managed to solve the final challenge, creatively combining what they had learned during the course.
Regarding the other 3 objectives:
- Increase interest in the STEM (Science Technology Engineering Maths) disciplines.
- Improve students ability to work in groups.
- Increase the students’ awareness of their cognitive processes.

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

  1. What is computer software?

_______________________________________________________________________________

  1. What does the word “random” mean?

_______________________________________________________________________________

  1. Give an example of a random relationship and a non-random relationship.

_______________________________________________________________________________

  1. What is a random number generator?

_______________________________________________________________________________

  1. Give an example of how you could use a mathematical concept to solve an engineering problem.

_______________________________________________________________________________

Random Numbers Pre-Quiz Answer Key

  1. 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.

  1. What does the word “random” mean?

A random event is one that happens without conscious decision or pattern. Random events do not have “order.”

  1. 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.

  1. 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.

  1. 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

  1. If you were designing a robot vacuum cleaner, how would you make sure the robot cleans the entire kitchen floor?

_______________________________________________________________________________

_______________________________________________________________________________

  1. What is “computer software”?

_______________________________________________________________________________

_______________________________________________________________________________

  1. What is a programming language?

_______________________________________________________________________________

_______________________________________________________________________________

  1. What does “random” mean?

_______________________________________________________________________________

_______________________________________________________________________________

  1. Give an example of a random relationship and a non-random relationship.

_______________________________________________________________________________

_______________________________________________________________________________

  1. What is a random number generator?

_______________________________________________________________________________

_______________________________________________________________________________

  1. For what kind of mathematical/engineering problem might a random number generator be useful?

_______________________________________________________________________________

_______________________________________________________________________________

WORKSHEET

for students

ONE-DIMENSIONAL RANDOM WALK ( 1 )

  1. Draw a line of length L ( for example 184.5cm ) on the floor.

L = 184.5 cm

  1. Divide L into n steps of λ length, λ = L/n ( for example n = 5, λ = 36.9 )

  2. 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 )

  1. Draw a line of length L .

  2. Sets the length of the single step λ

  3. 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 )

  1. Connect a pencil to the robot

  2. 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.

  3. In the first case, set the speed and length of the single step constant

  4. In the second case, set only the speed constant

  5. In the third case the speed, length and direction are totally random

Next Section - Robot Arm