Getting Started with ROS Application Development Client Programming

ROS is the abbreviation of Robot Operating System. This article introduces ROS application development and client programming. The function of the program is to create a simulated turtle with code. C++ code first, then python code, you can also choose to look at only one that you are familiar with.

Function package creation

The  ROS workspace  is established in the article ROS Development and Application Preparation: Create a Workspace . Now, create a functional package:

cd ~/catkin_ws/src

catkin_create_pkg  learning_service std_msgs roscpp rospy geometry_msgs turtlesim

The first line is to return to the src directory of the workspace. The function package creation must be run in this directory.

The second line is the command to create the function package, the first parameter is the name of the function package, here is learning_service, and the next is the dependency library of the function package, here is std_msgs roscpp rospy geometry_msgs turtlesim There are 5 dependency libraries.

After the above command is executed, you can see a directory under the src directory, the name is learning_service, and the following content:

CMakeLists.txt  include  package.xml  scripts  src

Among them, scripts were manually created later to store python code.

c++ source code

In the src directory, which is also the ~/catkin_ws/src/learning_service/src directory, create a file turtle_spawn.cpp,

cd ~//catkin_ws/src/learning_service/src

nano turtle_spawn.cpp

The content is:

/**
 * 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn
 */

#include <ros/ros.h>
#include <turtlesim/Spawn.h>

int main(int argc, char** argv)
{
    // 初始化ROS节点
	ros::init(argc, argv, "turtle_spawn");

    // 创建节点句柄
	ros::NodeHandle node;

    // 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
	ros::service::waitForService("/spawn");
	ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");

    // 初始化turtlesim::Spawn的请求数据
	turtlesim::Spawn srv;
	srv.request.x = 2.0;
	srv.request.y = 2.0;
	srv.request.name = "turtle2";

    // 请求服务调用
	ROS_INFO("Call service to spwan turtle[x:%0.6f, y:%0.6f, name:%s]", 
			 srv.request.x, srv.request.y, srv.request.name.c_str());

	add_turtle.call(srv);

	// 显示服务调用结果
	ROS_INFO("Spwan turtle successfully [name:%s]", srv.response.name.c_str());

	return 0;
};


This code has clear comments. This is a basic process for implementing the client:

Initialize the ROS node

Create node handle

Create a service client and connect to the service named /spawn

Initialize the request data of turtlesim::Spawn

Request service call

Display service call results

Configure cmake file

In the ~/catkin_ws/src/learning_service/ directory, there is a CMakeLists.txt file, we need to modify this file

cd ~/catkin_ws/src/learning_service

nano CMakeLists.txt 

Add the following 2 lines to this file,

add_executable(turtle_spawn src/turtle_spawn.cpp)
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})

The added location is the last part of Build. Refer to the following location, which is ## install ## in front:

# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )

add_executable(turtle_spawn src/turtle_spawn.cpp)
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})

#############
## Install ##
#############

 Save, exit

This completes the compilation and configuration.

Compile and run the test

Compilation must go back to the ~/catkin_ws directory

cd ~/catkin_ws

catkin_make

It should be sourced once after compilation:

source devel/setup.bash

If there is an error in the compilation, you must eliminate the error, and then run the test.

Open a terminal, start ros, execute

roscore

Open another terminal and run the turtle

rosrun turtlesim turtlesim_node

The above is to start the test environment. If there is a problem, you can refer to:  The simulation turtle under ROS , but the keyboard control is not started.

Finally execute in our terminal:

rosrun learning_service turtle_spawn

After executing the above command, we see one more turtle, now there are two.

This completes the development of c++.

python code 

In order not to mix with C++, create a scripts directory under the ~/catkin_ws/src/learning_service/ directory, and then go to this directory and create a turtle_spawn.py file

cd ~/catkin_ws/src/learning_service/scripts

nano turtle_spawn.py

The content of the file is:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn

import sys
import rospy
from turtlesim.srv import Spawn

def turtle_spawn():
	# ROS节点初始化
    rospy.init_node('turtle_spawn')

	# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
    rospy.wait_for_service('/spawn')
    try:
        add_turtle = rospy.ServiceProxy('/spawn', Spawn)

		# 请求服务调用,输入请求数据
        response = add_turtle(3.0,3.0, 0.0, "turtle3")
        return response.name
    except rospy.ServiceException, e:
        print "Service call failed: %s"%e

if __name__ == "__main__":
	#服务调用并显示调用结果
    print "Spwan turtle successfully [name:%s]" %(turtle_spawn())

There are clear instructions in the code, and the process is:

ROS node initialization

After discovering the /spawn service, create a service client and connect to the service named /spawn

Request service call, enter request data

python run test

Python can be executed directly without compilation,

Change turtle_spawn.py to have execution attributes 

chmod +x *.py

This can be checked with ls -l:

At the same time, it should be sourced once: 

source ~/catkin_ws/devel/setup.bash

First, you need to start the test environment. One terminal starts roscore, and another terminal starts the turtle rosrun turtlesim turtlesim_node. If you have any problems, you can refer to:  The simulation turtle under ROS , but the keyboard control is not activated. 

Then execute the following command:

rosrun learning_service turtle_spawn.py

The result of the execution is that there are more turtles.

The source code can also be downloaded at https://github.com/huchunxu/ros_21_tutorials

That's it for the introduction.
 

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/114994379