[ev3dev × Python] SSH Control (remote control with keyboard)

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.

table of contents

  1. What to prepare 1 . SSH Control
  2. Glossary

0. What to prepare

◯ 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

  1. Right-click on ev3dev with the green light on and press Open SSH Terminal.

  2. Enter ssh [email protected].

  3. You will be asked for a password, so enter maker.

  4. Type pwd to see the current directory. (Current whereabouts)

  5. Move to the directory where your program exists by doing "cd directory`"! (Directory is a folder)

  6. If you can move it, type python3 program name.py and press enter.

  7. The screen of the intelligent block does not change, but it can be executed.

1. Glossary

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

Finally

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

[ev3dev × Python] SSH Control (remote control with keyboard)
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
[ev3dev × Python] Single motor control
Get keyboard events with python
screen and split screen with python and ssh login to remote server
[ev3dev × Python] Control of multiple motors
[ev3dev × Python] Display, audio, LED control
Try frequency control simulation with Python
Exclusive control with lock file in Python
Manually ssh registration for coreserver with python
[Automation] Manipulate mouse and keyboard with Python
Connect with mysql.connector with ssh tunnel in Python 3.7
FizzBuzz with Python3
Log in to the remote server with SSH
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
PyCharm remote interpreter with SSH (WSL2, venv, pyenv)
Integrate with Python
Play with 2016-Python
Precautions when dealing with control structures in Python 2.6
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Let's control EV3 motors and sensors with Python
Install Python Control
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Pepper-kun remote control environment construction with Docker + IPython Notebook
Ssh to virtual environment with remote development of vscode
Version control of Node, Ruby and Python with anyenv
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
[ev3dev × Python] Ultrasonic sensor
Handling yaml with python
Solve AtCoder 167 with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python