ros_topology.py
Go to the documentation of this file.
00001 # [db] dan@danbrooks.net
00002 #
00003 # diarc topology data structures for ROS
00004 # This renames some things into ROS terminology to be more conveient, and also
00005 # adds some attributes we want to track. 
00006 #
00007 # Renaming Looks like this
00008 #   Vertex = Node
00009 #   Edge = Topic
00010 #   Sink = Subscriber
00011 #   Source = Publisher
00012 #
00013 from diarc.topology import *
00014 
00015 class RosSystemGraph(Topology):
00016     def __init__(self):
00017         super(RosSystemGraph,self).__init__()
00018 
00019     @property
00020     def nodes(self):
00021         return dict([(v.name,v) for v in self.vertices])
00022 
00023     @property
00024     def topics(self):
00025         return dict(filter(lambda x: None not in x, [(topic.name,topic) for topic in self.edges]))
00026 
00027     def nextFreeNodeIndex(self):
00028         """ returns the next available node index """
00029         return max(self.blocks.keys())+1 if len(self.blocks)>0 else 0
00030     
00031     def nextFreeAltitudes(self):
00032         """ returns a 2-tuple of (posAltitude,negAltitude) of the avaliable altitudes """
00033         altitudes = [band.altitude for band in self.bands.values()] + [0]
00034         return (max(altitudes)+1,min(altitudes)-1)
00035 
00036 
00037 
00038 
00039 class Node(Vertex):
00040     def __init__(self,rsg,name=None):
00041         typecheck(rsg,RosSystemGraph,"rsg")
00042         super(Node,self).__init__(rsg)
00043 
00044         # dumb placement - just get the next free index
00045         self.block.index = rsg.nextFreeNodeIndex()
00046 
00047         self.name = name
00048         self.location = None
00049         self.pid = None
00050 
00051     @property
00052     def publishers(self):
00053         # NOTE: This must be a property function (instead of just saying 
00054         # self.publishers = self.sources in the constructor) because self.sources
00055         # just returns a static list once, and we need this to be dynamically
00056         # queried every time we ask. This is because Vertex.sources and Edge.sources
00057         # are just syntax sugar for functions that are being called.
00058         return self.sources
00059 
00060     @property
00061     def subscribers(self):
00062         return self.sinks
00063 
00064 
00065 class Topic(Edge):
00066     def __init__(self,rsg,name=None,msgType=None):
00067         typecheck(rsg,RosSystemGraph,"rsg")
00068         super(Topic,self).__init__(rsg)
00069         
00070         # Dumb placement - just get the enxt free altitudes
00071         self.posBand.altitude,self.negBand.altitude = rsg.nextFreeAltitudes()
00072         self.posBand.rank = self.posBand.altitude
00073         self.negBand.rank = self.posBand.altitude
00074 
00075         self.name = name
00076         self.msgType = msgType
00077 
00078     @property
00079     def publishers(self):
00080         # NOTE: See note on Node class about why this MUST be a property.
00081         return self.sources
00082 
00083     @property
00084     def subscribers(self):
00085         # NOTE: See note on Node class about why this MUST be a property.
00086         return self.sinks
00087 
00088 
00089 
00090 
00091 class Publisher(Source):
00092     def __init__(self,rsg,node,topic):
00093         typecheck(rsg,RosSystemGraph,"rsg")
00094         typecheck(node,Node,"node")
00095         typecheck(topic,Topic,"topic")
00096         super(Publisher,self).__init__(rsg,node,topic)
00097         # Dumb placement
00098         self.snap.order = max(filter(lambda x: isinstance(x,int), [pub.snap.order for pub in node.publishers] + [-1]))+1
00099 
00100         self.bandwidth = None
00101         self.msgType = None
00102 
00103     @property
00104     def topic(self):
00105         # NOTE: See note on Node class about why this MUST be a property.
00106         return self.edge
00107 
00108     @property
00109     def node(self):
00110         # NOTE: See note on Node class about why this MUST be a property.
00111         return self.vertex
00112 
00113 class Subscriber(Sink):
00114     def __init__(self,rsg,node,topic):
00115         typecheck(rsg,RosSystemGraph,"rsg")
00116         typecheck(node,Node,"node")
00117         typecheck(topic,Topic,"topic")
00118         super(Subscriber,self).__init__(rsg,node,topic)
00119 
00120         # Dumb placement
00121         self.snap.order = max(filter(lambda x: isinstance(x,int), [sub.snap.order for sub in node.subscribers] + [-1]))+1
00122 
00123         self.bandwidth = None
00124         self.msgType = None
00125 
00126     @property
00127     def topic(self):
00128         # NOTE: See note on Node class about why this MUST be a property.
00129         return self.edge
00130 
00131     @property
00132     def node(self):
00133         # NOTE: See note on Node class about why this MUST be a property.
00134         return self.vertex
00135 
00136 


rqt_graphprofiler
Author(s): Dan Brooks
autogenerated on Thu Jun 6 2019 20:29:31