How to debug with vscode debugger in rosject

In the past I have been able to launch my ros node from vs code and step through and debug it. For my rosjec enviroment I can’t seem to be able to. I get the following error.

user:~$ cd '/home/user' && env '/bin/python3' '/home/theia/plugins/vscode-python/extension/pythonFiles/lib/python/debugpy/launcher' '32785' '--' '/home/user/ros2_ws/src/wall_follower/wall_follower/wall_following.py'
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/theia/plugins/vscode-python/extension/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/theia/plugins/vscode-python/extension/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/theia/plugins/vscode-python/extension/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "/usr/lib/python3.8/runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/user/ros2_ws/src/wall_follower/wall_follower/wall_following.py", line 76, in <module>
    main()
  File "/home/user/ros2_ws/src/wall_follower/wall_follower/wall_following.py", line 64, in main
    rclpy.init(args=args)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/__init__.py", line 71, in init
    return context.init(args)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/context.py", line 64, in init
    rclpy_implementation.rclpy_logging_configure(capsule)
_rclpy.RCLError: Failed to initialize logging: Failed to create log directory: /home/user/.ros/log, at /tmp/binarydeb/ros-foxy-rcl-logging-spdlog-1.1.0/src/rcl_logging_spdlog.cpp:100
user:~$ 

I am able to run the node normally if I launch it with python from the command line and it works as expected. Also I tried to source the environment where the debug terminal is but that doesn’t seem to help. Online on a similarish issue it mentions that you should do the following: remove --user or add --home to your docker arguments. In a similar vein it looks like the command is targeting the /home/theia/ directory for the user and not the user directory so I’m not sure if that is the issue.

Has anyone else been able to debug a python script using ROS2 in a rosject? Thanks.

Hi @gupyfish ,

Welcome to this Community!

Let me ask you a logical question.

How can you debug a simulation process?

In a simulation, anything can happen due to factors like sensor noise, movement noise, etc.
Any debugger, be it C++ or Python, will not be able to tell you where an error occurs, when the program has compiled successfully.

Assume that you are a robot programmed to walk in a straight line forever. Suddenly you step on a stone and you deviate from your straight path. Can the debugger actually tell you that you stepped on a stone or you are not in a straight line anymore? No. The debugger cannot do this - unless you have a logic implemented to detect these signals and provide a warning statement.

Therefore, for any simulation, not only ROS[1/2], but for any simulation system, debugging is not possible - unless the process is completely controlled - as in, there are no random variables involved. In other words, program semantics cannot be handled by the debugger.

So, the best debugging solution is to use print statements and see your terminal outputs - rather than depending on your IDE’s debugger. IDE debugger only comes handy if you are writing applications - where every input and output is known.

At this point, I would like to know something - What do you wish to debug and why?

Regards,
Girish

PS: The above is my understanding of simulation programs, it may not be exactly true, but it is reasonable.

Q: How can you debug a simulation process?
A: My thoughts would be that you can halt the simulation when it hits your break point. When it breaks you can inspect the state it was in when that break point tripped. I’m not trying to debug a simulation as much as I am trying to debug my python publisher node.

Q: Can the debugger actually tell you that you stepped on a stone or you are not in a straight line anymore?
A: I think it could, you could add a branch in your code to run if you are outside your respective expected axis bounds. Ie if the odometer says I am deviated from my path then print off course. Then you could put a break point at the print statement. You would be able to halt the program that is publishing the velocity to see why it deviated. I’m sure there is some way to pause the gazebo simulation via some kind of RPC but am not sure.

S:IDE debugger only comes handy if you are writing applications
A: I am writing a publisher node right now and want to inspect a data structure. I’m more or less writing an application.

Q: What do you wish to debug and why?
A: I’m working on the wall following algorithm for one of the courses and can’t quite figure out the orientation of the /scan topic. I can’t quite tell if pi / 2 is the front of the robot or if it is the right side of the robot. I get that the min is -3.14 radian and the max is +3.14 radian but I don’t understand where the measurement is starting from with respect to the robot. The most logical thing to me would be that the robot forward is pi / 2 radian and the right side is 0 radian. I don’t know if the manufacturer thought differently and put 0 radian as the front and - pi / 2 as the right side. I wanted to inspect the scan message to figure out how it was oriented and what index(s) I should use.

Hi @gupyfish ,

As far as I know, you can only pause gazebo physics, not the entire simulation for a specified time. I have not come across a function yet - perhaps there is a plugin to achieve this.

Exactly my point. Even to know something wrong has happened, you need to add some inputs to your program - from your example - odometer readings. But then odometer is very noise prone. Raw data from odometer may be way off without any noise filtering and estimation. So without additional information, debugger cannot help you best.

Well, you know the data structure and contents when you are publishing something. The best thing that you can do while simulating is to print what you are publishing. Simulation cannot wait for you to set a breakpoint and debug line by line.

To be honest, you don’t need a debugger for this. Just some common sense.
Doing ros2 topic echo /scan will give you at least one set of laser scan data. From this you have already noted angle_min = -3.14 and angle_max = +3.14. With the help of angle_increments you can also know how many laser scan rays you have - which is equivalent to number of measurements that you will get for ranges. The 0th reading (first element) of the laser scanner has to be in one of the sides - front or left or right or back. Take your robot towards a corner (L-shaped region with two walls) and keep printing the range values of the indexes of 0 degrees, 90 degrees, 180 degrees, 270 degrees. Whichever aligns correctly, that is the orientation of the laser scanner.
To save you the trouble, the laser scanner on the rosject robot has 0 on its back.
So, if your laser scanner has 720 values:
0/719 = back, 180 = right, 360 = front, 540 = left
Else, if your laser scanner has 360 values:
0/359 = back, 90 = right, 180 = front, 270 = left

Regards,
Girish

1 Like

As far as I know, you can only pause gazebo physics, not the entire simulation for a specified time. I have not come across a function yet - perhaps there is a plugin to achieve this.

I think that you could get the desired effect to step through a simulation by using a rosbag to control / pause the message streams. Maybe that in conjunction with pausing the physics would work. I wouldn’t know since I haven’t tried it.

To save you the trouble, the laser scanner on the rosject robot has 0 on its back.

:sweat_smile: Thanks for that info, I didn’t suspect 0 was on the back. I was thinking of it as a polar graph with the robot facing pi / 2. I was confused when my numbers weren’t really making sense. I was thinking 0 was forward and then using - or + sign to denote the left or right side. That way like - pi / 2 would be left and + pi / 2 would be right.