给小乌龟GUI控制器添加位置、角速度、线速度的实时显示

学习目标:

给小乌龟控制器添加位置、角速度、线速度的实时显示

学习内容:

再原有的GUI的基础上,增加显示X坐标,Y坐标,当前线速度,角速度以及角度信息

学习产出:

window.py

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *import rospy
from geometry_msgs.msg import Twist
from turtlesim.msg import Posefrom math import degrees, radiansclass MainTurtle(QWidget):def __init__(self):super(MainTurtle, self).__init__()self.setWindowTitle("小乌龟控制器")layout = QFormLayout()self.setLayout(layout)self.resize(400, 80)self.le_linear = QLineEdit()self.le_angular = QLineEdit()btn = QPushButton("发送")self.lb_x = QLabel()self.lb_y = QLabel()self.lb_linear = QLabel()self.lb_angular = QLabel()self.lb_theta = QLabel()layout.addRow("线速度", self.le_linear)layout.addRow("角速度(角度)", self.le_angular)layout.addRow("当前x坐标", self.lb_x)layout.addRow("当前y坐标", self.lb_y)layout.addRow("当前线速度", self.lb_linear)layout.addRow("当前角速度", self.lb_angular)layout.addRow("当前角度", self.lb_theta)layout.addRow(btn)btn.clicked.connect(self.btn_clicked)topic_name = "/turtle1/cmd_vel"self.publisher = rospy.Publisher(topic_name, Twist, queue_size=100)topic_pose = "/turtle1/pose"self.subscriber = rospy.Subscriber(topic_pose, Pose, self.pose_callback)def btn_clicked(self):linear = float(self.le_linear.text())angular = float(self.le_angular.text())# 角度转弧度 让用户输入的是熟悉的类型angular = radians(angular)twist = Twist()twist.linear.x = lineartwist.angular.z = angularself.publisher.publish(twist)def pose_callback(self, msg):# !!!! 让msg出现提示!!!if not isinstance(msg, Pose): returnself.lb_x.setText(str(msg.x))self.lb_y.setText(str(msg.y))self.lb_linear.setText(str(msg.linear_velocity))self.lb_angular.setText(str(msg.angular_velocity))self.lb_theta.setText(str(degrees(msg.theta)))

后记:增加qtUI的自刷新,这样我们可以在终端用Ctrl+C去退出

window(QtUI_update).py

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import rospy
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose
from math import degrees, radiansclass MainWindow(QWidget):def __init__(self):super(MainWindow, self).__init__()self.setWindowTitle("小乌龟控制器")layout = QFormLayout()self.setLayout(layout)self.resize(400, 80)self.le_linear = QLineEdit()self.le_angular = QLineEdit()btn = QPushButton("发送")layout.addRow("线速度", self.le_linear)layout.addRow("角速度", self.le_angular)layout.addRow(btn)btn.clicked.connect(self.btn_clicked)topic_name = "/turtle1/cmd_vel"self.publisher = rospy.Publisher(topic_name, Twist, queue_size=100)def btn_clicked(self):linear = float(self.le_linear.text())angular = float(self.le_angular.text())twist = Twist()twist.linear.x = lineartwist.angular.z = angularself.publisher.publish(twist)class MainTurtle(QWidget):def __init__(self):super(MainTurtle, self).__init__()# 创建自己的渲染的定时器update_timer = QTimer(self)# 设置定时器的频率  16ms刷新一次 一秒刷新60帧update_timer.setInterval(16)# 开始渲染窗口update_timer.start()# 需要有时间去监听timerupdate_timer.timeout.connect(self.on_update)self.setWindowTitle("小乌龟控制器")layout = QFormLayout()self.setLayout(layout)self.resize(400, 80)self.le_linear = QLineEdit()self.le_angular = QLineEdit()btn = QPushButton("发送")btn1 = QPushButton("复位")self.lb_x = QLabel()self.lb_y = QLabel()self.lb_linear = QLabel()self.lb_angular = QLabel()self.lb_theta = QLabel()layout.addRow("线速度", self.le_linear)layout.addRow("角速度(角度)", self.le_angular)layout.addRow("当前x坐标", self.lb_x)layout.addRow("当前y坐标", self.lb_y)layout.addRow("当前线速度", self.lb_linear)layout.addRow("当前角速度", self.lb_angular)layout.addRow("当前角度", self.lb_theta)layout.addRow(btn)btn.clicked.connect(self.btn_clicked)topic_name = "/turtle1/cmd_vel"self.publisher = rospy.Publisher(topic_name, Twist, queue_size=100)topic_pose = "/turtle1/pose"self.subscriber = rospy.Subscriber(topic_pose, Pose, self.pose_callback)def btn_clicked(self):linear = float(self.le_linear.text())angular = float(self.le_angular.text())# 角度转弧度 让用户输入的是熟悉的类型angular = radians(angular)twist = Twist()twist.linear.x = lineartwist.angular.z = angularself.publisher.publish(twist)def pose_callback(self, msg):# !!!! 让msg出现提示!!!if not isinstance(msg, Pose): returnself.lb_x.setText(str(msg.x))self.lb_y.setText(str(msg.y))self.lb_linear.setText(str(msg.linear_velocity))self.lb_angular.setText(str(msg.angular_velocity))self.lb_theta.setText(str(degrees(msg.theta)))def on_update(self):# 手动渲染我们的uiself.update()if rospy.is_shutdown():# 如果rospy被关闭,则关闭ui窗口self.close()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部