forked from NevPalmer/ob_inst_survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranging_survey_raw_logging.py
173 lines (157 loc) · 4.91 KB
/
ranging_survey_raw_logging.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
"""
Log NMEA & Ranging data streams to a combined CSV text file.
"""
from argparse import ArgumentParser
import csv
from datetime import datetime
from pathlib import Path
from queue import Queue
import sys
from time import sleep
import ob_inst_survey as obsurv
OBSVN_COLS = (
"utcTime",
"rangeTime",
"range",
"lat",
"latDec",
"lon",
"lonDec",
"qlty",
"noSats",
"hdop",
"htAmsl",
"htAmslUnit",
"geiodSep",
"geiodSepUnit",
"cog",
"sogKt",
"heading",
"roll",
"pitch",
"heave",
"turnTime",
"sndSpd",
"tx",
"rx",
)
DISPLAY_COLS = (
"utcTime",
"rangeTime",
"range",
"lat",
"lon",
"cog",
"sogKt",
"heading",
)
STARTTIME = datetime.now()
TIMESTAMP_START = STARTTIME.strftime("%Y-%m-%d_%H-%M")
DFLT_PREFIX = "RANGELOG"
DFLT_PATH = Path.home() / "logs/"
ACCOU_TURNTIME = 12.5 # millisec
ACCOU_SPD = 1500 # m/sec
def main():
"""
Initialise NMEA and EdgeTech data streams and log to CSV text file.
"""
# Default CLI arguments.
ip_param = obsurv.IpParam()
etech_param = obsurv.EtechParam()
# Retrieve CLI arguments.
helpdesc: str = (
"Receives an NMEA data stream via UDP or TCP, and a serial data stream "
"from an EdgeTech deckbox. Alternatively these streams can be simulated "
"by replaying previously recorded text files (one containing NMEA data "
"and the other containing EdgeTech ranging responses).\n"
"For every range response received a record will be logged to a text "
"file, containing all relevant NMEA and Ranging fields."
)
parser = ArgumentParser(
parents=[
obsurv.out_filepath_parser(DFLT_PATH),
obsurv.out_fileprefix_parser(DFLT_PREFIX),
obsurv.ip_arg_parser(ip_param),
obsurv.edgetech_arg_parser(etech_param),
obsurv.replay2files_parser(None),
],
description=helpdesc,
)
parser.add_argument(
"--lograw",
help=(
"Option to additionally log raw NMEA and Range data to files in "
"subdirectories of the provided <outfile_path>."
),
action="store_true",
default=False,
)
args = parser.parse_args()
outfile_path: Path = args.outfilepath
outfile_log: str = outfile_path / f"{args.outfileprefix}_{TIMESTAMP_START}.csv"
rawfile_path = None
if args.lograw:
rawfile_path = outfile_path
ip_param = obsurv.IpParam(
port=args.ipport,
addr=args.ipaddr,
prot=args.ipprot,
buffer=args.ipbuffer,
)
etech_param = obsurv.EtechParam(
port=args.serport,
baud=args.serbaud,
stop=args.serstop,
parity=args.serparity,
bytesize=args.serbytesize,
turn_time=args.acouturn,
snd_spd=args.acouspd,
)
replay_nmeafile: Path = args.replaynmea
replay_rngfile: Path = args.replayrange
replay_start: datetime = args.replaystart
replay_speed: float = args.replayspeed
timestamp_offset: float = args.timestampoffset
# Create directories for logging (included raw NMEA and Ranging streams).
outfile_path.mkdir(parents=True, exist_ok=True)
print(f"Logging survey observations to {outfile_log}")
# Initiate NMEA and Ranging data streams to the observation queue.
obsvn_q: Queue[dict] = Queue()
obsurv.ranging_survey_stream(
obsvn_q=obsvn_q,
nmea_conn=ip_param,
etech_conn=etech_param,
nmea_filename=replay_nmeafile,
etech_filename=replay_rngfile,
replay_start=replay_start,
spd_fctr=replay_speed,
timestamp_offset=timestamp_offset,
rawfile_path=rawfile_path,
rawfile_prefix=args.outfileprefix,
)
print(",".join(DISPLAY_COLS))
with open(outfile_log, "a+", newline="", encoding="utf-8") as csvfile:
logwriter = csv.DictWriter(csvfile, delimiter=",", fieldnames=OBSVN_COLS)
logwriter.writeheader()
try:
while True:
if obsvn_q.empty():
sleep(0.001) # Prevents idle loop from 100% CPU thread usage.
continue
result_dict = obsvn_q.get()
if result_dict["flag"] in ["TimeoutError", "EOF"]:
sys.exit(f"*** Survey Ended: {result_dict['flag']} ***")
# Display summary values to screen
display_vals = [result_dict[key] for key in DISPLAY_COLS]
print(str(display_vals).strip("[]"))
# Save values to log file
save_dict = {key: result_dict[key] for key in OBSVN_COLS}
with open(outfile_log, "a+", newline="", encoding="utf-8") as csvfile:
logwriter = csv.DictWriter(
csvfile, delimiter=",", fieldnames=OBSVN_COLS
)
logwriter.writerow(save_dict)
except KeyboardInterrupt:
sys.exit("*** End Ranging Survey ***")
if __name__ == "__main__":
main()