[ev3dev × Python] Touch 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 touch sensor.

table of contents

  1. What to prepare
  2. Touch sensor program

0. What to prepare

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

1. Touch sensor program (Document p.33)

1-0. Program to press the touch sensor

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 ↓ スクリーンショット 2020-06-19 10.11.20.png

1-1. Program "If, if not"

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 ↓ スクリーンショット 2020-06-19 10.14.50.png

1-2. Programs that do ~ while the button is not pressed

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

1-3. Program on reciprocating motion and long press

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

1-4. Program to do when the touch sensor is pressed at the same time

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 ↓ スクリーンショット 2020-06-19 16.58.59.png

1-5. Program to operate radio control with two touch sensors ①

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 ↓ スクリーンショット 2020-06-19 16.59.05.png

1-6. Program to operate radio control with two touch sensors ②

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 ↓ スクリーンショット 2020-06-19 16.58.41.png

1-7. Program to operate radio control with two touch sensors ③

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 ↓ スクリーンショット 2020-06-19 16.59.13.png

1-8. Manual and automatic switching program

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 ↓ スクリーンショット 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

Finally

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

[ev3dev × Python] Touch sensor
[ev3dev × Python] Ultrasonic sensor
[ev3dev × Python] Gyro sensor
[ev3dev × Python] Color sensor
Touch MySQL from Python 3
Python beginners touch Pytorch (3)
Python beginners touch Pytorch (1)
Python beginners touch Pytorch (2)
[ev3dev × Python] Build ev3dev development environment
[ev3dev × Python] Single motor control
Touch Apache Beam in Python
Touch a Python object from Elixir
I tried to touch Python (installation)
Python
[ev3dev × Python] Display, audio, LED control
Get keystrokes from / dev / input (python evdev)
Touch virtual robots with Pepper's Python SDK
I tried to touch Python (basic syntax)
[ev3dev × Python] SSH Control (remote control with keyboard)
API explanation to touch mastodon from python
Touch AWS with Serverless Framework and Python