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

Import file for executing cmd_vel [closed]

asked 2013-01-09 15:37:59 -0500

Chik gravatar image

updated 2013-01-20 21:14:16 -0500

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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Chik
close date 2013-03-10 15:15:03

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-01-20 19:32:27 -0500

mmwise gravatar image

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()
edit flag offensive delete link more

Comments

Thank you very much indeed. It works now.

Chik gravatar image Chik  ( 2013-01-20 21:44:42 -0500 )edit

Do you have the launch file for this example?

Christopher gravatar image Christopher  ( 2013-10-15 10:17:56 -0500 )edit
3

answered 2013-01-09 16:27:13 -0500

allenh1 gravatar image

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!

edit flag offensive delete link more

Comments

Hmmm.......

Chik gravatar image Chik  ( 2013-01-17 18:15:32 -0500 )edit

Question Tools

Stats

Asked: 2013-01-09 15:37:59 -0500

Seen: 1,469 times

Last updated: Jan 20 '13