|
| 1 | +import sys |
| 2 | +from pyulog import ULog |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def get_ulg_data(log, topic_name, variable_name, instance=0): |
| 8 | + variable_data = np.array([]) |
| 9 | + for elem in log.data_list: |
| 10 | + if elem.name == topic_name: |
| 11 | + if instance == elem.multi_id: |
| 12 | + variable_data = elem.data[variable_name] |
| 13 | + break |
| 14 | + |
| 15 | + return variable_data |
| 16 | + |
| 17 | + |
| 18 | +def get_data(log_dir): |
| 19 | + log_name = log_dir.split("/")[-1] |
| 20 | + |
| 21 | + if "ulg" or "ulog" in log_name: |
| 22 | + log = ULog(log_dir) |
| 23 | + timestamps = get_ulg_data(log, "vehicle_local_position", "timestamp") |
| 24 | + positions = np.array( |
| 25 | + [ |
| 26 | + get_ulg_data(log, "vehicle_local_position", "x"), |
| 27 | + get_ulg_data(log, "vehicle_local_position", "y"), |
| 28 | + get_ulg_data(log, "vehicle_local_position", "z"), |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + return timestamps, positions |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + timestamps, positions = get_data(sys.argv[1]) |
| 37 | + |
| 38 | + close_calls = np.empty((1, 0)) |
| 39 | + close_calls_t = np.empty((1, 0)) |
| 40 | + |
| 41 | + target = np.array([10, -50, -50]) # virtual target |
| 42 | + distance_threshold = 5 # meters to consider a landing successful |
| 43 | + print("Target defined as: ", target) |
| 44 | + print("Distance threshold defined as: ", distance_threshold, "m") |
| 45 | + |
| 46 | + best_result_dist = float("inf") |
| 47 | + best_result_t = 0 |
| 48 | + |
| 49 | + print("") |
| 50 | + for i in range(len(timestamps) - 1): |
| 51 | + v = positions[:, i + 1] - positions[:, i] |
| 52 | + w = target - positions[:, i] |
| 53 | + t = np.dot(v, w) / np.dot(v, v) |
| 54 | + |
| 55 | + if t >= 0 and t <= 1: |
| 56 | + distance = np.linalg.norm(target - (positions[:, i] + t * v)) |
| 57 | + |
| 58 | + if distance < distance_threshold: |
| 59 | + print( |
| 60 | + "Close call detected with distance: ", |
| 61 | + round(distance, 2), |
| 62 | + "m at timestamp: ", |
| 63 | + round(timestamps[i] / 1e6, 2), |
| 64 | + "s", |
| 65 | + ) |
| 66 | + close_calls = np.append(close_calls, [distance]) |
| 67 | + close_calls_t = np.append(close_calls_t, [timestamps[i]]) |
| 68 | + |
| 69 | + best_result_dist = min(best_result_dist, distance) |
| 70 | + best_result_t = ( |
| 71 | + timestamps[i] if best_result_dist == distance else best_result_t |
| 72 | + ) |
| 73 | + |
| 74 | + print("") |
| 75 | + print("-- Summary --") |
| 76 | + print("Total number of close calls: ", len(close_calls)) |
| 77 | + print( |
| 78 | + "Best result: \n\r Distance to target: ", |
| 79 | + round(best_result_dist, 2), |
| 80 | + "m at timestamp: ", |
| 81 | + round(best_result_t / 1e6), |
| 82 | + "s", |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments