yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
AI View Depth camera📖
01.Linux basic course
  • 1.Introduction to Linux system
  • 2.Ubuntu file system
  • 3.Ubuntu common commands
  • 4.Ubuntu common editors
  • 5.Ubuntu software operation commands
  • 6.Virtual machine installation
  • 7.SSH remote control
  • 8.VNC remote control
  • 9.Transfer files remotely
  • 10.Driver library and communication
  • 11.Static IP and hotspot mode
  • 12.Bind device ID
  • 13.Capacity expansion and resource allocation
  • 14.Update system software sources
  • 15.Set root user password
  • 16.sudo free password
  • 17.Connect to WiFi network
  • 18.View system version
  • 19.Customized service management
  • 20.Back up system image
02.ROS1-camera usage course
  • 1. Camera usage instructions
  • 2. Environment Construction
  • 3. Camera recording video
03.ROS1-OpenCV course
  • 1.Opencv Source CV Introduction
  • 2.Image reading and display
  • 3.Picture writing
  • 4.Picture Quality
  • 5.Pixel Operation
  • 6.Picture scaling
  • 7.Picture cutting
  • 8.Picture translation
  • 9.Picture mirroring
  • 10.Affine transformation
  • 11.Picture rotation
  • 12.Perspective transformation
  • 13.Grayscale processing
  • 14.Image binarization
  • 15.Edge detection
  • 16.Line segment drawing
  • 17.Rectangle and circle drawing
  • 18.Text and picture drawing
4.ROS1-basic course
  • 1.ROS introduction
  • 2.ROS installation
  • 3.ROS common command tools
  • 4.ROS workspace
  • 5.ROS function package
  • 6.ROS node
  • 7.ROS topic publisher
  • 8.ROS topic subscribers
  • 9.ROS service client
  • 10.ROS service server
  • 11.ROS action client
  • 12.ROS action server
  • 13.ROS custom message reception
  • 14.ROS-launch file
  • 15.ROS-TF transformation
  • 16.ROS parameter service
  • 17.ROS-rviz use
  • 18.ROS-rqt tool usage
  • 19.Topic message recording and playback
  • 20.urdf model introduction
  • 21.gazebo introduction
  • 22.ROS distributed communication
5.ROS1-Mediapipe course
  • 1.Hand detection
  • 2.Posture detection
  • 3.Overall detection
  • 4.Face detection
  • 5.Face recognition
  • 6.Face effects
  • 7.3D object recognition
  • 8.Brush
  • 9.Finger control
  • 10.Gesture recognition
6.ROS+Opencv application
  • 1.Camera calibration
  • 2.QR code
  • 3.Human body pose estimation
  • 4.Target detection
  • 5.ROS+Opencv basics
  • 6.Face recognition
  • 7.Harris corner detection
  • 8.Target tracking algorithm
  • 9.Contour moment
  • 10.Polygon outline
  • 11.Discrete Fourier transform algorithm
  • 12.Edge detection algorithm
  • 13.Face detection algorithm
  • 14.Optical flow detection algorithm
  • 15.Contour detection
  • 16.General contour detection
  • 17.Feature point tracking
  • 18.HLS color filtering
  • 19.Hough circle detection
  • 20.Hough line detection
  • 21.HSV color filtering
  • 22.LK optical flow algorithm
  • 23.Human body detection algorithm
  • 24.Phase-related displacement detection
  • 25.Image pyramid sampling algorithm
  • 26.RGB color filtering
  • 27.Clear background detection algorithm
  • 28.Simplified optical flow algorithm
  • 29.Simple filter
  • 30.Threshold image processing
  • 31.Watershed segmentation algorithm
  • 32.View point cloud data
  • 33.AR vision
  • 34.AR QR code tracking
  • 35.Color tracking
  • 36.Object tracking
07.ROS2-camera usage course
  • Instructions for using camera
08.ROS2-basic course
  • 1.Introduction to ROS2
  • 2.ROS2 install Humble
  • 3.ROS2 development environment
  • 4.ROS2 workspace
  • 5.ROS2 function package
  • 6.ROS2 node
  • 7.ROS2 topic communication
  • 8.ROS2 service communication
  • 9.ROS2 action communication
  • 10.ROS2 custom interface message
  • 11.ROS2 parameter service case
  • 12.ROS2 meta-function package
  • 13.ROS2 distributed communication
  • 14.ROS2 DDS
  • 15.ROS2 time related API
  • 16.ROS2 common command tools
  • 17.ROS2 rviz2 use
  • 18.ROS2 rqt toolbox
  • 19.ROS2 Launch startup file configuration
  • 20.ROS2 recording and playback tool
  • 21.ROS2 URDF model
  • 22.ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
09.ROS2-OpenCV course
  • 1. ROS+opencv application
  • 2. QR code creation and recognition
  • 3. AR vision
10.ROS2-Mediapipe course
  • 1.Hand detection
  • 2.Posture detection
  • 3.Overall detection
  • 4.Face detection
  • 5.Personal insurance identification
11.ROS2-Depth camera course
  • 1. Use of depth camera
  • 2. Camera internal parameter calibration
  • 3. Color tracking
  • 4. KCF object tracking
  • 5. ORB_SLAM2 basics
  • 6. ORB_SLAM2 PCL mapping
  • 7. ORB_SLAM2 Octomap mapping
12.Bracket installation video
  • Bracket installation video
Download
  • VM_ROS1
  • VM_ROS2
  • Code-ROS1
  • Code-ROS2
  • 3D_File
  • ROS_Basic_VM
Download ZIP

Welcome to AI View Depth camera repository

9.ROS service client

9.ROS service client

