Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
crnicholson committed Nov 25, 2024
1 parent 678c1a8 commit be2370f
Showing 1 changed file with 197 additions and 98 deletions.
295 changes: 197 additions & 98 deletions Code/groundStation/uploader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pandas as pd
import os
import datetime
import threading

PORT = 8080 # Match this with the port in your ESP32 code.
COMMENT = "StratoSoar MK3 high altitude glider."
Expand Down Expand Up @@ -46,7 +47,7 @@ async def handle_connection(websocket, path):
hour = received["hour"]
minute = received["minute"]
second = received["second"]
receivedAbort = received["abort"]
received_abort = received["abort"]
tx_count = received["txCount"]
rx_count = received["rxCount"]
u_lat = received["u_lat"]
Expand All @@ -68,114 +69,143 @@ async def handle_connection(websocket, path):
)
utc_time_str = utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")

df = pd.DataFrame()

if os.path.exists(f"{cwd}/data/{call_sign}.csv"):
df = pd.read_csv(f"{cwd}/data/{call_sign}.csv")
if df.loc[-1, "Time"] == utc_time_str:
print("Duplicate data received. Ignoring.")

data = {
if not df.empty and df.loc[-1, "Time"] == utc_time_str:
print("Duplicate data received. Ignoring.")
else:
uploader = Uploader(
call_sign,
uploader_position=[u_lat, u_lon, u_alt],
uploader_radio=RADIO,
uploader_antenna=ANTENNA,
)

uploader.add_telemetry(
call_sign,
utc_time_str,
lat,
lon,
alt,
frame=tx_count,
batt=volts,
temp=temp,
pressure=pressure,
snr=snr,
frequency=FREQ,
rssi=rssi,
modulation="LoRa",
extra_fields={
"ID": id,
"Time": utc_time_str,
"Call sign": call_sign,
"SNR": snr,
"RSSI": rssi,
}

df = pd.DataFrame([data])

file = f"{cwd}/stations.csv"
df.to_csv(
file,
mode="a",
header=not pd.io.common.file_exists(file),
index=False,
)

if datetime.datetime.second() - last_print > 60:
print("Stations receiving the in the last 10 minutes.")
df = pd.read_csv(f"{cwd}/stations.csv")
for
else:
uploader = Uploader(
call_sign,
uploader_position=[u_lat, u_lon, u_alt],
uploader_radio=RADIO,
uploader_antenna=ANTENNA,
)

uploader.add_telemetry(
call_sign,
utc_time_str,
lat,
lon,
alt,
frame=tx_count,
batt=volts,
temp=temp,
pressure=pressure,
snr=snr,
frequency=FREQ,
rssi=rssi,
modulation="LoRa",
extra_fields={
"ID": id,
"Comment": COMMENT,
"Target lat": t_lat,
"Target lon": t_lon,
"Yaw": yaw,
"Pitch": pitch,
"Roll": roll,
"Humidity": humidity,
"RX count": rx_count,
"Abort": receivedAbort,
},
)

data = {
"Lat": lat,
"Lon": lon,
"Alt": alt,
"Comment": COMMENT,
"Target lat": t_lat,
"Target lon": t_lon,
"Yaw": yaw,
"Pitch": pitch,
"Roll": roll,
"Time": utc_time_str,
"Temperature": temp,
"Pressure": pressure,
"Humidity": humidity,
"Voltage": volts,
"Abort": receivedAbort,
"TX count": tx_count,
"RX count": rx_count,
"RSSI": rssi,
"SNR": snr,
"Uploader lat": u_lat,
"Uploader lon": u_lon,
"Uploader alt": u_alt,
"ID": id,
}

df = pd.DataFrame([data])

file = f"{cwd}/data/{call_sign}.csv"
df.to_csv(
file,
mode="a",
header=not pd.io.common.file_exists(file),
index=False,
)

# # Example: Create a response JSON
# response = {
# "message": "Data received successfully",
# "chipId": chip_id,
# "status": status,
# }

# # Send response back to the client
# await websocket.send(json.dumps(response))
# print("Response sent.")
"Abort": received_abort,
},
)

data = {
"Lat": lat,
"Lon": lon,
"Alt": alt,
"Target lat": t_lat,
"Target lon": t_lon,
"Yaw": yaw,
"Pitch": pitch,
"Roll": roll,
"Time": utc_time_str,
"Temperature": temp,
"Pressure": pressure,
"Humidity": humidity,
"Voltage": volts,
"Received abort": received_abort,
"TX count": tx_count,
"RX count": rx_count,
"RSSI": rssi,
"SNR": snr,
"Uploader lat": u_lat,
"Uploader lon": u_lon,
"Uploader alt": u_alt,
"ID": id,
}

