@@ -31,10 +31,6 @@ class ROT2Prog:
31
31
_pulses_per_degree = 1
32
32
33
33
_limits_lock = Lock ()
34
- _min_az = 0.0
35
- _max_az = 360.0
36
- _min_el = 0.0
37
- _max_el = 180.0
38
34
39
35
def __init__ (self , port , timeout = 5 ):
40
36
"""Creates object and opens serial connection.
@@ -60,15 +56,15 @@ def __init__(self, port, timeout = 5):
60
56
# set the limits to default values
61
57
self .set_limits ()
62
58
63
- def _send_command (self , cmd ):
59
+ def _send_command (self , command_packet ):
64
60
"""Sends a command packet.
65
61
66
62
Args:
67
- cmd (list of int): Command packet queued.
63
+ command_packet (list of int): Command packet queued.
68
64
"""
69
- self ._ser .write (bytearray (cmd ))
70
- self ._log .debug ('Command packet sent: ' + str (cmd ))
71
-
65
+ self ._ser .write (bytearray (command_packet ))
66
+ self ._log .debug ('Command packet sent: ' + str (list ( map ( hex , list ( command_packet ))) ))
67
+
72
68
def _recv_response (self ):
73
69
"""Receives a response packet.
74
70
@@ -83,14 +79,13 @@ def _recv_response(self):
83
79
response_packet = list (self ._ser .read (12 ))
84
80
85
81
# attempt to receive 12 bytes, the length of response packet
86
- if len (response_packet ) == 0 :
87
- raise ReadTimeout ('response timed out' )
88
- elif len (response_packet ) != 12 :
89
- self ._log .debug ('Response packet received: ' + str (list (response_packet )))
90
- raise PacketError ('incomplete response packet' )
82
+ self ._log .debug ('Response packet received: ' + str (list (map (hex , list (response_packet )))))
83
+ if len (response_packet ) != 12 :
84
+ if len (response_packet ) == 0 :
85
+ raise ReadTimeout ('Response timed out' )
86
+ else :
87
+ raise PacketError ('Incomplete response packet' )
91
88
else :
92
- self ._log .debug ('Response packet received: ' + str (list (response_packet )))
93
-
94
89
# convert from byte values
95
90
az = (response_packet [1 ] * 100 ) + (response_packet [2 ] * 10 ) + response_packet [3 ] + (response_packet [4 ] / 10.0 ) - 360.0
96
91
el = (response_packet [6 ] * 100 ) + (response_packet [7 ] * 10 ) + response_packet [8 ] + (response_packet [9 ] / 10.0 ) - 360.0
@@ -156,9 +151,9 @@ def set(self, az, el):
156
151
157
152
with self ._limits_lock :
158
153
if az > self ._max_az or az < self ._min_az :
159
- raise ValueError ('Azimuth of ' + str (az ) + '° is out of range: [' + str (self ._min_az ) + '°, ' + str (self ._max_az ) + '°]' )
154
+ raise ValueError ('Azimuth of ' + str (az ) + '° is out of range [' + str (self ._min_az ) + '°, ' + str (self ._max_az ) + '°]' )
160
155
if el > self ._max_el or el < self ._min_el :
161
- raise ValueError ('Elevation of ' + str (el ) + '° is out of range: [' + str (self ._min_el ) + '°, ' + str (self ._max_el ) + '°]' )
156
+ raise ValueError ('Elevation of ' + str (el ) + '° is out of range [' + str (self ._min_el ) + '°, ' + str (self ._max_el ) + '°]' )
162
157
163
158
self ._log .debug ('Set command queued' )
164
159
self ._log .debug ('-> Azimuth: ' + str (az ) + '°' )
@@ -178,9 +173,9 @@ def set(self, az, el):
178
173
# build command
179
174
cmd = [
180
175
0x57 ,
181
- int (H [- 4 ]), int (H [- 3 ]), int (H [- 2 ]), int (H [- 1 ]),
176
+ int (H [- 4 ]) + 0x30 , int (H [- 3 ]) + 0x30 , int (H [- 2 ]) + 0x30 , int (H [- 1 ]) + 0x30 ,
182
177
resolution ,
183
- int (V [- 4 ]), int (V [- 3 ]), int (V [- 2 ]), int (V [- 1 ]),
178
+ int (V [- 4 ]) + 0x30 , int (V [- 3 ]) + 0x30 , int (V [- 2 ]) + 0x30 , int (V [- 1 ]) + 0x30 ,
184
179
resolution ,
185
180
0x2f ,
186
181
0x20 ]
@@ -265,45 +260,43 @@ def _run(self):
265
260
"""
266
261
while self ._keep_running :
267
262
command_packet = list (self ._ser .read (13 ))
263
+ self ._log .debug ('Command packet received: ' + str (list (map (hex , list (command_packet )))))
268
264
if len (command_packet ) != 13 :
269
- self ._log .debug ('Command packet received: ' + str (command_packet ))
270
265
self ._log .critical ('Incomplete command packet' )
271
266
else :
272
- self ._log .debug ('Command packet received: ' + str (command_packet ))
273
-
274
267
K = command_packet [11 ]
275
268
276
269
if K in [0x0F , 0x1F ]:
277
270
if K == 0x0F :
278
- self ._log .info ('Stop command received' )
271
+ self ._log .debug ('Stop command received' )
279
272
elif K == 0x1F :
280
- self ._log .info ('Status command received' )
273
+ self ._log .debug ('Status command received' )
281
274
282
275
# convert to byte values
283
276
H = "00000" + str (round (float (self ._az + 360 ), 1 ))
284
277
V = "00000" + str (round (float (self ._el + 360 ), 1 ))
285
278
286
- rsp = [
279
+ response_packet = [
287
280
0x57 ,
288
281
int (H [- 5 ]), int (H [- 4 ]), int (H [- 3 ]), int (H [- 1 ]),
289
282
self ._pulses_per_degree ,
290
283
int (V [- 5 ]), int (V [- 4 ]), int (V [- 3 ]), int (V [- 1 ]),
291
284
self ._pulses_per_degree ,
292
285
0x20 ]
293
286
294
- self ._log .info ('Response queued' )
295
- self ._log .info ('-> Azimuth: ' + str (self ._az ) + '°' )
296
- self ._log .info ('-> Elevation: ' + str (self ._el ) + '°' )
297
- self ._log .info ('-> PH: ' + str (self ._pulses_per_degree ))
298
- self ._log .info ('-> PV: ' + str (self ._pulses_per_degree ))
287
+ self ._log .debug ('Response queued' )
288
+ self ._log .debug ('-> Azimuth: ' + str (self ._az ) + '°' )
289
+ self ._log .debug ('-> Elevation: ' + str (self ._el ) + '°' )
290
+ self ._log .debug ('-> PH: ' + hex (self ._pulses_per_degree ))
291
+ self ._log .debug ('-> PV: ' + hex (self ._pulses_per_degree ))
299
292
300
- self ._ser .write (bytearray (rsp ))
293
+ self ._ser .write (bytearray (response_packet ))
301
294
302
- self ._log .debug ('Response packet sent: ' + str (rsp ))
295
+ self ._log .debug ('Response packet sent: ' + str (list ( map ( hex , list ( response_packet ))) ))
303
296
elif K == 0x2F :
304
297
# convert from ascii characters
305
- H = (command_packet [1 ] * 1000 ) + (command_packet [2 ] * 100 ) + (command_packet [3 ] * 10 ) + command_packet [4 ]
306
- V = (command_packet [6 ] * 1000 ) + (command_packet [7 ] * 100 ) + (command_packet [8 ] * 10 ) + command_packet [9 ]
298
+ H = (( command_packet [1 ] - 0x30 ) * 1000 ) + (( command_packet [2 ] - 0x30 ) * 100 ) + (( command_packet [3 ] - 0x30 ) * 10 ) + ( command_packet [4 ] - 0x30 )
299
+ V = (( command_packet [6 ] - 0x30 ) * 1000 ) + (( command_packet [7 ] - 0x30 ) * 100 ) + (( command_packet [8 ] - 0x30 ) * 10 ) + ( command_packet [9 ] - 0x30 )
307
300
308
301
# decode with resolution
309
302
self ._az = H / self ._pulses_per_degree - 360.0
@@ -312,9 +305,9 @@ def _run(self):
312
305
self ._az = float (round (self ._az , 1 ))
313
306
self ._el = float (round (self ._el , 1 ))
314
307
315
- self ._log .info ('Set command received' )
316
- self ._log .info ('-> Azimuth: ' + str (self ._az ) + '°' )
317
- self ._log .info ('-> Elevation: ' + str (self ._el ) + '°' )
308
+ self ._log .debug ('Set command received' )
309
+ self ._log .debug ('-> Azimuth: ' + str (self ._az ) + '°' )
310
+ self ._log .debug ('-> Elevation: ' + str (self ._el ) + '°' )
318
311
else :
319
312
self ._log .error ('Invalid command received (K = ' + str (hex (K )) + ')' )
320
313
0 commit comments