-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_ground_station.py
189 lines (139 loc) · 5.53 KB
/
run_ground_station.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
from utils.file_io import read_netcdf
from utils.comms import Comms
from utils.visuals import plot_navigation
from threading import Thread
import yaml
import sys
import time
import numpy as np
import warnings
import logging
# Initialize global variables
temp = None
path = None
pos = None
dest = None
latlons = None
currents = None
# Create logger
logger = logging.getLogger('gs_logger')
logger.setLevel(logging.INFO)
handler = logging.FileHandler('./logs/gs.log')
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
logger.setLevel(logging.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)
def main(config):
global dest, latlons, currents
# Get initial destination
dest = config['destination']
# Read the initial currents from file
latlons, currents = read_netcdf(config['init_map'])
# Initialize communications
ground_station = config['gs_addr']
port_list = config['ports']
# Send new maps and manual instructions
Thread(target=controls, args=(ground_station, port_list,)).start()
# Receive telemetry, path, and GPS
Thread(target=telem_list, args=(ground_station, port_list,)).start()
Thread(target=path_list, args=(ground_station, port_list,)).start()
Thread(target=gps_list, args=(ground_station, port_list,)).start()
# Plotting
Thread(target=visualize).start()
def controls(ground_station, port_list):
global latlons, currents, logger
print("Waiting for connection...")
az_comms = Comms(ground_station, port_list['manual_az'], listener=True)
logger.info("Azimuth transmitter connected.")
prop_comms = Comms(ground_station, port_list['manual_prop'], listener=True)
logger.info("Propulsion transmitter connected.")
map_comms = Comms(ground_station, port_list['maps'], listener=True)
logger.info("Current map transmitter connected.")
# Wait for all of our comms to connect to boat before entering command loop
while az_comms.conn is None and prop_comms.conn is None and map_comms.conn is None:
pass
print("Connected! ")
# Initialize
quit = False
while not quit:
# Prompt for additional instructions
choice = input("Please input instruction type (AZ, PROP, MAP, QUIT): ")
# Receive azimuth input and send
if choice == "AZ":
new_az = input("New azimuth: (degrees)")
logger.info(f"Manual azimuth inputted. Azimuth: {new_az}.")
duration = input("Duration (seconds): ")
logger.info(f"Manual azimuth inputted for Duration: {duration}.")
packet = np.stack((new_az, duration))
az_comms.send(packet)
logger.info(f"Manual azimuth input sent. Azimuth: {new_az} for Duration: {duration}.")
# Receive propulsion input and send
elif choice == "PROP":
new_prop = input("New propulsion setting (m/s): ")
logger.info(f"Manual propulsion inputted. Propulsion: {new_prop}.")
duration = input("Duration (seconds): ")
logger.info(f"Manual propulsion inputted for Duration: {duration}.")
packet = np.stack((new_prop, duration))
prop_comms.send(packet)
logger.infoa(f"Manual propulsion input sent. Propulsion: {new_prop} for Duration {duration}.")
# Receive new map file input and send
elif choice == "MAP":
new_map = input("File path to new currents map (.netcdf file): ")
latlons, currents = read_netcdf(new_map)
map_stack = np.stack((latlons, currents))
logger.info("Current map updated.")
map_comms.send(map_stack)
logger.info("Updated current map sent")
# Quit controls
elif choice == "QUIT":
quit = True
else:
print("Invalid Choice")
print("\n")
def telem_list(ground_station, port_list):
global temp, logger
# Establish connection
telem_listener = Comms(ground_station, port_list['telem'], listener=True)
while telem_listener.conn is None:
pass
# Constantly grab temperatures and assign to global variable
while True:
temp = telem_listener.recv()
logger.info(f"New telemetry received. Temperature: {temp:.4f}.")
def path_list(ground_station, port_list):
global path, logger
# Establish connection
path_listener = Comms(ground_station, port_list['nav_path'], listener=True)
while path_listener.conn is None:
pass
# Grab path and assign to a global variable
while True:
path = path_listener.recv()
logger.info(f"New path received. Path has length {len(path)}.")
def gps_list(ground_station, port_list):
global pos, logger
# Establish connection
gps_listener = Comms(ground_station, port_list['gps'], listener=True)
while gps_listener.conn is None:
pass
# Grab GPS and assign to a global variable
while True:
pos = gps_listener.recv()
logger.info(f"New GPS received. {pos[0]:.4f} N {pos[1]:.4f} E.")
def visualize():
global pos, path, temp, latlons, currents
idx = 0
while True:
# Wait until we have recieved telemetry and path data from the boat
if path is None or pos is None:
continue
# Plot most recent navigation
plot_navigation(pos, dest, path, latlons, currents, temp, plot_idx=idx)
idx += 1
time.sleep(10)
if __name__ == '__main__':
warnings.filterwarnings("ignore")
# Load configuration YAML
with open(sys.argv[1]) as f:
config = yaml.load(f)
main(config)