def myhook(): print "shutdown time!"rospy.on_shutdown(myhook)
或
rospy.signal_shutdown(reason)
- 初始化节点关闭
- reason 为关闭理由,字符串内容。
例子:
#!/usr/bin/env pythonimport rospyimport mathimport sysfrom tf import transformationsfrom geometry_msgs.msg import PoseWithCovarianceStampedclass PoseSetter(rospy.SubscribeListener): def __init__(self, pose): self.pose = pose def peer_subscribe(self, topic_name, topic_publish, peer_publish): p = PoseWithCovarianceStamped() print("hh") p.header.frame_id = "map" p.pose.pose.position.x = self.pose[0] p.pose.pose.position.y = self.pose[1] (p.pose.pose.orientation.x, p.pose.pose.orientation.y, p.pose.pose.orientation.z, p.pose.pose.orientation.w) = transformations.quaternion_from_euler(0, 0, self.pose[2]) p.pose.covariance[6*0+0] = 0.5 * 0.5 p.pose.covariance[6*1+1] = 0.5 * 0.5 p.pose.covariance[6*3+3] = math.pi/12.0 * math.pi/12.0 peer_publish(p) print("ll") rospy.signal_shutdown("closed!")x=Trueprint("dd")pose = [-9.983256,-2.641909,-1.201580]#x,y,arospy.init_node('pose_setter', anonymous=True)if(x): print("tt") pub=rospy.Publisher("initialpose", PoseWithCovarianceStamped,PoseSetter(pose),queue_size=10) x=Falseelse: print("x=False")print("ff")rospy.spin()print("pp")