[ROS study notes] (seven) the realization of the server server

First, the purpose

The server waits for a signal, and every time it receives a signal from the client, the turtle's motion state is switched once (movement→stop, stop→movement)

Two, create server code

~/catkin_ws/src/learning_service/srcCreate a turtle_command_server.cppfile in the directory

/**
 * 该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger
 REFERENCE:www.guyuehome.com
 */
 
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <std_srvs/Trigger.h>

ros::Publisher turtle_vel_pub;
bool pubCommand = false;

// service回调函数,输入参数req,输出参数res
bool commandCallback(std_srvs::Trigger::Request  &req,
         			std_srvs::Trigger::Response &res)
{
	pubCommand = !pubCommand;

    // 显示请求数据
    ROS_INFO("Publish turtle velocity command [%s]", pubCommand==true?"Yes":"No");

	// 设置反馈数据
	res.success = true;
	res.message = "Change turtle command state!";

    return true;
}

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

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

    // 创建一个名为/turtle_command的server,注册回调函数commandCallback
    ros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback);

	// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
	turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

    // 循环等待回调函数
    ROS_INFO("Ready to receive turtle command.");

	// 设置循环的频率
	ros::Rate loop_rate(10);

	while(ros::ok())
	{
		// 查看一次回调函数队列
    	ros::spinOnce();
		
		// 如果标志为true,则发布速度指令
		if(pubCommand)
		{
			geometry_msgs::Twist vel_msg;
			vel_msg.linear.x = 0.5;
			vel_msg.angular.z = 0.2;
			turtle_vel_pub.publish(vel_msg);
		}

		//按照循环频率延时
	    loop_rate.sleep();
	}

    return 0;
}

Implementation process:

  1. Initialize the ROS node
  2. Create Server instance
  3. Wait for the service request in a loop and enter the callback function
  4. Complete the processing of the service function in the callback function, and feedback the response data
    Insert picture description here

Three, configure the server code compilation rules

Open learning_service, CMakeLists.txtadd code in the image area

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

Compile turtle_command_server.cpp into turtle_command_server file, and link the library files that need to be depended on at the same time.
Insert picture description here

Four, compile and run

Compile server

cd ~/catkin_ws
cadkin_make

Entry into force of environment variables (if already in .bashrcthe added environment variable is not need to perform this step)

source devel/setup.bash

Then run the following code (the following three lines need to start a terminal each)

roscore
rosrun turtlesim turtlesim_node
rosrun learning_service turtle_command_server

Start a terminal again, enter the code to rosservice call /turtle_command+空格+两次Tabsend a signal, the turtle starts to move, send the same signal again , the little turtle stops.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44543463/article/details/114139830
Recommended