在海龟仿真器下输入按键,通过服务端接收数据,并把数据发送给海龟仿真器
1.创建功能包
在上一篇博客创建的功能包下开发,所以就不用创建了 然后在你创建的learning_service文件下的src文件下创建一个pose_subscriber.cpp文件
2.在.cpp文件中输入以下内容
<code class="prism language-c has-numbering"><span class="token comment">/** * 该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger */</span> <span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"><ros/ros.h></span></span> <span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"><geometry_msgs/Twist.h></span></span> <span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"><std_srvs/Trigger.h></span></span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span>Publisher turtle_vel_pub<span class="token punctuation">;</span> bool pubCommand <span class="token operator">=</span> false<span class="token punctuation">;</span> <span class="token comment">// service回调函数,输入参数req,输出参数res</span> bool <span class="token function">commandCallback</span><span class="token punctuation">(</span>std_srvs<span class="token punctuation">:</span><span class="token punctuation">:</span>Trigger<span class="token punctuation">:</span><span class="token punctuation">:</span>Request <span class="token operator">&</span>req<span class="token punctuation">,</span> std_srvs<span class="token punctuation">:</span><span class="token punctuation">:</span>Trigger<span class="token punctuation">:</span><span class="token punctuation">:</span>Response <span class="token operator">&</span>res<span class="token punctuation">)</span> <span class="token punctuation">{</span> pubCommand <span class="token operator">=</span> <span class="token operator">!</span>pubCommand<span class="token punctuation">;</span> <span class="token comment">// 显示请求数据</span> <span class="token function">ROS_INFO</span><span class="token punctuation">(</span><span class="token string">"Publish turtle velocity command [%s]"</span><span class="token punctuation">,</span> pubCommand<span class="token operator">==</span>true<span class="token operator">?</span><span class="token string">"Yes"</span><span class="token punctuation">:</span><span class="token string">"No"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置反馈数据</span> res<span class="token punctuation">.</span>success <span class="token operator">=</span> true<span class="token punctuation">;</span> res<span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token string">"Change turtle command state!"</span><span class="token punctuation">;</span> <span class="token keyword">return</span> true<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>argv<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// ROS节点初始化</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">init</span><span class="token punctuation">(</span>argc<span class="token punctuation">,</span> argv<span class="token punctuation">,</span> <span class="token string">"turtle_command_server"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建节点句柄</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span>NodeHandle n<span class="token punctuation">;</span> <span class="token comment">// 创建一个名为/turtle_command的server,注册回调函数commandCallback</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span>ServiceServer command_service <span class="token operator">=</span> n<span class="token punctuation">.</span><span class="token function">advertiseService</span><span class="token punctuation">(</span><span class="token string">"/turtle_command"</span><span class="token punctuation">,</span> commandCallback<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10</span> turtle_vel_pub <span class="token operator">=</span> n<span class="token punctuation">.</span>advertise<span class="token operator"><</span>geometry_msgs<span class="token punctuation">:</span><span class="token punctuation">:</span>Twist<span class="token operator">></span><span class="token punctuation">(</span><span class="token string">"/turtle1/cmd_vel"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 循环等待回调函数</span> <span class="token function">ROS_INFO</span><span class="token punctuation">(</span><span class="token string">"Ready to receive turtle command."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置循环的频率</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span>Rate <span class="token function">loop_rate</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>ros<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 查看一次回调函数队列</span> ros<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">spinOnce</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 如果标志为true,则发布速度指令</span> <span class="token keyword">if</span><span class="token punctuation">(</span>pubCommand<span class="token punctuation">)</span> <span class="token punctuation">{</span> geometry_msgs<span class="token punctuation">:</span><span class="token punctuation">:</span>Twist vel_msg<span class="token punctuation">;</span> vel_msg<span class="token punctuation">.</span>linear<span class="token punctuation">.</span>x <span class="token operator">=</span> <span class="token number">0.5</span><span class="token punctuation">;</span> vel_msg<span class="token punctuation">.</span>angular<span class="token punctuation">.</span>z <span class="token operator">=</span> <span class="token number">0.2</span><span class="token punctuation">;</span> turtle_vel_pub<span class="token punctuation">.</span><span class="token function">publish</span><span class="token punctuation">(</span>vel_msg<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//按照循环频率延时</span> loop_rate<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> </code>
综上,如何实现一个服务器
- 初始化ROS节点;
- 创建Server实例
- 循环等待服务请求,进入回调函数;
- 在回调函数中完成服务功能的处理,并反馈应答数据。
3.编辑CMakeLists.txt
加上这两句
<code class="prism language-c has-numbering"><span class="token function">add_executable</span><span class="token punctuation">(</span>turtle_command_server src<span class="token operator">/</span>turtle_command_server<span class="token punctuation">.</span>cpp<span class="token punctuation">)</span> <span class="token function">target_link_libraries</span><span class="token punctuation">(</span>turtle_command_server $<span class="token punctuation">{</span>catkin_LIBRARIES<span class="token punctuation">}</span><span class="token punctuation">)</span> </code>
4.编译并运行发布者
<code class="prism language-c has-numbering">cd <span class="token operator">/</span>catkin_ws catkin_make </code>
5.验证
<code class="prism language-c has-numbering">roscore rosrun turtlesim turtlesim_node rosrun learning_service turtle_command_server rosservice call <span class="token operator">/</span>turtle_command <span class="token string">"{}"</span> </code>
当然,以上四个命令都是在不同终端下输入 第四个命令是Trigger的参数,输入为空
结果