ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to publish array of velocities?

asked 2020-12-03 12:03:10 -0500

logimaker gravatar image

Hello guys, so I am very new to ROS and python. Now I understand the following: to publish on a topic, for example Twist() you write something like this and you publish one value for a couple of seconds (4 seconds in this case):

   1 #!/usr/bin/env python
   2 import rospy
   3 from geometry_msgs.msg import Twist
   4 
   5 def move():
   6     # Starts a new node
   7     rospy.init_node('robot_cleaner', anonymous=True)
   8     velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
   9     vel_msg = Twist()
  10
  11     vel_msg.linear.x = 0.5
  12     vel_msg.linear.y = 0
  13     vel_msg.linear.z = 0
  14     vel_msg.angular.x = 0
  15     vel_msg.angular.y = 0
  16     vel_msg.angular.z = 0
  17     
  18     now = rospy.Time.now()
  19
  20     while rospy.Time.now() < now + rospy.Duration.from_sec(4):
  21     pub.publish(vel_msg)
  22     
  23 move()

But so instead of publishing one velocity for a couple of seconds I want now to publish like an array of velocities. For example vel_msg.linear.x = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]. So every timestep there will be another velocity published until the last value is reached.

How can you do this? And maybe another question: how do you know the timestep that ROS is using between the values of the array? Thanks in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-12-09 03:06:10 -0500

rodrigo55 gravatar image

Hi, you can create a python code that iterates through the array of velocities you want to send, and you can define a rospy.rate to ensure the velocities are sent at the rate you want them to. If you need help how to do that, I made this video that might help:

https://www.youtube.com/watch?v=rqA7h...

edit flag offensive delete link more

Comments

Hi, this worked indeed. Thanks a lot!

logimaker gravatar image logimaker  ( 2020-12-11 10:13:36 -0500 )edit

I still do have another problem: So the python code iterates through the array of velocities but now at the same time I also want to subscribe to a topic ( for example /pose ) and save this in a text file until the code has iterated over all the velocities. Because now I am doing this manually in a new terminal with "rostopic echo -p /pose > data.txt" which is not very practical... Do you know how to do this? Thanks in advance!

logimaker gravatar image logimaker  ( 2020-12-11 10:27:49 -0500 )edit

Can you please update your answer with main points from the video? If your video goes down or isn't available for certain regions then your answer/solution isn't as useful. Also, some prefer text to 14+ minute video

jayess gravatar image jayess  ( 2021-03-11 00:11:14 -0500 )edit
1

answered 2020-12-06 08:41:45 -0500

miura gravatar image

We think it's a good idea to define an array of geometry_msgs/Twist in a custom message.

geometry_msgs/Twist[] twists

and use it as follows

custom_msg::Twists msg;
msg.twists[0].linear.x = 0.1;
msg.twists[1].linear.x = 0.2;
// ...

Custom Message Definition Tutorial: http://wiki.ros.org/ROS/Tutorials/Cre... ...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-12-03 12:03:10 -0500

Seen: 754 times

Last updated: Dec 09 '20