ros_server.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import sys
3 import time
4 
5 import rosgraph
6 import rosnode
7 import rospy
8 from opcua import Server, ua
9 
10 import ros_services
11 import ros_topics
12 
13 
14 # Returns the hierachy as one string from the first remaining part on.
15 def nextname(hierachy, index_of_last_processed):
16  try:
17  output = ""
18  counter = index_of_last_processed + 1
19  while counter < len(hierachy):
20  output += hierachy[counter]
21  counter += 1
22  return output
23  except Exception as e:
24  rospy.logerr("Error encountered ", e)
25 
26 
28  pinged, unpinged = rosnode.rosnode_ping_all()
29  if unpinged:
30  master = rosgraph.Master(rosnode.ID)
31  # noinspection PyTypeChecker
32  rosnode.cleanup_master_blacklist(master, unpinged)
33 
34 
35 class ROSServer:
36  def __init__(self):
37  self.namespace_ros = rospy.get_param("/rosopcua/namespace")
38  self.topicsDict = {}
39  self.servicesDict = {}
40  self.actionsDict = {}
41  rospy.init_node("rosopcua")
42  self.server = Server()
43  self.server.set_endpoint("opc.tcp://0.0.0.0:21554/")
44  self.server.set_server_name("ROS ua Server")
45  self.server.start()
46  # setup our own namespaces, this is expected
47  uri_topics = "http://ros.org/topics"
48  # two different namespaces to make getting the correct node easier for get_node (otherwise had object for service and topic with same name
49  uri_services = "http://ros.org/services"
50  uri_actions = "http://ros.org/actions"
51  idx_topics = self.server.register_namespace(uri_topics)
52  idx_services = self.server.register_namespace(uri_services)
53  idx_actions = self.server.register_namespace(uri_actions)
54  # get Objects node, this is where we should put our custom stuff
55  objects = self.server.get_objects_node()
56  # one object per type we are watching
57  topics_object = objects.add_object(idx_topics, "ROS-Topics")
58  services_object = objects.add_object(idx_services, "ROS-Services")
59  actions_object = objects.add_object(idx_actions, "ROS_Actions")
60  while not rospy.is_shutdown():
61  # ros_topics starts a lot of publisher/subscribers, might slow everything down quite a bit.
62  ros_services.refresh_services(self.namespace_ros, self, self.servicesDict, idx_services, services_object)
64  idx_topics, idx_actions, topics_object, actions_object)
65  # Don't clog cpu
66  time.sleep(60)
67  self.server.stop()
68  quit()
69 
70  def find_service_node_with_same_name(self, name, idx):
71  rospy.logdebug("Reached ServiceCheck for name " + name)
72  for service in self.servicesDict:
73  rospy.logdebug("Found name: " + str(self.servicesDict[service].parent.nodeid.Identifier))
74  if self.servicesDict[service].parent.nodeid.Identifier == name:
75  rospy.logdebug("Found match for name: " + name)
76  return self.servicesDict[service].parent
77  return None
78 
79  def find_topics_node_with_same_name(self, name, idx):
80  rospy.logdebug("Reached TopicCheck for name " + name)
81  for topic in self.topicsDict:
82  rospy.logdebug("Found name: " + str(self.topicsDict[topic].parent.nodeid.Identifier))
83  if self.topicsDict[topic].parent.nodeid.Identifier == name:
84  rospy.logdebug("Found match for name: " + name)
85  return self.topicsDict[topic].parent
86  return None
87 
88  def find_action_node_with_same_name(self, name, idx):
89  rospy.logdebug("Reached ActionCheck for name " + name)
90  for topic in self.actionsDict:
91  rospy.logdebug("Found name: " + str(self.actionsDict[topic].parent.nodeid.Identifier))
92  if self.actionsDict[topic].parent.nodeid.Identifier == name:
93  rospy.logdebug("Found match for name: " + name)
94  return self.actionsDict[topic].parent
95  return None
96 
97 
98 def main(args):
99  rosserver = ROSServer()
100 
101 
102 if __name__ == "__main__":
103  main(sys.argv)
def refresh_services(namespace_ros, server, servicesdict, idx, services_object_opc)
def nextname(hierachy, index_of_last_processed)
Definition: ros_server.py:15
def main(args)
Definition: ros_server.py:98
def __init__(self)
Definition: ros_server.py:36
def find_topics_node_with_same_name(self, name, idx)
Definition: ros_server.py:79
def find_service_node_with_same_name(self, name, idx)
Definition: ros_server.py:70
def refresh_topics_and_actions(namespace_ros, server, topicsdict, actionsdict, idx_topics, idx_actions, topics, actions)
Definition: ros_topics.py:269
def find_action_node_with_same_name(self, name, idx)
Definition: ros_server.py:88
def own_rosnode_cleanup()
Definition: ros_server.py:27


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:44