Ask Your Question
0

How can I publish Twist messages to /gazebo node(amcl_demo.launch)

asked 2018-09-06 01:56:36 -0500

Reulix gravatar image

Hello! I'm runnning TurtleBot on rviz, and I want to publish Twist messages to the "/gazebo" node in order to control robot's velocity.

I wrote a publisher as below but it didn't work.
How can I solve this ploblem? Could you tell me please?

my code↓↓

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
def callback(data):
    pub=rospy.Publisher("/gazebo",Twist,queue_size=10)
    vel_msg=Twist()
    print(data.linear.x,data.angular.z)
    vel_msg.linear.x=0
    vel_msg.angular.z=0
    pub.publish(vel_msg)

def main():
    rospy.init_node("GetVelocity")
    sub=rospy.Subscriber("/mobile_base/commands/velocity",Twist,callback)
    rospy.spin()

if __name__== "__main__":
    try:
        main()
    except rospy.ROSInterruptException:
        pass

rqt_graph Image↓↓(while running "amcl_demo.launch"&"rviz"&"turtlebot_world.launch"&"above code")
rqt_graph(red arrow in this picture shows what I want to do)

edit retag flag offensive close merge delete

Comments

can you post the whole rqt_graph?

Choco93 gravatar image Choco93  ( 2018-09-06 02:21:24 -0500 )edit

Yes, sure. Here you are! all nodes

Reulix gravatar image Reulix  ( 2018-09-06 03:21:29 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2018-09-06 03:09:40 -0500

Delb gravatar image

There are few mistakes in your code :

  • You shouldn't initialize the publisher inside the callback it can create errors. Initialize it in the main function.

  • You don't have to subscribe to any topic if you only want to publish a speed command.

  • You are trying to publish data on the topic "/gazebo" but it's actually the name of the gazebo node so it cannot work.

  • Since you have a cmd_vel multiplexer you can publish data on the topic /cmd_vel OR /mobile_base/commands/velocity

  • You should set vel_msg.linear.x to some value (1 for example) to directly see your robot moving (it will be easier for you to debug)

It should work fine with those change.

edit flag offensive delete link more

Comments

Thank you for your quick reply!!
Yes, subscriber isn't needed for this purpose, but I just wanted to check how TurtleBot would move😁
Ok I'll try it!

Reulix gravatar image Reulix  ( 2018-09-06 03:32:32 -0500 )edit

as mentioned in answer, publish to /mobile_base/commands/velocity, gazebo is just subscribing to this topic so you shouldn't publish anything to gazebo.

Choco93 gravatar image Choco93  ( 2018-09-06 03:41:51 -0500 )edit

I made a Publisher in main function, set it to publish Twist topics to /mobile_base/commands velocity as you said, and I finally found TurtleBot would move as desired speed!!! Thanks to you, I could solve the problem!

Reulix gravatar image Reulix  ( 2018-09-06 04:05:13 -0500 )edit

I'm glad it worked well ! You can mark the answer as correct if you have no more issue

Delb gravatar image Delb  ( 2018-09-06 04:11:06 -0500 )edit

I did it!😄

Reulix gravatar image Reulix  ( 2018-09-06 04:47:17 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-09-06 01:46:48 -0500

Seen: 942 times

Last updated: Sep 06 '18