forked from emlid/ReachView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RtkController.py
298 lines (199 loc) · 7.99 KB
/
RtkController.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# ReachView code is placed under the GPL license.
# Written by Egor Fedorov ([email protected])
# Copyright (c) 2015, Emlid Limited
# All rights reserved.
# If you are interested in using ReachView code as a part of a
# closed source project, please contact Emlid Limited ([email protected]).
# This file is part of ReachView.
# ReachView is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ReachView is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with ReachView. If not, see <http://www.gnu.org/licenses/>.
import time
import signal
import pexpect
from threading import Semaphore, Thread
# This module automates working with RTKRCV directly
# You can get sat levels, current status, start and restart the software
class RtkController:
def __init__(self, rtklib_path):
self.bin_path = rtklib_path + "/app/rtkrcv/gcc"
self.config_path = rtklib_path + "/app/rtkrcv"
self.child = 0
self.status = {}
self.obs_rover = {}
self.obs_base = {}
self.info = {}
self.semaphore = Semaphore()
self.started = False
self.launched = False
self.current_config = ""
def expectAnswer(self, last_command = ""):
a = self.child.expect(["rtkrcv>", pexpect.EOF, "error"])
# check rtklib output for any errors
if a == 1:
print("got EOF while waiting for rtkrcv> . Shutting down")
print("This means something went wrong and rtkrcv just stopped")
print("output before exception: " + str(self.child))
return -1
if a == 2:
print("Could not " + last_command + ". Please check path to binary or config name")
print("You may also check serial port for availability")
return -2
return 1
def launch(self, config_name = None):
# run an rtkrcv instance with the specified config:
# if there is a slash in the name we consider it a full location
# otherwise, it's supposed to be in the upper directory(rtkrcv inside app)
if config_name is None:
config_name = "reach_single_default.conf"
if not self.launched:
self.semaphore.acquire()
if "/" in config_name:
spawn_command = self.bin_path + "/rtkrcv -o " + config_name
else:
spawn_command = self.bin_path + "/rtkrcv -o " + self.config_path + "/" + config_name
self.child = pexpect.spawn(spawn_command, cwd = self.bin_path, echo = False)
print('Launching rtklib with: "' + spawn_command + '"')
if self.expectAnswer("spawn") < 0:
self.semaphore.release()
return -1
self.semaphore.release()
self.launched = True
self.current_config = config_name
# launch success
return 1
# already launched
return 2
def shutdown(self):
if self.launched:
self.semaphore.acquire()
self.child.kill(signal.SIGUSR2)
# wait for rtkrcv to shutdown
try:
self.child.wait()
except pexpect.ExceptionPexpect:
print("Already dead!!")
if self.child.isalive():
r = -1
else:
r = 1
self.semaphore.release()
self.launched = False
return r
# already shut down
return 2
def start(self):
if not self.started:
self.semaphore.acquire()
self.child.send("start\r\n")
if self.expectAnswer("start") < 0:
self.semaphore.release()
return -1
self.semaphore.release()
self.started = True
return 1
# already started
return 2
def stop(self):
if self.started:
self.semaphore.acquire()
self.child.send("stop\r\n")
if self.expectAnswer("stop") < 0:
self.semaphore.release()
return -1
self.semaphore.release()
self.started = False
return 1
# already stopped
return 2
def restart(self):
if self.started:
self.semaphore.acquire()
self.child.send("restart\r\n")
if self.expectAnswer("restart") < 0:
self.semaphore.release()
return -1
self.semaphore.release()
return 3
else:
# if we are not started yet, just start
return self.start()
def loadConfig(self, config_name = "rtk.conf"):
self.semaphore.acquire()
if "/" not in config_name:
# we assume this is not the full path
# so it must be in the upper dir
self.child.send("load " + "../" + config_name + "\r\n")
else:
self.child.send("load " + config_name + "\r\n")
if self.expectAnswer("load config") < 0:
self.semaphore.release()
return -1
self.semaphore.release()
self.current_config = config_name
return 1
def getStatus(self):
self.semaphore.acquire()
self.child.send("status\r\n")
if self.expectAnswer("get status") < 0:
self.semaphore.release()
return -1
status = self.child.before.split("\r\n")
if status != {}:
for line in status:
spl = line.split(":", 1)
if len(spl) > 1:
param = spl[0].strip()
value = spl[1].strip()
self.status[param] = value
self.semaphore.release()
return 1
def getObs(self):
self.semaphore.acquire()
self.obs_rover = {}
self.obs_base = {}
self.child.send("obs\r\n")
if self.expectAnswer("get obs") < 0:
self.semaphore.release()
return -1
obs = self.child.before.split("\r\n")
obs = filter(None, obs)
matching_strings = [s for s in obs if "SAT" in s]
if matching_strings != []:
# find the header of the OBS table
header_index = obs.index(matching_strings[0])
# split the header string into columns
header = obs[header_index].split()
if "S1" in header:
# find the indexes of the needed columns
sat_name_index = header.index("SAT")
sat_level_index = header.index("S1")
sat_input_source_index = header.index("R")
if len(obs) > (header_index + 1):
# we have some info about the actual satellites:
self.obs_rover = {}
self.obs_base = {}
for line in obs[header_index+1:]:
spl = line.split()
if len(spl) > sat_level_index:
name = spl[sat_name_index]
level = spl[sat_level_index]
# R parameter corresponds to the input source number
if spl[sat_input_source_index] == "1":
# we consider 1 to be rover,
self.obs_rover[name] = level
elif spl[sat_input_source_index] == "2":
# 2 to be base
self.obs_base[name] = level
else:
self.obs_base = {}
self.obs_rover = {}
self.semaphore.release()
return 1