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.
This article is based on the following articles. Let's study socket communication with python
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.
This time, the following two types of communication will be performed. We will describe how each of them does a server / client. Part 1
Part 2
PC Windows10 Python 3.7.3
EV3 ev3dev2-python Python 3.5.3
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.
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.
Ethernet Adapter Bluetooth Network Address
.
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
.
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.
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.
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.
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
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.
Recommended Posts