[Ev3dev] Let's make a remote control program by Python with RPyC protocol

Introduction

This article assumes that you have ev3dev installed on your EV3 and have an SSH connection. If you have not built the environment, please refer to this article.

mindstorm-Let's control EV3 with Linux! Install ev3dev OS and SSH connection

Overview

RPyC (Remote Python Call) is a protocol that calls Python remotely. With RPyC, you can easily call and use Python installed on EV3 from a Python program on your desktop PC (client side). Until now, it was necessary to connect to SSH and edit and execute Python programs remotely, but with RPyC, you can freely develop on the client just by setting up an RPyC server on EV3. It is.

Environment

Install RPyC

First, install Python's RPyC library on the remote side (EV3) and the client side (desktop PC, etc.).

robot@ev3dev:~$ sudo easy_install rpyc
Searching for rpyc
Reading https://pypi.python.org/simple/rpyc/
Best match: rpyc 3.3.0
Downloading https://pypi.python.org/packages/c5/b0/5425118bf8f209ebc863425acb37f49f71c7577dffbfaeaf0d80722e57c5/rpyc-3.3.0.zip#md5=f60bb91b46851be45363cd72e078e6ba
Processing rpyc-3.3.0.zip
Writing /tmp/easy_install-MDBKzw/rpyc-3.3.0/setup.cfg
Running rpyc-3.3.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-MDBKzw/rpyc-3.3.0/egg-dist-tmp-_uJbLx
Adding rpyc 3.3.0 to easy-install.pth file
Installing rpyc_registry.py script to /usr/local/bin
Installing rpyc_classic.py script to /usr/local/bin

Installed /usr/local/lib/python2.7/dist-packages/rpyc-3.3.0-py2.7.egg
Processing dependencies for rpyc
Searching for plumbum
Reading https://pypi.python.org/simple/plumbum/
Best match: plumbum 1.6.3
Downloading https://pypi.python.org/packages/50/15/f26f60e1bb82aabed7ff86f3fd2976784047f9a291c63ac9019086a69559/plumbum-1.6.3.tar.gz#md5=e0c588ba9271711fae3beb8c0511e8a9
Processing plumbum-1.6.3.tar.gz
Writing /tmp/easy_install-hcpumS/plumbum-1.6.3/setup.cfg
Running plumbum-1.6.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-hcpumS/plumbum-1.6.3/egg-dist-tmp-GirT6d
zip_safe flag not set; analyzing archive contents...
plumbum.__init__: module references __file__
plumbum.__init__: module references __path__
Adding plumbum 1.6.3 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/plumbum-1.6.3-py2.7.egg
Finished processing dependencies for rpyc

Start the RPyC server on EV3

First, create the shell script file below

rpyc_server.sh


#!/bin/bash
python `which rpyc_classic.py`

Give execute permission

robot@ev3dev:~$ chmod +x rpyc_server.sh

Server startup

robot@ev3dev:~$ ./rpyc_server.sh
INFO:SLAVE/18812:server started on [0.0.0.0]:18812

The rest can be freely developed on the client side.

Development on the client

As an example, let's create a program in which the motor rotates while pressing the touch sensor. Connect the motor to port A and the touch sensor to port 1. 1.jpg

Create the following program on the client side.

rpyc_client.py


import rpyc
conn = rpyc.classic.connect('ev3dev') #Specify the EV3 host name or IP address
ev3 = conn.modules['ev3dev.ev3'] #Remote ev3dev.Import ev3
m = ev3.LargeMotor('outA')
ts = ev3.TouchSensor('in1')

m.run_forever(speed_sp=300)

#Rotate the motor while the touch sensor is pressed
while True:
  if ts.value() == 0:
    m.stop(stop_action="hold")
  else:
    m.run_forever(speed_sp=300)

By using the rpyc library in this way, it is possible to operate the module on the remote side from the client side.

Let's run it.

client@user:~$ python rpyc_client.py

Such a log is displayed on the remote side.

robot@ev3dev:~$ ./rpyc_server.sh
INFO:SLAVE/18812:server started on [0.0.0.0]:18812
INFO:SLAVE/18812:accepted 192.168.2.209:56985
INFO:SLAVE/18812:welcome [192.168.2.209]:56985

192.168.2.209 is the IP address on the client side. You can see that the connection is correct.

When I pressed the touch sensor, the motor also rotated.

Summary

The impression was that the RPyC protocol was fairly lightweight and had little communication lag. It's pretty easy to develop a program locally.

reference

python-ev3dev official documentation

Recommended Posts

[Ev3dev] Let's make a remote control program by Python with RPyC protocol
Let's make a GUI with python.
Let's make a graph with python! !!
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
[ev3dev × Python] SSH Control (remote control with keyboard)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's replace UWSC with Python (5) Let's make a Robot
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
[Super easy] Let's make a LINE BOT with Python.
Make a fortune with Python
Let's make a websocket client with Python. (Access token authentication)
Let's make a remote rumba [Hardware]
Let's make a remote rumba [Software]
Let's make a breakout with wxPython
Make a recommender system with python
Let's make a supercomputer with xCAT
Let's make a web chat using WebSocket with AWS serverless (Python)!
[Python] A program that creates stairs with #
Let's make a simple language with PLY 1
[Python] Let's make matplotlib compatible with Japanese
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
Make a desktop app with Python with Electron
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
I tried to communicate with a remote server by Socket communication with Python.
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
Let's control EV3 motors and sensors with Python
Let's write a Python program and run it
Try to make a dihedral group with Python
Let's make a module for Python using SWIG
Read line by line from a file with Python
If you want to make a discord bot with python, let's use a framework
Let's make a leap in the manufacturing industry by utilizing the Web in addition to Python
Make one repeating string with a Python regular expression.
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
Try embedding Python in a C ++ program with pybind11
Make a breakpoint on the c layer with python
From buying a computer to running a program with python
Let's make dependency management with pip a little easier
[For play] Let's make Yubaba a LINE Bot (Python)
Make a recommender system with python
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app
A program to write Lattice Hinge with Rhinoceros with Python
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
[Python] Delete by specifying a tag with Beautiful Soup
[Python] Create a Tkinter program distribution file with cx_Freeze
What is God? Make a simple chatbot with python
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Using a Python program with fluentd's exec_filter Output Plugin