RobotecAI / ros2-for-unity

High-performance ROS2 solution for Unity3D
Apache License 2.0
446 stars 58 forks source link

Output data from ROS2 Talker Example.cs and ROS2 listener Example.cs to the scene. #88

Closed izakisyoutarou closed 5 months ago

izakisyoutarou commented 11 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. We thought it would be easier to see the data if the talker and listener display could be reflected in the scene. I have not had any problems using it and thought the user interface could be improved by introducing this feature.

Describe the solution you'd like A clear and concise description of what you want to happen. I want to display data on the scene to make it easier to see the data display.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered. Using TextMeshPro to output data to the scene

Additional context Add any other context or screenshots about the feature request here. TalkerExample.cs could output data to the scene. The script looks like this. image

It will be displayed in the scene like this. image

I tried to output ListenerExample.cs in the same way, but an error occurred and it did not work. I wrote the script like this, and I thought I could get there by changing the Debug.Log function, but I got an error. image

The error statement is as follows: It seems that the problem is not done in the main thread, but I don't know how to solve it, so I would like to know if you know how to solve this problem. Screenshot from 2023-09-23 16-22-54

I know this is a long story, but the point is that I want to use TextMeshPro to output to a scene. Thank you in advance for your help. Thank you for reading to the end.

pijaro commented 11 months ago

Hi,

The ROS 2 spin works in a different thread. Some Unity functions require the main thread to work, therefore you need to pass the data between "other" and "main" threads.

You can do it, for example, with ConcurrentQueue<T>. Enqueue the data in the ROS 2 spin thread, and TryDequeue in the main thread (in Update or FixedUpdate).

izakisyoutarou commented 11 months ago

Thanks for your reply. I was aware that there are different threads that process the code, but I don't know how to handle them, and after looking up how to use ConcurrentQueue, I don't know what type to put inside the T. I know I am ignorant, but could you please elaborate on this? I have looked into the SynchronizationContext.Current function to delegate processing to the main thread as there are many other ways to do this. I wrote the code but it did not work and I got the same error. The code is as follows Also, just to confirm, is it programmatically ok to replace the Debug.Log function with TMPro? Thank you in advance for your reply. image_720