当前位置:网站首页>9、 Alternative implementation of client for service communication
9、 Alternative implementation of client for service communication
2022-07-26 17:54:00 【Rock magnon】
List of articles
1、 Background
In service communication , Sometimes we need to create a new client , At this time, we can get service topics through relevant commands , data type , Thus, a new client sends a request to the server .
2、 Implementation steps
1. Create Feature Pack , Add dependency :roscpp rospy std_msgs turtlesim
2. Get service topics and information
rosservice list
/clear
/key/get_loggers
/key/set_logger_level
/kill
/reset
/rosout/get_loggers
/rosout/set_logger_level
/spawn
/turtle/get_loggers
/turtle/set_logger_level
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
[email protected]:/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05$ rosservice type /spawn
turtlesim/Spawn
[email protected]:/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05$ rossrv info turtlesim/Spawn
float32 x
float32 y
float32 theta
string name
---
string name
3. Code implementation
- C++
/* The goal is : Make a little turtle service :/spawn Message type :turtlesim/Spawn Get ready : start-up turtlesim_node node */ #include"ros/ros.h" #include"turtlesim/Spawn.h" int main(int argc, char *argv[]){ setlocale(LC_ALL,""); ros::init(argc,argv,"turtle_add"); ros::NodeHandle nh; //spawn For service topics ros::ServiceClient client = nh.serviceClient<turtlesim::Spawn>("/spawn"); ros::service::waitForService("/spawn"); // establish spawn object turtlesim::Spawn spawn; spawn.request.x = 1.0; spawn.request.y = 1.1; spawn.request.theta = 2.2; // When the tortoise name is repeated, it will report an error spawn.request.name = "turtle_add1"; bool flag = client.call(spawn); if (flag){ ROS_INFO(" Tortoise name :%s",spawn.response.name.c_str()); } else{ ROS_INFO(" Failure !!!"); } return 0; } - Python
#! /usr/bin/env python from tkinter import EXCEPTION from rosdep2 import RosdepLookup import rospy from turtlesim.srv import Spawn,SpawnRequest,SpawnResponse if __name__ == "__main__": rospy.init_node("turtle_add_p") client = rospy.ServiceProxy("/spawn",Spawn) client.wait_for_service() req = SpawnRequest() req.x = 1.0 req.y = 4.3 req.theta = 3.4 req.name = "turtle_p" try: response = client.call(req)# Return tortoise name rospy.loginfo(" Tortoise name :%s",response.name) except EXCEPTION as e: rospy.loginfo(" Failure !!!!!")
边栏推荐
- Analysis of interface testing
- Sequential storage structure of linear table -- sequential table
- [training Day2] torchbearer
- [cloud native kubernetes actual combat] kubeopertor installation tutorial
- 6-19漏洞利用-nsf获取目标密码文件
- Week 17 free intrusion pointer exercise - output maximum
- 大咖访谈 | 开源对安全是双刃剑——《大教堂与集市》中文译本作者卫剑钒
- 重磅!《2022中国开源发展蓝皮书》正式发布
- AI zhetianchuan ml integrated learning
- 点击劫持攻击
猜你喜欢

硬件开发与市场产业

重磅!《2022中国开源发展蓝皮书》正式发布

#夏日挑战赛# OpenHarmony基于JS实现的贪吃蛇

【模板】线段树 1

天翼云Web应用防火墙(边缘云版)支持检测和拦截Apache Spark shell命令注入漏洞

Redisdesktopmanager removes the upgrade prompt

AI遮天传 ML-无监督学习

Interview with celebrities | open source is a double-edged sword for security -- Wei Jianfan, author of the Chinese translation of cathedral and market

236. The nearest common ancestor of a binary tree

Week 16 OJ practice 1 calculates the day of the year
随机推荐
[Day2] cinema ticket
AI zhetianchuan ml unsupervised learning
性能调优bug层出不穷?这3份文档轻松搞定JVM调优
CCS tm4c123 new project
the loss outweighs the gain! Doctors cheated 2.1 million yuan and masters cheated 30000 yuan of talent subsidies, all of which were sentenced!
Cloud rendering volume cloud [theoretical basis and implementation scheme]
[training Day2] torchbearer
Tupu 3D visual national style design | collision between technology and culture "cool" spark“
6-19漏洞利用-nsf获取目标密码文件
[cloud native kubernetes actual combat] kubeopertor installation tutorial
[Oumi reading club] talk about the creator economy in the meta universe: infinite dimension
中国聚异丁烯市场研究与投资价值报告(2022版)
Week 16 OJ practice 1 calculates the day of the year
Brief introduction to CUDA image construction
A detailed explanation of throughput, QPS, TPS, concurrency and other high concurrency indicators
AI sky covering DL multilayer perceptron
【云原生】 iVX 低代码开发 引入腾讯地图并在线预览
深度学习实验:Softmax实现手写数字识别
【集训Day2】Sculpture
二层管理型交换机如何设置IP