Skip to content

Commit b61ba41

Browse files
Merging dev branch into main (#22)
* Renaming and reorganizing package to better catkinify/pep8ify * Commands Unity can send to manage the ROS endpoint * Unregister the old handler when a new one is registered (#21) * Rewrite server core loop * Handle socket timeout * Remove the pointless threads list * Start the server on its own thread Co-authored-by: Devin Miller <devin.miller@unity3d.com>
1 parent 68ade1c commit b61ba41

30 files changed

+461
-764
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.DS_Store
22
*.pyc
33
.idea
4+
*~
5+
build
6+
devel

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(ros_tcp_endpoint)
3+
4+
find_package(catkin REQUIRED COMPONENTS
5+
rospy
6+
std_msgs
7+
message_generation
8+
)
9+
10+
catkin_python_setup()
11+
12+
add_message_files(DIRECTORY msg)
13+
14+
add_service_files(DIRECTORY srv)
15+
16+
generate_messages(DEPENDENCIES std_msgs)
17+
18+
catkin_package(CATKIN_DEPENDS message_runtime)
File renamed without changes.

msg/RosUnitySysCommand.msg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
string command
2+
string params_json

package.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<package format="2">
3+
<name>ros_tcp_endpoint</name>
4+
<version>0.0.1</version>
5+
<description>Acts as the bridge between Unity messages sent via Websocket and ROS messages.</description>
6+
7+
<maintainer email="unity-robotics@unity3d.com">Unity Robotics</maintainer>
8+
9+
<license>Apache 2.0</license>
10+
<buildtool_depend>catkin</buildtool_depend>
11+
<build_depend>rospy</build_depend>
12+
<build_depend>message_generation</build_depend>
13+
<build_export_depend>rospy</build_export_depend>
14+
<exec_depend>rospy</exec_depend>
15+
<exec_depend>message_runtime</exec_depend>
16+
17+
18+
<!-- The export tag contains other, unspecified, tags -->
19+
<export>
20+
<!-- Other tools can request additional information be placed here -->
21+
22+
</export>
23+
</package>

tcp_endpoint/src/setup.py renamed to setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# fetch values from package.xml
77
setup_args = generate_distutils_setup(
8-
packages=['tcp_endpoint'],
8+
packages=['ros_tcp_endpoint'],
99
package_dir={'': 'src'})
1010

11-
setup(**setup_args)
11+
setup(**setup_args)

src/ros_tcp_endpoint/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2020 Unity Technologies
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from .publisher import RosPublisher
16+
from .subscriber import RosSubscriber
17+
from .service import RosService
18+
from .server import TcpServer
19+

tcp_endpoint/src/tcp_endpoint/RosTCPClientThread.py renamed to src/ros_tcp_endpoint/client.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
#!/usr/bin/env python
1+
# Copyright 2020 Unity Technologies
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
214

315
import struct
416
import socket
@@ -7,7 +19,8 @@
719

820
from threading import Thread
921

10-
from tcp_endpoint.TCPEndpointExceptions import TopicOrServiceNameDoesNotExistError
22+
from .exceptions import TopicOrServiceNameDoesNotExistError
23+
1124

1225
class ClientThread(Thread):
1326
"""
@@ -133,16 +146,14 @@ def run(self):
133146
print("No data for a message size of {}, breaking!".format(full_message_size))
134147
return
135148

136-
if destination.startswith('__'):
137-
if destination not in self.tcp_server.special_destination_dict.keys():
138-
error_msg = "System message '{}' is undefined! Known system calls are: {}"\
139-
.format(destination, self.tcp_server.special_destination_dict.keys())
140-
self.conn.close()
141-
self.tcp_server.send_unity_error(error_msg)
142-
raise TopicOrServiceNameDoesNotExistError(error_msg)
143-
else:
144-
ros_communicator = self.tcp_server.special_destination_dict[destination]
145-
ros_communicator.set_incoming_ip(self.incoming_ip)
149+
if destination == '__syscommand':
150+
self.tcp_server.handle_syscommand(data)
151+
return
152+
elif destination == '__handshake':
153+
response = self.tcp_server.unity_tcp_sender.handshake(self.incoming_ip, data)
154+
response_message = self.serialize_message(destination, response)
155+
self.conn.send(response_message)
156+
return
146157
elif destination not in self.tcp_server.source_destination_dict.keys():
147158
error_msg = "Topic/service destination '{}' is not defined! Known topics are: {} "\
148159
.format(destination, self.tcp_server.source_destination_dict.keys())

src/ros_tcp_endpoint/communication.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2020 Unity Technologies
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
class RosSender:
17+
"""
18+
Base class for ROS communication where data is sent to the ROS network.
19+
"""
20+
def __init__(self):
21+
pass
22+
23+
def send(self, *args):
24+
raise NotImplementedError
25+
26+
27+
class RosReceiver:
28+
"""
29+
Base class for ROS communication where data is being sent outside of the ROS network.
30+
"""
31+
def __init__(self):
32+
pass
33+
34+
def send(self, *args):
35+
raise NotImplementedError
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
import rospy
4+
import actionlib_msgs.msg
5+
import diagnostic_msgs.msg
6+
import geometry_msgs.msg
7+
import nav_msgs.msg
8+
import sensor_msgs.msg
9+
import shape_msgs.msg
10+
import stereo_msgs.msg
11+
import trajectory_msgs.msg
12+
import visualization_msgs.msg
13+
14+
from ros_tcp_endpoint import TcpServer
15+
16+
def main():
17+
ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
18+
tcp_server = TcpServer(ros_node_name)
19+
20+
# Start the Server Endpoint
21+
rospy.init_node(ros_node_name, anonymous=True)
22+
tcp_server.start()
23+
rospy.spin()
24+
25+
26+
if __name__ == "__main__":
27+
main()

0 commit comments

Comments
 (0)