ros(6) service server编程实现

创建代码

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,注册回调函数commandCallbackros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback);// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10turtle_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;
}

修改cmake

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

编译

catkin_make

运行

roscore

rosrun turtlesim turtlesmi_node

rosrun service_example turtle_command_server

rosservice call /turtle_command tab键

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/510095.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

程序员要如何学英语?

一般来说&#xff0c;程序员可算是英语水平比较好的群体&#xff0c;因为在这个行业&#xff0c;英文资料是最全面、最及时&#xff0c;对英文资料的需求也最迫切的。就我观察&#xff0c;刚入门不久的程序员一般都能查阅英文文档&#xff0c;找到需要的信息。但是另一方面&…

Impala 查询导出到文本,本地系统

Hive查询导出到文本使用 insert overwrite local directory 在Impala中不支持&#xff0c;Impala使用&#xff0c;注意--output_delimiter与-B 一同使用&#xff0c;否则不生效 impala-shell -q "select birthday,b.uid from dw_crawler.bsl_weibo_article a,ods_xxx.…

html怎么设计自动出现提示的数据列表?怎么设计数据列表像百度一样怎么随用户输入而更新

<!DOCTYPE html> <html><head><meta charset"utf-8" /><title>hello</title></head><body><h2>输入查询的城市</h2><form autocomplete"on"><input type"text" id"s…

程序员从优秀到卓越的几点建议

和其他技术一样&#xff0c;编程也有层次阶段之分——业余爱好者、普通级别和专家级别。关于这个问题我问过很多很多次—— 如何从优秀到卓越&#xff1f;这是一个程序员应该扪心自问的最重要的问题。至少&#xff0c;在他们职业生涯的某些时候。 为了给出一个令人满意的答案&a…

ros(7)自定义service数据

创建Persom.srv 新建srv文件夹 在srv文件夹中创建Persom.srv&#xff0c;编辑文件 string name uint8 age uint8 sexuint8 unknown 0 uint8 male 1 uint8 female 2--- string result 修改package,xml add_service_files(FILESPerson.srv )generate_messages(DEPE…

Hive表 Parquet压缩 , Gzip,Snappy,uncompressed 效果对比

创建两张表&#xff0c;通过一种是parquet &#xff0c; 一种使用parquet snappy压缩 创建表 使用snappy CREATE EXTERNAL TABLE IF NOT EXISTS tableName(xxx string) partitioned by (pt_xvc string) ROW FORMAT DELIMITED FIELDS TERMINATED BY \001 STORED AS PARQUET TB…

html中怎么自动获得搜索文本框的光标焦点

<!DOCTYPE html> <html> <head> <meta charset"utf-8" /> <title>hello</title> </head> <body> <form action"demo_form.php"method"get"> <!--一个页面只能指定一个autofo…

ubuntu16.04 + kinetic +turtlebot2配置

安装ros-kinetic请参考ubuntu 16.0安装ros-kinetic 配置turtlebot2 1.更新 sudo apt-get update2.安装turtlebot相应的源 sudo apt-get install ros-kinetic-turtlebot ros-kinetic-turtlebot-apps ros-kinetic-turtlebot-interactions ros-kinetic-kobuki-ftdi ros-kineti…

Impala UDF - Impala调用Hive UDF函数

Impala 中运行 Hive UDF 场景&#xff1a;部分查询需要快速返回&#xff0c;使用Impala进行快速、复杂的查询 1.简单的UDF函数过滤&#xff0c;判断是否包含“好”字&#xff0c;返回boolean类型 import org.apache.hadoop.hive.ql.exec.UDF;/*** ClassName: UdfTest* Descri…

想要成为真正优秀的程序员是不是真的很难?

很多人认为要想成为一名优秀的程序员&#xff0c;那就需要一天24小时每天不断地编程——睁开眼睛要编程&#xff0c;闭上眼睛睡觉的时候还要梦到编程——我认为这是一种矫枉过正的方法。沿着这条路走&#xff0c;只会让你精疲力尽&#xff0c;犹如夸父逐日一般&#xff0c;死在…

Impala查询 - HDFS缓存数据

HDFS缓存数据命令 查看缓存池信息 hdfs cacheadmin -listPools -stats 查看已缓存的数据信息 hdfs cacheadmin -listDirectives -stats Impala表卸载缓存数据 alter table dw_crawler.bsl_zhongda_weibo_article_hive set uncached; 创建缓存池 hdfs cacheadmin -addPool a…

Turtlebot入门-配置网络

配置网络 在工作站和Turtlebot都要同步时钟 输入如下命令&#xff1a; sudo apt-get install chrony ntpdate sudo ntpdate ntp.ubuntu.com 2.获取Turtlebot和工作站的IP地址&#xff0c;在终端执行&#xff1a; ifconfig sudo service ssh status 如果显示ssh: unrecogn…

RTTI-运行时类型识别

RTTI 编辑RTTI&#xff08;Run-Time Type Information)&#xff0c;通过运行时类型信息程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。中文名RTTI外文名Run-Time Type Information属 于程序函 数typeid目录 1 RTTI介绍 ▪ typeid函数 ▪ …

html怎样实现数据列表的下拉效果

目前支持浏览器&#xff1a;火狐、欧朋。。 谷歌不支持 <!DOCTYPE html> <html> <head> <meta charset"utf-8" /> <title>hello</title> </head> <body> <form action"testform.php"method&q…

Java 调用 Impala - JDBC 调用Impala

java通过JDBC 调用Impala服务 Maven项目中 pom.xml引用<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.1</version></dependency><dependency><groupId>…

Linux 、shell 时间函数 - 获取七天前所在周

[rootiZj6c3ral1ugubn9usrsi8Z ~]# echo $(date -d -7day %Y%W) 201912

动态创建二维数组

int **p; p new int*[10]; //注意&#xff0c;int*[10]表示一个有10个元素的指针数组 for (int i 0; i ! 10; i) { p[i] new int[5]; } 这里是将p作为一个指向指针的指针&#xff0c;它指向一个包含10个元素的指针数组&#xff0c;并且每个元素指向一个有5个元素的数…

html上传文件

<!DOCTYPE html> <html> <head> <meta charset"utf-8" /> <title>hello</title> </head> <body> <form action"testform.php"method"get"> 请选择上传的多个文件&#xff1a;&l…

相机参数关系

焦距 f 35mm 最高分辨率&#xff1a;42562832 传感器尺寸&#xff1a;36.023.9 mm 根据以上定义可以有&#xff1a;u0 4256/2 2128 v0 2832/2 1416 dx 36.0/4256 dy 23.9/2832 fx f/dx 4137.8 fy f/dy 4147.3

Kafka 不停机修改某一个topic数据保存时间

查看topic信息 ./kafka-topics.sh --describe --zookeeper zk:2181 --topic topicName25/03/08 16:05:29 INFO zkclient.ZkClient: zookeeper state changed (SyncConnected) Topic:topicName PartitionCount:3 ReplicationFactor:3 Configs:Topic: topicName …