首页 > 解决方案 > 将乌龟移动一圈并将其作为 ros 节点停止在初始位置?

问题描述

我使用 rosrun 命令作为节点运行以下代码,但不再循环运行。但是这个函数没有错误。如何解决这个问题并转了一圈并停在初始位置?

ROS:旋律 Ubuntu 18.04

#!/usr/bin/env python
import rospy
import rospkg
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose
#defining variables
x = 0
y = 0
theta = 0.0
    
#main function  
def pose_callback(msg):
    global x, y, psi
    x = msg.x
    y = msg.y
    psi = msg.theta
    print(msg.theta)
if __name__=="__main__":

    
    rospy.init_node('node_turtle_revolve', anonymous = True)
    r = rospy.Rate(10)
    velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) 
    
    while not rospy.is_shutdown():
        
    sub = rospy.Subscriber('/turtle1/pose', Pose, pose_callback)
        vel_msg = Twist()
    vel_msg.linear.x = 0.2
    vel_msg.linear.x = 0.0
    vel_msg.linear.x = 0.0
    vel_msg.angular.x= 0.0
    vel_msg.angular.y= 0.0
    vel_msg.angular.z= 0.1
    velocity_publisher.publish(vel_msg)
    r.sleep

标签: pythonpython-2.7ubuntu-18.04ros

解决方案


您应该将 msg 更改为姿势

defpose_callback(pose): global x, y, psi x =pose.x y =pose.y psi = msg.theta print(msg.theta) 你应该写一个if语句


推荐阅读