ros工作空间和功能包
** 所有的ROS程序,包括我们自己开发的程序,都被组织成功能包
** ROS的功能包被存放在工作空间。
** 因此,在我们写程序之前,第一步是创建一个工作空间以容纳我们的功能包。ROS工作空间就是linux下的一个目录,创建ROS工作空间就是创建一个linux目录(默认情况下我们创建名为catkin_ws的工作空间),只是需要按照ROS的规范在这个目录下添加一个src的子目录,然后执行一个ROS的命令。
具体参见:
https://blog.csdn.net/wwwlyj123321/article/details/83147242
话题通信
在工作空间目录下创建一个功能包 hello
catkin_create_pkg hello roscpp
该功能包在工作空间src目录下生成了一个hello文件夹
在功能包hello的src目录下创建hello.cpp
内容:
#include "ros/ros.h"
#include<iostream>
using namespace std;
int main(int argc, char **argv)
{
cout<<"hello ros!"<<endl;
return 0;
}
修改hello功能包目录下的CMakeLists.txt文件
打开该文件,添加:
## Declare a cpp executable
add_executable(printf_hello src/hello.cpp)
编译工作空间
catkin_make
打开新终端,启动ros核
roscore
在打开一个新终端,运行
rosrun hello printf_hello
就会出现
hello ros!
服务通信
新建功能包hello2,在其src目录下创建server节点和client节点
实现两个整数相加并输出和
add_two_ints_server.cpp
#include "ros/ros.h"
#include "learning_communication/AddTwoInts.h"
// service回调函数,输入参数req,输出参数res
bool add(learning_communication::AddTwoInts::Request &req,
learning_communication::AddTwoInts::Response &res)
{
// 将输入参数中的请求数据相加,结果放到应答变量中
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "add_two_ints_server");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个名为add_two_ints的server,注册回调函数add()
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
// 循环等待回调函数
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
add_two_ints_client.cpp
#include <cstdlib>
#include "ros/ros.h"
#include "learning_communication/AddTwoInts.h"
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "add_two_ints_client");
// 从终端命令行获取两个加数
if (argc != 3)
{
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
// 创建节点句柄
ros::NodeHandle n;
// 创建一个client,请求add_two_int service,service消息类型是learning_communication::AddTwoInts
ros::ServiceClient client = n.serviceClient<learning_communication::AddTwoInts>("add_two_ints");
// 创建learning_communication::AddTwoInts类型的service消息
learning_communication::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
// 发布service请求,等待加法运算的应答结果
if (client.call(srv))
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}
编译节点,在功能包的CMakeLists.txt中加入
add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${
catkin_LIBRARIES})
add_dependencies(add_two_ints_server learning_communication_gencpp)
add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${
catkin_LIBRARIES})
add_dependencies(add_two_ints_client learning_communication_gencpp)
功能包名:learning_communication
编译
cd ~/catkin_ws/
catkin_make
运行
终端一
roscore
终端二
rosrun learning_communication add_two_ints_server
终端三
rosrun learning_communication add_two_ints_client 53 66
参考:
https://blog.csdn.net/weixin_42237429/article/details/90301566
Rviz安装使用
本人ubantu版本18.04
安装
终端命令
sudo apt-get install ros-melodic-rviz
rosdep install rviz
rosmake rviz
使用rviz调用电脑摄像头
1.设置usb控制器兼容性为3.0
2.启动ros
roscore
3.新建终端,运行相机节点
rosrun uvc_camera uvc_camera_node
4.新建终端,查看相机和图像信息
rostopic list
5.打印相机信息,后用ctrl+c停止打印
rostopic echo /camera_info
6.打开摄像头界面
rosrun image_view image_view image:=/image_raw
7.启动rviz
rosrun rviz rviz
点击add,选择image
设置image topic属性
然后就会在rviz中出现摄像头画面