Service service is also a common communication method between ros nodes. Different from the topic, the service has a server and a client. The client requests the server to provide services. After the server completes the service, it needs to return the service results. Also called a response. In this lesson, we will explain how to write a client node program.

9.1 Create a client

The general creation steps are as follows:

  • Initialize ROS nodes
  • Create handle
  • Create a Client instance
  • Initialize and publish service request data
  • Wait for the response result after Server processing

9.2 Create function package

In order to distinguish it from the learn_topic function package, we re-create a function package learn_service and enter it in the terminal.

cd ~/ros_ws/src
catkin_create_pkg learn_service std_msgs rospy roscpp geometry_msgs turtlesim

Then compile,

xxxxxxxxxx
cd ~/ros_ws
catkin_make

9.3 C++ version

9.3.1 Writing source code

In the src folder of the function package learn_service, create a C++ file (the file suffix is .cpp), name it a_new_turtle.cpp, and paste the following content into a_new_turtle.cpp,

xxxxxxxxxx
/**
* This routine will request the /spawn service in the little turtle node, and a new little turtle will appear at the specified location.
*/
#include <ros/ros.h>
#include <turtlesim/Spawn.h>
int main(int argc, char** argv)
{
    ros::init(argc, argv, "a_nes_turtle");// Initialize ROS node
    ros::NodeHandle node;
    ros::service::waitForService("/spawn"); // Wait/spawn service
    ros::ServiceClient new_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");//Create a service client and connect to the service named /spawn
    // Initialize turtlesim::Spawn's request data
    turtlesim::Spawn new_turtle_srv;
    new_turtle_srv.request.x = 6.0;
    new_turtle_srv.request.y = 8.0;
    new_turtle_srv.request.name = "turtle2";
    // Request the service to pass in the xy position parameters and name parameters
    ROS_INFO("Call service to create a new turtle name is %s,at the x:%.1f,y:%.1f", new_turtle_srv.request.name.c_str(),
    new_turtle_srv.request.x,new_turtle_srv.request.y);
    new_turtle.call(new_turtle_srv); //Request service
    ROS_INFO("Spwan turtle successfully [name:%s]",
    new_turtle_srv.response.name.c_str());// Display service call results
    return 0;
};

9.3.2 Modify CMakeList.txt file

Configure in CMakelist.txt, under the build area, add the following content,

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

add_executable shows that the generated executable program file is a_new_turtle, and the compiled source code is a_new_turtle.cpp in the src directory.

target_link_libraries specifies the libraries that need to be linked when compiling and generating an executable file.

9.3.3 Compile

Terminal input,

xxxxxxxxxx
cd ~/ros_ws
catkin_make

image-20231023173644143

After the compilation is passed, you need to re-source the current environment variables to find or update the program. Enter in the terminal.

xxxxxxxxxx
cd ~/ros_ws
source devel/setup.bash

9.3.4 Running the program

Open roscore,

xxxxxxxxxx
roscore

Run the little turtle node program,

xxxxxxxxxx
rosrun turtlesim turtlesim_node

Run the client node program to generate a small turtle at the specified location.

xxxxxxxxxx
rosrun learn_service a_new_turtle

image-20231023174232607

After starting the little turtle node, and then running the a_new_turtle program, you will find that another little turtle will appear on the screen. This is because the little turtle node provides service/spawn, which corresponds to ros::ServiceClient new_turtle in the code. = node.serviceClient<turtlesim::Spawn>("/spawn");//Creating this service will generate another little turtle turtle2. To view the services provided by the little turtle, you can view it through the rosservice list command, as shown in the figure below.

image-20231023174328925

You can view the parameters required by this service through rosservice info /spawn, as shown in the figure below.

image-20231023174404010

It can be seen that 4 parameters are required: x, y, theta, name. These four parameters are initialized in a_new_turtle.cpp.

xxxxxxxxxx
srv.request.x = 6.0;
srv.request.y = 8.0;
srv.request.name = "turtle2";
Note: theta is not assigned a value and defaults to 0

9.3.5 Program flow chart

image-20231023174621881

9.4 Python version

9.4.1 Writing source code

Create a new scripts folder under the function package learn_service, then create a new python file (file suffix .py) in this scripts folder, name it a_new_turtle.py, copy and paste the following program code into the a_new_turtle.py file,

xxxxxxxxxx
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import rospy
from turtlesim.srv import Spawn
def turtle_spawn():
    rospy.init_node('new_turtle')# ROS node initialization
    rospy.wait_for_service('/spawn')# Wait/spawn service
    try:
        new_turtle = rospy.ServiceProxy('/spawn', Spawn)
        response = new_turtle(2.0, 2.0, 0.0, "turtle2")# Enter request data
        return response.name
    except rospy.ServiceException as e:
        print ("failed to call service : %s")
if __name__ == "__main__":
    #Call the service and display the call results
    print ("a new turtle named %s." %(turtle_spawn()))

The python program does not need to be compiled, but it needs to add executable permissions and enter it in the terminal.

xxxxxxxxxx
cd ~/ros_ws/src/learn_service/scripts
sudo chmod a+x a_new_turtle.py

9.4.2 Run

Open roscore,

xxxxxxxxxx
rocore

Run the little turtle node,

xxxxxxxxxx
rosrun turtlesim turtlesim_node

Run the publisher node program and continue to send speed to the little turtle.

xxxxxxxxxx
rosrun learn_service a_new_turtle.py 

image-20231023182843856

Similarly, after running, a little turtle will appear, and the terminal will print the returned content.

9.4.3 Program flow chart

image-20231023183128531

ROS topic subscribers
ROS service server

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文