浏览代码

Unity service example (#199)

* added example of implementing a UnityService

* adding tutorial for unity service

* added a couple comments to the example code

* typos and code standard fixes

* consistency: changing the service name for position_service to pos_srv

* updating the tutorial to match the corresponding script
/merge-resolve-conflicts
peifeng-unity 3 年前
当前提交
4743202d
共有 3 个文件被更改,包括 21 次插入16 次删除
  1. 9
      tutorials/ros_unity_integration/README.md
  2. 4
      tutorials/ros_unity_integration/unity_scripts/RosUnityServiceExample.cs
  3. 24
      tutorials/ros_unity_integration/unity_service.md

9
tutorials/ros_unity_integration/README.md


- [ROS–Unity Integration: Publisher](publisher.md) - Adding a Publisher to a Unity Scene
- [ROS–Unity Integration: Subscriber](subscriber.md) - Adding a Subscriber to a Unity Scene
- [ROS–Unity Integration: Service](service.md) - Adding a Service call to a Unity Scene
- [ROS–Unity Integration: UnityService](unity_service.md) - Adding a Service that runs in a Unity Scene
- [ROS–Unity Integration: Server Endpoint](server_endpoint.md) - How to write a Server Endpoint
## Example Unity Scripts

- `unity_scripts/RosPublisherExample.cs`
- Publishes the position of a GameObject every 0.5 seconds.
- `unity_scripts/RosSubscriberExample.cs`
- Subscribes to a topic that accepts color messages and uses them to change the color of a GameObject in the Unity scene.
- `unity_scripts/RosSubscriberExample.cs`
- Subscribes to a topic that accepts color messages and uses them to change the color of a GameObject in the Unity scene.
- `unity_scripts/RosUnityServiceExample.cs`
- Runs a service in the Unity scene that takes a GameObject's name and responds with the Pose of that object.

4
tutorials/ros_unity_integration/unity_scripts/RosUnityServiceExample.cs


MObjectPoseServiceResponse objectPoseResponse = new MObjectPoseServiceResponse();
// Find a game object with the requested name
GameObject gameObject = GameObject.Find(request.object_name);
if (gameObject)
if (gameObject)
return objectPoseResponse;
}
}

24
tutorials/ros_unity_integration/unity_service.md


- Follow the [ROS–Unity Initial Setup](setup.md) guide.
- Open a new terminal window, navigate to your ROS workspace, and run the following commands:
```bash
source devel/setup.bash
rosrun robotics_demo server_endpoint.py

![](images/generate_messages_2.png)
- The generated files will be saved in the default directory `Assets/RosMessages/RoboticsDemo/srv`.
- Create a new C# script and name it `RosUnityServiceExample.cs`
- Paste the following code into `RosUnityServiceExample.cs`
- **Note:** This script can be found at `tutorials/ros_unity_integration/unity_scripts`.

MObjectPoseServiceResponse objectPoseResponse = new MObjectPoseServiceResponse();
// Find a game object with the requested name
GameObject gameObject = GameObject.Find(request.object_name);
if (gameObject)
if (gameObject)
return objectPoseResponse;
}
}

- 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`.
- 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 `ROS-Unity Handshake received, will connect to 192.168.50.130:5005`.
## Start the Client

```bash
Requesting pose for Cube
Pose for Cube:
position:
Pose for Cube:
position:
orientation:
orientation:
x: 0.0
y: -0.0
z: 0.0

rosservice call /obj_pose_srv Cube
```
```bash
object_pose:
position:
object_pose:
position:
orientation:
orientation:
x: 0.0
y: -0.0
z: 0.0
正在加载...
取消
保存