[ev3dev × Python] Ultrasonic sensor

This article is for anyone who wants to work with ev3 in Python. This time, I would like to perform various operations using the ultrasonic sensor.

table of contents

  1. What to prepare
  2. Ultrasonic sensor program

0. What to prepare

◯ ev3 (tank) and ultrasonic sensor ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)

1. Ultrasonic sensor program (Document p.35)

1-0. A program that compares the distance to an obstacle and the threshold

ultrasonicsensor00.py


#!/usr/bin/env python3

#Import only what you need from the module
ev3dev2.sensor.lego import UltrasonicSensor
from ev3dev2.motor import MoveSteering,OUTPUT_A,OUTPUT_B,SpeedPercent

#Instance generation
us = UltrasonicSensor()
steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)

#Loop executed while the value of the ultrasonic sensor is greater than 30
while us.distance_centimeters > 30:
    steering_drive.on(0,SpeedPercent(75))
#Stop when the loop ends
steering_drive.stop()
#Exit the program
exit()

** Point **: A program that goes straight while the distance to an obstacle is greater than 30 cm

** Point **: Reference article about thresholds What is the threshold

Point : distance_centimeters Variable that stores the value of the ultrasonic sensor

1-1. A program that displays the distance to an object

ultrasonicsensor01.py


#Import only what you need from the module
#!/usr/bin/env python3
from ev3dev2.display import Display
ev3dev2.sensor.lego import UltrasonicSensor
from time import sleep

#Instance generation
dsp = Display()
us = UltrasonicSensor()

#infinite loop
while True:
    dsp.update()
    #round()Round off with
    distance = round(us.distance_centimeters)
    dsp.text_pixels(str(distance) + 'cm',True,0,52,font = 'helvB' + '24')
    sleep(0.1)

** Point **: A program that keeps displaying the value of the ultrasonic sensor

1-2. Manual and automatic switching program

ultrasonicsensor02.py


#!/usr/bin/env python3
#Import only what you need from the module
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from ev3dev2.sensor.lego import TouchSensor,UltrasonicSensor
from ev3dev2.sensor import INPUT_1,INPUT_2,INPUT_3

#Instance generation
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
us = UltrasonicSensor(INPUT_3)
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)

#infinite loop
while True:
    #Loop to run while ultrasonic sensor value is greater than 20
    while us.distance_centimeters > 20:
        #ts_1 was pressed
        if ts_1.is_pressed:
            #ts_1 was pressed and ts_2 was pressed
            if ts_2.is_pressed:
                tank_drive.on(100,100)
            #ts_1 was pressed and ts_2 is not pressed
            else:
                tank_drive.on(40,100)
        #ts_1 is not pressed
        else:
            #ts_1 is not pressed and ts_2 was pressed
            if ts_2.is_pressed:
                tank_drive.on(100,40)
            #ts_1 is not pressed and ts_2 is not pressed
            else:
                tank_drive.stop()
    #Loop to run while ultrasonic sensor value is greater than 20
    while us.distance_centimeters < 30:
        tank_drive.on(-80,-80)

Point : When there are no obstacles closer than 20 cm, you can manually operate the radio control. However, if the obstacle is closer than 20 cm, the program will automatically escape to the back until the distance to the obstacle is more than 30 cm for emergency avoidance.

** Point **: This mechanism (same content) when compared to scratch ↓ スクリーンショット 2020-06-19 16.59.25.png

** Point **: ** Repeat until ** and ** Repeat between **

** Repeat until **: Repeat until the condition is no longer true ** Repeat while ~ **: Repeat while the condition is true

1-3. Parallel processing program

ultrasonicsensor03.py


#!/usr/bin/env python3

#Import only what you need from the module
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from ev3dev2.display import Display
from ev3dev2.sensor.lego import UltrasonicSensor
from time import sleep
from threading import Thread

#Instance generation
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
dsp = Display()
us = UltrasonicSensor()

#distance_display()Function definition
def distance_display():
    #loop =Repeat while True
    while loop:
        dsp.update()
        distance = us.distance_centimeters
        dsp.text_pixels(str(distance) + 'cm',True,0,52,font = 'helvB' + '24')
        sleep(0.1)
#Assign True to loop to make it loopable
loop = True
#Create an instance t of Thread class
t = Thread(target = distance_display)
#Start a thread
t.start()

#Repeat 10 times
for i in range(10):
    #Repeat as long as the ultrasonic sensor value is greater than 30
    while us.distance_centimeters > 30:
        tank_drive.on(10,10)
    tank_drive.stop()
    #Repeat as long as the ultrasonic sensor value is less than 50
    while us.distance_centimeters < 50:
        tank_drive.on(5,-5)
tank_drive.stop()
#Assign False to loop to stop the thread
loop = False

Point : If there are no obstacles closer than 30 cm, go straight. However, if the obstacle is closer than 30 cm, a program like Roomba that rotates until the distance to the obstacle is more than 50 cm. Then, the distance to the obstacle is displayed at the same time as traveling.

** Point **: Reference article on parallel processing Multithreading

1-4. Concurrent processing program

main.py


#!/usr/bin/env python3

#Import only what you need from the module
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from ev3dev2.sensor.lego import UltrasonicSensor
import subprocess

#Instance generation
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
us = UltrasonicSensor()

#Start a child program in a new process
subprocess.Popen(['python3','sub.py'])

#Repeat 10 times
for i in range(10):
    while us.distance_centimeters > 30:
        tank_drive.on(10,10)
    tank_drive.stop()
    while us.distance_centimeters < 50:
        tank_drive.on(5,-5)
tank_drive.stop()
#Create an instance of class Popen
proc = subprocess.Popen(['python3','sub.py'])
#Terminate the child program
proc.kill()
exit()

sub.py


from ev3dev2.sensor.lego import UltrasonicSensor
from ev3dev2.display import Display
from time import sleep

dsp = Display()
us = UltrasonicSensor()

while True:
    dsp.update()
    distance = round(us.distance_centimeters)
    dsp.text_pixels(str(distance) + 'cm',True,0,52,font = 'helvB' + '24')
    sleep(0.5)
exit()

** Point **: How to call sub.py from main.py for parallel processing. (Processing is heavy)

** Point **: Reference article on concurrency Introduction to Concurrency Programming in Python

Finally

Thank you for reading! !! Next time, I would like to write about gyro sensors!

I want to make a better article ◯ This is easier to understand ◯ This is difficult to understand ◯ This is wrong ◯ I want you to explain more here We appreciate your opinions and suggestions.

Recommended Posts

[ev3dev × Python] Ultrasonic sensor
[ev3dev × Python] Touch sensor
[ev3dev × Python] Gyro sensor
[ev3dev × Python] Color sensor
[ev3dev × Python] Intelligent block button
[ev3dev × Python] Build ev3dev development environment
[ev3dev × Python] Single motor control
[ev3dev × Python] Control of multiple motors
Python
[ev3dev × Python] Display, audio, LED control
Get keystrokes from / dev / input (python evdev)
[ev3dev × Python] SSH Control (remote control with keyboard)