This article is for anyone who wants to work with ev3 in Python. This time, I would like to perform various operations using the touch sensor.
◯ ev3 (tank) and touch sensor ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)
touchsensor00.py
#!/usr/bin/env python3
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1,INPUT_2
from ev3dev2.sound import Sound
from ev3dev2.led import Leds
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)
snd = Sound()
led = Leds()
while True:
if ts_1.is_pressed:
snd.beep()
if ts_2.is_pressed:
led.set_color("LEFT","YELLOW")
led.set_color("RIGHT","YELLOW")
Point : A program that makes a sound each time the touch sensor (port 1) is pressed and the light glows yellow each time the touch sensor (port 2) is pressed.
Point : ʻIf condition: Process 1` ** If the condition is true, process 1 **
Point : is_pressed A boolean indicating whether the current touch sensor is being pressed.
** Point **: This is how it works when compared to scratch ↓
touchsensor01.py
#!/usr/bin/env python3
from ev3dev2.motor import MediumMotor,OUTPUT_A
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1
M_A = MediumMotor(OUTPUT_A)
ts_1 = TouchSensor(INPUT_1)
while True:
if ts_1.is_pressed:
M_A.on(100)
else:
M_A.stop()
Point : A program that rotates the M motor when the touch sensor (port 1) is pressed and stops when it is released.
Point : ʻIf condition: Process 1 else: Process 2`
** If the condition is true, do process 1, If false, process 2 ** Program
** Point **: This is how it works when compared to scratch ↓
touchsensor02.py
#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveSteering,SpeedPercent
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1
from ev3dev2.sound import Sound
steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)
ts_1 = TouchSensor(INPUT_1)
snd = Sound()
while True:
while not ts_1.is_pressed:
steering_drive.on(0,SpeedPercent(40))
snd.beep()
steering_drive.on_for_seconds(0,SpeedPercent(-10),1)
steering_drive.on_for_seconds(100,SpeedPercent(40),2)
** Point **: A program that goes straight, makes a sound when the touch sensor is pressed, moves back a little, and then turns around for 2 seconds.
Point :
while not ~: Process 1
** Continue process 1 while not ~ ** Program
touchsensor03.py
#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,MediumMotor
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1
M_A = MediumMotor(OUTPUT_A)
ts_1 = TouchSensor(INPUT_1)
while True:
ts_1.wait_for_bump()
M_A.on(50)
ts_1.wait_for_bump()
M_A.on(-50)
if not ts_1.wait_for_bump(2000):
exit()
** Point **: A program that changes the direction of rotation when the touch sensor is bumped. Press and hold for 2 seconds or longer to end the program.
wait_for_pressed(timeout_ms=None, sleep_ms=10) → Wait until pressed. wait_for_released(timeout_ms=None, sleep_ms=10) → Wait until released. wait_for_bump(timeout_ms=None, sleep_ms=10) → Wait until the button is bumped (pressed and released). The time limit from pressing to releasing can be set with the first argument
** Point **: if not ts_1.wait_for_bump (2000): About. if not Press and release the touch sensor within 2 seconds:
= if Do not release the touch sensor within 2 seconds after pressing it
= if If you press and hold the touch sensor for 2 seconds or longer
touchsensor04.py
#!/usr/bin/env python3
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1,INPUT_2
from ev3dev2.sound import Sound
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)
snd = Sound()
while True:
if ts_1.is_pressed and ts_2.is_pressed:
snd.beep()
** Point **: A program that makes a sound when both touch sensors are pressed at the same time.
** Point **: Conditional expression of if statement
◯ When condition 1 is true ** and ** condition 2 is true, processing is performed.
ʻIf Condition 1 and Condition 2: Process 1`
◯ Condition 1 is true ** or ** When condition 2 is true, processing is performed.
ʻIf Condition 1 or Condition 2: Process 1`
◯ Process when condition 1 is not true **
ʻIf not condition 1: Process 1`
** Point **: This is how it works when compared to scratch ↓
touchsensor05.py
#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,LargeMotor
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1,INPUT_2
L_A = LargeMotor(OUTPUT_A)
L_B = LargeMotor(OUTPUT_B)
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)
while True:
if ts_1.is_pressed:
L_A.on(50)
else:
L_A.stop()
if ts_2.is_pressed:
L_B.on(50)
else:
L_B.stop()
** Point **: A program that uses the touch sensor (1) to move the L motor (A) and the touch sensor (2) to move the L motor (B).
** Point **: This is how it works when compared to scratch ↓
touchsensor06.py
#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1,INPUT_2
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)
while True:
if ts_1.is_pressed:
tank_drive.on(50,50)
else:
if ts_2.is_pressed:
tank_drive.on(-50,-50)
else:
tank_drive.stop()
** Point **: A program that moves forward with the touch sensor (1) and moves backward with the touch sensor (2).
** Point **: This is how it works when compared to scratch ↓
touchsensor07.py
#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.sensor import INPUT_1,INPUT_2
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)
while True:
if ts_1.is_pressed:
if ts_2.is_pressed:
tank_drive.on(100,100)
else:
tank_drive.on(40,100)
else:
if ts_2.is_pressed:
tank_drive.on(100,40)
else:
tank_drive.stop()
Point : Press both touch sensors: forward Press only touch sensor 1: Go left forward Press only touch sensor 2: Go forward to the right Do not press both touch sensors: stop
** Point **: You can adjust the degree of bending of the tank when only one touch sensor is pressed.
** Point **: This is how it works when compared to scratch ↓
touchsensor08.py
#!/usr/bin/env python3
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
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
us = UltrasonicSensor(INPUT_3)
ts_1 = TouchSensor(INPUT_1)
ts_2 = TouchSensor(INPUT_2)
while True:
while us.distance_centimeters > 20:
if ts_1.is_pressed:
if ts_2.is_pressed:
tank_drive.on(100,100)
else:
tank_drive.on(40,100)
else:
if ts_2.is_pressed:
tank_drive.on(100,40)
else:
tank_drive.stop()
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 ↓
** Point **: ** Repeat until ** and ** Repeat between **
** Repeat until **: Repeat until the condition is no longer true ** Repeat while ~ **: Repeat while the condition is true
Thank you for reading! !! Next time, I would like to write about color 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