Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.apo.nmsu.edu/Telescopes/TCC/html/udp_packet_8py_source.html
Дата изменения: Sat Oct 17 02:45:35 2015
Дата индексирования: Sun Apr 10 01:51:54 2016
Кодировка:

Поисковые слова: molecular cloud
lsst.tcc: python/tcc/msg/udpPacket.py Source File
lsst.tcc  1.2.2-3-g89ecb63
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
udpPacket.py
Go to the documentation of this file.
1 import struct
2 import itertools
3 import collections
4 
5 import coordConv
6 from twisted.internet.protocol import DatagramProtocol
7 from twisted.internet import reactor
8 
9 __all__ = ["UDPSender", "getBinaryForUDP"]
10 
11 class UDPBroadcastProtocol(DatagramProtocol):
12  """!A variant of DatagramProtocol that enables broadcasting
13  """
14  def startProtocol(self):
15  self.transport.setBroadcastAllowed(True)
16 
17 class UDPSender(object):
18  def __init__(self, port):
19  """!Object which will broadcast UDP Packets containing current position and other data
20 
21  The packet format is documented here: http://www.apo.nmsu.edu/Telescopes/TCC/UDPPackets.html
22 
23  @param[in] port port from which to send UDP packets
24  """
26  self.port = port
27  self.udpListen = None
28 
29  def stopListening(self):
30  """!Shut down the port, to close cleanly
31  """
32  if self.udpListen is not None:
33  self.udpListen.stopListening()
34  self.udpListen = None
35 
36  def startListening(self):
37  """!Open the port to allow writing data
38  """
39  if self.udpListen is None:
40  self.udpListen = reactor.listenUDP(0, self.protocol)
41 
42  def sendPacket(self, binaryData):
43  """!Write binaryData
44  """
45  self.protocol.transport.write(binaryData, addr=("<broadcast>", self.port))
46 
47 
48 # the format used for struct:
49 udpFmt = "!iiiidd8sdddddddddi4xddddddddddddd" \
50  + "i4xi4xi4xi4xi4xi4xdddddddddi4xi4xi4x"
51 
52 fields = [
53  "size",
54  "type",
55  "majorVers",
56  "minorVers",
57  "taiDate",
58  "slewEndtime",
59  "coordSys",
60  "epoch",
61  "objNetPos.pos1",
62  "objNetPos.vel1",
63  "objNetPos.pos2",
64  "objNetPos.vel2",
65  "boresight1",
66  "boresight2",
67  "boresight3",
68  "boresight4",
69  "rotType",
70  "rotUser.pos",
71  "rotUser.vel",
72  "objUserInstAng.pos",
73  "objUserInstAng.vel",
74  "spiderInstAng.pos",
75  "spiderInstAng.vel",
76  "tccPos.pos1",
77  "tccPos.vel1",
78  "tccPos.pos2",
79  "tccPos.vel2",
80  "tccPos.pos3",
81  "tccPos.vel3",
82  "secFocus",
83 
84  "axisCmdState1",
85  "axisCmdState2",
86  "axisCmdState3",
87  "axisErrCode1",
88  "axisErrCode2",
89  "axisErrCode3",
90  "actMount.pos1",
91  "actMount.vel1",
92  "actMount.t1",
93  "actMount.pos2",
94  "actMount.vel2",
95  "actMount.t2",
96  "actMount.pos3",
97  "actMount.vel3",
98  "actMount.t3",
99  "axisStatusWord1",
100  "axisStatusWord2",
101  "axisStatusWord3",
102 ]
103 
104 def getBinaryForUDP(inst, obj):
105  """Format a binary representation of the state of the actor, for sending via UDP
106 
107  The format is described here: http://www.apo.nmsu.edu/Telescopes/TCC/UDPPackets.html
108 
109  @param[in] inst: instrument block
110  @param[in] obj: object block
111  """
112  updateTime = obj.updateTime
113  netUserEquat = coordConv.PVT()
114  netUserPolar = coordConv.PVT()
115  obj.netUserPos.getSphPVT(netUserEquat, netUserPolar)
116  tccPos = obj.targetMount
117  toPack = collections.OrderedDict((
118  ("size", 248), # header, copied from tccdev output
119  ("type", -559038737), # header, copied from tccdev output
120  ("majorVers", 2), # header, copied from tccdev output
121  ("minorVers", 4), # header, copied from tccdev output
122  ("taiDate", updateTime),
123  ("slewEndtime", obj.slewEndTime),
124  ("coordSys", obj.userSys.getName()),
125  ("epoch", obj.userSys.getDate()),
126  ("objNetPos.pos1", netUserEquat.getPos(updateTime)),
127  ("objNetPos.vel1", netUserEquat.vel),
128  ("objNetPos.pos2", netUserPolar.getPos(updateTime)),
129  ("objNetPos.vel2", netUserPolar.vel),
130  ("boresight.pos1", obj.objInstXY[0].getPos(updateTime)),
131  ("boresight.vel1", obj.objInstXY[0].vel),
132  ("boresight.pos2", obj.objInstXY[1].getPos(updateTime)),
133  ("boresight.vel2", obj.objInstXY[1].vel),
134  ("rotType", obj.rotType),
135  ("rotUser.pos", obj.rotUser.pos),
136  ("rotUser.vel", obj.rotUser.vel),
137  ("objUserInstAng.pos", obj.objUserInstAng.getPos(updateTime)),
138  ("objUserInstAng.vel", obj.objUserInstAng.vel),
139  ("spiderInstAng.pos", obj.spiderInstAng.getPos(updateTime)),
140  ("spiderInstAng.vel", obj.spiderInstAng.vel),
141  ("tccPos.pos1", tccPos[0].getPos(updateTime)),
142  ("tccPos.vel1", tccPos[0].vel),
143  ("tccPos.pos2", tccPos[1].getPos(updateTime)),
144  ("tccPos.vel2", tccPos[1].vel),
145  ("tccPos.pos3", tccPos[2].getPos(updateTime)),
146  ("tccPos.vel3", tccPos[2].vel),
147  ("secFocus", inst.secUserFocus),
148  ("axisCmdState1", obj.axisCmdState[0]),
149  ("axisCmdState2", obj.axisCmdState[1]),
150  ("axisCmdState3", obj.axisCmdState[2]),
151  ("axisErrCode1", obj.axisErrCode[0]),
152  ("axisErrCode2", obj.axisErrCode[1]),
153  ("axisErrCode3", obj.axisErrCode[2]),
154 
155  # report actMount at the time reported by the axis controller
156  ("actMount.pos1", obj.actMount[0].pos),
157  ("actMount.vel1", obj.actMount[0].vel),
158  ("actMount.t1", obj.actMount[0].t),
159  ("actMount.pos2", obj.actMount[1].pos),
160  ("actMount.vel2", obj.actMount[1].vel),
161  ("actMount.t2", obj.actMount[1].t),
162  ("actMount.pos3", obj.actMount[2].pos),
163  ("actMount.vel3", obj.actMount[2].vel),
164  ("actMount.t3", obj.actMount[2].t),
165  ("axisStatusWord1", obj.axisStatusWord[0]),
166  ("axisStatusWord2", obj.axisStatusWord[1]),
167  ("axisStatusWord3", obj.axisStatusWord[2]),
168  ))
169  return struct.pack(udpFmt, *toPack.values())
170 
171 def unwrapPacket(pkt):
172  """!Produce readable text from an encoded UDP packet broadcasted from the tcc
173  return the datagram in a dictionary format
174  """
175  outDict = {}
176  out = struct.unpack(udpFmt,pkt)
177  print 'unwrappingPacket:'
178  for field, data in itertools.izip(fields, out):
179  outDict[field] = data
180  print "%s: %s" % (field, data)
181  print
182  return outDict
183 
184 class BroadcastUDPClient(DatagramProtocol):
185  packetsReceived = []
186  def startProtocol(self):
187  self.transport.setBroadcastAllowed(True)
188 
189  def datagramReceived(self, datagram, address):
190  self.packetsReceived.append(unwrapPacket(datagram))
191  print "Number %i Datagram received from %s" % (len(self.packetsReceived), repr(address),)
192 
194 
195  def startProtocol(self):
196  self.transport.joinGroup("X.X.X.X")
197 
198 class UDPListener(object):
199  def __init__(self, port, vms=False):
200  self.port = port
201  # start listening
202  print "Listening to UDP broadcasts on port %s" % (self.port,)
204  self.listener = None
205  self.startListening()
206 
207  def startListening(self):
208  """!Create a twisted protocol that listens for UDP output at port
209  """
210  if self.listener is None:
211  self.listener = reactor.listenMulticast(self.port, self.broadcastClient, listenMultiple=True)
212 
213  def stopListening(self):
214  """!cleanly stop listening to avoid dirty reactors
215  """
216  if self.listener is not None:
217  self.listener.stopListening()
218  self.listener = None
219 
220  @property
221  def packetsReceived(self):
222  return self.broadcastClient.packetsReceived
def stopListening
Shut down the port, to close cleanly.
Definition: udpPacket.py:29
def __init__
Object which will broadcast UDP Packets containing current position and other data.
Definition: udpPacket.py:18
A variant of DatagramProtocol that enables broadcasting.
Definition: udpPacket.py:11
def startListening
Create a twisted protocol that listens for UDP output at port.
Definition: udpPacket.py:207
def unwrapPacket
Produce readable text from an encoded UDP packet broadcasted from the tcc return the datagram in a di...
Definition: udpPacket.py:171
def startListening
Open the port to allow writing data.
Definition: udpPacket.py:36
def sendPacket
Write binaryData.
Definition: udpPacket.py:42
def stopListening
cleanly stop listening to avoid dirty reactors
Definition: udpPacket.py:213