-
Notifications
You must be signed in to change notification settings - Fork 2
/
rotor.py
135 lines (113 loc) · 4.11 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
import sys
import os
import Hamlib
import time
import subprocess
from pathlib import Path
from configparser import ConfigParser
# default vars
MINSTEP = 5.0
configfile = 'rotor.conf'
class Rotor():
def __init__(self):
# load config and start the sub-process control if needed
self.loadconfig()
self.start_rotor()
# limits
self.MIN_AZ = 0
self.MIN_EL = 0
self.MAX_AZ = 360
self.MAX_EL = 90
def loadconfig(self):
# find the correct place for the config file
local = True
cwd = os.getcwd()
defcf = os.path.join(cwd, configfile)
# load config from the rotor.conf file
config = ConfigParser()
if os.path.exists(defcf) and os.path.isfile(defcf):
print("rotor.conf at: {}".format(defcf))
config.read(defcf)
else:
if local:
print("Can't find the '{}' in the local directory".format(configfile))
print("Please copy & edit the rotor.conf file from the project page to this directory.")
sys.exit()
else:
import shutils
shutil.copyfile(os.path.join(sys._MEIPASS, configfile), defcf)
print("No rotor config file, a new one has been created, please edit it with your particular rotor configs and try it again.")
print(defcf)
sys.exit()
self.default = config.get('DEFAULT', 'rotor')
self.model = config.get(self.default, 'model')
self.device = config.get(self.default, 'device')
self.options = config.get(self.default, 'options')
def start_rotor(self):
# Disable all debug output from Hamlib
Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE)
# Create rotor object of type net
# self.rot = Hamlib.Rot(Hamlib.ROT_MODEL_NETROTCTL)
self.rot = Hamlib.Rot(int(self.model))
# Setup device
self.rot.set_conf("rot_pathname", self.device)
# setup extra options
opts = self.options.strip()
if len(opts) > 0:
if not (len(opt) % 2):
print("Strange, options must be in pairs, trying but may fail...")
pairs = []
cmds = []
s = opts.split(' ')
for i in range(len(s)/2):
cmd = s[i*2]
data = s[i*2 + 1]
if cmd == '-s':
cmds.append(['serial-speed', data])
if cmd == '-C':
cmds.append([data.split('=')[0],
data.split('=')[1]])
for (opt, value) in cmds:
self.rot.set_conf(opt, value)
# Open rotor
# The Python bindings for Hamlib does not return anything
# so we have no knowledge if this was actually successful...
self.rot.open()
# get the limits from the rotor
self.MIN_AZ = float(self.rot.get_conf("min_az"))
self.MIN_EL = float(self.rot.get_conf("min_el"))
self.MAX_AZ = float(self.rot.get_conf("max_az"))
self.MAX_EL = float(self.rot.get_conf("max_el"))
def set_position(self, az, elv):
# check limits
az = min(self.MAX_AZ, max(self.MIN_AZ, az))
elv = min(self.MAX_EL, max(self.MIN_EL, elv))
# ask postion
[aaz, ael] = self.get_position()
diff = az - aaz
if abs(diff) <= MINSTEP:
self.rot.set_position(aaz + (2 * diff), ael)
time.sleep(0.25)
self.rot.set_position(az, elv)
while (abs(aaz - az) > 1):
time.sleep(1)
[aaz, ael] = self.get_position()
def get_position(self):
pos = self.rot.get_position()
return pos
def go_to(self, az, elv=0):
self.rot.set_position(az, elv)
def close(self):
# close all connections
if self.rot:
self.rot.close()
if __name__ == "__main__":
r = Rotor()
a = 0
for az in range(0, 90, 10):
r.set_position(az, 0)
(a, e) = r.get_position()
print("Azimuth, actual: {}, set: {}".format(a, az))
# park
r.go_to(0, 0)
r.close()