Examples of ros communication methods

ros workspace and feature pack

** All ROS programs, including those developed by ourselves, are organized
into functional packages ** ROS functional packages are stored in the workspace.
** Therefore, before we write the program, the first step is to create a workspace to accommodate our functional package. The ROS workspace is a directory under linux. To create a ROS workspace is to create a linux directory (by default we create a workspace named catkin_ws), but you need to add a src subdirectory to this directory according to the ROS specification. Then execute a ROS command.
For details, please refer to:
https://blog.csdn.net/wwwlyj123321/article/details/83147242

Topic communication

Create a function package hello in the workspace directory

catkin_create_pkg hello roscpp

Insert picture description here
The function package generates a hello folder in the src directory of the workspace

Create the hello.cpp
content in the src directory of the function package hello :

#include "ros/ros.h"
#include<iostream>
using namespace std;
int main(int argc, char **argv)
{
    
    
    cout<<"hello ros!"<<endl;
    return 0;
 
 
}

Modify the CMakeLists.txt file in the hello package directory.
Open the file and add:

## Declare a cpp executable
add_executable(printf_hello src/hello.cpp)

Compile workspace

catkin_make

Open a new terminal and start the ros core

roscore

After opening a new terminal, run

rosrun hello printf_hello

Will appear

hello ros!

Service communication

Create a new function package hello2, create a server node and a client node in its src directory to
add two integers and output the sum

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;
}

Compile the node and add it to the CMakeLists.txt of the function package

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)

Function package name: learning_communication
compilation

cd ~/catkin_ws/
catkin_make

Run
terminal one

roscore

Terminal two

rosrun learning_communication add_two_ints_server

Terminal three

rosrun learning_communication add_two_ints_client 53 66

Reference:
https://blog.csdn.net/weixin_42237429/article/details/90301566

Rviz installation and use

I
installed ubantu version 18.04
Terminal command

sudo apt-get install ros-melodic-rviz

rosdep install rviz

rosmake rviz

Use rviz to call the computer camera
1. Set the usb controller compatibility to 3.0
2. Start ros

roscore

3. Create a new terminal and run the camera node

rosrun uvc_camera uvc_camera_node

4. Create a new terminal to view camera and image information

rostopic list

5. Print the camera information, then use ctrl+c to stop printing

rostopic echo /camera_info

6. Open the camera interface

rosrun image_view image_view image:=/image_raw

7. Start rviz

rosrun rviz rviz

Click add, select image

Insert picture description here
Insert picture description here
Set image topic properties

Insert picture description here
Then the camera screen will appear in rviz

Guess you like

Origin blog.csdn.net/xianyudewo/article/details/115029775