Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
python-ev3dev2 (2.1.0) UNRELEASED; urgency=medium

[Daniel Walton]
* RPyC update docs and make it easier to use

[Matěj Volf]
* LED animation fix duration None
* Add rest notes to sound.Sound.play_song()
Expand Down
156 changes: 92 additions & 64 deletions docs/rpyc.rst
Original file line number Diff line number Diff line change
@@ -1,87 +1,115 @@
Working with ev3dev remotely using RPyC
=======================================
**************
RPyC on ev3dev
**************

RPyC_ (pronounced as are-pie-see), or Remote Python Call, is a transparent
python library for symmetrical remote procedure calls, clustering and
distributed-computing. RPyC makes use of object-proxying, a technique that
employs python’s dynamic nature, to overcome the physical boundaries between
processes and computers, so that remote objects can be manipulated as if they
were local. Here are simple steps you need to follow in order to install and
use RPyC with ev3dev:
`RPyC_ <https://rpyc.readthedocs.io/en/latest/>`_ (pronounced as are-pie-see) can be used to:
* run a python program on an ev3dev device that controls another ev3dev device.
This is more commonly known as daisy chaining.
* run a python program on your laptop that controls an ev3dev device. This can be
useful if your robot requires CPU intensive code that would be slow to run on the
EV3. A good example of this is a Rubik's cube solver, calculating the solution to
solve a Rubik's cube can be slow on an EV3.

1. Install RPyC both on the EV3 and on your desktop PC. For the EV3, enter the
following command at the command prompt (after you `connect with SSH`_):
For both of these scenarios you can use RPyC to control multiple remote ev3dev devices.

.. code-block:: shell

sudo easy_install3 rpyc

On the desktop PC, it really depends on your operating system. In case it is
some flavor of linux, you should be able to do
Networking
==========
You will need IP connectivity between the device where your python code runs
(laptop, an ev3dev device, etc) and the remote ev3dev devices. Some common scenarios
might be:
* Multiple EV3s on the same WiFi network
* A laptop and an EV3 on the same WiFi network
* A bluetooth connection between two EV3s

.. code-block:: shell
The `ev3dev networking documentation <https://www.ev3dev.org/docs/networking/>`_ should get
you up and running in terms of networking connectivity.

sudo pip3 install rpyc

In case it is Windows, there is a win32 installer on the project's
`sourceforge page`_. Also, have a look at the `Download and Install`_ page
on their site.
Install
=======

2. Create file ``rpyc_server.sh`` with the following contents on the EV3:
1. RPyC is installed on ev3dev but we need to create a service that launches
``rpyc_classic.py`` at bootup. `SSH <http://www.ev3dev.org/docs/tutorials/connecting-to-ev3dev-with-ssh/>`_ to your remote ev3dev devices and
cut-n-paste the following commands at the bash prompt.

.. code-block:: shell

#!/bin/bash
python3 `which rpyc_classic.py`
echo "[Unit]
Description=RPyC Classic Service
After=multi-user.target

and make the file executable:
[Service]
Type=simple
ExecStart=/usr/bin/rpyc_classic.py

.. code-block:: shell
[Install]
WantedBy=multi-user.target" > rpyc-classic.service

chmod +x rpyc_server.sh
sudo cp rpyc-classic.service /lib/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable rpyc-classic.service
sudo systemctl start rpyc-classic.service

Launch the created file either from SSH session (with
``./rpyc_server.sh`` command), or from brickman. It should output something
like

.. code-block:: none
2. If you will be using an ev3dev device to control another ev3dev device you
can skip this step. If you will be using your desktop PC to control an ev3dev
device you must install RPyC on your desktop PC. How you install RPyC depends
on your operating system. For Linux you should be able to do:

INFO:SLAVE/18812:server started on [0.0.0.0]:18812
.. code-block:: shell

sudo apt-get install python3-rpyc
Comment thread
dwalton76 marked this conversation as resolved.
Resolved

and keep running.
For Windows there is a win32 installer on the project's `sourceforge page`_.
Also, have a look at the `Download and Install`_ page on their site.

3. Now you are ready to connect to the RPyC server from your desktop PC. The
following python script should make a large motor connected to output port
``A`` spin for a second.
Example
=======
We will run code on our laptop to control the remote ev3dev device with IP
address X.X.X.X. The goal is to have the LargeMotor connected to ``OUTPUT_A``
run when the TouchSensor on ``INPUT_1`` is pressed.

.. code-block:: py

import rpyc
conn = rpyc.classic.connect('ev3dev') # host name or IP address of the EV3
ev3 = conn.modules['ev3dev2.ev3'] # import ev3dev2.ev3 remotely
m = ev3.LargeMotor('outA')
m.run_timed(time_sp=1000, speed_sp=600)

You can run scripts like this from any interactive python environment, like
ipython shell/notebook, spyder, pycharm, etc.

Some *advantages* of using RPyC with ev3dev are:

* It uses much less resources than running ipython notebook on EV3; RPyC server
is lightweight, and only requires an IP connection to the EV3 once set up (no
ssh required).
* The scripts you are working with are actually stored and edited on your
desktop PC, with your favorite editor/IDE.
* Some robots may need much more computational power than what EV3 can give
you. A notable example is the Rubics cube solver: there is an algorithm that
provides almost optimal solution (in terms of number of cube rotations), but
it takes more RAM than is available on EV3. With RPYC, you could run the
heavy-duty computations on your desktop.

The most obvious *disadvantage* is latency introduced by network connection.
This may be a show stopper for robots where reaction speed is essential.

.. _RPyC: http://rpyc.readthedocs.io/
.. _sourceforge page: http://sourceforge.net/projects/rpyc/files/main
.. _Download and Install: http://rpyc.readthedocs.io/en/latest/install.html
.. _connect with SSH: http://www.ev3dev.org/docs/tutorials/connecting-to-ev3dev-with-ssh/

# Create a RPyC connection to the remote ev3dev device.
# Use the hostname or IP address of the ev3dev device.
# If this fails, verify your IP connectivty via ``ping X.X.X.X``
conn = rpyc.classic.connect('X.X.X.X')

# import ev3dev2 on the remote ev3dev device
ev3dev2_motor = conn.modules['ev3dev2.motor']
Comment thread
dwalton76 marked this conversation as resolved.
Resolved
ev3dev2_sensor = conn.modules['ev3dev2.sensor']
ev3dev2_sensor_lego = conn.modules['ev3dev2.sensor.lego']

# Use the LargeMotor and TouchSensor on the remote ev3dev device
motor = ev3dev2_motor.LargeMotor(ev3dev2_motor.OUTPUT_A)
ts = ev3dev2_sensor_lego.TouchSensor(ev3dev2_sensor.INPUT_1)

# If the TouchSensor is pressed, run the motor
while True:
ts.wait_for_pressed()
motor.run_forever(speed_sp=200)

ts.wait_for_released()
motor.stop()

Pros
====
* RPyC is lightweight and only requires an IP connection (no ssh required).
* Some robots may need much more computational power than an EV3 can give
you. A notable example is the Rubik's cube solver.

Cons
====
* Latency will be introduced by the network connection. This may be a show stopper for robots where reaction speed is essential.
* RPyC is only supported by python, it is *NOT* supported by micropython

References
==========
* `RPyC <http://rpyc.readthedocs.io/>`_
* `sourceforge page <http://sourceforge.net/projects/rpyc/files/main>`_
* `Download and Install <http://rpyc.readthedocs.io/en/latest/install.html>`_
* `connect with SSH <http://www.ev3dev.org/docs/tutorials/connecting-to-ev3dev-with-ssh/>`_