ROS入门之Server

Client发送std_srvs::Trigger信号给Server,Server反馈Ressponse给Client。

1.配置CMakeLists.txt:

  • 设置需要编译的代码和生成的可执行文件
  • 设置链接库
add_executable(turtle_command_server src/turtle_command_server.cpp)
target_link_libraries(turtle_command_server ${catkin_LIBRARIES})

2.源码:

turtle_command_server.cpp

/**
 * 该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger
 */
 
#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;
}

3.编译:
在catkin_make目录下执行catkin_make

4.运行:

roscore
rosrun turtlesim turtlesim_node
rosrun learning_service turtle_command_server
rosservice call /turtle_command "{}"
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目錄 前言 第一章 ROS简介 机器人时代的到来 ROS发展历程 什么是ROS 安装ROS 安装ROS-Academy-for-Beginners教学包 二进制与源码包 安装RoboWare Studio 单元测试一 第二章 ROS文件系统 Catkin编译系统 Catkin工作空间 Package软件包 CMakeLists.txt package.xml Metapacakge软件元包 其他常见文件类型 单元测试二 第三章 ROS通信架构(一) Node & Master Launch文件 Topic Msg 常见msg类型 单元测试三 第四章 ROS通信架构(二) Service Srv Parameter server 11.5.4 1.5.5 1.5.6 1.5.7 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.6.1 1.9.6.2 Action 常见srv类型 常见action类型 单元测试四 第五章 常用工具 Gazebo RViz Rqt Rosbag Rosbridge moveit! 单元测试五 第六章 roscpp Client Library与roscpp 节点初始、关闭与NodeHandle Topic in roscpp Service in roscpp Param in roscpp 时钟 日志与异常 第七章 rospy Rospy与主要接口 Topic in rospy Service in rospy Param与Time 第八章 TF与URDF 认识TF TF消息 tf in c++ tf in python 统一机器人描述格式 附录:TF数学基础 三维空间刚体运动---旋转矩阵 三维空间刚体运动---欧拉角 21.9.6.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.11 1.11.1 1.11.2 1.11.3 1.11.4 1.12 1.12.1 1.12.2 1.12.3 1.12.4 1.12.5 1.12.6 1.12.7 1.12.8 1.12.9 1.12.10 1.12.11 1.12.12 1.12.13 1.12.14 1.12.15 1.12.16 三维空间刚体运动---四元数 第九章 SLAM 地图 Gmapping Karto Hector 第十章 Navigation Navigation Stack move_base costmap Map_server & Amcl 附录:Navigation工具包说明 amcl local_base_planner carrot_planner clear_costmap_recovery costmap_2d dwa_local_planner fake_localization global_planner map_server move_base_msg move_base move_slow_and_clear navfn nav_core robot_pose_ekf rotate_recovery 3中国大学MOOC---《机器人操作系统入门》 课 程讲义

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值