I want to subscribe to a topic that sends a geometric message that is being published. But within a node that also publishes in the same topic under one condition.
My node publishes a cmd_vel message when an object is near the robot, to tell the robot to stop. And if not, then it moves at a constant speed.
But now I want the robot to subscribe to a topic sent by cmd_vel that is published when the condition is not met, for example, to be able to move it using the keyboard or others.
I tried to write it but nothing works, so I do not think it's useful to show you what did not work. Because it does not work. On the other hand, if you could help me do this, especially with the part of the code, I'll thank you.
Could the node be made to subscribe and publish only for the condition and when the condition is not met simply do not publish anything?
#!/usr/bin/env python import rospy from sensor_msgs.msg import LaserScan from geometry_msgs.msg import Twist class Obstacle(): def __init__(self): self.LIDAR_ERR = 0.05 self._cmd_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1) self.obstacle() def get_scan(self): msg = rospy.wait_for_message("scan", LaserScan) self.scan_filter =  for i in range(360): if i <= 15 or i > 335: if msg.ranges[i] >= self.LIDAR_ERR: self.scan_filter.append(msg.ranges[i]) def obstacle(self): self.twist = Twist() while not rospy.is_shutdown(): self.get_scan() if min(self.scan_filter) < 0.2: self.twist.linear.x = 0.0 self.twist.angular.z = 0.0 self._cmd_pub.publish(self.twist) rospy.loginfo('Stop!') else: self.twist.linear.x = 0.5 self.twist.angular.z = 0.0 rospy.loginfo('distance of the obstacle : %f', min(self.scan_filter)) self._cmd_pub.publish(self.twist) def main(): rospy.init_node('turtlebot3_obstacle') try: obstacle = Obstacle() except rospy.ROSInterruptException: pass if __name__ == '__main__': main()