[PYTHON] ROS course 107 Make a client for rosblidge

environment

This article works in the following environment.

item value
CPU Core i5-8250U
Ubuntu 16.04
ROS Kinetic
Gazebo 7.0.0
python 2.7.12

For installation, refer to ROS Course 02 Installation. Also, the program in this article has been uploaded to github. See ROS Course 11 git repository.

Overview

Last time I set up rosbridge_server and introduced an example of accessing from javascript of the browser. This time, use roslibpy to create a client in python that connects to rosbridge_server.

Installation

pip problem

This time we will use pip to install roslibpy. The trouble with pip (rather than python) is that there are various directories to install, and mixing them can be quite annoying. For example

This will pollute the environment and cause an error in pip when running the following command

I get an error after doing this


sudo apt install python-pip
pip install --upgrade pip

There are many problems with using apt and pip together, so it is better to install without using apt. If you have done the above, you can uninstall all pips with the following command.

Uninstall pip


sudo python -m pip uninstall pip
sudo apt autoremove python-pip

Command to execute

You can install roslibpy by executing the following.

Install roslibpy


curl -kL https://bootstrap.pypa.io/get-pip.py | sudo python
sudo pip install roslibpy
sudo pip install service_identity

Source code

web_lecture/scripts/rosbridge_client.py


#!/usr/bin/env python
# -*- coding:utf8 -*-
import time
from roslibpy import Message, Ros, Topic
import time

class rosbridge_client:
    def __init__(self):
        self.ros_client = Ros('127.0.0.1', 9090)
        print("wait for server")
        self.publisher = Topic(self.ros_client, '/cmd_vel', 'geometry_msgs/Twist')
        self.listener = Topic(self.ros_client, '/odom', 'nav_msgs/Odometry')
        self.listener.subscribe(self.callback)

        self.ros_client.on_ready(self.start_thread, run_in_thread=True)
        self.ros_client.run_forever()

    def callback(self, message):
        x = message["pose"]["pose"]["position"]["x"]
        y = message["pose"]["pose"]["position"]["y"]
        print(x, y)

    def start_thread(self):
        while True:
            if self.ros_client.is_connected:
                self.publisher.publish(Message({
                    'linear': {
                        'x': 0.5,
                        'y': 0,
                        'z': 0
                    },
                    'angular': {
                        'x': 0,
                        'y': 0,
                        'z': 0.5
                    }
                }))
            else:
                print("Disconnect")
                break
            time.sleep(1.0)

if __name__ == '__main__':
    rosbridge_client()

Run

Run sim


roslaunch web_lecture web_if.launch 

Run roslibpy client


roscd web_lecture/scripts && rospy_client.py`You can do it with.

web_client.gif

Use Case

rosbridge is a tool for accessing ros from a system where ros is not originally installed. However, if you use this loosely coupled system, you can also build a multi-master system as shown in the figure below. rosbridge_connection.png

reference

roslibpy example Details of implementation

Link to table of contents page

Link to ROS course table of contents

Recommended Posts

ROS course 107 Make a client for rosblidge
Make a Tweet box for Pepper
Building a conda environment for ROS users
Make a chessboard pattern for camera calibration
Let's make a Backend plugin for Errbot
Make a bot for Skype on EC2 (CentOS)
Make a histogram for the time being (matplotlib)
Make for VB6.
Let's make a module for Python using SWIG
[For play] Let's make Yubaba a LINE Bot (Python)
Make a squash game
Make a function decorator
Make a distance matrix
I'll make a password!
Make a Nyan button
Make a Tetris-style game!
Make a Base64 decoder
Make an IP address allocation / allocation list for a certain area
Experiment to make a self-catering PDF for Kindle with Python
How to make a Python package (written for an intern)