-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotor.py
164 lines (133 loc) · 4.6 KB
/
rotor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import serial
import time
class RotorDegreeOutOfRange(Exception):
pass
class Rotor:
serial_port = ""
_rotor_port = None
MaxRange_azi = 75 # (+/- value) depends on your SAT-rotor type, check data sheet
MaxRange_ele = 75
_current_azi = 0
_current_ele = 0
def __init__(self, serial_port):
""" Rotor control.
Connects to an arduino that runs Easy DiSEqC: https://www.e-callisto.org/hardware/callisto-hardware.html
via serial port.
Checks boundaries of azimuth and elevation and keeps track of current position of the rotor.
Args:
serial_port (str): arduino serial port
"""
print(f"opening Serial Port {serial_port}")
try:
self._rotor_port = serial.Serial(
port = serial_port,
baudrate = 9600,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
timeout = 2)
except IOError:
print ("Problem communication with tracker. Check serial port and cables/connectors!")
exit(0)
#endtry
time.sleep(2) # wait for arduino to be initialized
self._send_command("-h") # Send command that does nothing to arduino, because it behaves weird if we start changing settings right away
self._send_command(f"max{self.MaxRange_azi}") # TODO: what if max range ele is smaller / bigger than azi ?
#enddef
def goto_azimuth(self, azi):
""" turn rotor to a specific azimuth angle
Args:
azi (float): destination azimuth
Raises:
RotorDegreeOutOfRange: rotor can not reach desired azimuth
"""
if self._can_move_azimuth(azi):
cmd = "azi{:6.2f}".format(azi)
self._send_command(cmd)
self._current_azi = azi
else:
print("WARN: azimuth out of range")
raise RotorDegreeOutOfRange
#enddef
#enddef
def step_azimuth(self, step=1):
""" setp azimuth by specific amount of degrees
Args:
step (float, optional): degrees plus or minus to step the rotor by. Defaults to 1.
"""
new_azi = self._current_azi+step
self.goto_azimuth(new_azi)
#enddef
def get_azimuth(self):
return self._current_azi
#enddef
def _can_move_azimuth(self, new_azi):
"""checks if the new azimuth is accessible
Args:
new_azi (float): destination azimuth
Returns:
bool: True if azimuth can be accessed, False if not
"""
if abs(new_azi) < self.MaxRange_azi:
return True
else:
return False
#endif
#enddef
def goto_elevation(self, ele):
""" turn rotor to a specific elevation angle
Args:
azi (float): destination elevation
Raises:
RotorDegreeOutOfRange: rotor can not reach desired elevation
"""
if self._can_move_elevation(ele):
cmd = "ele{:6.2f}".format(ele)
self._send_command(cmd)
self._current_ele=ele
else:
print("WARN: elevation out of range")
raise RotorDegreeOutOfRange
#enddef
def step_elevation(self, step=1):
""" setp elevation by specific amount of degrees
Args:
step (float, optional): degrees plus or minus to step the rotor by. Defaults to 1.
"""
new_ele = self._current_ele+step
self.goto_elevation(new_ele)
#enddef
def get_elevation(self):
return self._current_ele
#enddef
def _can_move_elevation(self, new_ele):
"""checks if the new elevation is accessible
Args:
new_azi (float): destination elevation
Returns:
bool: True if elevation can be accessed, False if not
"""
if abs(new_azi) < self.MaxRange_ele:
return True
else:
return False
#endif
#enddef
def _send_command(self, cmd):
"""send command to the arduino via serial connection.
Adds newline, so cmd should just be a command string without newlines
Args:
cmd (str): command string to send
"""
cmd = f"{cmd}\n"
#print(f"{cmd}")
try:
self._rotor_port.write(cmd.encode())
self._rotor_port.flush()
except IOError:
print ("Problem communication with tracker. Check COM-port and cables/connectors!")
#endtry
#enddef
def __del__(self):
if self._rotor_port:
self._rotor_port.close()
#enddef