浏览代码

Reordered the ros-unity tutorial to do Unity Service before Service, also renamed it to Service Call

/laurie-Ros2Update
LaurieCheers 3 年前
当前提交
9401e9ca
共有 7 个文件被更改,包括 15 次插入170 次删除
  1. 15
      tutorials/ros_unity_integration/README.md
  2. 2
      tutorials/ros_unity_integration/setup.md
  3. 2
      tutorials/ros_unity_integration/subscriber.md
  4. 4
      tutorials/ros_unity_integration/unity_service.md
  5. 8
      tutorials/ros_unity_integration/service_call.md
  6. 154
      tutorials/ros_unity_integration/server_endpoint.md
  7. 0
      /tutorials/ros_unity_integration/service_call.md

15
tutorials/ros_unity_integration/README.md


## Tutorials
- [ROS–Unity Integration: Initial Setup](setup.md) - ROS-Unity Initial Setup
- [ROS–Unity Integration: Network Description](network.md) - Description of network settings and troubleshooting
- [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
- [ROS–Unity Integration: Publisher](publisher.md) - Publish messages from a Unity Scene
- [ROS–Unity Integration: Subscriber](subscriber.md) - Subscribe to receive messages in a Unity Scene
- [ROS–Unity Integration: Unity Service](unity_service.md) - Implement a service inside a Unity Scene
- [ROS–Unity Integration: Service Call](service_call.md) - Call an external service from a Unity Scene
## Example Unity Scripts

- `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/RosServiceExample.cs`
- Returns a destination position for a GameObject to move towards each time the service is called.
- `unity_scripts/RosServiceExample.cs`
- Returns a destination position for a GameObject to move towards each time the service is called.

2
tutorials/ros_unity_integration/setup.md


ros2 run ros2_tcp_endpoint default_server_endpoint --ros-args -p ROS_IP:=127.0.0.1
```
Once the server_endpoint has started, it will print something similar to `[INFO] [1603488341.950794]: Starting server on 192.168.50.149:10000`.
Once the server_endpoint has started, it will print something similar to `[INFO] [1603488341.950794]: Starting server on 192.168.50.149:10000`.
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:

2
tutorials/ros_unity_integration/subscriber.md


![](images/tcp_2.gif)
Continue to the [ROS–Unity Integration Service](service.md).
Continue to the [ROS–Unity Integration Unity Service](unity_service.md).

4
tutorials/ros_unity_integration/unity_service.md


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)))
```
```
Continue to the [ROS–Unity Integration Service Call](service_call.md).

8
tutorials/ros_unity_integration/service_call.md


# ROS–Unity Integration: Service
# ROS–Unity Integration: Service Call
Create a simple Unity scene which calls a [ROS service](http://wiki.ros.org/Services) with a GameObject's position and rotation to receive a new position to move the GameObject towards.
Create a simple Unity scene which calls an external [ROS service](http://wiki.ros.org/Services) with a GameObject's position and rotation to receive a new position to move the GameObject towards.
## Setting Up ROS

- For this tutorial we will start the position_service executable, which runs a ros service. Open a new terminal window, navigate to your ROS workspace, and run the following commands:
- For this tutorial we will need a ros service for Unity to call. Open a new terminal window, navigate to your ROS workspace, and run the following commands:
```bash
source devel/setup.bash

> Please reference [networking troubleshooting](network.md) doc if any errors are thrown.
![](images/tcp_3.gif)
Continue to the [ROS–Unity Integration Unity Service](unity_service.md).

154
tutorials/ros_unity_integration/server_endpoint.md


# ROS–Unity Integration: Server Endpoint
A walkthrough of the important components of a ROS TCP endpoint script using the `robotics_demo` package as a example.
The following is an example of a server endpoint Python script that:
- Gets parameters from `rosparam`
- Creates corresponding ROS Publisher, Subscriber, and Service objects to interact with topics and services running in ROS network
- Starts TCP Server process to handle incoming and outgoing connections
```python
#!/usr/bin/env python
import rospy
from ros_tcp_endpoint import TcpServer, RosPublisher, RosSubscriber, RosService, UnityService
from robotics_demo.msg import PosRot, UnityColor
from robotics_demo.srv import PositionService, ObjectPoseService
def main():
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
buffer_size = rospy.get_param("/TCP_BUFFER_SIZE", 1024)
connections = rospy.get_param("/TCP_CONNECTIONS", 10)
tcp_server = TcpServer(ros_node_name, buffer_size, connections)
rospy.init_node(ros_node_name, anonymous=True)
tcp_server.start({
'pos_rot': RosPublisher('pos_rot', PosRot, queue_size=10),
'color': RosSubscriber('color', UnityColor, tcp_server),
'pos_srv': RosService('pos_srv', PositionService),
'obj_pose_srv': UnityService('obj_pose_srv', ObjectPoseService, tcp_server),
})
rospy.spin()
if __name__ == "__main__":
main()
```
## Import Statements for Services and Messages
```python
from ros_tcp_endpoint import TcpServer, RosPublisher, RosSubscriber, RosService, UnityService
from robotics_demo.msg import PosRot, UnityColor
from robotics_demo.srv import PositionService, ObjectPoseService
```
## Creating the Server
Requires:
- The ROS node name
```python
tcp_server = TcpServer(ros_node_name, buffer_size, connections)
```
The `ros_node_name` argument is required and the `buffer_size` and `connections` are optional. They are set to `1024` and `10` by default if not provided in the constructor arguments.
## Instantiate the ROS Node
```python
rospy.init_node(ros_node_name, anonymous=True)
```
## Starting the Server
```python
tcp_server.start({
'pos_rot': RosPublisher('pos_rot', PosRot, queue_size=10),
'color': RosSubscriber('color', UnityColor, tcp_server),
'pos_srv': RosService('pos_srv', PositionService),
'obj_pose_srv': UnityService('obj_pose_srv', ObjectPoseService, tcp_server),
})
rospy.spin()
```
## Source Destination Dictionary
The argument to start() is a dictionary keyed by topic or service with the corresponding ROS communication class as the value. The dictionary is used by the TCP server to direct messages to and from the ROS network.
## ROS Publisher
A ROS Publisher allows a Unity component to send messages on a given topic to other ROS nodes. It requires three components:
- Topic name
- ROS message class generated from running `catkin_make` command
- Queue size (optional)
`RosPublisher('pos_rot', PosRot, queue_size=10)`
## ROS Subscriber
A ROS Subscriber allows a Unity component to receive messages from other ROS nodes on a given topic. It requires three components:
- Topic name
- ROS message class generated from running `catkin_make` command
- The tcp server that will connect to Unity
`RosSubscriber('color', UnityColor, tcp_server)`
## ROS Service
A ROS Service is similar to a RosPublisher, in that a Unity component sends a Request message to another ROS node. Unlike a Publisher, the Unity component then waits for a Response back. It requires two components:
- Service name
- ROS Service class generated from running `catkin_make` command
`RosService('pos_srv', PositionService)`
## Unity Service
A Unity Service is similar to a RosSubscriber, in that a Unity component receives a Request message from another ROS node. It then sends a Response back. It requires three components:
- Service name
- ROS Service class generated from running `catkin_make` command
- The tcp server that will connect to Unity
`UnityService('obj_pose_srv', ObjectPoseService, tcp_server)`
## Parameters
The following parameters can be hardcoded, but for the sake of portability, we recommend setting the parameters using the `rosparam set` command, or a `rosparam` YAML file.
```python
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
buffer_size = rospy.get_param("/TCP_BUFFER_SIZE", 1024)
connections = rospy.get_param("/TCP_CONNECTIONS", 10)
```
In addition, the TCPServer class uses the ROS parameters ROS_IP and ROS_TCP_PORT to determine what ip & port to listen on.
> Note: Read more about the ROS Parameter Server [here](http://wiki.ros.org/Parameter%20Server).
## Launch File
An example launch file that will set the appropriate ROSPARAM values required for a parameterized TCP Endpoint script.
```
<launch>
<env name="ROS_IP" value="127.0.0.1"/>
<env name="ROS_HOSTNAME" value="$(env ROS_IP)"/>
<param name="ROS_IP" type="str" value="$(env ROS_IP)" />
<param name="ROS_TCP_PORT" type="int" value="10000" />
<param name="TCP_NODE_NAME" type="str" value="TCPServer" />
<group ns="position_service_and_endpoint">
<node pkg="robotics_demo" name="position_service" type="position_service.py"/>
<node pkg="robotics_demo" name="server_endpoint" type="server_endpoint.py"/>
</group>
</launch>
```

/tutorials/ros_unity_integration/service.md → /tutorials/ros_unity_integration/service_call.md

正在加载...
取消
保存