Skip to content

Commit 885785f

Browse files
fixed issue with H and V in SET command packets
1 parent 1cb433e commit 885785f

File tree

7 files changed

+126
-155
lines changed

7 files changed

+126
-155
lines changed

docs/rot2prog/rot2prog.html

Lines changed: 66 additions & 80 deletions
Large diffs are not rendered by default.

docs/rot2prog/utils/run.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ <h1 class="title">Module <code>rot2prog.utils.run</code></h1>
5050
port = input(&#39;Please enter the serial port: &#39;)
5151
try:
5252
return rot2prog.ROT2Prog(port)
53-
except:
54-
pass
53+
except Exception as e:
54+
print(e)
5555

5656
if __name__ == &#39;__main__&#39;:
5757
logging.basicConfig(level = logging.DEBUG)
@@ -118,8 +118,8 @@ <h2 class="section-title" id="header-functions">Functions</h2>
118118
port = input(&#39;Please enter the serial port: &#39;)
119119
try:
120120
return rot2prog.ROT2Prog(port)
121-
except:
122-
pass</code></pre>
121+
except Exception as e:
122+
print(e)</code></pre>
123123
</details>
124124
</dd>
125125
<dt id="rot2prog.utils.run.help"><code class="name flex">

docs/rot2prog/utils/sim.html

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,23 @@ <h1 class="title">Module <code>rot2prog.utils.sim</code></h1>
3333
import rot2prog
3434

3535
if __name__ == &#39;__main__&#39;:
36-
debug = &#39;&#39;
37-
resolution = 0
36+
pulses_per_degree = 0
3837

39-
while debug.lower() not in [&#39;y&#39;, &#39;n&#39;]:
40-
debug = input(&#39;Run debugger? (y/n) &#39;)
38+
# set log level
39+
logging.basicConfig(level = logging.DEBUG)
4140

42-
if debug.lower() == &#39;y&#39;:
43-
logging.basicConfig(level = logging.DEBUG)
44-
print(&#39;Running in debug mode&#39;)
45-
else:
46-
logging.basicConfig(level = logging.INFO)
41+
# get serial port
42+
port = input(&#39;Please enter the serial port: &#39;)
4743

48-
while resolution not in [1, 2, 4]:
49-
resolution = input(&#39;Pulses per degree: &#39;)
44+
# set pulses per degree
45+
while pulses_per_degree not in [1, 2, 4]:
46+
pulses_per_degree = input(&#39;Pulses per degree: &#39;)
5047
try:
51-
resolution = int(resolution)
48+
pulses_per_degree = int(pulses_per_degree)
5249
except ValueError:
53-
pass
50+
print(&#39;Please select 1, 2, or 4 pulses per degree.&#39;)
5451

55-
port = input(&#39;Please enter the serial port: &#39;)
56-
sim = rot2prog.ROT2ProgSim(port, resolution)
52+
sim = rot2prog.ROT2ProgSim(port, pulses_per_degree)
5753

5854
logging.getLogger().info(&#39;Press [Enter] to close simulator&#39;)
5955
input()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name = "rot2prog",
12-
version = "0.0.8",
12+
version = "0.0.9",
1313
author = "TJ Scherer",
1414
author_email = "tjtractorboy@gmail.com",
1515
description = "A python interface to the Alfa ROT2Prog Controller.",

src/rot2prog/rot2prog.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class ROT2Prog:
3131
_pulses_per_degree = 1
3232

3333
_limits_lock = Lock()
34-
_min_az = 0.0
35-
_max_az = 360.0
36-
_min_el = 0.0
37-
_max_el = 180.0
3834

3935
def __init__(self, port, timeout = 5):
4036
"""Creates object and opens serial connection.
@@ -60,15 +56,15 @@ def __init__(self, port, timeout = 5):
6056
# set the limits to default values
6157
self.set_limits()
6258

63-
def _send_command(self, cmd):
59+
def _send_command(self, command_packet):
6460
"""Sends a command packet.
6561
6662
Args:
67-
cmd (list of int): Command packet queued.
63+
command_packet (list of int): Command packet queued.
6864
"""
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+
7268
def _recv_response(self):
7369
"""Receives a response packet.
7470
@@ -83,14 +79,13 @@ def _recv_response(self):
8379
response_packet = list(self._ser.read(12))
8480

8581
# 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')
9188
else:
92-
self._log.debug('Response packet received: ' + str(list(response_packet)))
93-
9489
# convert from byte values
9590
az = (response_packet[1] * 100) + (response_packet[2] * 10) + response_packet[3] + (response_packet[4] / 10.0) - 360.0
9691
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):
156151

157152
with self._limits_lock:
158153
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) + '°]')
160155
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) + '°]')
162157

163158
self._log.debug('Set command queued')
164159
self._log.debug('-> Azimuth: ' + str(az) + '°')
@@ -178,9 +173,9 @@ def set(self, az, el):
178173
# build command
179174
cmd = [
180175
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,
182177
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,
184179
resolution,
185180
0x2f,
186181
0x20]
@@ -265,45 +260,43 @@ def _run(self):
265260
"""
266261
while self._keep_running:
267262
command_packet = list(self._ser.read(13))
263+
self._log.debug('Command packet received: ' + str(list(map(hex, list(command_packet)))))
268264
if len(command_packet) != 13:
269-
self._log.debug('Command packet received: ' + str(command_packet))
270265
self._log.critical('Incomplete command packet')
271266
else:
272-
self._log.debug('Command packet received: ' + str(command_packet))
273-
274267
K = command_packet[11]
275268

276269
if K in [0x0F, 0x1F]:
277270
if K == 0x0F:
278-
self._log.info('Stop command received')
271+
self._log.debug('Stop command received')
279272
elif K == 0x1F:
280-
self._log.info('Status command received')
273+
self._log.debug('Status command received')
281274

282275
# convert to byte values
283276
H = "00000" + str(round(float(self._az + 360), 1))
284277
V = "00000" + str(round(float(self._el + 360), 1))
285278

286-
rsp = [
279+
response_packet = [
287280
0x57,
288281
int(H[-5]), int(H[-4]), int(H[-3]), int(H[-1]),
289282
self._pulses_per_degree,
290283
int(V[-5]), int(V[-4]), int(V[-3]), int(V[-1]),
291284
self._pulses_per_degree,
292285
0x20]
293286

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))
299292

300-
self._ser.write(bytearray(rsp))
293+
self._ser.write(bytearray(response_packet))
301294

302-
self._log.debug('Response packet sent: ' + str(rsp))
295+
self._log.debug('Response packet sent: ' + str(list(map(hex, list(response_packet)))))
303296
elif K == 0x2F:
304297
# 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)
307300

308301
# decode with resolution
309302
self._az = H/self._pulses_per_degree - 360.0
@@ -312,9 +305,9 @@ def _run(self):
312305
self._az = float(round(self._az, 1))
313306
self._el = float(round(self._el, 1))
314307

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) + '°')
318311
else:
319312
self._log.error('Invalid command received (K = ' + str(hex(K)) + ')')
320313

src/rot2prog/utils/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def connect():
2121
port = input('Please enter the serial port: ')
2222
try:
2323
return rot2prog.ROT2Prog(port)
24-
except:
25-
pass
24+
except Exception as e:
25+
print(e)
2626

2727
if __name__ == '__main__':
2828
logging.basicConfig(level = logging.DEBUG)

src/rot2prog/utils/sim.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,23 @@
44
import rot2prog
55

66
if __name__ == '__main__':
7-
debug = ''
8-
resolution = 0
7+
pulses_per_degree = 0
98

10-
while debug.lower() not in ['y', 'n']:
11-
debug = input('Run debugger? (y/n) ')
9+
# set log level
10+
logging.basicConfig(level = logging.DEBUG)
1211

13-
if debug.lower() == 'y':
14-
logging.basicConfig(level = logging.DEBUG)
15-
print('Running in debug mode')
16-
else:
17-
logging.basicConfig(level = logging.INFO)
12+
# get serial port
13+
port = input('Please enter the serial port: ')
1814

19-
while resolution not in [1, 2, 4]:
20-
resolution = input('Pulses per degree: ')
15+
# set pulses per degree
16+
while pulses_per_degree not in [1, 2, 4]:
17+
pulses_per_degree = input('Pulses per degree: ')
2118
try:
22-
resolution = int(resolution)
19+
pulses_per_degree = int(pulses_per_degree)
2320
except ValueError:
24-
pass
21+
print('Please select 1, 2, or 4 pulses per degree.')
2522

26-
port = input('Please enter the serial port: ')
27-
sim = rot2prog.ROT2ProgSim(port, resolution)
23+
sim = rot2prog.ROT2ProgSim(port, pulses_per_degree)
2824

2925
logging.getLogger().info('Press [Enter] to close simulator')
3026
input()

0 commit comments

Comments
 (0)