forked from emlid/ReachView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Str2StrController.py
299 lines (216 loc) · 9.92 KB
/
Str2StrController.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
293
294
295
296
297
# 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 os
import signal
import pexpect
from glob import glob
from reach_tools import reach_tools
# This module automates working with STR2STR software
class Str2StrController:
def __init__(self, rtklib_path):
self.bin_path = rtklib_path + "/app/str2str/gcc"
self.gps_cmd_file_path = rtklib_path + "/app/rtkrcv"
self.gps_cmd_file = "GPS_10Hz.cmd"
self.child = 0
self.started = False
# port settings are kept as class properties:
self.input_stream = ""
self.output_stream = ""
# Reach defaults for base position and rtcm3 messages:
self.rtcm3_messages = ["1002", "1006", "1008", "1010", "1019", "1020"]
self.base_position = [] # lat, lon, height
self.setSerialStream() # input ublox serial
self.setTCPServerStream(input = False) # output tcp server on port 9000
def getAvailableReceiverCommandFiles(self):
# returns a list of available cmd files in the working rtkrcv directory
available_cmd_files = glob(self.gps_cmd_file_path + "/" +"*.cmd")
available_cmd_files = [os.path.basename(cmd_file) for cmd_file in available_cmd_files]
return available_cmd_files
def formCommentString(self, options_list):
comment = "("
for index, option in enumerate(options_list):
comment += str(index) + ":" + str(option)
if index < len(options_list) - 1:
comment += ","
comment += ")"
return comment
def readConfig(self):
parameters_to_send = {}
parameters_to_send["0"] = {
"parameter": "outstr-path",
"value": self.output_stream,
"comment": self.formCommentString(reach_tools.getAvailableSerialPorts()),
"description": "Output path for corrections"
}
parameters_to_send["1"] = {"parameter": "rtcm3_out_messages", "value": ",".join(self.rtcm3_messages), "description": "RTCM3 messages for output"}
# if we don't have a set base position we want to send empty strings
if not self.base_position:
base_pos = ["", "", ""]
else:
base_pos = self.base_position
parameters_to_send["2"] = {"parameter": "base_pos_lat", "value": base_pos[0], "description": "Base latitude"}
parameters_to_send["3"] = {"parameter": "base_pos_lon", "value": base_pos[1], "description": "Base longitude"}
parameters_to_send["4"] = {"parameter": "base_pos_height", "value": base_pos[2], "description": "Base height"}
parameters_to_send["5"] = {
"parameter": "gps_cmd_file",
"value": self.gps_cmd_file,
"description": "Receiver configuration file",
"comment": self.formCommentString(self.getAvailableReceiverCommandFiles())
}
print("DEBUG read")
print(parameters_to_send)
return parameters_to_send
def writeConfig(self, parameters_received):
print("DEBUG write")
print(parameters_received)
coordinate_filled_flag = 3
base_pos = []
self.output_stream = parameters_received["0"]["value"]
# llh
self.base_position = []
self.base_position.append(parameters_received["2"]["value"])
self.base_position.append(parameters_received["3"]["value"])
self.base_position.append(parameters_received["4"]["value"])
self.rtcm3_messages = parameters_received["1"]["value"].split(",")
self.gps_cmd_file = parameters_received["5"]["value"]
def setPort(self, port, input = True, format = "ubx"):
if input:
self.input_stream = port + "#" + format
else:
# str2str only supports rtcm3 for output
self.output_stream = port + "#" + "rtcm3"
def setSerialStream(self, serial_parameters = None, input = True, format = "ubx"):
# easier way to specify serial port for str2str
# serial_parameters is a list of options for our serial device:
# 1. serial port
# 2. baudrate
# 3. byte size
# 4. parity bit
# 5. stop bit
# 6. fctr
# default parameters here are Reach standards
def_parameters = [
"ttyMFD1",
"230400",
"8",
"n",
"1",
"off"
]
if serial_parameters is None:
serial_parameters = def_parameters
port = "serial://" + ":".join(serial_parameters)
self.setPort(port, input, format)
def setTCPClientStream(self, tcp_client_parameters = None, input = True, format = "ubx"):
# easier way to specify tcp connection parameters for str2str
# tcp client parameters include:
# 1. ip address
# 2. port number
def_parameters = [
"localhost",
"9000"
]
if tcp_client_parameters is None:
tcp_client_parameters = def_parameters
port = "tcpcli://" + ":".join(tcp_server_parameters)
self.setPort(port, input, format)
def setTCPServerStream(self, tcp_server_parameters = None, input = True, format = "ubx"):
# tcp server parameters only include the port number:
# 1. port number
def_parameters = [
"9000"
]
if tcp_server_parameters is None:
tcp_server_parameters = def_parameters
port = "tcpsvr://:" + def_parameters[0]
self.setPort(port, input, format)
def setNTRIPClientStream(self, ntrip_client_parameters = None, input = True, format = "ubx"):
# ntrip client parameters:
# 1. user
# 2. password
# 3. address
# 4. port
# 5. mount point
port = "ntrip://" + ntrip_client_parameters[0] + ":"
port += ntrip_client_parameters[1] + "@" + ntrip_client_parameters[2] + ":"
port += ntrip_client_parameters[3] + "/" + ntrip_client_parameters[4]
self.setPort(port, input, format)
def setNTRIPServerStream(self, ntrip_server_parameters = None, input = True, format = "ubx"):
# ntrip client parameters:
# 1. password
# 2. address
# 3. port
# 4. mount point
# 5. str ???
port = "ntrips://:" + ntrip_client_parameters[0] + "@" + ntrip_client_parameters[1]
port += ":" + ntrip_client_parameters[2] + "/" + ntrip_client_parameters[3] + ":"
port += ntrip_client_parameters[4]
self.setPort(port, input, format)
def start(self, rtcm3_messages = None, base_position = None, gps_cmd_file = None):
# when we start str2str we also have 3 important optional parameters
# 1. rtcm3 message types. We have standard 1002, 1006, 1013, 1019 by default
# 2. base position in llh. By default we don't pass any values, however it is best to use this feature
# 3. gps cmd file will take care of msg frequency and msg types
# To pass parameters to this function use string lists, like ["1002", "1006"] or ["60", "30", "100"]
print(self.bin_path)
if not self.started:
if rtcm3_messages is None:
rtcm3_messages = self.rtcm3_messages
if base_position is None:
base_position = self.base_position
if gps_cmd_file is None:
gps_cmd_file = self.gps_cmd_file
cmd = "/str2str -in " + self.input_stream + " -out " + self.output_stream + " -msg " + ",".join(rtcm3_messages)
if "" in base_position:
base_position = []
if base_position:
cmd += " -p " + " ".join(base_position)
if gps_cmd_file:
cmd += " -c " + self.gps_cmd_file_path + "/" + gps_cmd_file
cmd = self.bin_path + cmd
print("Starting str2str with")
print(cmd)
self.child = pexpect.spawn(cmd, cwd = self.bin_path, echo = False)
a = self.child.expect(["stream server start", pexpect.EOF, "error"])
# check if we encountered any errors launching str2str
if a == 1:
print("got EOF while waiting for stream start. Shutting down")
print("This means something went wrong and str2str just stopped")
print("output before exception: " + str(self.child))
return -1
if a == 2:
print("Could not start str2str. Please check path to binary or parameters, like serial port")
print("You may also check serial, tcp, ntrip ports for availability")
return -2
# if we are here, everything is good
self.started = True
return 1
# str2str already started
return 2
def stop(self):
# terminate the stream
if self.started:
self.child.kill(signal.SIGUSR2)
try:
self.child.wait()
except pexpect.ExceptionPexpect:
print("Str2str already down")
self.started = False
return 1
# str2str already stopped
return 2