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

PYTHON + ROS + V-REP

asked 2018-01-05 15:03:32 -0600

aefka gravatar image

updated 2018-01-05 17:15:46 -0600

Hi I have got a problem with publish data. I want to publish msg.linear.x and msg.linear.y to V-REP via ROS.

When I write in terminal

rostopic pub -1 /cmd_vel geometry_msgs/Twist '{linear: {x: -3.0, y: -3.0}}'

Everything is ok, but I want to publish this from PYTHON.

My actually code is:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
#added
from geometry_msgs.msg import Twist 


def callback(data):
    global msg
    if data.data=="Unknown":
        pub = rospy.Publisher('cmd_vel', Twist) 
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        msg.angular.z = 0
        speed = 0.4 
        rospy.loginfo("checking for cmd" + str(msg.linear))
        pub.publish(msg)
    elif data.data=="Check":
        rospy.loginfo("Classifiers output: %s in check" % data.data)
    else:      
        rospy.loginfo("Classifiers output: %s and not unknown or check" % data.data)



def listener():

     global msg
     rospy.init_node('listener', anonymous=True)
     msg = Twist()
     rospy.Subscriber("ccc", String, callback)
     rospy.spin()


if __name__ == '__main__':



    listener()

I am newbie in Python. Above code, I found on the Internet, and I try to translate into my case. What I want to do? I want to have control model in V-REP, by ROS. I can control by terminal thanks for egzample:

rostopic pub -1 /cmd_vel geometry_msgs/Twist '{linear: {x: -3.0, y: -3.0}}'

but I want control this model via Python, but I dont know how to do this :/

Thanks for suggestion

edit retag flag offensive close merge delete

Comments

What is your question/problem? Is your code not working as expected?

jayess gravatar image jayess  ( 2018-01-05 15:43:00 -0600 )edit

Yes :/ I want to publish msg.linear.x = 2 msg.linear.y = 2 from python. I am newbie in this language :/

aefka gravatar image aefka  ( 2018-01-05 15:59:52 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-01-05 15:51:07 -0600

jayess gravatar image

updated 2018-01-05 16:38:13 -0600

If you're having any issues with the publishing, part of the problem may be that you're creating your publisher inside of your callback function. What you should do is create it inside your listener function so you're not re-instantiating it every time your a message is published on the /ccc topic.

For example:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
#added
from geometry_msgs.msg import Twist 


def callback(data):
    global msg
    global pub
    if data.data=="Unknown":
        rospy.loginfo("Classifiers output: %s in unknown" % data.data)
        msg.linear.x = 2
        msg.linear.y = 2
        msg.angular.z = 0
        speed = 0.4 
        rospy.loginfo("checking for cmd" + str(msg.linear))
        pub.publish(msg)
    elif data.data=="Check":
        rospy.loginfo("Classifiers output: %s in check" % data.data)
    else:      
        rospy.loginfo("Classifiers output: %s and not unknown or check" % data.data)



def listener():
     global msg
     rospy.init_node('listener', anonymous=True)
     msg = Twist()
     rospy.Subscriber("ccc", String, callback)
     global pub
     pub = rospy.Publisher('cmd_vel', Twist, queue_size=10) # move instantiation of publisher
     rospy.spin()


if __name__ == '__main__':
    listener()

Although, what you most likely would want to do is create a class to do this since the use of globals isn't always the greatest of ideas.

edit flag offensive delete link more

Comments

Ok, but I want to topic 'cmd_vel' publish msg.linear.x and msg. linear.y

aefka gravatar image aefka  ( 2018-01-05 16:33:31 -0600 )edit

What do you mean? The pub publisher is sending Twist messages along the /cmd_vel topic.

jayess gravatar image jayess  ( 2018-01-05 16:36:48 -0600 )edit

But in this case, which messages are sending? I want send such messages to set velocities in V-REP

aefka gravatar image aefka  ( 2018-01-05 16:43:50 -0600 )edit

thanks rostopic pub I set velocities, and I have to do the same by Python

aefka gravatar image aefka  ( 2018-01-05 16:47:12 -0600 )edit

Alright, I was just going along with what you had stated earlier

I want to publish this from PYTHON.

the "this" being the Twist message. I have never used V-REP before.

jayess gravatar image jayess  ( 2018-01-05 16:50:03 -0600 )edit

ok, maybe on the other hand. How can I write rostopic pub -1 /cmd_vel geometry_msgs/Twist '{linear: {x: -3.0, y: -3.0}}' in python?

only no os.system :D

aefka gravatar image aefka  ( 2018-01-05 16:56:26 -0600 )edit

That's what we did here. You should go through the Python subscriber/publisher tutorials

jayess gravatar image jayess  ( 2018-01-05 16:57:57 -0600 )edit

I saw this, but I still have got problems with this :/ Unfortunately with your corrections, the model still does not move

Thanks for trying :)

aefka gravatar image aefka  ( 2018-01-05 17:09:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-05 15:03:32 -0600

Seen: 893 times

Last updated: Jan 05 '18