浏览代码

All working. In ROS2 at least. I hope.

/laurie-Ros2Update
LaurieCheers 3 年前
当前提交
c632b284
共有 8 个文件被更改,包括 445 次插入70 次删除
  1. 38
      tutorials/ros_unity_integration/publisher.md
  2. 4
      tutorials/ros_unity_integration/ros2_packages/unity_robotics_demo/setup.py
  3. 4
      tutorials/ros_unity_integration/ros2_packages/unity_robotics_demo/unity_robotics_demo/position_service.py
  4. 15
      tutorials/ros_unity_integration/setup.md
  5. 1
      tutorials/ros_unity_integration/unity_scripts/RosSubscriberExample.cs
  6. 1
      tutorials/ros_unity_integration/unity_scripts/RosUnityServiceExample.cs
  7. 53
      tutorials/ros_unity_integration/unity_service.md
  8. 399
      tutorials/ros_unity_integration/images/generate_messages_3.png

38
tutorials/ros_unity_integration/publisher.md


- Follow the [ROS–Unity Initial Setup](setup.md#ros2-environment) guide.
## Start the Echo monitor
- For this tutorial we will run the rostopic echo command, which watches the topic we're going to publish messages on, to prove they are being received.
- Open a new terminal window, navigate to your ROS workspace, and run the following commands:
```bash
source devel/setup.bash
rostopic echo pos_rot
```
- <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> In ROS2, the commands to run are
```bash
source install/setup.bash
ros2 topic echo pos_rot
```
- If it's working correctly it will print nothing and wait for a message to be published.
- (Alternatively, you can drag the script file into Unity from `tutorials/ros_unity_integration/unity_scripts/RosPublisherExample.cs`)
- (Alternatively, you can drag the script file into Unity from `tutorials/ros_unity_integration/unity_scripts/RosPublisherExample.cs` in this repo.)
```csharp
using UnityEngine;

- Drag the cube GameObject onto the `Cube` parameter.
- Press play in the Editor. You should see the connection lights at the top left corner of the Game window turn blue, and something like `[INFO] [1622242057.562860400] [TCPServer]: Connection from 172.17.0.1` appear in the terminal running your server_endpoint.
## Start the Echo monitor
- To prove that messages are actually being received by ROS, let's run the rostopic echo command.
- Open a new terminal window, navigate to your ROS workspace, and run the following commands:
```bash
source devel/setup.bash
rostopic echo pos_rot
```
- <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> In ROS2, the commands to run are
In the window running your echo monitor, you should see the contents of your messages from Unity appearing every 0.5 seconds.
```bash
source install/setup.bash
ros2 topic echo pos_rot
```
- If it's working correctly, you should see the contents of the message Unity is sending appearing every 0.5 seconds.
> Please reference [networking troubleshooting](network.md) doc if any errors are thrown.

4
tutorials/ros_unity_integration/ros2_packages/unity_robotics_demo/setup.py


tests_require=['pytest'],
entry_points={
'console_scripts': [
'color_publisher = ros2_test.color_publisher:main',
'position_service = ros2_test.position_service:main',
'color_publisher = unity_robotics_demo.color_publisher:main',
'position_service = unity_robotics_demo.position_service:main',
],
},
)

4
tutorials/ros_unity_integration/ros2_packages/unity_robotics_demo/unity_robotics_demo/position_service.py


import random
import rclpy
#from unity_interfaces.srv import PositionService
from unity_robotics_demo_msgs.srv import PositionService
from rclpy.node import Node

super().__init__('position_service_node')
#self.srv = self.create_service(PositionService, 'pos_srv', self.new_position_callback)
self.srv = self.create_service(PositionService, 'pos_srv', self.new_position_callback)
def new_position_callback(self, request, response):
response.output.pos_x = random.uniform(-4.0, 4.0)

15
tutorials/ros_unity_integration/setup.md


Once ROS Core has started, it will print `started core service [/rosout]` to the terminal window.
5. Run the following command, replacing the IP address 127.0.0.1 with your ROS machine's IP or hostname. (If you don't know your IP address, you can find it out with the command `hostname -I`. If you're running ROS in a Docker container, the default incoming IP address is 0.0.0.0.)
5. In your previous terminal, run the following command, replacing the IP address 127.0.0.1 with your ROS machine's IP or hostname. (If you don't know your IP address, you can find it out with the command `hostname -I`. If you're running ROS in a Docker container, the default incoming IP address is 0.0.0.0.)
```bash
rosparam set ROS_IP 127.0.0.1
```

Ensure there are no errors.
5. Open a new terminal window and run the following commands, replacing the IP address 127.0.0.1 with your ROS machine's IP or hostname. (If you don't know your IP address, you can find it out with the command `hostname -I`. If you're running ROS in a Docker container, the default incoming IP address is 0.0.0.0.)
5. Run the following command, replacing the IP address 127.0.0.1 with your ROS machine's IP or hostname. (If you don't know your IP address, you can find it out with the command `hostname -I`. If you're running ROS in a Docker container, the default incoming IP address is 0.0.0.0.)
source install/setup.bash
6. (Alternative) If you need the server to listen on a port that's different from the default 10000, here's the command line to set the ROS_TCP_PORT parameter:
6. (Alternative) If you need the server to listen on a port that's different from the default 10000, here's the command line to also set the ROS_TCP_PORT parameter:
`ros2 run unity_robotics_demo server_endpoint --ros-args -p ROS_IP:=127.0.0.1 -p ROS_TCP_PORT:=10000`
ros2 run unity_robotics_demo server_endpoint --ros-args -p ROS_IP:=127.0.0.1 -p ROS_TCP_PORT:=10000
```
## Unity Scene

![](images/add_package_2.png)
3. From the Unity menu bar, open `Robotics/ROS Settings`, and set the `ROS IP Address` variable to the IP you set earlier.
3. From the Unity menu bar, open `Robotics/ROS Settings`, and set the `ROS IP Address` variable to the IP you set earlier. (If using a Docker container, you can leave it on the default 127.0.0.1).
4. <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> If you're using ROS2, you should also switch the protocol to ROS2 now.
4. <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> ROS2 users should also switch the protocol to ROS2 now.
![](images/ros2_protocol.png)
## Setting up the Ros-Unity Integration tutorials

3. In the message browser, expand the unity_robotics_demo_msgs subfolder and click "Build 2 msgs" and "Build 2 srvs" to generate C# scripts from the ROS .msg and .srv files.
![](images/generate_messages_1.png)
![](images/generate_messages_3.png)
The generated files will be saved in the default directories `Assets/RosMessages/UnityRoboticsDemo/msg` and `Assets/RosMessages/UnityRoboticsDemo/srv`.

1
tutorials/ros_unity_integration/unity_scripts/RosSubscriberExample.cs


void Start()
{
ros = ROSConnection.instance;
ros.RegisterSubscriber<RosColor>("color");
ros.Subscribe<RosColor>("color", ColorChange);
}

1
tutorials/ros_unity_integration/unity_scripts/RosUnityServiceExample.cs


{
// register the service with ROS
ros = ROSConnection.instance;
ros.RegisterUnityService<ObjectPoseServiceRequest>(m_ServiceName);
ros.ImplementService<ObjectPoseServiceRequest>(m_ServiceName, GetObjectPose);
}

53
tutorials/ros_unity_integration/unity_service.md


}
```
- From the main menu bar, open `Robotics/ROS Settings`, and change the `ROS IP Address` variable to the ROS IP.
- Create an empty GameObject and name it `UnityService`.
- Attach the `RosUnityServiceExample` script to the `UnityService` GameObject.
- Pressing play in the Editor should start running as a ROS node, waiting to accept ObjectPose requests. Once a connection to ROS has been established, a message will be printed on the ROS terminal similar to `Connection from 172.17.0.1`.

- On your ROS system, open a new terminal window, navigate to your ROS workspace, and run the following commands:
```bash
source devel/setup.bash
rosrun unity_robotics_demo object_pose_client.py Cube
```
- <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> If using ROS2, the command is:
```bash
source install/setup.bash
ros2 run unity_robotics_demo object_pose_client Cube
```
- This wil print an output similar to the following with the current pose information of the game object (note that the coordinates are converted to the ROS coordinate system in our Unity Service):
```bash
Requesting pose for Cube
Pose for Cube:
position:
x: 0.0
y: -1.0
z: 0.20000000298023224
orientation:
x: 0.0
y: -0.0
z: 0.0
w: -1.0
```
You may replace `Cube` with the name of any other GameObject currently present in the Unity hierarchy.
- Alternatively you may also call the ROS service using `rosservice call`:
- To test our new service is working, run the following command in your ROS terminal:
- <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> If using ROS2, the command is:
```bash
ros2 service call obj_pose_srv unity_robotics_demo_msgs/ObjectPoseService "{object_name: Cube}"
```
- In your Unity console you should see the log message `Received request for object: Cube`, and in your terminal it will report the object's position, like this:
```bash
object_pose:
position:

y: -0.0
z: 0.0
w: -1.0
```
```
- <img src="images/ros2_icon.png" alt="ros2" width="23" height="14"/> If you're using ROS2, the command is:
```bash
ros2 service call obj_pose_srv unity_robotics_demo_msgs/ObjectPoseService "{object_name: Cube}"
```
And the output will look like this:
```bash
requester: making request: unity_robotics_demo_msgs.srv.ObjectPoseService_Request(object_name='Cube')
response:
unity_robotics_demo_msgs.srv.ObjectPoseService_Response(object_pose=geometry_msgs.msg.Pose(position=geometry_msgs.msg.Point(x=0.0, y=-0.0, z=0.0), orientation=geometry_msgs.msg.Quaternion(x=-0.558996319770813, y=-0.3232670724391937, z=-0.6114855408668518, w=-0.4572822153568268)))
```

399
tutorials/ros_unity_integration/images/generate_messages_3.png

之前 之后
宽度: 2019  |  高度: 627  |  大小: 116 KiB
正在加载...
取消
保存