This article is for anyone who wants to work with ev3 in Python. This time, I would like to use the keyboard for remote control. Specifically, it's like pressing the w key to move forward.
◯ ev3 (tank) + M motor ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.) ◯ Reference site: SSH Control
1.SSH Control
sshcontrol.py
#!/usr/bin/env python3
#Import only what you need
import termios, tty, sys
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,OUTPUT_C,MoveTank,MediumMotor
#Instance generation
tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
m_c = MediumMotor(OUTPUT_C)
#Define a function to get the pressed key
def getch():
#Get standard input file descriptor
fd = sys.stdin.fileno()
#Get terminal attributes of file descriptor
old_settings = termios.tcgetattr(fd)
#Make it unnecessary to press the enter key
tty.setcbreak(fd)
#Receive characters typed on the keyboard
ch = sys.stdin.read(1)
#Restore the terminal attributes of the file descriptor
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
#ch(character =In this case the pressed key)return it
return ch
#==============================================
#Define the function that drives the M motor
def fire():
m_c.on(80)
#==============================================
#Define a function for the tank to move forward
def forward():
tank_drive.on(80,80)
#==============================================
#Define a function for the tank to move backwards
def back():
tank_drive.on(-80,-80)
#==============================================
#Define a function for the tank to turn left
def left():
tank_drive.on(80,50)
#==============================================
#Define a function for the tank to turn right
def right():
tank_drive.on(50,80)
#==============================================
#Define a function that stops all motors
def stop():
tank_drive.stop()
m_c.stop()
#==============================================
#infinite loop
while True:
#getch()Substitute the value returned by
k = getch()
#k(Pressed key)To display
print(k)
#Determine the movement corresponding to the pressed key
if k == 'w':
forward()
if k == 's':
back()
if k == 'd':
right()
if k == 'a':
left()
if k == 'f':
fire()
#If the key pressed is the space bar
if k == ' ':
stop()
if k == 'q':
exit()
** Point **: A program that acquires the pressed key and remotely controls it with SSH
** Point **: Program execution
Right-click on ev3dev with the green light on and press Open SSH Terminal.
Enter ssh [email protected]
.
You will be asked for a password, so enter maker
.
Type pwd
to see the current directory. (Current whereabouts)
Move to the directory where your program exists by doing "cd directory`"! (Directory is a folder)
If you can move it, type python3 program name.py
and press enter.
The screen of the intelligent block does not change, but it can be executed.
I think there were a lot of words I didn't understand this time, so I'll explain them briefly.
◯SSH : A mechanism for connecting to other computers (mainly servers) via a network and operating them remotely. During communication, the information handled is encrypted.
◯ ** termios module **: Low level terminal control interface.
・ ** Terminal **: The part responsible for input and output of information. A device that performs input / output and communication with the main computer.
·interface A common area that connects different types of things.
◯ ** tty module **: A set of convenient functions for general terminal control operations.
・ ** tty **: Abbreviation that means any text terminal. Refers to ** terminal emulator **, etc. via a pseudo terminal device in a window system.
・ ** Text terminal **: Input / output device that inputs and displays text (character string)
・ ** Terminal emulator **: Software that works as a terminal.
・ ** Functions **: A collection of functions
◯ ** File descriptor **: File descriptor, fd. A mark assigned to a file (path to) to identify the file.
·File : What can read and write data as a byte string
· ** Byte string **: A byte string is a set of 1-byte data consisting of arbitrary bit patterns that are not given a specific format or meaning such as characters or numbers.
・ ** Bit pattern **: A combination of "0" and "1" bits, which is the smallest unit that a computer can handle.
·Part-Time Job : It is abbreviated as "B" in the unit that expresses the amount of information on a computer. One byte is composed of eight units called bits that represent 0 or 1. It is used for memory and disk capacity, minimum instruction code for programs, etc.
◯ ** sys module **: Module for handling information about Python interpreter and execution environment
·module : (Python) A file that contains code.
-** Python standard library **: A collection of modules. Official Standard Library Tutorial
・ ** Interpreter **: Software that works while reading source code line by line and converting it into instructions that can be executed by a computer. Since the program is processed line by line, the processing speed is not fast.
◯stdin : Standard input from the keyboard. standard input. You can receive characters entered on the keyboard. fd = 0
◯fileno : = file-number = file.No 0,1,2.. Function to get the file descriptor (fd) of a file object Returns an integer file descriptor This time, File object = characters typed on the keyboard
◯tcgetattr(fd) : Returns a list containing the terminal attributes of the file descriptor fd.
・ Tc = terminal computer = terminal computer ・ Get = get ・ Attr = attribute = attribute
◯setbreak() : Allows you to enter characters without pressing the enter key. On the contrary, without this, it is not possible to input with one character.
◯read() : Read file
◯termios.tcsetattr(fd, when, attributes) : Extract the terminal attributes of the file descriptor fd from attributes and set them. attributes (= attributes) is a list that tcgetattr () returns. The argument when determines when the attribute changes: -TCSANOW makes immediate changes. -TCSADRAIN makes changes after transferring all currently queued output. · TCSAFLUSH transfers all currently queued outputs, ignores all queued inputs, and then makes changes.
Reference article: Detect keystrokes in Python (without Enter) What is standard input / standard output? Mac Terminal Command List (Basic) TERMIOS
Thank you for reading! !!
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