一、需求
1、需求
C++服务通信,做个巡逻海龟。
让小海龟在海龟模拟器中随机游走进行巡逻。
2、分析:
1)控制海龟到达目标点我知道,怎么改成动态接收?服务
2)用什么接口?自定义的
3)随机游走?客户端来产生随机点,请求巡逻服务
3、整体框架
二、自定义服务接口
1、自定义服务接口
2、注册
3、构建
4、查看接口定义
ros2 interface show chapt4_interfaces/srv/Partol
查看前一定要source。
三、服务端代码实现
1、创建功能包
ros2 pkg create demo_cpp_service --build-type ament_cmake --dependencies chapt4_interfaces rclcpp geometry_msgs turtlesim --license Apache-2.0
turtlesim:小海龟的位置功能包
geometry_msgs:控制的话题命令的功能包
2、创建服务
1)编写turtle_control.cpp
2)修改CMakeLists.txt
3)启动
记得启动小海龟模拟器:ros2 run turtlesim turtlesim_node
4)查看
ros2 service list
5)请求服务
新开终端,一定要记得source
rqt:使用界面来调用
四、客户端代码实现
1、实现步骤
1)创建客户端和定时器
2)定时产生目标标点并请求服务端巡逻
2、编写partol_client.cpp
2、修改CMakeLists.txt
3、启动
启动客户端节点前,先启动turtlesim和turtle_control节点,最后启动我们的client。