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

Unable to run turtle_tf2_broadcaster.py. Tutorial: tf2TutorialsWriting a tf2 broadcaster (Python)

asked 2019-10-17 17:43:37 -0500

Suyash gravatar image

updated 2019-10-18 10:48:40 -0500

gvdhoorn gravatar image

Hello,

I am new to ROS and I am trying to complete the tf2 tutorials. While running this particular tutorial (http://wiki.ros.org/tf2/Tutorials/Wri... a tf2 broadcaster (Python)) I created the turtle_tf2_broadcaster.py file and the start_demo.launch file exactly as described in the tutorial and in the directory as described in the tutorial. After this, I invoked catkin_make and I source devel_isolated/setup.bash in the terminal (I also tried sourcing devel/setup.bash). However, the following error comes up and it closes:

    RLException: Unable to launch [turtle1_tf2_broadcaster-4]. 
    If it is a script, you may be missing a '#!' declaration at the top.
    The traceback for the exception was written to the log file

The full output of the command I have attached at the end.

I am not able to figure out a way to get through this. Please suggest me what should I do. Please also find below my code for turtle_tf2_broadcaster.py and start_demo.launch. On lines 5 and 22 in turtle_tf2_broadcaster.py, I tried changing tf (as mentioned on the tutorial http://wiki.ros.org/tf2/Tutorials/Wri... a tf2 broadcaster (Python)) to tf2 and it still didn't work.

   #!/usr/bin/env python  
   import rospy

   # Because of transformations
   import tf2_conversions

   import tf2_ros
   import geometry_msgs.msg
   import turtlesim.msg


   def handle_turtle_pose(msg, turtlename):
       br = tf2_ros.TransformBroadcaster()
       t = geometry_msgs.msg.TransformStamped()

       t.header.stamp = rospy.Time.now()
       t.header.frame_id = "world"
       t.child_frame_id = turtlename
       t.transform.translation.x = msg.x
       t.transform.translation.y = msg.y
       t.transform.translation.z = 0.0
       q = tf2_conversions.transformations.quaternion_from_euler(0, 0, msg.theta)
       t.transform.rotation.x = q[0]
       t.transform.rotation.y = q[1]
       t.transform.rotation.z = q[2]
       t.transform.rotation.w = q[3]

       br.sendTransform(t)

   if __name__ == '__main__':
       rospy.init_node('tf2_turtle_broadcaster')
       turtlename = rospy.get_param('~turtle')
       rospy.Subscriber('/%s/pose' % turtlename,
                        turtlesim.msg.Pose,
                        handle_turtle_pose,
                        turtlename)
       rospy.spin()

Code for start_demo.launch

  <launch>
      <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
      <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>


    <node name="turtle1_tf2_broadcaster" pkg="learning_tf2" type="turtle_tf2_broadcaster.py" respawn="false" output="screen" >
      <param name="turtle" type="string" value="turtle1" />
    </node>
    <node name="turtle2_tf2_broadcaster" pkg="learning_tf2" type="turtle_tf2_broadcaster.py" respawn="false" output="screen" >
      <param name="turtle" type="string" value="turtle2" /> 
    </node>

  </launch>

Full out put when lunching the start_demo.launch.

suyash@ROS:~/tutorial_ws$ roslaunch learning_tf2 start_demo.launch
... logging to /home/suyash/.ros/log/b7a6d97a-f1bb-11e9-a228-08002776bdd9/roslaunch-ROS-2614.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://ROS:36971/

SUMMARY
========

PARAMETERS
 * /rosdistro: melodic
 * /rosversion: 1.14.3
 * /turtle1_tf2_broadcaster/turtle: turtle1
 * /turtle2_tf2_broadcaster/turtle: turtle2

NODES
  /
    sim (turtlesim/turtlesim_node)
    teleop (turtlesim/turtle_teleop_key)
    turtle1_tf2_broadcaster (learning_tf2/turtle_tf2_broadcaster.py)
    turtle2_tf2_broadcaster (learning_tf2/turtle_tf2_broadcaster.py)

auto-starting new master
process[master]: started with pid [2625]
ROS_MASTER_URI=http://localhost:11311

setting /run_id to b7a6d97a-f1bb-11e9-a228-08002776bdd9
process[rosout-1]: started with pid [2636]
started core service [/rosout]
process[sim-2]: started with pid ...
(more)
edit retag flag offensive close merge delete

Comments

Always include links to tutorials, source code, pages etc you're reading, following or copy-pasting.

Without seeing what you're actually doing, we cannot help you.

Seeing the error message you received, please also show us what exactly you copied (ie: the contents of your copy of turtle_tf2_broadcaster.py).

gvdhoorn gravatar image gvdhoorn  ( 2019-10-18 04:50:25 -0500 )edit

Thank you for your response, sorry I missed those details. I have edited my question and included the details.Also one more update is I tried doing the tf tutorial as well (http://wiki.ros.org/tf/Tutorials/Writing a tf broadcaster (Python), and I am still getting the exact same error, so there is something fundamental that I am missing.

Suyash gravatar image Suyash  ( 2019-10-18 10:21:39 -0500 )edit

Please show us the exact contents of your turtle_tf2_broadcaster.py.

And also the output of ls -al turtle_tf2_broadcaster.py (run it in the directory that contains the file).

gvdhoorn gravatar image gvdhoorn  ( 2019-10-18 10:28:09 -0500 )edit

I ran the command and the output was:

suyash@ROS:~/tutorial_ws/src/learning_tf2/nodes$ ls -al turtle_tf2_broadcaster.py
-rwxr-xr-x 1 suyash suyash 1218 Oct 17 18:47 turtle_tf2_broadcaster.py
suyash@ROS:~/tutorial_ws/src/learning_tf2/nodes$

I have also copied and pasted the exact contents of turtle_tf2_broadcaster.py. Also I have included full output of roslaunch learning_tf2 start_demo.launch.

Suyash gravatar image Suyash  ( 2019-10-18 10:44:37 -0500 )edit

Hi there. Is there any update for me on this question?

Suyash gravatar image Suyash  ( 2019-10-21 10:28:10 -0500 )edit

4 Answers

Sort by ยป oldest newest most voted
0

answered 2019-10-23 22:29:36 -0500

Suyash gravatar image

Hi, I have figured out the solution. There was around 3 space characters between the start and #!/usr/bin/env python. When I removed the spaces for this line and all subsequent lines, it worked.

edit flag offensive delete link more

Comments

There was around 3 space characters between the start and #!/usr/bin/env python

This was indeed the reason why I asked you to copy the contents of your script verbatim.

Good to hear you figured it out.

gvdhoorn gravatar image gvdhoorn  ( 2019-10-24 02:43:58 -0500 )edit
0

answered 2019-10-18 09:37:00 -0500

Alberto E. gravatar image

Hi Suyash,

Without more data, and assuming that you are following the ROS wiki tutorials, here: http://wiki.ros.org/tf2/Tutorials/Wri...

The most probable cause for your error is that you missed the 1st line in your script, this one:

#!/usr/bin/env python

Basically, this line makes sure that your script is executed as a Python script. Try to add it at the top of your script and the error should be gone.

edit flag offensive delete link more

Comments

Hi Alberto.Thank you for your response and pardon me for the delayed response. I am using the same tutorial as mentioned by you. I actually found that the line of code is included in my script. Please take a look at my edited question ( I have added my codes). Also one more update is I tried doing the tf tutorial as well (http://wiki.ros.org/tf/Tutorials/Writing a tf broadcaster (Python)), and I am still getting the exact same error, so there is something fundamental that I am missing.

Suyash gravatar image Suyash  ( 2019-10-18 10:24:17 -0500 )edit
0

answered 2020-04-08 06:41:36 -0500

HI, while you copy the turtle_tf2_broadcaster code from the ros page, use the "Toggle line number " before copying the code. If you copy the code with the line number and then edit that code in your text editor you may face Indentation error in python which will not be shown in the editor ("where you cant compile that code") and will not be shown in the Terminal of ROS as direct error indication. That's why you have ; RLException: Unable to launch [turtle1_tf2_broadcaster-4]. If it is a script, you may be missing a '#!' declaration at the top. The traceback for the exception was written to the log file.

!/usr/bin/env python is not the problem in your case.

edit flag offensive delete link more

Comments

in my case i had to change !/env/bin/python to !/env/bin/python3, i dont know if it could help here

Fredtzu gravatar image Fredtzu  ( 2021-03-30 08:50:46 -0500 )edit
0

answered 2022-01-28 11:57:17 -0500

Hamid-mn gravatar image

Hi there. Note to what you copy and paste from web pages. In my case I copied even the line numbers, first click on "Toggle line number" then copy source code. The exception arose as shebang was not identified .

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-10-17 17:43:37 -0500

Seen: 1,050 times

Last updated: Oct 18 '19