Unstable Communication between ROS Node and Robot
Hey ROS Forum,
I am just trying to make my robot navigate from a initial to a goal position. The node then calculates the next action in form of the angular rotation and continuous calculating and observing the state. Here the computing time for one step mainly depends on the waiting time for the Lidar data (5 data sets per second).
data = rospy.wait_for_message('scan', LaserScan, timeout=5)
Based on that data, the algo calculates the next action and publishes it. Here the problem occurs, if I just write
self.pub_cmd_vel.publish(vel_cmd)
the robot misses that published topic and continuous following an old action. I tried to use an additional node that permanently subscribes to the first node and also publishes to the robot (first fast design):
if __name__ == '__main__':
rospy.init_node('turtlebot3_signal_transmission')
pub_cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=5)
vel_cmd = Twist()
t = True
action = rospy.wait_for_message('cmd_vel_1', Twist)
while t:
a = time.time()
time_diff = 0
while time_diff < 0.2:
vel_cmd.linear.x = action.linear.x; vel_cmd.angular.z = action.angular.z
pub_cmd_vel.publish(vel_cmd)
action = rospy.Subscriber('cmd_vel_1', Twist, queue_size=10)
time_diff = time.time() - a
But here, when reaching the second while loop, the subscriber delivers the action with the following value:
<rospy.topics.Subscriber object at 0x7fd66c79b210>
My question is now, how can I ensure a continuos communication, where the actual value for the action is permanently published and subscribed by the middle node? If I put the wait_for_message into the while loop, the whole procedure of receiving and sending lasts 0,4sec instead of 0,2 sec. The goal would be to have a robot action_update rate of 0,2 sec as well. I also tried the
self.pub_cmd_vel.get_num_connections()
command, but it wasn`t fully working due to a constant 1 as a value after first execution of the node.
Thanks in advance!