首页 > 其他分享 >get_coords耗时大于20ms, 累积延迟较大

get_coords耗时大于20ms, 累积延迟较大

时间:2025-01-19 17:11:05浏览次数:1  
标签:publisher get 20ms coordinates fetch coords coordinate self

[coordinate_publisher-6] [INFO] [1737204129.386587739] [coordinate_publisher]: Time to fetch coordinates: 0.0201s
[publishCoords2audio-7] [INFO] [1737204129.391599837] [ambisonics_sound_node]: Published audio with azimuth: 136.29, elevation: 97.91
[coordinate_publisher-6] [INFO] [1737204129.409960361] [coordinate_publisher]: Time to fetch coordinates: 0.0216s
[coordinate_publisher-6] [INFO] [1737204129.435476604] [coordinate_publisher]: Time to fetch coordinates: 0.0240s
[coordinate_publisher-6] [INFO] [1737204129.458264791] [coordinate_publisher]: Time to fetch coordinates: 0.0212s
[coordinate_publisher-6] [INFO] [1737204129.481453334] [coordinate_publisher]: Time to fetch coordinates: 0.0217s
[publishCoords2audio-7] [INFO] [1737204129.491790249] [ambisonics_sound_node]: Published audio with azimuth: 137.41, elevation: 97.52
[coordinate_publisher-6] [INFO] [1737204129.506789359] [coordinate_publisher]: Time to fetch coordinates: 0.0241s
[angles_publisher-5] [WARN] [1737204129.546469382] [angles_publisher]: Relative RPY is not available or invalid
[coordinate_publisher-6] [INFO] [1737204129.546551160] [coordinate_publisher]: Time to fetch coordinates: 0.0376s
[coordinate_publisher-6] [INFO] [1737204129.587112946] [coordinate_publisher]: Time to fetch coordinates: 0.0376s
[publishCoords2audio-7] [INFO] [1737204129.595711244] [ambisonics_sound_node]: Published audio with azimuth: 137.88, elevation: 96.51
[coordinate_publisher-6] [INFO] [1737204129.613406799] [coordinate_publisher]: Time to fetch coordinates: 0.0236s

It seems that the time for the robot to fetch coordinates is long?
ChatGPT said:
ChatGPT

Yes, the log messages indicate that the time to fetch coordinates from the robotic arm varies, often exceeding 20 ms and occasionally reaching 37 ms or more. This is indeed a significant contributor to the latency in your system, especially since the publishing rate is intended to be 50 Hz (one message every 20 ms). When the coordinate fetching takes longer than 20 ms, it directly impacts the ability to publish coordinates on time, creating lag in the perceived sound position.
Why the Fetching Time is Long

Communication Overhead with the Robot:
    Fetching coordinates involves TCP/IP communication with the robotic arm (MyCobotSocket). This process includes:
        Sending a command to the robot.
        Waiting for a response.
        Processing the response.

Any delay in the network or on the robot's side will add to the fetching time.

Robot Response Time:
    The robot itself may take time to compute and send back the requested coordinates, especially if it is under load or if the API implementation on the robot is inefficient.

Blocking Calls:
    The mc.get_coords() method appears to be a blocking call, meaning your code waits for the robot's response before continuing. This contributes to the variability in fetching time.

How to Address This

  1. Optimize the Coordinate Fetching Process

    Batch Requests:
    If possible, configure the robot to send continuous updates for its coordinates, rather than requiring repeated requests. For example, if the robot supports streaming its position at a fixed rate, use that instead of polling (mc.get_coords).
    Check the MyCobotSocket API documentation to see if a streaming mode is available.

    Asynchronous Fetching:
    Use a non-blocking or asynchronous method to fetch coordinates. For example:

     import threading
    
     class CoordinatePublisher(Node):
         def __init__(self):
             super().__init__('coordinate_publisher')
             self.publisher_ = self.create_publisher(Float32MultiArray, '/robot_arm/coordinates', 10)
             self.timer = self.create_timer(0.02, self.publish_coordinates)
             self.tcp_ip = '192.168.62.179'
             self.tcp_port = 9001
             self.mc = None
             self.latest_coords = None
             self.lock = threading.Lock()
             self.connect_to_robot_arm()
    
             # Start a background thread to fetch coordinates
             self.fetch_thread = threading.Thread(target=self.fetch_coordinates_loop)
             self.fetch_thread.daemon = True
             self.fetch_thread.start()
    
         def fetch_coordinates_loop(self):
             """Continuously fetch coordinates in the background."""
             while rclpy.ok():
                 if self.mc:
                     try:
                         coords = self.mc.get_coords()
                         with self.lock:
                             self.latest_coords = coords
                     except Exception as e:
                         self.get_logger().error(f"Error fetching coordinates: {e}")
                         self.mc = None
                         self.connect_to_robot_arm()
    
         def publish_coordinates(self):
             """Publish the most recent coordinates."""
             if self.latest_coords is None:
                 self.get_logger().warning("No coordinates available yet.")
                 return
    
             with self.lock:
                 coords = self.latest_coords
    
             if coords and len(coords) == 6:
                 msg = Float32MultiArray()
                 msg.data = coords
                 self.publisher_.publish(msg)
                 self.get_logger().debug(f"Published coordinates: {coords}")
    
  2. Log Fetching Variability

    Log both the individual fetching times and the variability (e.g., standard deviation or range) over time to monitor improvements.

  3. Increase Publishing Timer Interval (Temporarily)

    If the coordinate fetching cannot be optimized immediately, you can increase the timer interval in the CoordinatePublisher node to match the fetching time (e.g., 30–40 ms). While this reduces the publishing frequency, it ensures consistent updates.

