0
$\begingroup$

Rosanswers logo

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


Originally posted by aefka on ROS Answers with karma: 15 on 2018-01-05

Post score: 0


Original comments

Comment by jayess on 2018-01-05:
What is your question/problem? Is your code not working as expected?

Comment by aefka on 2018-01-05:
Yes :/ I want to publish msg.linear.x = 2 msg.linear.y = 2 from python. I am newbie in this language :/

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by jayess with karma: 6155 on 2018-01-05

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by aefka on 2018-01-05:
Ok, but I want to topic 'cmd_vel' publish msg.linear.x and msg. linear.y

Comment by jayess on 2018-01-05:
What do you mean? The pub publisher is sending Twist messages along the /cmd_vel topic.

Comment by aefka on 2018-01-05:
But in this case, which messages are sending? I want send such messages to set velocities in V-REP

Comment by aefka on 2018-01-05:
thanks rostopic pub I set velocities, and I have to do the same by Python

Comment by jayess on 2018-01-05:
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.

Comment by aefka on 2018-01-05:
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

Comment by jayess on 2018-01-05:
That's what we did here. You should go through the Python subscriber/publisher tutorials

Comment by aefka on 2018-01-05:
I saw this, but I still have got problems with this :/ Unfortunately with your corrections, the model still does not move

Thanks for trying :)

Comment by jayess on 2018-01-05:
You can always ask a new question regarding that specific problem

$\endgroup$

Your Answer

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