网站首页 > 技术文章 正文

实验平台 ubuntu18.04
实验目的 掌握python ros 定时发送数据
两种方式:
1.ROS 自带 Rate 实现
2.ROS 自带Timer
1.ROS 自带 Rate 实现
我们将在之前做过测试的程序添加rate 实现定时发送,测试一秒五帧的数据发送与消息订阅。
实现步骤:
- 多进程启动roscore
- 声明消息节点
- 定时发布消息
主要的学习内容是:
rate = rospy.Rate(5)
设置发送频率
无限循环中添加rate.sleep()
具体代码如下所示:
import os
import sys
import time
import rospy
import multiprocessing
from std_msgs.msg import String
def start_ros_core():
result_roscore = os.system('roscore')
# 使用代码
ros1bag_core_process = multiprocessing.Process(target=start_ros_core)
ros1bag_core_process.start()
rospy.init_node("test_pub", anonymous=True)#初始化节点 名称:test
rate = rospy.Rate(5) # ROS Rate at 5Hz
def sub_mutil():
num = 0
while True:
my_msg = String()
my_msg.data = f'{num}'
pub_parking.publish(my_msg)
rate.sleep()
print(f"发送消息:{num}")
num+=1
#发布话题
pub_parking = rospy.Publisher('mystring', String, queue_size=10)
sub_mutil()
订阅部分代码
import rospy
from std_msgs.msg import String
rospy.init_node("test_sub", anonymous=True)#初始化节点 名称:test
def call_back_String(msg):
print(f"接收到消息: {msg.data}")
rospy.Subscriber('mystring',String, call_back_String)
rospy.spin()
实现GIF:
2.ROS 自带Timer
主要使用到rospy timer 设置0.2s 触发一次函数
#定时触发
rospy.Timer(rospy.Duration(0.2), call_back)
所有代码给出:
import os
import sys
import time
import rospy
import multiprocessing
from std_msgs.msg import String
num = 0
def call_back(s):
global num
my_msg = String()
my_msg.data = f'{num}'
pub_parking.publish(my_msg)
print(f"发送消息:{num}")
num+=1
rospy.init_node("test_pub", anonymous=True)#初始化节点 名称:test
#发布话题
pub_parking = rospy.Publisher('mystring', String, queue_size=10)
#定时触发
rospy.Timer(rospy.Duration(0.2), call_back)
rospy.spin()
接收话题解析的代码与之前的一致:
import rospy
from std_msgs.msg import String
rospy.init_node("test_sub", anonymous=True)#初始化节点 名称:test
def call_back_String(msg):
print(f"接收到消息: {msg.data}")
rospy.Subscriber('mystring',String, call_back_String)
rospy.spin()
实验结果GIF:
边学边用好记忆,随学随记烂笔头。
猜你喜欢
- 2024-12-17 python线程安全:使用Rlock实现可重入锁
- 2024-12-17 python打开内置函数:模式a,a +,w,w +和r +之间的区别?
- 2024-12-17 国产化之虚拟ARM64-CPU安装银河麒麟操作系统
- 2024-12-17 Python中字符串前的u、r、b、f分别代表的含义
- 2024-12-17 Python 和 R 数据分析/挖掘工具互查
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)