Skip to content

Commit

Permalink
Update recieve_statustext.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tqmsh authored Jan 17, 2025
1 parent 8862538 commit eed6917
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions modules/recieve_statustext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import argparse
from pathlib import Path


from pymavlink import mavutil
import modules.common.modules.data_encoding.message_encoding_decoding
import modules.common.modules.data_encoding.metadata_encoding_decoding
from modules.common.modules.data_encoding import message_encoding_decoding
from modules.common.modules.data_encoding import metadata_encoding_decoding
from modules.common.modules.data_encoding import worker_enum
import modules.common.modules.kml.kml_conversion

Expand All @@ -31,7 +32,12 @@ def main(save_directory: str, document_name_prefix: str) -> int:
Returns:
int: 0 on success, -1 on error (connection failure, invalid data, or failure to save KML).
"""
save_directory = Path(args.save_directory)
document_name_prefix = args.document_name_prefix

vehicle = mavutil.mavlink_connection(CONNECTION_ADDRESS, source_system=255, source_component=0)
# Catch library and other unexpected errors
# pylint: disable=broad-exception-caught
try:
vehicle.wait_heartbeat()
print("connected")
Expand All @@ -52,42 +58,43 @@ def main(save_directory: str, document_name_prefix: str) -> int:
success, worker_id, expected_positions_count = (
metadata_encoding_decoding.decode_metadata(msg.data)
)
if success and worker_id == worker_enum.WorkerEnum.COMMUNICATIONS_WORKER:
received_positions_count = 0
while received_positions_count < expected_positions_count:
gps_msg = vehicle.recv_match(type="STATUSTEXT", blocking=True)
if not gps_msg:
print("no message")
elif gps_msg.get_type() == "BAD_DATA":
if mavutil.all_printable(gps_msg.data):
print(gps_msg.data)
return -1
else:
success, worker_id, global_position = (
message_encoding_decoding.decode_bytes_to_position_global(gps_msg.data)
)
if success and worker_id == worker_enum.WorkerEnum.COMMUNICATIONS_WORKER:
print(
f"Decoded GPS Data: {global_position.latitude}, {global_position.longitude}, {global_position.altitude}"
)
positions.append(global_position)
received_positions_count += 1
else:
print(
f"Unsuccessful or Non-communications worker message (worker_id: {worker_id})"
)
return -1
success, kml_path = kml_conversion.positions_to_kml(
positions, document_name_prefix, save_directory
if not success or worker_id != worker_enum.WorkerEnum.COMMUNICATIONS_WORKER:
return

received_positions_count = 0
while received_positions_count < expected_positions_count:
gps_msg = vehicle.recv_match(type="STATUSTEXT", blocking=True)
if not gps_msg:
print("no message")
continue

if gps_msg.get_type() == "BAD_DATA":
if mavutil.all_printable(gps_msg.data):
print(gps_msg.data)
return -1

success, worker_id, global_position = (
message_encoding_decoding.decode_bytes_to_position_global(gps_msg.data)
)
if success:
print(f"KML file saved to {kml_path}")
else:
print("Failed to save KML file")
if not success or worker_id != worker_enum.WorkerEnum.COMMUNICATIONS_WORKER:
print(f"Unsuccessful or Non-communications worker message (worker_id: {worker_id})")
return -1

print(
f"Decoded GPS Data: {global_position.latitude}, {global_position.longitude}, {global_position.altitude}"
)
positions.append(global_position)
received_positions_count += 1

success, kml_path = kml_conversion.positions_to_kml(
positions, document_name_prefix, save_directory
)
if not success:
print("Failed to save KML file")
return -1
print(f"KML file saved to {kml_path}")
return 0


DEFAULT_SAVE_DIRECTORY = "logs"
DEFAULT_DOCUMENT_NAME_PREFIX = "IR hotspot locations"

Expand All @@ -107,7 +114,4 @@ def main(save_directory: str, document_name_prefix: str) -> int:
)

args = parser.parse_args()
save_directory = Path(args.save_directory)
document_name_prefix = args.document_name_prefix

main(save_directory, document_name_prefix)

0 comments on commit eed6917

Please sign in to comment.