yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ROS Robot Expansion Board📖
1.Expansion board STM32 driver
  • 0. Precautions
  • 1.1 Expansion Board Introduction_V3.0
  • 1.1 Expansion Board Introduction_V1.0
  • 1.2 STM32 Development environment
  • 1.3 Button to control the buzzer to whistle
  • 1.4 FreeRTOS application
  • 1.5 Serial communication
  • 1.6 SBUS model aircraft remote control
  • 1.7 CAN bus communication
  • 1.8 RGB colorful light bar
  • 1.9 Timer interrupt to control PWM servo
  • 1.10 Control serial port servos
  • 1.11 Nine-axis attitude sensor to obtain data
  • 1.12 Control the forward and reverse rotation of the motor
  • 1.13 The timer captures the encoder data
  • 1.14.1 Robot kinematics analysis theory
  • 1.14.2 Mecanum wheel kinematics analysis
  • 1.15 PID control robot movement
  • 1.16 OLED character display
2.Python basic control
  • 2.0 Bind PCB port devices
  • 2.1 Update the firmware of the expansion board
  • 2.2 Turn off the self-starting large program at startup
  • 2.3 Install the Rosmaster driver library
  • 2.4 Buzzer whistle
  • 2.5 Control PWM servo
  • 2.6 RGB colorful light bar special effects display
  • 2.7 Control the forward and reverse rotation of the motor
  • 2.8 Control the movement of the robot
  • 2.9 Control Serial Servo
3.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
4.ROS1 robot control
  • 0. Bind PCB port devices
  • 1. Package Use the tutorial
  • 2. Package Description
  • 3. Robot keyboard control
  • 4. Robot state estimation
  • 5. Robot imu calibration
  • 6. Robot URDF Model
5.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
6.ROS2 robot control
  • 0. Bind PCB port devices
  • 1. Package Use the tutorial
  • 2. Robot information release
  • 3. Robot keyboard control
  • 4. Robot state estimation
  • 5. Robot calibration
  • 6. URDF model
7.RDK X3 course
  • 1. Update the firmware of the expansion board
  • 2. Turn off the self-starting large program at startup
  • 3. Install the Rosmaster driver library
  • 4. Buzzer whistle
  • 5. Control PWM servo
  • 6. RGB colorful light bar special effects
  • 7. Control motor forward reverse
  • 8. Control robot movement
  • 9. Control Serial Servo
Download
  • Rules_File
  • Hardware Info
  • Code
  • 3D Model File
  • RDK Code
  • ROS2_Basic_VM
Download ZIP

Welcome to ROS Robot Expansion Board 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
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文