Socket communication with Python LEGO Mindstorms

If you are developing robot programming etc. using LEGO Mindstorms EV3 (hereinafter EV3) in Python environment, naturally apply analysis such as machine learning Sometimes I want to control. At that time, the first thing to think about is to send the EV3 sensor value to a PC and use it for analysis. There may be several methods, but this time I will write a method to send the sensor value to the PC side using socket communication and send a message from the PC side to the EV3.

reference

This article is based on the following articles. Let's study socket communication with python

What is socket communication?

A socket is an entrance / exit between communication applications (two python programs in this case), and can be expressed by a combination of IP address and port number. There are TCP (Transmission Control Protocol) and UDP (User Diagram Protocol) communication standards for socket communication, and TCP is used this time. The difference between the two is that TCP emphasizes reliability, while UDP is a communication standard that emphasizes high speed.

Communication relations this time

This time, the following two types of communication will be performed. We will describe how each of them does a server / client. Part 1 1.png .png

Part 2 2.png 4.png

Operating environment

PC: Server / EV3: Client

First, the PC is the server side and the EV3 is the client. However, there is no big difference in what you can do because both the server and the client can send and receive data. Since the server accepts the client from the connection waiting state when establishing communication, the server side must be in the connection waiting state when the client requests a connection.

Preparation

Please refer to the following articles for the environment and construction of EV3. Machine learning with EV3 Part 1 Environment construction

After connecting the EV3 to the PC via Bluetooth, open a command prompt on the PC and check the IP address used for the Bluetooth connection between the PC and EV3. When the PC and EV3 are connected via Bluetooth, the link local address of 169.254.XXX.YYY is assigned. Follow the steps below to find out the IP address.

  1. Open a command prompt
  2. Execute the ʻipconfig` command
  3. Make a note of the IP address displayed in the displayed Ethernet Adapter Bluetooth Network Address. 6.png

program

The following is the server program on the PC side

import socket
import sys
import time

ev3_massage = None

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind(('169.254.85.105', 50010))  # your PC's Bluetooth IP & PORT
    s.listen(1)
    print('Start program...')
    while True:
        conn, addr = s.accept()
        with conn:
            while True:
                ev3_massage = conn.recv(1024)
                if ev3_massage is not None:
                    ev3_massage = ev3_massage.decode()
                    print('get' + ev3_massage)
                    time.sleep(1.0)
                    if ev3_massage == 'BACKSPACE':
                        break
                    ev3_massage = None
            print('End program')
            sys.exit()

Write the IP address of the Ethernet adapter Bluetooth network address mentioned above in parentheses in s.bind.

The following is the EV3 side client program

import socket
import sys
import time
from ev3dev2.button import Button
from ev3dev2.display import Display
import ev3dev2.fonts as fonts

 definition
button = Button()
screen = Display()
font = fonts.load('luBS12')
ev3_message = None


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect(('169.254.85.105', 50010))    # your PC's Bluetooth IP & PORT
    print('connected')
    while not(button.backspace):
        if button.up:
            screen.draw.text((10,10), 'UP Button Pressed', font=font)
            ev3_message = 'UP'
            s.send(ev3_message.encode())
            time.sleep(0.5)
        if button.down:
            screen.draw.text((10,10), 'DOWN Button Pressed', font=font)
            ev3_message = 'DOWN'
            s.send(ev3_message.encode())
            time.sleep(0.5)
        if button.left:
            screen.draw.text((10,10), 'LEFT Button Pressed', font=font)
            ev3_message = 'LEFT'
            s.send(ev3_message.encode())
            time.sleep(0.5)
        if button.right:
            screen.draw.text((10,10), 'RIGHT Button Pressed', font=font)
            ev3_message = 'RIGHT'
            s.send(ev3_message.encode())
            time.sleep(0.5)
        screen.update()
        screen.clear()
        
    screen.draw.text((10,10), 'ENTER Button Pressed', font=font)
    ev3_message = 'BACKSPACE'
    s.send(ev3_message.encode())
screen.clear()
print('End program')
sys.exit()

Describe the IP address of the Ethernet adapter Bluetooth network address mentioned above in parentheses of s.connect.

Run

Try running the program. Execute from the PC side, which is a server program.

From the command prompt, use the cd command to move to the folder where the programs on the PC side are stored. Below, a folder called test is created on the desktop, and PC_server.py is created in it. 5.png When the server program is executed, Start program ... is displayed and the connection is waited for.

Next, the EV3 side, which is the client program, is executed. Please refer to Machine Learning Part 1 Environment Construction for EV3 program execution with VSC Code. 6.png After execution, when socket communication is established, connected is displayed. After that, if you press the up / down / left / right buttons of EV3, the corresponding message will be sent to the program on the PC side, and when it is received, it will be displayed at the command prompt. 17.png    7.png If you press the button on the upper left, the programs on both the PC side and EV3 side will end. You can see that character strings such as UP and RIGHT can be sent to the program on the PC side via socket communication. To be precise, the data that can be sent by socket communication is byte type, so the character string is ʻencode and converted before sending, and when it is received, it is decode` and converted back to a character string and then printed.

Appendix

Record the value received by socket communication in a CSV file

By rewriting the program on the server side, the received value can be stored in the CSV file.

import socket
import sys
import time
import os.path
import csv

ev3_massage = None

if os.path.exists('button.csv') == False:
    writedata = ['button']
    f = open('button.csv', 'w', newline='')
    writer = csv.writer(f)
    writer.writerow(writedata)
    f.close()

def write(data):
    f = open('button.csv', 'a', newline='')
    writer = csv.writer(f)
    writer.writerow([data])
    f.close()



with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind(('169.254.85.105', 50010))  # your PC's Bluetooth IP & PORT
    s.listen(1)
    print('Start program...')
    while True:
        conn, addr = s.accept()
        with conn:
            while True:
                ev3_massage = conn.recv(1024)
                if ev3_massage is not None:
                    ev3_massage = ev3_massage.decode()
                    print('get' + ev3_massage)
                    write(ev3_massage)
                    time.sleep(1.0)
                    if ev3_massage == 'BACKSPACE':
                        break
                    ev3_massage = None
                    
            print('End program')
            sys.exit()

Csv and ʻos.path` are newly used for the module. When you run the program, you can see that a CSV file is generated and the received character string is recorded in each cell. 13.png

Recommended Posts

Socket communication with Python LEGO Mindstorms
Socket communication with Python
Socket communication using socketserver with python now
Serial communication with Python
Serial communication with python
HTTP communication with Python
LEGO Mindstorms 51515 Python Programming
View Python communication with Fiddler
Python3 socket module and socket communication flow
I tried SMTP communication with Python
Socket communication and multi-thread processing by Python
Socket communication by C language and Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
I tried to communicate with a remote server by Socket communication with Python.
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Python Socket communication sample / simple data throwing tool
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
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
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
Communication processing by Python
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python