print(f"Lat: {lat}, lon: {lon}, alt: {alt}, target lat: {t_lat}, target lon: {t_lon}, yaw: {yaw}, pitch: {pitch}, roll: {roll}, time: {utc_time_str}, temp: {temp}, pressure: {pressure}, humidity: {humidity}, volts: {volts}, received abort: {received_abort}, tx count: {tx_count}, rx count: {rx_count}, rssi: {rssi}, snr: {snr}, uploader lat: {u_lat}, uploader lon: {u_lon}, uploader alt: {u_alt}, id: {id}")

df = pd.DataFrame([data])

file = f"{cwd}/data/{call_sign}.csv"
df.to_csv(
file,
mode="a",
header=not pd.io.common.file_exists(file),
index=False,
)

df = pd.read_csv(f"{cwd}/data/{call_sign}_to_glider.csv")

response = {
"callSign": call_sign,
"tLat": df.loc[-1, "tLat"],
"tLon": df.loc[-1, "tLon"],
"abort": df.loc[-1, "abort"],
}

# Send response back to the client.
await websocket.send(json.dumps(response))
print("Response sent.")

# This creates a list of all stations that have received data. It also prints active stations.
data = {
"ID": id,
"Time": utc_time_str,
"Call sign": call_sign,
"SNR": snr,
"RSSI": rssi,
}

df = pd.DataFrame([data])

file = f"{cwd}/stations.csv"
df.to_csv(
file,
mode="a",
header=not pd.io.common.file_exists(file),
index=False,
)

if datetime.datetime.second() - last_print > 60:
print("Printing stations receiving in the last 60 seconds.")
df = pd.read_csv(f"{cwd}/stations.csv")
last_print = datetime.datetime.second()
for i in range(len(df)):
if (
datetime.datetime.utcnow()
- datetime.datetime.strptime(
df.loc[i, "Time"], "%Y-%m-%dT%H:%M:%SZ"
)
).seconds < 60:
print(
df.loc[i, "ID"],
"received data from",
df.loc[i, "Call sign"],
"at",
df.loc[i, "Time"],
"with SNR",
df.loc[i, "SNR"],
"and RSSI",
df.loc[i, "RSSI"],
)
else:
print("No stations received data in the last 60 seconds.")

except json.JSONDecodeError:
print("Error: Received data is not valid JSON")
# await websocket.send(json.dumps({"error": "Invalid JSON"}))
Expand All @@ -186,6 +216,75 @@ async def handle_connection(websocket, path):
print(f"Error: {e}")


def input_thread():
while True:
try:
print("Avialable call signs:")
os.listdir(f"{cwd}/data").replace(".csv", "")
s_call_sign = input("Enter the call sign you would like to change:\n")
if s_call_sign in os.listdir(f"{cwd}/data").replace(".csv", ""):
df = pd.read_csv(f"{cwd}/data/{s_call_sign}.csv")

print(f"Lat: {df.loc[-1, 'Lat']}, lon: {df.loc[-1, 'Lon']}, alt: {df.loc[-1, 'Alt']}, last received at {df.loc[-1, 'Time']}, voltage: {df.loc[-1, 'Voltage']}")
print(f"Abort status: {df.loc[-1, "Abort"]}")
print(f"Target latitude: {df.loc[-1, "Target lat"]}")
print(f"Target longitude: {df.loc[-1, "Target lon"]}")

new_t_lat = float(
input("Enter the new target latitude. Enter 0 to do nothing:\n")
)
new_t_lon = float(
input("Enter the new target longitude. Enter 0 to do nothing:\n")
)
abort = bool(
input(
"Enter 1 to abort the mission or 0 to do nothing or revert your change:\n"
)
)

if new_t_lat == 0:
new_t_lat = df.loc[-1, "Target lat"]
if new_t_lon == 0:
new_t_lon = df.loc[-1, "Target lon"]

if abort != 0 or 1:
print(f"Abort changed to {abort}, which is not 0 or 1.")
abort = input("Please enter 0 (do nothing) or 1 (abort mission):\n")
if abort != 0 or 1:
print("Abort status still invalid. Resorting to orginal value.")
abort = df.loc[-1, "Abort"]

if os.path.exists(f"{cwd}/data/{s_call_sign}_to_glider.csv"):
df = pd.read_csv(f"{cwd}/data/{s_call_sign}_to_glider.csv")
df.loc[-1, "tLat"] = new_t_lat
df.loc[-1, "tLon"] = new_t_lon
df.loc[-1, "abort"] = abort
else:
data = {
"tLat": new_t_lat,
"tLon": new_t_lon,
"abort": abort,
}

df = pd.DataFrame([data])

file = f"{cwd}/data/{s_call_sign}_to_glider.csv"
df.to_csv(
file,
mode="a",
header=not pd.io.common.file_exists(file),
index=False,
)

print(
f"Data for {s_call_sign} has been changed to coordinates {new_t_lat, new_t_lon} with abort status of {abort}. Sending to ground station shortly."
)
except ValueError:
print(
"Invalid input. Please enter numerical values for latitude and longitude."
)


# Main WebSocket server coroutine
async def main():
print(f"Starting server on ws://0.0.0.0:{PORT}")
Expand Down

0 comments on commit be2370f

Please sign in to comment.