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
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.
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
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.
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.
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.
The impression was that the RPyC protocol was fairly lightweight and had little communication lag. It's pretty easy to develop a program locally.
python-ev3dev official documentation
Recommended Posts