0
$\begingroup$

Rosanswers logo

Sorry I am new to ROS. I have an external program that generates an ASCII text file (say 'walk.dat') which consists of two columns of values of linear velocity and angular velocity, say 1000 rows of (a,b). Would like to write a ROS package that can read in the data and execute rostopic pub cmd_vel geometry_msgs/Twist '[a,0,0]' '[0,0,b]' according to the values in the file. I also want to do it by Turtlebot simulator in Gazebo. I really appreciate if someone can teach me step by step from the very beginning (e.g. open a terminal) to the very end (e.g. rosrun something). Thank you very much indeed.


Originally posted by Chik on ROS Answers with karma: 229 on 2013-01-09

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Below is a bit of python that will do what you want. I tried to make it a bit generic for you so you can extend it.

#!/usr/bin/python                                                              

import roslib; roslib.load_manifest('test')
import rospy
import sys

from geometry_msgs.msg import Twist

class Walk(object):

    def __init__(self):
        rospy.init_node('walk_reader_node')
        self._init_params()
        self._init_pubsub()
        self.fh = open(self.file)

    def _init_pubsub(self):
        self.cmd_vel_pub = rospy.Publisher('cmd_vel', Twist)

    def _init_params(self):
        self.file = rospy.get_param('~file', 'walk.dat')

    def run(self):
        r = rospy.Rate(10.0)
        while not rospy.is_shutdown():
            for line in self.fh:
                l = line.split()
                cmd = Twist()
                cmd.linear.x = float(l[0])
                cmd.angular.z = float(l[1])
                self.cmd_vel_pub.publish(cmd)
                r.sleep()
            self.fh.close()
            exit()

if __name__ == '__main__':
    w = Walk()
    w.run()

Originally posted by mmwise with karma: 8372 on 2013-01-20

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Chik on 2013-01-20:
Thank you very much indeed. It works now.

Comment by Christopher on 2013-10-15:
Do you have the launch file for this example?

$\endgroup$
0
$\begingroup$

Rosanswers logo

This is an example of publishing some command topics with a threaded ros node. Basically, the message format is:

sim_velocity  = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 100);

geometry_msgs::Twist cmd_msg;
cmd_msg.linear.x = m_speed;
cmd_msg.angular.z = m_angle;

sim_velocity.publish(msg);

The above is published each time ROS spins. m_speed and m_angle are doubles that are member variables to the thread, but you could use global variables in a non class-based approach. Questions? I don't mind answering! Just comment and I'll revise this answer. Best of luck, and welcome to ROS!


Originally posted by allenh1 with karma: 3055 on 2013-01-09

This answer was NOT ACCEPTED on the original site

Post score: 3


Original comments

Comment by Chik on 2013-01-17:
Hmmm.......

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.