├── .docker ├── .husk │ └── Dockerfile ├── .version ├── Makefile ├── docker-compose.base.yml └── docker-compose.nvidia.yml ├── .env ├── .github └── workflows │ └── build.yml ├── Dockerfile ├── LICENSE ├── README.md ├── docker-compose.yml ├── run.sh └── workdir ├── .empty ├── media ├── buffered_port.png ├── callback.png ├── explore.png ├── port.png ├── problem.png ├── pub-sub.png ├── question_mark.png ├── rfmodule.png ├── rpc.png ├── test_port_1.png ├── test_rf_module.png ├── test_rpc.png ├── tested.png ├── yarp-problem1.png ├── yarp-python-tutorial.png └── yarp_problem.png └── tutorials ├── benchmarks ├── README.md ├── port │ ├── run.sh │ ├── test_reader_port.py │ └── test_writer_port.py ├── rfmodule │ ├── core │ ├── run.sh │ ├── test_client_rpc.py │ └── test_rfmodule.py └── rpc │ ├── run.sh │ ├── test_rpc_client.py │ └── test_rpc_server.py ├── buffered-ports ├── README.md ├── reader_bp.py ├── run.sh └── writer_bp.py ├── callbacks ├── README.md ├── reader_bp_callback.py ├── run.sh └── writer_bp.py ├── ports ├── README.md ├── reader.py ├── run.sh └── writer.py ├── producer-consumer ├── README.md ├── buffer.py ├── consumer.py ├── producer.py └── run.sh ├── publisher-subscriber ├── README.md ├── publisher.py ├── run.sh ├── subscriber1.py ├── subscriber2.py └── subscriber3.py ├── rfmodule ├── README.md ├── run.sh └── test_rfmodule.py ├── rpc ├── README.md ├── client.py ├── run.sh └── server.py └── yarpimage ├── run.sh └── snap.py /.docker/.husk/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG DOCKER_SRC 2 | 3 | FROM ${DOCKER_SRC} AS base 4 | LABEL maintainer="Davide De Tommaso , Adam Lukomski " 5 | 6 | ENV DEBIAN_FRONTEND noninteractive 7 | 8 | ARG LOCAL_USER_ID 9 | ARG NVIDIA_ENV=0 10 | 11 | USER root 12 | 13 | ENV LOCAL_USER_ID=${LOCAL_USER_ID} 14 | ENV NVIDIA_ENV=${NVIDIA_ENV} 15 | 16 | RUN if [ "$NVIDIA_ENV" = 1 ] ; then apt-get update && apt-get install -y --no-install-recommends \ 17 | libxau6 libxdmcp6 libxcb1 libxext6 libx11-6 && \ 18 | export NVIDIA_VISIBLE_DEVICES=all && \ 19 | export NVIDIA_DRIVER_CAPABILITIES=graphics,utility && \ 20 | /bin/sh -c echo "/usr/local/nvidia/lib" >> /etc/ld.so.conf.d/nvidia.conf && \ 21 | echo "/usr/local/nvidia/lib64" >> /etc/ld.so.conf.d/nvidia.conf && \ 22 | export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/lib/i386-linux-gnu:/usr/local/nvidia/lib:/usr/local/nvidia/lib64 && \ 23 | apt-get update && apt-get install -y --no-install-recommends \ 24 | libglvnd0 libgl1 libglx0 libegl1 libgles2 && \ 25 | apt-get update && apt-get install -y --no-install-recommends \ 26 | pkg-config libglvnd-dev libgl1-mesa-dev libegl1-mesa-dev libgles2-mesa-dev ; fi 27 | 28 | RUN usermod -u ${LOCAL_USER_ID} docky 29 | USER docky 30 | -------------------------------------------------------------------------------- /.docker/.version: -------------------------------------------------------------------------------- 1 | RELEASE_TAG=v1.0 2 | -------------------------------------------------------------------------------- /.docker/Makefile: -------------------------------------------------------------------------------- 1 | LOCAL_USER_ID := $(shell id -u) 2 | export LOCAL_USER_ID 3 | 4 | include ../.env 5 | include .version 6 | 7 | SHELL := /bin/bash 8 | HN := $(shell hostname) 9 | export HN 10 | 11 | PJT_DOCKER_IMAGE := iitschri/${PROJECT_NAME}-docker:${RELEASE_TAG} 12 | export PJT_DOCKER_IMAGE 13 | 14 | CONTAINER_NAME := ${PROJECT_NAME}.${RELEASE_TAG}-${USER}.${HN} 15 | export CONTAINER_NAME 16 | 17 | ifeq (, $(shell which nvidia-smi)) 18 | NVIDIA_ENV := 0 19 | NVIDIA_COMPOSE := 20 | LOCAL_DOCKER_IMAGE := ${PJT_DOCKER_IMAGE}-${USER}.${HN} 21 | else 22 | NVIDIA_ENV := 1 23 | NVIDIA_COMPOSE := -f docker-compose.nvidia.yml 24 | LOCAL_DOCKER_IMAGE := ${PJT_DOCKER_IMAGE}-${USER}.${HN}-nvidia 25 | endif 26 | 27 | export NVIDIA_ENV 28 | export NVIDIA_COMPOSE 29 | export LOCAL_DOCKER_IMAGE 30 | 31 | ID := $(shell lsb_release -is | tr '[:upper:]' '[:lower:]') 32 | VERSION_ID := $(shell lsb_release -rs) 33 | export ID 34 | export VERSION_ID 35 | 36 | define install_reqs 37 | @echo "Checking Docker ..." 38 | @(if which docker; then \ 39 | echo "docker found, skipping"; \ 40 | else \ 41 | echo "docker not found, installing"; \ 42 | if [ -e /etc/os-release ]; then \ 43 | if cat /etc/os-release | grep Ubuntu; then \ 44 | echo "Found Ubuntu, proceeding with docker-ce install"; \ 45 | echo "sudo apt update"; \ 46 | sudo apt update; \ 47 | sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release; \ 48 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - ; \ 49 | sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ 50 | `lsb_release -cs` stable"; \ 51 | sudo apt update; \ 52 | sudo apt-get install docker-ce docker-ce-cli containerd.io; \ 53 | sudo usermod -aG docker ${USER}; \ 54 | else \ 55 | echo "Only Ubuntu is supported for apt installs, skipping"; \ 56 | fi; \ 57 | fi; \ 58 | fi) 59 | @echo "Checking docker-compose ..." 60 | @(if which docker-compose; then \ 61 | echo "docker-compose found, skipping"; \ 62 | else \ 63 | sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m`" -o /usr/local/bin/docker-compose; \ 64 | sudo chmod 777 /usr/local/bin/docker-compose ; \ 65 | fi ) 66 | @echo "Checking nvidia container runtime ..." 67 | @(if lsmod | grep nvidia; then \ 68 | echo "found nvidia card drivers"; \ 69 | if which nvidia-container-runtime-hook; then \ 70 | echo "found nvidia container runtime, skipping"; \ 71 | else \ 72 | if [ -e /etc/os-release ]; then \ 73 | if cat /etc/os-release | grep Ubuntu; then \ 74 | echo "Adding nvidia apt repository for ${ID}${VERSION_ID}" ; \ 75 | curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | sudo apt-key add -; \ 76 | curl -s -L https://nvidia.github.io/nvidia-container-runtime/${ID}${VERSION_ID}/nvidia-container-runtime.list | sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list; \ 77 | sudo apt-get update; \ 78 | sudo apt install nvidia-container-runtime; \ 79 | else \ 80 | echo "Only Ubuntu is supported for apt installs, skipping"; \ 81 | fi; \ 82 | fi; \ 83 | fi; \ 84 | fi ) 85 | @echo "Checking if user in group docker ..." 86 | @(if groups | grep -o docker; then \ 87 | echo "user ${USER} already in group docker"; \ 88 | else \ 89 | sudo usermod -aG docker ${USER}; \ 90 | fi ) 91 | @echo "All Docker requirements are satisfied!" 92 | endef 93 | 94 | clean: setup 95 | docker system prune 96 | 97 | @(if docker images --format "{{.Repository}}:{{.Tag}}" | grep "${PJT_DOCKER_IMAGE}"; then \ 98 | echo "Removing project docker image ${PJT_DOCKER_IMAGE}"; \ 99 | docker image rm ${PJT_DOCKER_IMAGE}; \ 100 | fi) 101 | 102 | @(if docker images --format "{{.Repository}}:{{.Tag}}" | grep "${LOCAL_DOCKER_IMAGE}"; then \ 103 | echo "Removing local docker image ${LOCAL_DOCKER_IMAGE}"; \ 104 | docker image rm ${LOCAL_DOCKER_IMAGE}; \ 105 | fi) 106 | 107 | setup: 108 | @echo "Checking requirements ..." 109 | $(call install_reqs) 110 | 111 | 112 | init: setup 113 | echo "Building project docker image ${PJT_DOCKER_IMAGE}"; \ 114 | docker build .. -t ${PJT_DOCKER_IMAGE} --build-arg DOCKER_SRC=${DOCKER_SRC} ${BUILD_ARGS}; 115 | 116 | 117 | build: 118 | ifeq ($(shell docker images --format "{{.Repository}}:{{.Tag}}" | grep "${LOCAL_DOCKER_IMAGE}"),) 119 | @echo "Building local docker image ${LOCAL_DOCKER_IMAGE}"; \ 120 | docker build .husk \ 121 | -t ${LOCAL_DOCKER_IMAGE} \ 122 | --build-arg DOCKER_SRC=${PJT_DOCKER_IMAGE} \ 123 | --build-arg LOCAL_USER_ID=${LOCAL_USER_ID} \ 124 | --build-arg NVIDIA_ENV=${NVIDIA_ENV}; 125 | endif 126 | 127 | 128 | run: build 129 | ifneq (, ${XRANDR_CONF}) 130 | @echo "Setting resolution ${XRANDR_CONF}" 131 | $(shell ${XRANDR_CONF}) 132 | endif 133 | @echo "Running docker image ${LOCAL_DOCKER_IMAGE}" 134 | docker-compose -f ../docker-compose.yml -f docker-compose.base.yml ${NVIDIA_COMPOSE} up --remove-orphans 135 | docker-compose -f ../docker-compose.yml -f docker-compose.base.yml ${NVIDIA_COMPOSE} down 136 | 137 | distro: build 138 | @echo "Pushing docker image ${PJT_DOCKER_IMAGE} on dockerhub" 139 | docker push ${PJT_DOCKER_IMAGE} 140 | docker tag ${PJT_DOCKER_IMAGE} iitschri/${PROJECT_NAME}-docker:latest 141 | docker push iitschri/${PROJECT_NAME}-docker:latest 142 | -------------------------------------------------------------------------------- /.docker/docker-compose.base.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | 5 | main: 6 | image: ${LOCAL_DOCKER_IMAGE} 7 | 8 | environment: 9 | - DISPLAY=${DISPLAY} 10 | - QT_X11_NO_MITSHM=1 11 | - XAUTHORITY=/home/docky/.Xauthority 12 | - PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native 13 | - XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR} 14 | 15 | privileged: true 16 | stdin_open: true 17 | tty: true 18 | network_mode: host 19 | 20 | volumes: 21 | - type: bind 22 | source: ${XAUTHORITY} 23 | target: /home/docky/.Xauthority 24 | 25 | - type: bind 26 | source: /tmp/.X11-unix 27 | target: /tmp/.X11-unix 28 | 29 | - type: bind 30 | source: ${XDG_RUNTIME_DIR}/pulse 31 | target: ${XDG_RUNTIME_DIR}/pulse 32 | 33 | - type: bind 34 | source: ${XDG_RUNTIME_DIR}/dconf 35 | target: ${XDG_RUNTIME_DIR}/dconf 36 | 37 | - type: bind 38 | source: ./workdir 39 | target: /home/docky/workdir 40 | 41 | container_name: ${CONTAINER_NAME} 42 | -------------------------------------------------------------------------------- /.docker/docker-compose.nvidia.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | 5 | main: 6 | container_name: ${CONTAINER_NAME}-nvidia 7 | 8 | deploy: 9 | resources: 10 | reservations: 11 | devices: 12 | - driver: nvidia 13 | count: all 14 | capabilities: [graphics,utility] 15 | -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | PROJECT_NAME=yarp-python-tutorials 2 | DOCKER_SRC=iitschri/yarp-docker:v3.4.5 3 | BUILD_ARGS= 4 | XRANDR_CONF= 5 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # This is a basic workflow to help you get started with Actions 2 | 3 | name: CI 4 | 5 | # Controls when the workflow will run 6 | on: 7 | release: 8 | types: [published] 9 | 10 | # Allows you to run this workflow manually from the Actions tab 11 | workflow_dispatch: 12 | 13 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 14 | jobs: 15 | # This workflow contains a single job called "build" 16 | build: 17 | # The type of runner that the job will run on 18 | runs-on: ubuntu-latest 19 | 20 | # Steps represent a sequence of tasks that will be executed as part of the job 21 | steps: 22 | - name: checkout repo 23 | uses: actions/checkout@v2 24 | 25 | - name: Log in to Docker Hub 26 | uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 27 | with: 28 | username: ${{ secrets.DOCKER_USERNAME }} 29 | password: ${{ secrets.DOCKER_PASSWORD }} 30 | 31 | - name: write version 32 | run: echo RELEASE_TAG=${{ github.event.release.tag_name }} > .docker/.version 33 | 34 | - name: commit files 35 | run: | 36 | git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" 37 | git config --local user.name "github-actions[bot]" 38 | git commit -m "Add changes" -a 39 | 40 | - name: push changes 41 | uses: ad-m/github-push-action@master 42 | with: 43 | github_token: ${{ secrets.GITHUB_TOKEN }} 44 | branch: refs/heads/master 45 | 46 | - name: init build 47 | run: cd .docker && make init build distro 48 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | ARG DOCKER_SRC 2 | 3 | FROM $DOCKER_SRC 4 | 5 | # Install essential for python3 6 | RUN pip3 install matplotlib numpy -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2021, Social Cognition in Human-Robot Interaction 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YARP Tutorials (Python) 2 | ![alt text][YARP-PY] 3 | 4 | [YARP-PY]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/yarp-python-tutorial.png 5 | 6 | This repository contains open source software to start programming in YARP using Python. 7 | 8 | [YARP](http://www.yarp.it) is an open source library designed for implementing robotics applications. 9 | 10 | 11 | # 1. Basic Tutorials 12 | In the following you can find some tutorials for Python developers to start using YARP with: 13 | 14 | - [Ports](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/ports) 15 | 16 | - [Buffered Port](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/buffered-ports) 17 | 18 | - [Rpc](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/rpc) 19 | 20 | - [Callback](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/callbacks) 21 | 22 | 23 | 24 | # 2. Common Patterns 25 | 26 | In the following are implemented some example by using YARP for generical purpose, where communication are required 27 | between different actors: 28 | 29 | - [RFModule](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/rfmodule) 30 | 31 | - [Producer-Consumer](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/producer-consumer) 32 | 33 | - [Publisher-Subscriber](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/publisher-subscriber) 34 | 35 | 36 | # 3. Performance 37 | At the end, some benchmarks have been implemented to test the performance 38 | 39 | - [Benchmarks](https://github.com/s4hri/yarp-python-tutorials/tree/master/workdir/tutorials/benchmarks) 40 | 41 | 42 | # 4. How to run the tutorials (GNU/Linux) 43 | 44 | - Requirements 45 | 46 | - make 47 | - Docker CE 48 | 49 | - How to run the tutorials 50 | The tutorials run inside a Docker container. To build and run the environment you can simply do: 51 | 52 | cd yarp-python-tutorials 53 | ./run.sh 54 | 55 | A terminator process will be executed inside the Docker container just built. You can then, execute the tutorials browsing them in the file system. 56 | For example to run the first tutorial you can type: 57 | 58 | cd ports 59 | bash run.sh 60 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | 5 | main: 6 | image: ${LOCAL_DOCKER_IMAGE} 7 | 8 | working_dir: /home/docky/workdir/tutorials 9 | 10 | environment: 11 | - PROJECT_NAME=${PROJECT_NAME} 12 | - PYTHONPATH=/home/docky/workdir/tutorials 13 | 14 | command: sh -c "yarpserver --write & yarprun --server /root --log & terminator" 15 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | cd .docker && make run && cd .. 2 | -------------------------------------------------------------------------------- /workdir/.empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/.empty -------------------------------------------------------------------------------- /workdir/media/buffered_port.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/buffered_port.png -------------------------------------------------------------------------------- /workdir/media/callback.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/callback.png -------------------------------------------------------------------------------- /workdir/media/explore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/explore.png -------------------------------------------------------------------------------- /workdir/media/port.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/port.png -------------------------------------------------------------------------------- /workdir/media/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/problem.png -------------------------------------------------------------------------------- /workdir/media/pub-sub.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/pub-sub.png -------------------------------------------------------------------------------- /workdir/media/question_mark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/question_mark.png -------------------------------------------------------------------------------- /workdir/media/rfmodule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/rfmodule.png -------------------------------------------------------------------------------- /workdir/media/rpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/rpc.png -------------------------------------------------------------------------------- /workdir/media/test_port_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/test_port_1.png -------------------------------------------------------------------------------- /workdir/media/test_rf_module.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/test_rf_module.png -------------------------------------------------------------------------------- /workdir/media/test_rpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/test_rpc.png -------------------------------------------------------------------------------- /workdir/media/tested.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/tested.png -------------------------------------------------------------------------------- /workdir/media/yarp-problem1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/yarp-problem1.png -------------------------------------------------------------------------------- /workdir/media/yarp-python-tutorial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/yarp-python-tutorial.png -------------------------------------------------------------------------------- /workdir/media/yarp_problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/media/yarp_problem.png -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/README.md: -------------------------------------------------------------------------------- 1 | # YARP benchmarks 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a simpye test benchmarks to evaluate the difference performance between different 6 | type of port and connection. 7 | 8 | At this momemnt are tested: 9 | - port 10 | - rpc port 11 | - rfmodule 12 | with 2 different type of communication protocol: 13 | - tcp 14 | - fast_tcp 15 | 16 | The test consist in send 100.000 sync message and wait for the reply. Each test is repeat for 10 time. 17 | Above are reported the result from this test (each plot is plotting the mean and std for each test). 18 | 19 | 20 | ![alt text][port] 21 | ![alt text][rpc] 22 | ![alt text][rfmodule] 23 | 24 | [port]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/test_port_1.png 25 | [rpc]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/test_rpc.png 26 | [rfmodule]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/test_rf_module.png 27 | 28 | ## EXAMPLES: TEST YOURSELF 29 | 30 | ### RUN THE SCRIPTS 31 | Be sure to be in the following path inside the docker: 32 | 33 | ~$ yarp-python-tutorial/tutorials/benchmarks 34 | 35 | In each sub-folder you will find a run.sh. 36 | 37 | 38 | Open a terminal and run: 39 | ```terminal 40 | ~$ bash run.sh 41 | ``` 42 | 43 | This command will open 2 terminal 44 | 45 | - **terminal 1**: 46 | 47 | ~$ python3 test_rfmodule.py 48 | - **terminal 2**: 49 | 50 | ~$ python3 test_client_rpc.py 51 | -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/port/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 test_reader_port.py 2 | sleep 2 3 | terminator -x python3 test_writer_port.py -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/port/test_reader_port.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import time 34 | 35 | # create the network 36 | yarp.Network.init() 37 | 38 | # define ports 39 | port = yarp.Port() 40 | 41 | # activate ports 42 | port.open("/reader") 43 | port.setTimeout(10.0) 44 | 45 | # place to store thing 46 | bottle_in = yarp.Bottle() 47 | bottle_out = yarp.Bottle() 48 | 49 | while True: 50 | 51 | # read the message 52 | port.read(bottle_in, True) 53 | print("Readed: ", bottle_in.toString()) 54 | bottle_out.clear() 55 | bottle_out.addString("received") 56 | # respond to the message 57 | port.reply(bottle_out) 58 | # close the network 59 | yarp.Network.fini() 60 | -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/port/test_writer_port.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import time 34 | import numpy as np 35 | import matplotlib.pyplot as plt 36 | 37 | # create the network 38 | yarp.Network.init() 39 | 40 | # define ports 41 | port = yarp.Port() 42 | 43 | # activate ports 44 | port.open("/writer") 45 | 46 | # place to store thing 47 | bottle_in = yarp.Bottle() 48 | bottle_out = yarp.Bottle() 49 | 50 | 51 | # define 52 | kind = 2 # tcp / fast_tcp 53 | sets = 10 # how many reps 54 | item = 2 # mean & std 55 | reps = 100000 # repetitions 56 | 57 | 58 | means = np.zeros((kind, sets)) 59 | stds = np.zeros((kind, sets)) 60 | 61 | delta_t = np.zeros(reps) 62 | 63 | 64 | for k in range(kind): 65 | # choose carrier 66 | if k == 0: 67 | yarp.Network.connect(port.getName(), '/reader', 'tcp') 68 | else: 69 | yarp.Network.connect(port.getName(), '/reader', 'fast_tcp') 70 | # start test 71 | for j in range(sets): 72 | for i in range(reps): 73 | bottle_out.clear() 74 | # prepare a message to send 75 | bottle_out.addString("test") 76 | bottle_out.addFloat32(i) 77 | # send the message 78 | print ("Sending ", bottle_out.toString()) 79 | t0 = time.perf_counter() # time msg sended 80 | port.write(bottle_out, bottle_in) 81 | # reply received 82 | print ("Reply received: ", bottle_in.toString()) 83 | ti = time.perf_counter() # time reply 84 | delta_t[i] = (ti - t0) * 1000.0 85 | # statistics 86 | means[k][j] = np.mean(delta_t) 87 | stds [k][j] = np.std (delta_t) 88 | # disconnect for the new connection 89 | yarp.Network.disconnect(port.getName(), '/myModule') 90 | 91 | # plot 92 | plt.figure() 93 | plt.title("port synch") 94 | plt.ylabel("time (ms)") 95 | plt.xlabel("sets") 96 | plt.grid(axis='y') 97 | plt.errorbar(np.arange(sets), means[0], yerr=stds[0], fmt='-o', label='tcp') 98 | plt.errorbar(0.2 + np.arange(sets), means[1], yerr=stds[1], fmt='-o', label='fast_tcp') 99 | plt.legend() 100 | plt.show() 101 | 102 | # close the network 103 | yarp.Network.fini() 104 | -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rfmodule/core: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/s4hri/yarp-python-tutorials/0da65b7d054255765944cba20d609c682613d506/workdir/tutorials/benchmarks/rfmodule/core -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rfmodule/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 test_rfmodule.py 2 | sleep 2 3 | terminator -x python3 test_client_rpc.py -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rfmodule/test_client_rpc.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import time 34 | import numpy as np 35 | import matplotlib.pyplot as plt 36 | 37 | # create the network 38 | yarp.Network.init() 39 | 40 | # define ports 41 | port = yarp.RpcClient() 42 | 43 | # activate ports 44 | port.open("/client") 45 | 46 | # place to store thing 47 | bottle_in = yarp.Bottle() 48 | bottle_out = yarp.Bottle() 49 | 50 | 51 | # define 52 | kind = 2 # tcp / fast_tcp 53 | sets = 10 # how many reps 54 | item = 2 # mean & std 55 | reps = 100000 # repetitions 56 | 57 | 58 | means = np.zeros((kind, sets)) 59 | stds = np.zeros((kind, sets)) 60 | 61 | delta_t = np.zeros(reps) 62 | 63 | 64 | for k in range(kind): 65 | # choose carrier 66 | if k == 0: 67 | yarp.Network.connect(port.getName(), '/myModule', 'tcp') 68 | else: 69 | yarp.Network.connect(port.getName(), '/myModule', 'fast_tcp') 70 | # start test 71 | for j in range(sets): 72 | for i in range(reps): 73 | bottle_out.clear() 74 | # prepare a message to send 75 | bottle_out.addString("test") 76 | bottle_out.addFloat32(i) 77 | # send the message 78 | print ("Sending ", bottle_out.toString()) 79 | t0 = time.perf_counter() # time msg sended 80 | port.write(bottle_out, bottle_in) 81 | # reply received 82 | print ("Reply received: ", bottle_in.toString()) 83 | ti = time.perf_counter() # time reply 84 | delta_t[i] = (ti - t0) * 1000.0 85 | # statistics 86 | means[k][j] = np.mean(delta_t) 87 | stds [k][j] = np.std (delta_t) 88 | # disconnect for the new connection 89 | yarp.Network.disconnect(port.getName(), '/myModule') 90 | 91 | # plot 92 | plt.figure() 93 | plt.title("rpc client with rf_module") 94 | plt.ylabel("time (ms)") 95 | plt.xlabel("sets") 96 | plt.grid(axis='y') 97 | plt.errorbar(np.arange(sets), means[0], yerr=stds[0], fmt='-o', label='tcp') 98 | plt.errorbar(0.2 + np.arange(sets), means[1], yerr=stds[1], fmt='-o', label='fast_tcp') 99 | plt.legend() 100 | plt.show() 101 | 102 | # close the network 103 | yarp.Network.fini() 104 | -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rfmodule/test_rfmodule.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import sys 34 | 35 | VOCAB_QUIT = yarp.createVocab("q", "u", "i", "t") 36 | 37 | class Mymodule(yarp.RFModule): 38 | 39 | def __init__(self): 40 | yarp.RFModule.__init__(self) 41 | 42 | self.handlePort = yarp.Port() 43 | self.count = 0 44 | self.period = 0.0 45 | self.test = 0 46 | 47 | def getPeriod(self): 48 | return self.period 49 | 50 | def updateModule(self): 51 | self.count += 1 52 | print("%s updateModule" % self.count) 53 | return True 54 | 55 | def respond(self, command, reply): 56 | print("Responding to command") 57 | if command.check("period"): 58 | self.period = command.find("period").asFloat64() 59 | reply.addString("ack") 60 | return True 61 | elif command.check("test"): 62 | self.test = command.get(1).asFloat32() 63 | reply.addString("received") 64 | reply.addFloat32(self.test) 65 | return True 66 | elif command.get(0).asVocab() == VOCAB_QUIT: 67 | print("bye bye ...") 68 | reply.addString("bye") 69 | return False 70 | return True 71 | 72 | def configure(self, rf): 73 | self.count = 0 74 | self.period = 0.000000001 75 | self.handlePort.open("/myModule") 76 | self.attach(self.handlePort) 77 | if rf.check("period"): 78 | self.period = rf.find("period").asFloat64() 79 | return True 80 | 81 | def interruptModule(self): 82 | print("Interrupting your module, for port cleanup") 83 | return True 84 | 85 | def close(self): 86 | print("Calling close function") 87 | self.handlePort.close() 88 | return True 89 | 90 | 91 | if __name__ == '__main__': 92 | 93 | yarp.Network.init() 94 | module = Mymodule() 95 | rf = yarp.ResourceFinder() 96 | rf.configure(sys.argv) 97 | print("Configure module...") 98 | module.configure(rf) 99 | print("Start module...") 100 | module.runModule() 101 | print("Main returning...") 102 | -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rpc/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 test_rpc_server.py 2 | sleep 2 3 | terminator -x python3 test_rpc_client.py -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rpc/test_rpc_client.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import time 34 | import numpy as np 35 | import matplotlib.pyplot as plt 36 | 37 | # create the network 38 | yarp.Network.init() 39 | 40 | # define ports 41 | port = yarp.RpcClient() 42 | 43 | # activate ports 44 | port.open("/client") 45 | 46 | # place to store thing 47 | bottle_in = yarp.Bottle() 48 | bottle_out = yarp.Bottle() 49 | 50 | 51 | # define 52 | kind = 2 # tcp / fast_tcp 53 | sets = 10 # how many reps 54 | item = 2 # mean & std 55 | reps = 100000 # repetitions 56 | 57 | 58 | means = np.zeros((kind, sets)) 59 | stds = np.zeros((kind, sets)) 60 | 61 | delta_t = np.zeros(reps) 62 | 63 | 64 | for k in range(kind): 65 | # choose carrier 66 | if k == 0: 67 | yarp.Network.connect(port.getName(), '/server', 'tcp') 68 | else: 69 | yarp.Network.connect(port.getName(), '/server', 'fast_tcp') 70 | # start test 71 | for j in range(sets): 72 | for i in range(reps): 73 | bottle_out.clear() 74 | # prepare a message to send 75 | bottle_out.addString("test") 76 | bottle_out.addFloat32(i) 77 | # send the message 78 | print ("Sending ", bottle_out.toString()) 79 | t0 = time.perf_counter() # time msg sended 80 | port.write(bottle_out, bottle_in) 81 | # reply received 82 | print ("Reply received: ", bottle_in.toString()) 83 | ti = time.perf_counter() # time reply 84 | delta_t[i] = (ti - t0) * 1000.0 85 | # statistics 86 | means[k][j] = np.mean(delta_t) 87 | stds [k][j] = np.std (delta_t) 88 | # disconnect for the new connection 89 | yarp.Network.disconnect(port.getName(), '/myModule') 90 | 91 | # plot 92 | plt.figure() 93 | plt.title("rpc") 94 | plt.ylabel("time (ms)") 95 | plt.xlabel("sets") 96 | plt.grid(axis='y') 97 | plt.errorbar(np.arange(sets), means[0], yerr=stds[0], fmt='-o', label='tcp') 98 | plt.errorbar(0.2 + np.arange(sets), means[1], yerr=stds[1], fmt='-o', label='fast_tcp') 99 | plt.legend() 100 | plt.show() 101 | 102 | # close the network 103 | yarp.Network.fini() 104 | -------------------------------------------------------------------------------- /workdir/tutorials/benchmarks/rpc/test_rpc_server.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import time 34 | 35 | # create the network 36 | yarp.Network.init() 37 | 38 | # define ports 39 | port = yarp.RpcServer() 40 | 41 | # activate ports 42 | port.open("/server") 43 | 44 | # place to store thing 45 | bottle_in = yarp.Bottle() 46 | bottle_out = yarp.Bottle() 47 | 48 | while True: 49 | # read the message 50 | port.read(bottle_in, True) 51 | print("Readed: ", bottle_in.toString()) 52 | bottle_out.clear() 53 | bottle_out.addString("received") 54 | # respond to the message 55 | port.reply(bottle_out) 56 | # close the network 57 | yarp.Network.fini() 58 | -------------------------------------------------------------------------------- /workdir/tutorials/buffered-ports/README.md: -------------------------------------------------------------------------------- 1 | # YARP buffered-ports 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a basic example of communication between buffered-ports: 6 | - a buffered-port (writer) that will send messages 7 | - a buffered-port (reader) that will receive the messages 8 | 9 | To be able to communicate, the buffered-ports have to be connected. 10 | As communication protocols is used tcp. 11 | Messages are sended through bottles, that in this case are attribute of buffered-ports. 12 | 13 | The behavior of how buffered ports works is highlighted when the ports work a different rate. They behave in an asynch mode. 14 | 15 | In the following image we can see an usual case of how message are sended/readed between buffered-ports with different rate. 16 | ![alt text][bport] 17 | 18 | #### buffered-port write 19 | When a writer buffered-port is started, message are sended from this port, independently if there is some reader port connected. 20 | Also if a reader port is connected, the writer port will NOT wait to send a new message if the reader still has not terminated the read process. 21 | 22 | #### buffered port read 23 | When a reader buffered-port is started, message are received from this port. If there is no message available, the reader buffered-port will wait until a message is available. 24 | 25 | To found more info about buffered-ports and how they works have a look to [YARP - buffered-ports](https://www.yarp.it/latest/note_ports.html) 26 | 27 | [bport]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/buffered_port.png 28 | 29 | ## EXAMPLES: TEST YOURSELF 30 | ### RUN the script 31 | Be sure to be in the following path inside the docker 32 | 33 | 34 | ~$ yarp-python-tutorial/tutorials/buffered-port 35 | 36 | Open a terminal and run: 37 | ```terminal 38 | ~$ bash run.sh 39 | ``` 40 | 41 | This command will open 2 terminal 42 | 43 | - **terminal 1**: 44 | 45 | ~$ python3 writer_bp.py 46 | - **terminal 2**: 47 | 48 | ~$ python3 reader_bp.py 49 | 50 | ### WHAT HAPPENS IN THERE 51 | #### terminal 1 52 | The writer_bp.py script will: 53 | - open a buffered-port called: **/writer** 54 | - send 100 "Hello i-th". 55 | 56 | #### terminal 2 57 | **N.B.** : the **terminal 2** will start with a delay respect to **terminal 1**. 58 | 59 | The reader_bp.py script will: 60 | - open a buffered-port called: **/reader** 61 | - create a connection between **/writer** and **/reader** 62 | - receive only one message and then wait some time before ending the process. 63 | 64 | Notice that when the **/reader** has received the message, the **/writer** will NOT wait to send a new message either the **readerd** has not terminated. 65 | -------------------------------------------------------------------------------- /workdir/tutorials/buffered-ports/reader_bp.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define ports 38 | inport = yarp.BufferedPortBottle() 39 | 40 | # activate ports 41 | inport.open("/reader") 42 | 43 | # create connection betweeen ports inside the network: 44 | # note: connect(writer, reader) ok 45 | # connect(reader, writer) not ok 46 | yarp.Network.connect("/writer", inport.getName()) 47 | 48 | # read the messge 49 | bottle = inport.read().toString() 50 | print("Received ", bottle) 51 | yarp.delay(5) 52 | 53 | # deactivate ports 54 | inport.close() 55 | 56 | # close the network 57 | yarp.Network.fini() 58 | -------------------------------------------------------------------------------- /workdir/tutorials/buffered-ports/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 writer_bp.py 2 | sleep 4 3 | terminator -x python3 reader_bp.py -------------------------------------------------------------------------------- /workdir/tutorials/buffered-ports/writer_bp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | # Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT) 4 | # Copyright (C) 2006-2010 RobotCub Consortium 5 | # All rights reserved. 6 | # 7 | # This software may be modified and distributed under the terms of the 8 | # BSD-3-Clause license. See the accompanying LICENSE file for details. 9 | 10 | import yarp 11 | 12 | # create the network 13 | yarp.Network.init() 14 | 15 | # define ports 16 | outport = yarp.BufferedPortBottle() 17 | 18 | # activate ports 19 | outport.open("/writer") 20 | 21 | top = 100 22 | for i in range(1,top): 23 | # prepare a message to send 24 | bottle = outport.prepare() 25 | bottle.clear() 26 | bottle.addString("Hello") 27 | bottle.addInt32(i) 28 | print ("Sending ", bottle.toString()) 29 | 30 | # send the message 31 | outport.write() 32 | yarp.delay(0.5) 33 | 34 | # deactivate ports 35 | outport.close() 36 | 37 | # close the network 38 | yarp.Network.fini() -------------------------------------------------------------------------------- /workdir/tutorials/callbacks/README.md: -------------------------------------------------------------------------------- 1 | # YARP callback 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a basic example of communication between buffered-ports using callback: 6 | - a buffered-port (writer) that will send messages 7 | - a buffered-port (reader) that will receive the messages through callback 8 | 9 | To be able to communicate, the buffered-ports have to be connected. 10 | As communication protocols is used tcp. 11 | Messages are sended through bottles, that in this case are attribute of buffered-ports. 12 | 13 | The behavior of how callback works is highlighted when the ports work a different rate, expecally when the reader rate is greater thank writer rate. 14 | They behave in an asynch mode. 15 | 16 | In the following image we can see an usual case of how message are sended/readed between buffered-ports with different rate. 17 | ![alt text][bport] 18 | 19 | #### buffered-port writer 20 | When a writer buffered-port is started, message are sended from this port, independently if there is some reader port connected. 21 | Also if a reader port is connected, the writer port will NOT wait to send a new message if the reader still has not terminated the read process. 22 | 23 | #### buffered port read by callback 24 | When a reader buffered-port by callback is started, message are received from this port. If there is no message available, the reader buffered-port by callback 25 | will no wait for a new message. Instead, the process will go on with the same value from the last readed message. It will be automatically update when a new message 26 | is received through the port. 27 | 28 | To found more info about callback and how they works have a look to [YARP - buffered-ports](http://www.yarp.it/git-master/port_expert.html) 29 | 30 | [bport]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/callback.png 31 | 32 | ## EXAMPLES: TEST YOURSELF 33 | ### RUN the script 34 | Be sure to be in the following path inside the docker 35 | 36 | 37 | ~$ yarp-python-tutorial/tutorials/callback 38 | 39 | Open a terminal and run: 40 | ```terminal 41 | ~$ bash run.sh 42 | ``` 43 | 44 | This command will open 2 terminal 45 | 46 | - **terminal 1**: 47 | 48 | ~$ python3 writer_bp.py 49 | - **terminal 2**: 50 | 51 | ~$ python3 reader_bp_callback.py 52 | 53 | ### WHAT HAPPENS IN THERE 54 | #### terminal 1 55 | The writer_bp.py script will: 56 | - open a buffered-port called: **/writer** 57 | - send 100 "Hello i-th". 58 | 59 | #### terminal 2 60 | **N.B.** : the **terminal 2** will start with a delay respect to **terminal 1**. 61 | 62 | The reader_bp_callback.py script will: 63 | - open a buffered-port called: **/reader** 64 | - create a connection between **/writer** and **/reader** 65 | - continuously receiving updated message 66 | 67 | Notice how the **/reader** has not loop inside the script, and then still receving update message from the **/writer**. 68 | -------------------------------------------------------------------------------- /workdir/tutorials/callbacks/reader_bp_callback.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | yarp.Network.init() 35 | 36 | # alternative callback classes depending on the underlying data type: 37 | # - yarp.PropertyCallback 38 | # - yarp.TypedReaderCallbackImage(Rgb|Rgba|Mono|Mono16|Int|Float|RgbFloat) 39 | # - yarp.TypedReaderCallbackVector (yarp.DVector) 40 | # - yarp.TypedReaderCallbackSound 41 | class CustomCallback(yarp.BottleCallback): 42 | def __init__(self): 43 | super().__init__() 44 | # remove this constructor if no class members need to be initialized, 45 | # keep the above parent constructor invocation otherwise 46 | 47 | def onRead(self, bot, reader): 48 | print("Port %s received: %s" % (reader.getName(), bot.toString())) 49 | 50 | # alternative buffered port classes depending on the underlying data type: 51 | # - yarp.BufferedPortProperty 52 | # - yarp.BufferedPortImage(Rgb|Rgba|Mono|Mono16|Int|Float|RgbFloat) 53 | # - yarp.BufferedPortVector (yarp.DVector) 54 | # - yarp.BufferedPortSound 55 | p = yarp.BufferedPortBottle() 56 | p.open("/reader") 57 | c = CustomCallback() 58 | p.useCallback(c) 59 | 60 | print("Callback ready at port " + p.getName()) 61 | input("Press ENTER to quit\n") 62 | 63 | p.interrupt() 64 | p.close() 65 | 66 | yarp.Network.fini() 67 | -------------------------------------------------------------------------------- /workdir/tutorials/callbacks/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 writer_bp.py 2 | terminator -x python3 reader_bp_callback.py 3 | sleep 1 4 | yarp connect /writer /reader -------------------------------------------------------------------------------- /workdir/tutorials/callbacks/writer_bp.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define ports 38 | outport = yarp.BufferedPortBottle() 39 | 40 | # activate ports 41 | outport.open("/writer") 42 | 43 | top = 100 44 | for i in range(1,top): 45 | # prepare a message to send 46 | bottle = outport.prepare() 47 | bottle.clear() 48 | bottle.addString("Hello") 49 | bottle.addInt32(i) 50 | print ("Sending ", bottle.toString()) 51 | 52 | # send the message 53 | outport.write() 54 | yarp.delay(0.5) 55 | 56 | # deactivate ports 57 | outport.close() 58 | 59 | # close the network 60 | yarp.Network.fini() 61 | -------------------------------------------------------------------------------- /workdir/tutorials/ports/README.md: -------------------------------------------------------------------------------- 1 | # YARP ports 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a basic example of communication between ports: 6 | - a port (writer) that will send messages 7 | - a port (reader) that will receive the messages 8 | 9 | To be able to communicate, the ports have to be connected. 10 | As communication protocols is used tcp. 11 | Messages are sended through bottles objects. 12 | 13 | The behavior of how ports works is highlighted when the ports work a different rate. 14 | 15 | In the following image we can see an usual case of how message are sended/readed between ports with different rate. 16 | ![alt text][port] 17 | 18 | #### port write 19 | When a writer port is started, message are sended from this port, independently if there is some reader port connected. 20 | Once a reader port is connected, the writer port will wait to send a new message until the reader has terminate the read process. 21 | 22 | #### port read 23 | When a reader port is started, message are received from this port. If there is no message available, the reader port will wait until a message is available. 24 | 25 | To found more info about ports and how they works have a look to [YARP - ports](https://www.yarp.it/latest/note_ports.html) 26 | 27 | [port]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/port.png 28 | 29 | ## EXAMPLES: TEST YOURSELF 30 | 31 | ### RUN THE SCRIPTS 32 | Be sure to be in the following path inside the docker: 33 | 34 | ~$ yarp-python-tutorial/workdir/tutorials/port 35 | 36 | 37 | Open a terminal and run: 38 | ```terminal 39 | ~$ bash run.sh 40 | ``` 41 | 42 | This command will open 2 terminal 43 | 44 | - **terminal 1**: 45 | 46 | ~$ python3 writer.py 47 | - **terminal 2**: 48 | 49 | ~$ python3 reader.py 50 | 51 | ### WHAT HAPPENS IN THERE 52 | #### terminal 1 53 | The writer.py script will: 54 | - open a port called: **/writer** 55 | - send 100 "Hello i-th". 56 | 57 | #### terminal 2 58 | **N.B. **: the **terminal 2** will start with a delay respect to **terminal 1**. 59 | 60 | The reader.py script will: 61 | - open a port called: **/reader** 62 | - create a connection between **/writer** and **/reader** 63 | - receive only one message and then wait some time before ending the process. 64 | 65 | Notice that when the **/reader** has received the message, the **/writer** will wait until the **readerd** has terminated to send a new message. 66 | -------------------------------------------------------------------------------- /workdir/tutorials/ports/reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define ports 38 | inport = yarp.Port() 39 | bottle = yarp.Bottle() 40 | 41 | # activate ports 42 | inport.open("/reader") 43 | 44 | # create connection betweeen ports inside the network: 45 | # note: connect(writer, reader) ok 46 | # connect(reader, writer) not ok 47 | yarp.Network.connect("/writer", inport.getName()) 48 | 49 | # read the messge 50 | inport.read(bottle) 51 | print("Received ", bottle.toString()) 52 | yarp.delay(5) 53 | 54 | # close the network 55 | yarp.Network.fini() 56 | -------------------------------------------------------------------------------- /workdir/tutorials/ports/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 writer.py 2 | sleep 4 3 | terminator -x python3 reader.py 4 | -------------------------------------------------------------------------------- /workdir/tutorials/ports/writer.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define ports 38 | outport = yarp.Port() 39 | bottle = yarp.Bottle() 40 | 41 | # activate ports 42 | outport.open("/writer") 43 | 44 | top = 100 45 | for i in range(1,top): 46 | # prepare a message to send 47 | bottle.clear() 48 | bottle.addString("Hello") 49 | bottle.addInt32(i) 50 | print ("Sending ", bottle.toString()) 51 | 52 | # send the message 53 | outport.write(bottle) 54 | yarp.delay(0.5) 55 | 56 | # close the network 57 | yarp.Network.fini() 58 | -------------------------------------------------------------------------------- /workdir/tutorials/producer-consumer/README.md: -------------------------------------------------------------------------------- 1 | # YARP producer-consumer 2 | 3 | ## INTRODUCTION 4 | 5 | The producer-consumer problem is a classic example of multi-process synchronization problem. 6 | In a generic producer-consumer problem there are 3 parts: 7 | 1. prodcuer 8 | 2. consumer 9 | 3. buffer 10 | 11 | In this repository is reproduced a producer-consumer problem with the 12 | following features: 13 | - rate_producer different from rate_consumer 14 | - bounded buffer 15 | - unbounded producer 16 | - unbounded consumer 17 | ![alt_text][problem] 18 | 19 | 20 | ## YARP PROBLEM 21 | 22 | Let's translate the basic example of producer-consumer by using yarp ports. 23 | 24 | ### PROBLEM SOLUTION 25 | 26 | #### SOLUTION 1 27 | A first solution coud be by using 3 different ports: 28 | - a port (**/producer**) that will send items to the buffer 29 | - a port (**/consumer**) that will receive the messages from the buffer 30 | - a port (**/buffer**) that will receive the messages from the producer and send the messages to the consumer 31 | ![alt_text][yarp_problem1] 32 | 33 | With this approach we are not able to perform different rate for producer and consumer, since producer and consumer are connected to the same "buffer" port. 34 | 35 | #### SOLUTION 2 36 | To avoid this issue, we can add an additional port to the "buffer", so that producer and consumer can have a personal port and perform different rate. In total 37 | there will be 4 different ports: 38 | - a port (**/producer**) that will send items to the buffer 39 | - a port (**/consumer**) that will receive the items from the buffer 40 | - a port (**/buffer_in**) that will receive the items from the producer 41 | - a port (**/buffer_out**) that will send the items to the consumer 42 | 43 | ![alt_text][yarp_problem] 44 | 45 | ### PORT USAGE 46 | 47 | We will continue this example using the **SOLUTION 2** with 4 ports. 48 | 49 | #### /producer 50 | When the **/producer** port is started, the **/producer** port will send items to the **/buffer_in** port. 51 | 52 | If the buffer is full, **/producer** port will wait until the buffer is not full again. 53 | 54 | #### /consumer 55 | When a **/consumer** port is started, the **/consumer** port is ready to receive the items from the **/buffer_out** port. 56 | 57 | If the buffer is empty, **/consumer** port will wait until the buffer is not empty again. 58 | 59 | #### /buffer_in 60 | When the **/buffer_in** port is started, the **/buffer_in** port is ready to receive the items from the **/producer** port. 61 | 62 | If the buffer is full, **/buffer_in** port will append the read process until the buffer is not full again. 63 | 64 | 65 | #### /buffer_out 66 | When the **/buffer_out** port is started, the **/buffer_out** port is ready to send items to the **/consumer** port. 67 | 68 | If the buffer is empty, **/buffer_out** port append the write process until the buffer is not empty again. 69 | 70 | 71 | [problem]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/problem.png 72 | [yarp_problem]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/yarp_problem.png 73 | [yarp_problem1]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/yarp-problem1.png 74 | 75 | ## EXAMPLES: TEST YOURSELF 76 | 77 | ### RUN THE SCRIPTS 78 | Be sure to be in the following path inside the docker: 79 | 80 | ~$ yarp-python-tutorial/tutorials/producer-consumer/ 81 | 82 | 83 | Open a terminal and run: 84 | ```terminal 85 | ~$ bash run.sh 86 | ``` 87 | 88 | 89 | This command will open 3 terminal (inside the container): 90 | - **terminal 1**: 91 | 92 | ~$ python3 producer.py 93 | - **terminal 2**: 94 | 95 | ~$ python3 consumer.py 96 | - **terminal 3**: 97 | 98 | ~$ python3 buffer.py 99 | 100 | ### WHAT HAPPENS IN THERE 101 | #### terminal 1 102 | The producer.py script will: 103 | - open a port called: **/producer** 104 | - send n items to the **/buffer_in** port 105 | - perfom tasks at random time (**rate_producer**) 106 | 107 | #### terminal 2 108 | The consumer.py script will: 109 | - open a port called: **/consumer** 110 | - receive items from the **/buffer_out** port 111 | - perfom tasks at random time (**rate_consumer**) 112 | 113 | #### terminal 3 114 | The buffer.py script will: 115 | - open a port called: **/buffer_in** 116 | - open a port called: **/buffer_out** 117 | - receive items through **/buffer_in** port from the **/producer** port 118 | - send items from the **/buffer_out** port to **/consumer** port 119 | - perform task with **rate_producer** and **rate_consumer** 120 | -------------------------------------------------------------------------------- /workdir/tutorials/producer-consumer/buffer.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import threading 34 | from threading import Condition 35 | from queue import Queue 36 | import time 37 | 38 | 39 | ### ----------- DEFINE ------------ ### 40 | ## PORTS 41 | # define ports and bottles of node 42 | port_in = yarp.Port() 43 | port_out = yarp.Port() 44 | bottle_in = yarp.Bottle() 45 | bottle_out = yarp.Bottle() 46 | 47 | # define port name 48 | producer= "/producer" 49 | consumer = "/consumer" 50 | 51 | 52 | ## CLASSE BUFFER 53 | # define a queue with conditions 54 | class Buffer: 55 | def __init__(self, size): 56 | self.Common = Queue(size) 57 | self.EmptyCondition = Condition() 58 | self.FullCondition = Condition() 59 | 60 | 61 | ## PROCESSES 62 | # buffer read from producer: 63 | def buffer_reader(q, port, bottle, prod): 64 | print("Buffer reader started") 65 | while True: 66 | # read messge 67 | port.read(bottle) 68 | # exit condition 69 | if q.Common.empty() and not yarp.Network.exists(prod): 70 | break 71 | # waiting condition 72 | elif q.Common.full(): 73 | with q.FullCondition: 74 | print("buffer full, waiting consumer") 75 | q.FullCondition.wait() 76 | q.Common.put(bottle.toString()) 77 | print("added ", bottle.toString(), " in buffer: ", list(q.Common.queue)) 78 | # normal condition 79 | else: 80 | with q.EmptyCondition: 81 | q.Common.put(bottle.toString()) 82 | q.EmptyCondition.notify() 83 | print("added ", bottle.toString(), " in buffer: ", list(q.Common.queue)) 84 | port.close() 85 | print("Buffer reader ended") 86 | 87 | # buffer write to consumer 88 | def buffer_writer(q, port, bottle, prod, cons): 89 | exitFlag = False 90 | print("Buffer writer started") 91 | while not exitFlag: 92 | # before writing check connection 93 | if yarp.Network.isConnected(port.getName(), cons): 94 | if q.Common.empty(): 95 | # exit condition 96 | if not yarp.Network.exists(prod): 97 | exitFlag = True 98 | break 99 | # waiting condition 100 | else: 101 | with q.EmptyCondition: 102 | print("buffer empty, waiting producer") 103 | q.EmptyCondition.wait() 104 | # normal condition 105 | else: 106 | with q.FullCondition: 107 | # prepare the message 108 | bottle.clear() 109 | item = q.Common.get() 110 | bottle.addString(item) 111 | # send the message 112 | port.write(bottle) 113 | q.FullCondition.notify() 114 | print("taken ", bottle.toString(), " from buffer: ", list(q.Common.queue)) 115 | port.close() 116 | print("Buffer writer ended") 117 | 118 | 119 | ### ----------- MAIN ------------ ### 120 | 121 | # initialize queue 122 | queue = Buffer(3) 123 | 124 | # create the network 125 | yarp.Network.init() 126 | 127 | # active ports 128 | port_in.open("/buffer_in") 129 | port_out.open("/buffer_out") 130 | 131 | # initialize process 132 | br = threading.Timer(0, buffer_reader, [queue, port_in, bottle_in, producer]) 133 | bw = threading.Timer(0, buffer_writer, [queue, port_out, bottle_out, producer, consumer]) 134 | 135 | # start process 136 | br.start() 137 | bw.start() 138 | 139 | # waiting end process 140 | br.join() 141 | bw.join() 142 | 143 | # close the network 144 | yarp.Network.fini() 145 | -------------------------------------------------------------------------------- /workdir/tutorials/producer-consumer/consumer.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | from random import randint 34 | import time 35 | 36 | # create the network 37 | yarp.Network.init() 38 | 39 | # define port and bottle 40 | port = yarp.Port() 41 | bottle = yarp.Bottle() 42 | 43 | # activate ports 44 | port.open("/consumer") 45 | 46 | # define port name to connect 47 | buffer = "/buffer_out" 48 | 49 | print("Consumer started") 50 | while True: 51 | # read messge 52 | port.read(bottle) 53 | print("Received ", bottle.toString()) 54 | time.sleep(randint(1,7)) 55 | 56 | # deactivate ports 57 | port.close() 58 | print("Consumer ended") 59 | 60 | # close the network 61 | yarp.Network.fini() 62 | -------------------------------------------------------------------------------- /workdir/tutorials/producer-consumer/producer.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import random 34 | from random import randint 35 | import time 36 | 37 | # create the network 38 | yarp.Network.init() 39 | 40 | # define port and bottle 41 | port = yarp.Port() 42 | bottle = yarp.Bottle() 43 | 44 | # activate port 45 | port.open("/producer") 46 | 47 | # define port name to connect 48 | buffer = "/buffer_in" 49 | 50 | # items to produce 51 | itemsToProduce = ["apple", "orange", "pear", "banana", "kiwi", "watermelon", "peach", "grapes","strawberry"] 52 | 53 | print("Producer writer started") 54 | while itemsToProduce: 55 | # before writing check connection 56 | if yarp.Network.isConnected(port.getName(), buffer): 57 | # prepare the message 58 | bottle.clear() 59 | item = random.choice(itemsToProduce) 60 | itemsToProduce.remove(item) 61 | bottle.addString(item) 62 | # send message 63 | port.write(bottle) 64 | print ("sended ", bottle.toString()) 65 | time.sleep(randint(1,2)) 66 | 67 | # deactivate ports 68 | port.close() 69 | print("Producer writer ended") 70 | 71 | # close the network 72 | yarp.Network.fini() 73 | -------------------------------------------------------------------------------- /workdir/tutorials/producer-consumer/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 buffer.py 2 | terminator -x python3 producer.py 3 | terminator -x python3 consumer.py 4 | sleep 1 5 | yarp connect /producer /buffer_in 6 | yarp connect /buffer_out /consumer -------------------------------------------------------------------------------- /workdir/tutorials/publisher-subscriber/README.md: -------------------------------------------------------------------------------- 1 | # YARP publisher-subscriber 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a basic example of publisher-subscriber: 6 | - a **publisher** (writer) will send messages at certain rate 7 | - more **subscribers** (reader) can read the message at different rates 8 | 9 | To be able to read from publisher, the subscribers have to be connected. 10 | Buffered ports are used for this example. 11 | Tcp is used as communication protocol. 12 | Messages are sended through bottles, that in this case are attribute of buffered-ports. 13 | 14 | Publisher-subsriber behave in an asynch mode. 15 | 16 | In the following image we can see an example of publisher-subscirber, where each subscriber has a different rate. 17 | ![alt text][port] 18 | 19 | #### Publisher 20 | When a publisher is started, message are sended trhough buffered port, independently if there is some subscriber connected. 21 | 22 | #### Subscriber 23 | When a subscriber is started, message are readed from this port. If there is no message available, the subscriber will wait until a message is available. 24 | 25 | [port]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/pub-sub.png 26 | 27 | ## EXAMPLES: TEST YOURSELF 28 | ### RUN the script 29 | Be sure to be in the following path inside the docker. 30 | 31 | ~$ yarp-python-tutorial/workdir/tutorials/publish-subscribe 32 | 33 | 34 | Open a terminal and run: 35 | ```terminal 36 | ~$ bash run.sh 37 | ``` 38 | 39 | This command will open 4 terminal 40 | 41 | 42 | - **terminal 1**: 43 | 44 | ~$ python3 publisher.py 45 | - **terminal 2**: 46 | 47 | ~$ python3 subscriber1.py 48 | - **terminal 3**: 49 | 50 | ~$ python3 subscriber2.py 51 | - **terminal 4**: 52 | 53 | ~$ python3 subscriber3.py 54 | 55 | ### WHAT HAPPENS IN THERE 56 | #### terminal 1 57 | The publisher.py script will: 58 | - open a buffered-port called: **/publisher** 59 | - send 1000 "sended: i-th" with rate: 0.5 s. 60 | 61 | #### terminal 2 62 | The subscriber1.py script will: 63 | - open a buffered-port called: **/subscriber1** 64 | - create a connection between **/publisher** and **/subscriber1** 65 | - read the messages with rate: 1 s. 66 | 67 | #### terminal 3 68 | The subscriber1.py script will: 69 | - open a buffered-port called: **/subscriber2** 70 | - create a connection between **/publisher** and **/subscriber2** 71 | - read the messages with rate: 3 s. 72 | 73 | #### terminal 4 74 | The subscriber1.py script will: 75 | - open a buffered-port called: **/subscriber3** 76 | - create a connection between **/publisher** and **/subscriber3** 77 | - read the messages with rate: 5 s. 78 | 79 | Notice how each **subscriber** read from the **publisher** at different rate. 80 | -------------------------------------------------------------------------------- /workdir/tutorials/publisher-subscriber/publisher.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import random 34 | from random import randint 35 | import time 36 | 37 | # create the network 38 | yarp.Network.init() 39 | 40 | # define port and bottle 41 | port = yarp.BufferedPortBottle() 42 | bottle = port.prepare() 43 | 44 | # activate port 45 | port.open("/publisher") 46 | 47 | top = 1000 48 | for i in range(1,top): 49 | bottle = port.prepare() 50 | bottle.clear() 51 | bottle.addInt32(i) 52 | # send the message 53 | port.write() 54 | print("sended: ", i) 55 | yarp.delay(0.5) 56 | 57 | # deactivate ports 58 | port.close() 59 | 60 | # close the network 61 | yarp.Network.fini() 62 | -------------------------------------------------------------------------------- /workdir/tutorials/publisher-subscriber/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 publisher.py 2 | terminator -x python3 subscriber1.py 3 | terminator -x python3 subscriber2.py 4 | terminator -x python3 subscriber3.py 5 | sleep 1 6 | yarp connect /publisher /subscriber1 7 | yarp connect /publisher /subscriber2 8 | yarp connect /publisher /subscriber3 -------------------------------------------------------------------------------- /workdir/tutorials/publisher-subscriber/subscriber1.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define port and bottle 38 | port = yarp.BufferedPortBottle() 39 | bottle = port.prepare() 40 | 41 | # activate ports 42 | port.open("/subscriber1") 43 | 44 | while True: 45 | # read messge 46 | bottle = port.read().toString() 47 | print("Received ", bottle) 48 | yarp.delay(1) 49 | 50 | # deactivate ports 51 | port.close() 52 | # close the network 53 | yarp.Network.fini() 54 | -------------------------------------------------------------------------------- /workdir/tutorials/publisher-subscriber/subscriber2.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define port and bottle 38 | port = yarp.BufferedPortBottle() 39 | bottle = port.prepare() 40 | 41 | # activate ports 42 | port.open("/subscriber2") 43 | while True: 44 | # read messge 45 | bottle = port.read().toString() 46 | print("Received ", bottle) 47 | yarp.delay(3) 48 | 49 | # deactivate ports 50 | port.close() 51 | # close the network 52 | yarp.Network.fini() 53 | -------------------------------------------------------------------------------- /workdir/tutorials/publisher-subscriber/subscriber3.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define port and bottle 38 | port = yarp.BufferedPortBottle() 39 | bottle = port.prepare() 40 | 41 | # activate ports 42 | port.open("/subscriber3") 43 | 44 | while True: 45 | # read messge 46 | bottle = port.read().toString() 47 | print("Received ", bottle) 48 | yarp.delay(5) 49 | 50 | # deactivate ports 51 | port.close() 52 | 53 | # close the network 54 | yarp.Network.fini() 55 | -------------------------------------------------------------------------------- /workdir/tutorials/rfmodule/README.md: -------------------------------------------------------------------------------- 1 | # YARP RFModule 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a basic example of RFModule. 6 | 7 | To be able to communicate with a RFModule you can use a RPC Client port. 8 | As communication protocols is used tcp. 9 | 10 | The RFModule help you to wrtie a generic module. 11 | 12 | In the following image we can see a generic RFModule: it comes with pre-determinate function class, which are editable 13 | in the case we need some specific beahavior. Basically it will need at least one port (RPCServer or Port, in this example 14 | called "myModule") to be able to 15 | receive command. In addiction, with a RPC (Client) port it is possible to send command to the RFModule. 16 | In this case the RPC (Client) port works as an interface to control the RFModule. 17 | 18 | ![alt text][rfm] 19 | 20 | #### RFModule 21 | When a **RFModule** is started, a RPCServer Port (or simple Port) is open ready to receive some command. If the RFModule 22 | receive a message from the RPC Client, the RFModule will send a reply to RPC Client. 23 | Meanwhile, the RFModule it will start its configuration and functionality. The RFModule will be update at certain period/rate (also this parameter 24 | could be editable). 25 | 26 | #### RPC Client 27 | When a **RPC Client** is started, message are sended from this port to the **RFModule**. Once a message it sended, RPC Client 28 | will wait for the answer from the RFModule. 29 | 30 | To found more info about RFModule and how they works have a look to [YARP - RFModule](http://www.yarp.it/git-master/yarp_rfmodule_tutorial.html) 31 | 32 | [rfm]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/rfmodule.png 33 | 34 | ## EXAMPLES: TEST YOURSELF 35 | 36 | ### RUN THE SCRIPTS 37 | Be sure to be in the following path inside the docker: 38 | 39 | yarp-python-tutorial/workdir/tutorials/rfmodule 40 | 41 | 42 | Open a terminal and run: 43 | ```terminal 44 | ~$ bash run.sh 45 | ``` 46 | 47 | This command will open 2 terminal 48 | 49 | - **terminal 1**: 50 | 51 | ~$ python3 test_rfmodule.py 52 | 53 | - **terminal 2**: 54 | 55 | ~$ yarp rpc /myModule 56 | 57 | ### WHAT HAPPENS IN THERE 58 | #### terminal 1 59 | The test_rfmodule.py script will: 60 | - open a port called: **/myModule** 61 | - start the module (init, configure, ...) 62 | - update the module (each period, editable) 63 | - handle the command received 64 | - handle the endign of the module (closing, stopping, ..) 65 | 66 | #### terminal 2 67 | The RPC Client port will: 68 | - enable you to send message (in this case command) to the RFModule 69 | -------------------------------------------------------------------------------- /workdir/tutorials/rfmodule/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 test_rfmodule.py 2 | sleep 2 3 | terminator -x yarp rpc /myModule -------------------------------------------------------------------------------- /workdir/tutorials/rfmodule/test_rfmodule.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | """ 31 | 32 | import yarp 33 | import sys 34 | 35 | VOCAB_QUIT = yarp.createVocab("q", "u", "i", "t") 36 | 37 | class Mymodule(yarp.RFModule): 38 | 39 | def __init__(self): 40 | yarp.RFModule.__init__(self) 41 | 42 | self.handlePort = yarp.Port() 43 | self.count = 0 44 | self.period = 0.0 45 | 46 | def getPeriod(self): 47 | return self.period 48 | 49 | def updateModule(self): 50 | self.count += 1 51 | print("%s updateModule" % self.count) 52 | return True 53 | 54 | def respond(self, command, reply): 55 | print("Responding to command") 56 | if command.check("period"): 57 | self.period = command.find("period").asFloat64() 58 | reply.addString("ack") 59 | return True 60 | elif command.get(0).asVocab() == VOCAB_QUIT: 61 | print("bye bye ...") 62 | reply.addString("bye") 63 | #yarp.RFModule.stopModule(False) 64 | return False 65 | #return yarp.RFModule.respond(command, reply) 66 | return True 67 | 68 | def configure(self, rf): 69 | self.count = 0 70 | self.period = 1.0 71 | self.handlePort.open("/myModule") 72 | self.attach(self.handlePort) 73 | if rf.check("period"): 74 | self.period = rf.find("period").asFloat64() 75 | return True 76 | 77 | def interruptModule(self): 78 | print("Interrupting your module, for port cleanup") 79 | return True 80 | 81 | def close(self): 82 | print("Calling close function") 83 | self.handlePort.close() 84 | return True 85 | 86 | 87 | if __name__ == '__main__': 88 | 89 | yarp.Network.init() 90 | module = Mymodule() 91 | rf = yarp.ResourceFinder() 92 | rf.configure(sys.argv) 93 | print("Configure module...") 94 | module.configure(rf) 95 | print("Start module...") 96 | module.runModule() 97 | print("Main returning...") 98 | -------------------------------------------------------------------------------- /workdir/tutorials/rpc/README.md: -------------------------------------------------------------------------------- 1 | # YARP RPC ports 2 | 3 | ## INTRODUCTION 4 | 5 | In this repository is implemented a basic example of communication between RPC ports: 6 | - a RPCClient (**/client**) that will send messages 7 | - a RPCServer (**/server**) that will receive the messages 8 | 9 | To be able to communicate, the RPC ports have to be connected. 10 | As communication protocols is used tcp. 11 | Messages are sended through bottles objects. 12 | 13 | The principal behavior of RPC ports is getting replies when a message is sended. 14 | 15 | In the following image we can see an usual case of how message are sended/received/replied between ports with different rate. 16 | ![alt text][rpc] 17 | 18 | #### RPCClient 19 | When a **/client** is started, message are sended from this port, independently if there is some **/server** connected. 20 | Once a **/client** is connected, the **/client** will wait to send a new message until receive the reply from **/server**. 21 | 22 | #### RPCServer 23 | When a **/server** is started, message are received from this port. If there is no message available, the **/server** port will wait until a new message is 24 | available. 25 | 26 | To found more info about RPC ports and how they works have a look to [YARP - RPC ports](http://www.yarp.it/git-master/rpc_ports.html) 27 | 28 | [rpc]:https://github.com/s4hri/yarp-python-tutorials/blob/master/workdir/media/rpc.png 29 | 30 | ## EXAMPLES: TEST YOURSELF 31 | 32 | ### RUN THE SCRIPTS 33 | Be sure to be in the following path inside the docker: 34 | 35 | 36 | yarp-python-tutorial/workdir/tutorials/rpc 37 | 38 | 39 | Open a terminal and run: 40 | ```terminal 41 | ~$ bash run.sh 42 | ``` 43 | 44 | This command will open 2 terminal 45 | 46 | - **terminal 1**: 47 | 48 | ~$ python3 client.py 49 | 50 | - **terminal 2**: 51 | 52 | ~$ python3 server.py 53 | 54 | ### WHAT HAPPENS IN THERE 55 | #### terminal 1 56 | The client.py script will: 57 | - open a port called: **/client** 58 | - send 100 "Hello i-th". 59 | - print, if exists, the reply from the **/server** port 60 | 61 | #### terminal 2 62 | The server.py script will: 63 | - open a port called: **/server** 64 | - receive the message from the **/client** port and send a reply. 65 | 66 | Notice that when the **/server** has received the message, the **/client** will wait until the **server** send back a reply. 67 | -------------------------------------------------------------------------------- /workdir/tutorials/rpc/client.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """ 32 | 33 | import yarp 34 | 35 | # create the network 36 | yarp.Network.init() 37 | 38 | # define ports 39 | port = yarp.RpcClient() 40 | 41 | # activate ports 42 | port.open("/client") 43 | 44 | top = 100 45 | for i in range(1,top): 46 | # prepare a message to send 47 | bottle_in = yarp.Bottle() 48 | bottle_out = yarp.Bottle() 49 | bottle_out.addString("Hello") 50 | bottle_out.addInt32(i) 51 | print ("Sending ", bottle_out.toString()) 52 | # send the message 53 | port.write(bottle_out, bottle_in) 54 | print ("Reply received: ", bottle_in.toString()) 55 | yarp.delay(0.5) 56 | 57 | # close the network 58 | yarp.Network.fini() 59 | -------------------------------------------------------------------------------- /workdir/tutorials/rpc/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x python3 client.py 2 | terminator -x python3 server.py 3 | sleep 1 4 | yarp connect /client /server -------------------------------------------------------------------------------- /workdir/tutorials/rpc/server.py: -------------------------------------------------------------------------------- 1 | """ 2 | BSD 2-Clause License 3 | 4 | Copyright (c) 2021, Nicola Severino Russi (nicola.russi@iit.it), 5 | Davide De Tommaso (davide.detommaso@iit.it) 6 | Social Cognition in Human-Robot Interaction 7 | Istituto Italiano di Tecnologia, Genova 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright notice, this 14 | list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | """ 32 | import yarp 33 | 34 | # create the network 35 | yarp.Network.init() 36 | 37 | # define ports 38 | port = yarp.RpcServer() 39 | 40 | # activate ports 41 | port.open("/server") 42 | 43 | # place to store thing 44 | bottle_in = yarp.Bottle() 45 | bottle_out = yarp.Bottle() 46 | 47 | top = 100 48 | for i in range(1,top): 49 | port.read(bottle_in, True) 50 | print("Readed: ", bottle_in.toString()) 51 | bottle_out.clear() 52 | msg = "Message " + bottle_in.toString() + " received in cycle: " 53 | bottle_out.addString(msg) 54 | bottle_out.addInt32(i) 55 | port.reply(bottle_out) 56 | yarp.delay(3) 57 | 58 | # close the network 59 | yarp.Network.fini() 60 | -------------------------------------------------------------------------------- /workdir/tutorials/yarpimage/run.sh: -------------------------------------------------------------------------------- 1 | terminator -x yarpdev --device fakeFrameGrabber --name /grabber 2 | terminator -x yarpview 3 | sleep 1 4 | yarp connect /grabber /yarpview/img:i 5 | terminator -x python3 snap.py 6 | -------------------------------------------------------------------------------- /workdir/tutorials/yarpimage/snap.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | import yarp 3 | import matplotlib.pyplot as plt 4 | 5 | yarp.Network.init() 6 | 7 | 8 | input_port = yarp.Port() 9 | input_port.open("/snap") 10 | yarp.Network.connect("/grabber", "/snap") 11 | 12 | img_array = numpy.zeros((240, 320, 3), dtype=numpy.uint8) 13 | yarp_image = yarp.ImageRgb() 14 | yarp_image.resize(320, 240) 15 | yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0]) 16 | 17 | input_port.read(yarp_image) 18 | plt.imshow(img_array) 19 | plt.show() 20 | 21 | input_port.close() 22 | --------------------------------------------------------------------------------