Next Steps

Investigate the Robot’s Capabilities:
    Check if the robot supports streaming its coordinates rather than requiring polling.
    If not, try optimizing the polling mechanism with asynchronous fetching as shown above.

Profile and Optimize:
    Measure the time spent in each step of mc.get_coords() to identify specific bottlenecks (e.g., network latency, robot processing time).

Test the Asynchronous Solution:
    Implement and test the asynchronous fetching solution to see if it reduces delays.

Let me know if you’d like me to integrate the asynchronous solution into your coordinate_publisher.py file for immediate testing!

标签:publisher,get,20ms,coordinates,fetch,coords,coordinate,self
From: https://www.cnblogs.com/zhengshufang/p/18679722

相关文章

  • 使用wget下载Civitai教程和优化
    在现代互联网中,下载大规模的数据文件时,我们往往需要借助一些自动化工具来简化过程,尤其是当需要下载多个文件或特定资源时。本文将介绍如何使用wget工具配合Python脚本来批量下载Civitai网站上的数据,同时提供一些优化和定制化的技巧。简便的教程,一阵见血wget https:/......
  • 【QT】: 初识 QWidget 控件 | QWidget 核心属性(API) | qrc 文件
    ......
  • 在 nuget 私服 BaGet 中应用https 以及 gitea action runner 自动打包
    最近赋闲,想起之前工作中代码管理的一点经历,就是在打包项目的时候,类库的版本号几乎没变过,一个项目运行多少年了,版本号还是1.0.0。......
  • 多微信管理太低效?get自动化设置,效率超90%的人!
    在当今数字化营销的时代,管理多个微信号成为许多企业和个人运营者的日常工作。但手动操作有时候不仅耗时费力,还容易出错。微信管理系统的自动化功能,为我们提供了高效管理多个微信号的解决方案,让管理工作事半功倍。一、自动加好友只需将客户的手机号或微信号批量导入到系统中,......
  • 操作系统进程-概述、fork/vfork、exit/_exit、getpid/getppid、wait/waitpid与exec族
    进程基本概念什么是进程:进程是程序的一次执行过程,进程是程序执行和资源管理的最小单元。在Linux环境下,每个正在运行的程序都被称为进程并行与并发:并行(微观上,一个很短时间范围内):在很短的时间段内(时间点上),两个事情同时发生并发(宏观上,一个时间段内):在一段时间内,多个事情同......
  • HTML应用指南:利用GET请求获取星巴克门店数据
    本篇文章,我们将探究GET请求的实际应用,我们使用Python的requests库通过GET请求抓取星巴克门店信息。星巴克作为全球知名的咖啡连锁品牌,其门店分布广泛,获取这些门店的信息对于数据分析、市场研究以及商业决策都具有重要意义。我们将以获取全国的星巴克门店为例,详细讲解请求的构......
  • HTML应用指南:利用GET请求获取全国特斯拉充电桩位置
    随着电动汽车的普及,充电基础设施的建设变得至关重要。作为电动汽车领域的先驱,特斯拉不仅在车辆技术创新上持续领先,还积极构建广泛的充电网络,以支持其不断增长的用户群体。为了提升用户体验和服务质量,开发人员和数据分析师经常需要访问特斯拉充电桩的位置信息,并将其集成到导航......
  • map.merge(num, 1, Integer::sum); 和 map.put(num, map.getOrDefault(num, 0) + 1);
    以下是对map.merge(num,1,Integer::sum);和map.put(num,map.getOrDefault(num,0)+1);的比较:代码示例以下是使用map.merge(num,1,Integer::sum);的示例代码:for(intnum:nums){map.merge(num,1,Integer::sum);}以下是使用ma......
  • post、get请求(查询字符串参数)将对象拼接为地址栏请求参数new URLSearchParams
    constparams=newURLSearchParams({param1:'value1',param2:'value2'}).toString();该方法可将param1和param2拼接为param1=value1&param2=value2实例consturl='https://example.com/api/resource';constparams=newURLSearchP......
  • 使用Python爬虫获取1688网站item_get_company API接口的公司档案信息
    一、引言在当今的商业环境中,获取供应商的详细信息对于采购决策、市场分析和供应链管理至关重要。1688作为中国领先的B2B电子商务平台,提供了丰富的供应商档案信息。通过使用1688的item_get_companyAPI接口,我们可以方便地获取这些信息。本文将详细介绍如何使用Python爬虫来调用该A......