Skip to content

Commit 14605fc

Browse files
Unity server update (#49)
ROSConnection runs a listen server with auto IP detection
1 parent b2aaaae commit 14605fc

File tree

12 files changed

+77
-154
lines changed

12 files changed

+77
-154
lines changed

tutorials/pick_and_place/2_ros_tcp.md

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,10 @@ Most of the ROS setup has been provided via the `niryo_moveit` package. This sec
187187
sudo -H pip install rospkg jsonpickle
188188
```
189189

190-
> In your ROS workspace, find the directory `src/niryo_moveit/scripts`. Note the file `server_endpoint.py`. This script imports the necessary dependencies from tcp_endpoint, defines the TCP host address and port values, and starts the server. `rospy.spin()` ensures the node does not exit until it is shut down.
190+
> In your ROS workspace, find the directory `src/niryo_moveit/scripts`. Note the file `server_endpoint.py`. This script imports the necessary dependencies from tcp_endpoint and starts the server. `rospy.spin()` ensures the node does not exit until it is shut down.
191191
192192
```python
193193
...
194-
tcp_server = TCPServer(ros_tcp_ip, ros_tcp_port, ros_node_name, source_destination_dict)
195194
tcp_server.start()
196195
rospy.spin()
197196
...
@@ -203,32 +202,18 @@ Most of the ROS setup has been provided via the `niryo_moveit` package. This sec
203202

204203
1. The ROS parameters will need to be set to your configuration in order to allow the server endpoint to fetch values for the TCP connection. Navigate to `src/niryo_moveit/config/params.yaml` and open the file for editing. You will need to know the IP address of your ROS machine as well as the IP address of the machine running Unity.
205204
- The ROS machine IP, i.e. `ROS_IP` should be the same value as the one set as `Host Name` on the RosConnect component in Unity.
206-
- Finding the IP address of your local machine (the one running Unity), i.e. `UNITY_IP` depends on your operating system.
207-
- On a Mac, open `System Preferences > Network`. Your IP address should be listed on the active connection.
208-
- On Windows, click the Wi-Fi icon on the taskbar, and open `Properties`. Your IP address should be listed near the bottom, next to "IPv4 address."
209-
210-
- Update the `ROS_IP` and `UNITY_IP` below with the appropriate addresses and copy the contents into the `params.yaml` file.
205+
- Update the `ROS_IP` below with the appropriate addresses and copy the contents into the `params.yaml` file.
211206

212207
```yaml
213208
ROS_IP: <your ROS IP>
214-
ROS_TCP_PORT: 10000
215-
UNITY_IP: <your Unity IP>
216-
UNITY_SERVER_PORT: 5005
217-
rosdistro: 'melodic'
218209
```
219210
220211
e.g.
221212
222213
```yaml
223214
ROS_IP: 192.168.50.149
224-
ROS_TCP_PORT: 10000
225-
UNITY_IP: 192.168.50.13
226-
UNITY_SERVER_PORT: 5005
227-
rosdistro: 'melodic'
228215
```
229216
230-
Ensure that the `ROS_TCP_PORT` is set to 10000, and the `UNITY_SERVER_PORT` is set to 5005.
231-
232217
> Note: Learn more about the server endpoint and ROS parameters [here](../ros_unity_integration/server_endpoint.md).
233218
234219
> This YAML file is `rosparam set` from the launch files provided for this tutorial, which has been copied below for reference. Additionally, the server_endpoint and trajectory_subscriber nodes are launched from this file.

tutorials/pick_and_place/PickAndPlaceProject/Packages/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"com.unity.ide.rider": "2.0.7",
55
"com.unity.ide.visualstudio": "2.0.3",
66
"com.unity.ide.vscode": "1.2.2",
7-
"com.unity.robotics.urdf-importer": "git+https://github.com/Unity-Technologies/URDF-Importer.git#v0.0.4",
8-
"com.unity.robotics.ros-tcp-connector": "git+https://github.com/Unity-Technologies/ROS-TCP-Connector.git#v0.0.2",
7+
"com.unity.robotics.urdf-importer": "https://github.com/Unity-Technologies/URDF-Importer.git#v0.0.4",
8+
"com.unity.robotics.ros-tcp-connector": "https://github.com/Unity-Technologies/ROS-TCP-Connector.git#v0.0.3",
99
"com.unity.test-framework": "1.1.18",
1010
"com.unity.textmeshpro": "3.0.1",
1111
"com.unity.timeline": "1.4.3",

tutorials/pick_and_place/PickAndPlaceProject/Packages/packages-lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
"url": "https://packages.unity.com"
3939
},
4040
"com.unity.robotics.ros-tcp-connector": {
41-
"version": "git+https://github.com/Unity-Technologies/ROS-TCP-Connector.git#v0.0.2",
41+
"version": "https://github.com/Unity-Technologies/ROS-TCP-Connector.git#v0.0.3",
4242
"depth": 0,
4343
"source": "git",
4444
"dependencies": {},
45-
"hash": "0e963defca4eb075316d30e1e4224dee276c22cb"
45+
"hash": "251c159b0a4bab0950c43e6a3be4524876d2549f"
4646
},
4747
"com.unity.robotics.urdf-importer": {
48-
"version": "git+https://github.com/Unity-Technologies/URDF-Importer.git#v0.0.4",
48+
"version": "https://github.com/Unity-Technologies/URDF-Importer.git#v0.0.4",
4949
"depth": 0,
5050
"source": "git",
5151
"dependencies": {},
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
ROS_IP: 192.168.50.149
2-
ROS_TCP_PORT: 10000
3-
UNITY_IP: 192.168.50.13
4-
UNITY_SERVER_PORT: 5005
5-
rosdistro: 'melodic'
1+
ROS_IP: 127.0.0.1

tutorials/pick_and_place/ROS/src/niryo_moveit/scripts/server_endpoint.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,18 @@
1111
from niryo_moveit.srv import MoverService
1212

1313
def main():
14-
# Get variables set in rosparam used for
15-
# server/node communication
16-
ros_tcp_ip = rospy.get_param("/ROS_IP")
17-
ros_tcp_port = rospy.get_param("/ROS_TCP_PORT")
18-
1914
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
20-
21-
unity_machine_ip = rospy.get_param("/UNITY_IP")
22-
unity_machine_port = rospy.get_param("/UNITY_SERVER_PORT")
23-
24-
rospy.init_node(ros_node_name, anonymous=True)
25-
rate = rospy.Rate(10) # 10hz
15+
tcp_server = TCPServer(ros_node_name)
2616

2717
# Create ROS communication objects dictionary for routing messages
28-
source_destination_dict = {
18+
tcp_server.source_destination_dict = {
2919
'SourceDestination_input': RosPublisher('SourceDestination', NiryoMoveitJoints, queue_size=10),
30-
'NiryoTrajectory': RosSubscriber('NiryoTrajectory', NiryoTrajectory, unity_machine_ip, unity_machine_port),
20+
'NiryoTrajectory': RosSubscriber('NiryoTrajectory', NiryoTrajectory, tcp_server),
3121
'niryo_moveit': RosService('niryo_moveit', MoverService)
3222
}
3323

3424
# Start the Server Endpoint
35-
tcp_server = TCPServer(ros_tcp_ip, ros_tcp_port, ros_node_name, source_destination_dict)
25+
rospy.init_node(ros_node_name, anonymous=True)
3626
tcp_server.start()
3727
rospy.spin()
3828

tutorials/ros_packages/robotics_demo/scripts/server_endpoint.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,19 @@
1010
from robotics_demo.msg import PosRot, UnityColor
1111
from robotics_demo.srv import PositionService
1212

13-
1413
def main():
15-
ros_tcp_ip = rospy.get_param("/ROS_IP")
16-
ros_tcp_port = rospy.get_param("/ROS_TCP_PORT")
17-
1814
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
1915
buffer_size = rospy.get_param("/TCP_BUFFER_SIZE", 1024)
2016
connections = rospy.get_param("/TCP_CONNECTIONS", 10)
17+
tcp_server = TCPServer(ros_node_name, buffer_size, connections)
2118

22-
unity_machine_ip = rospy.get_param("/UNITY_IP")
23-
unity_machine_port = rospy.get_param("/UNITY_SERVER_PORT")
24-
25-
rospy.init_node(ros_node_name, anonymous=True)
26-
rate = rospy.Rate(10) # 10hz
27-
28-
source_destination_dict = {
19+
tcp_server.source_destination_dict = {
2920
'pos_srv': RosService('position_service', PositionService),
3021
'pos_rot': RosPublisher('pos_rot', PosRot, queue_size=10),
31-
'color': RosSubscriber('color', UnityColor, unity_machine_ip, unity_machine_port),
22+
'color': RosSubscriber('color', UnityColor, tcp_server)
3223
}
3324

34-
tcp_server = TCPServer(ros_tcp_ip, ros_tcp_port, ros_node_name, source_destination_dict, buffer_size, connections)
25+
rospy.init_node(ros_node_name, anonymous=True)
3526
tcp_server.start()
3627
rospy.spin()
3728

tutorials/ros_unity_integration/server_endpoint.md

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,25 @@ from tcp_endpoint.RosService import RosService
2222
from robotics_demo.msg import PosRot, UnityColor
2323
from robotics_demo.srv import PositionService
2424

25-
2625
def main():
27-
# Get variables set in rosparam used for
28-
# server/node communication
29-
ros_tcp_ip = rospy.get_param("/ROS_IP")
30-
ros_tcp_port = rospy.get_param("/ROS_TCP_PORT")
31-
3226
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
27+
buffer_size = rospy.get_param("/TCP_BUFFER_SIZE", 1024)
28+
connections = rospy.get_param("/TCP_CONNECTIONS", 10)
29+
tcp_server = TCPServer(ros_node_name, buffer_size, connections)
3330

34-
unity_machine_ip = rospy.get_param("/UNITY_IP")
35-
unity_machine_port = rospy.get_param("/UNITY_SERVER_PORT")
36-
37-
rospy.init_node(ros_node_name, anonymous=True)
38-
rate = rospy.Rate(10) # 10hz
39-
40-
# Create ROS communication objects dictionary for routing messages
41-
source_destination_dict = {
31+
tcp_server.source_destination_dict = {
4232
'pos_srv': RosService('position_service', PositionService),
4333
'pos_rot': RosPublisher('pos_rot', PosRot, queue_size=10),
44-
'color': RosSubscriber('color', UnityColor, unity_machine_ip, unity_machine_port),
34+
'color': RosSubscriber('color', UnityColor, tcp_server)
4535
}
4636

47-
# Start the Server Endpoint
48-
tcp_server = TCPServer(ros_tcp_ip, ros_tcp_port, ros_node_name, source_destination_dict)
37+
rospy.init_node(ros_node_name, anonymous=True)
4938
tcp_server.start()
5039
rospy.spin()
5140

5241

5342
if __name__ == "__main__":
5443
main()
55-
5644
```
5745

5846

@@ -74,7 +62,6 @@ from robotics_demo.srv import PositionService
7462

7563
```python
7664
rospy.init_node(ros_node_name, anonymous=True)
77-
rate = rospy.Rate(10) # 10hz
7865
```
7966

8067
## ROS Publisher
@@ -86,14 +73,13 @@ A ROS Publisher requires three components:
8673

8774
`RosPublisher('pos_rot', PosRot, queue_size=10)`
8875
## ROS Subscriber
89-
A ROS Subscriber requires four components:
76+
A ROS Subscriber requires three components:
9077

9178
- Topic name
9279
- ROS message class generated from running `catkin_make` command
93-
- IP address of machine running Unity
94-
- Corresponding TCP port of machine running Unity
80+
- The tcp server that will connect to Unity
9581

96-
`RosSubscriber('color', UnityColor, unity_machine_ip, unity_machine_port)`
82+
`RosSubscriber('color', UnityColor, tcp_server)`
9783

9884
## ROS Service
9985
A ROS Service requires two components:
@@ -103,48 +89,47 @@ A ROS Service requires two components:
10389

10490
`RosService('position_service', PositionService)`
10591

92+
## Creating the Server
93+
94+
Requires:
95+
96+
- The ROS node name
97+
98+
```python
99+
tcp_server = TCPServer(ros_node_name)
100+
```
101+
106102
## Source Destination Dictionary
107103

108104
Create 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.
109105

110106
```python
111-
source_destination_dict = {
107+
tcp_server.source_destination_dict = {
112108
'pos_srv': RosService('position_service', PositionService),
113109
'pos_rot': RosPublisher('pos_rot', PosRot, queue_size=10),
114-
'color': RosSubscriber('color', Color, unity_machine_ip, unity_machine_port),
110+
'color': RosSubscriber('color', Color, tcp_server),
115111
}
116112
```
117113

118-
119114
## Starting the Server
120115

121-
Requires:
122-
123-
- IP of machine the script will be executing on
124-
- The port to open for incoming connections
125-
- The ROS node name
126-
- Dictionary of service or topic names to corresponding class
127-
128116
```python
129-
tcp_server = TCPServer(ros_tcp_ip, ros_tcp_port, ros_node_name, source_destination_dict)
130117
tcp_server.start()
131118
rospy.spin()
132119

133120
```
134121

135122

136-
These values can be hardcoded, but for the sake of portability, we recommend setting the parameters using the `rosparam set` command, or a `rosparam` YAML file.
123+
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.
137124

138125
```python
139-
ros_tcp_ip = rospy.get_param("/ROS_IP")
140-
ros_tcp_port = rospy.get_param("/ROS_TCP_PORT")
141-
142126
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
143-
144-
unity_machine_ip = rospy.get_param("/UNITY_IP")
145-
unity_machine_port = rospy.get_param("/UNITY_SERVER_PORT")
127+
buffer_size = rospy.get_param("/TCP_BUFFER_SIZE", 1024)
128+
connections = rospy.get_param("/TCP_CONNECTIONS", 10)
146129
```
147130

131+
In addition, the TCPServer class uses the ROS parameters ROS_IP and ROS_TCP_PORT to determine what ip & port to listen on.
132+
148133
> Note: Read more about the ROS Parameter Server [here](http://wiki.ros.org/Parameter%20Server).
149134
150135
## Launch File
@@ -160,9 +145,6 @@ An example launch file that will set the appropriate ROSPARAM values required fo
160145
<param name="ROS_TCP_PORT" type="int" value="10000" />
161146
<param name="TCP_NODE_NAME" type="str" value="TCPServer" />
162147
163-
<param name="UNITY_IP" type="str" value="192.168.1.105" />
164-
<param name="UNITY_SERVER_PORT" type="int" value="5005" />
165-
166148
<group ns="position_service_and_endpoint">
167149
<node pkg="robotics_demo" name="position_service" type="position_service.py"/>
168150
<node pkg="robotics_demo" name="server_endpoint" type="server_endpoint.py"/>

tutorials/ros_unity_integration/service.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class RosServiceExample : MonoBehaviour
9898
}
9999
```
100100

101-
- Create an empty GameObject, name it `RosConnection`, and attach the `Plugins/TCPConnection/ROSConnection` script to it.
101+
- Create an empty GameObject, name it `RosConnection`, and attach the `Plugins/TCPConnection/ROSConnection` script to it. (Or, if you're reusing the same scene from the previous tutorials, you can just keep the existing RosConnection object.)
102102
- In the Inspector window of the Editor change the `Host Name` variable on the `RosConnection` GameObject to the ROS IP.
103103
- Create another empty GameObject and name it `RosService`.
104104
- Attach the `RosServiceExample` script to the `RosService` GameObject. Drag the cube GameObject onto its `cube` parameter and the RosConnection GameObject onto its `Ros` parameter.

tutorials/ros_unity_integration/setup.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@ Once ROS Core has started, it will print `started core service [/rosout]` to the
1919

2020
1. Note that in the `server_endpoint`, the script fetches parameters for the TCP connection. You will need to know the IP address of your ROS machine as well as the IP address of the machine running Unity.
2121
- The ROS machine IP, i.e. `ROS_IP` should be the same value as the one set as `Host Name` on the RosConnect component in Unity.
22-
- Finding the IP address of your local machine (the one running Unity), i.e. `UNITY_IP` depends on your operating system.
23-
- On a Mac, open `System Preferences > Network`. Your IP address should be listed on the active connection.
24-
- On Windows, click the Wi-Fi icon on the taskbar, and open `Properties`. Your IP address should be listed near the bottom, next to "IPv4 address."
25-
2622
1. The ROS parameter values can be set using a YAML file. Create a `params.yaml` file in your package, e.g. `./config/params.yaml`. Open the file for editing.
2723

28-
1. Update the `ROS_IP` and `UNITY_IP` below with the appropriate addresses and copy the contents into the `params.yaml` file.
24+
1. Update the `ROS_IP` below with the appropriate address and copy the contents into the `params.yaml` file.
2925

3026
```yaml
3127
ROS_IP: <your ROS IP>
3228
ROS_TCP_PORT: 10000
33-
UNITY_IP: <your Unity IP>
34-
UNITY_SERVER_PORT: 5005
3529
rosdistro: 'melodic'
3630
```
3731
@@ -40,12 +34,10 @@ Once ROS Core has started, it will print `started core service [/rosout]` to the
4034
```yaml
4135
ROS_IP: 192.168.50.149
4236
ROS_TCP_PORT: 10000
43-
UNITY_IP: 192.168.50.13
44-
UNITY_SERVER_PORT: 5005
4537
rosdistro: 'melodic'
4638
```
4739
48-
Ensure that the `ROS_TCP_PORT` is set to 10000, and the `UNITY_SERVER_PORT` is set to 5005.
40+
Ensure that the `ROS_TCP_PORT` is set to 10000.
4941

5042
1. Set these newly defined parameters by running `rosparam load`, e.g.:
5143

0 commit comments

Comments
 (0)