|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | + Copyright (c) 2022 PX4 Development Team |
| 5 | + Redistribution and use in source and binary forms, with or without |
| 6 | + modification, are permitted provided that the following conditions |
| 7 | + are met: |
| 8 | +
|
| 9 | + 1. Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | + 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in |
| 13 | + the documentation and/or other materials provided with the |
| 14 | + distribution. |
| 15 | + 3. Neither the name PX4 nor the names of its contributors may be |
| 16 | + used to endorse or promote products derived from this software |
| 17 | + without specific prior written permission. |
| 18 | +
|
| 19 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 26 | + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 | + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 | + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | + POSSIBILITY OF SUCH DAMAGE. |
| 31 | +
|
| 32 | +File: data_extractor.py |
| 33 | +Author: Mathieu Bresciani <mathieu@auterion.com> |
| 34 | +License: BSD 3-Clause |
| 35 | +Description: |
| 36 | +""" |
| 37 | + |
| 38 | +import numpy as np |
| 39 | +from scipy import signal |
| 40 | +from pyulog import ULog |
| 41 | + |
| 42 | +def getAllData(logfile): |
| 43 | + log = ULog(logfile) |
| 44 | + |
| 45 | + rng = getData(log, 'distance_sensor', 'current_distance') |
| 46 | + t_rng = ms2s(getData(log, 'distance_sensor', 'timestamp')) |
| 47 | + |
| 48 | + vz = getData(log, 'vehicle_local_position', 'vz') |
| 49 | + t_vz = ms2s(getData(log, 'vehicle_local_position', 'timestamp')) |
| 50 | + |
| 51 | + STATE_VZ = 6 |
| 52 | + vz_var = getData(log, 'estimator_states', f'covariances[{STATE_VZ}]') |
| 53 | + t_vz_var = ms2s(getData(log, 'estimator_states', 'timestamp')) |
| 54 | + |
| 55 | + (t_aligned, rng_aligned, vz_aligned, vz_var_aligned) = alignData(log, t_rng, rng, t_vz, vz, t_vz_var, vz_var) |
| 56 | + |
| 57 | + t_aligned -= t_aligned[0] |
| 58 | + |
| 59 | + return (t_aligned, rng_aligned, vz_aligned, vz_var_aligned) |
| 60 | + |
| 61 | +def getData(log, topic_name, variable_name, instance=0): |
| 62 | + variable_data = np.array([]) |
| 63 | + for elem in log.data_list: |
| 64 | + if elem.name == topic_name: |
| 65 | + if instance == elem.multi_id: |
| 66 | + variable_data = elem.data[variable_name] |
| 67 | + break |
| 68 | + |
| 69 | + return variable_data |
| 70 | + |
| 71 | +def ms2s(time_ms): |
| 72 | + return time_ms * 1e-6 |
| 73 | + |
| 74 | +def getDeltaMean(data_list): |
| 75 | + dx = 0 |
| 76 | + length = len(data_list) |
| 77 | + for i in range(1,length): |
| 78 | + dx = dx + (data_list[i]-data_list[i-1]) |
| 79 | + |
| 80 | + dx = dx/(length-1) |
| 81 | + return dx |
| 82 | + |
| 83 | +def alignData(log, t_u_data, u_data, t_y_data, y_data, t_y2_data, y2_data): |
| 84 | + len_y = len(t_y_data) |
| 85 | + len_y2 = len(t_y2_data) |
| 86 | + i_y = 0 |
| 87 | + i_y2 = 0 |
| 88 | + u_aligned = [] |
| 89 | + y_aligned = [] |
| 90 | + y2_aligned = [] |
| 91 | + t_aligned = [] |
| 92 | + |
| 93 | + for i_u in range(len(t_u_data)): |
| 94 | + t_u = t_u_data[i_u] |
| 95 | + |
| 96 | + while t_y_data[i_y] <= t_u and i_y < len_y-1: |
| 97 | + i_y += 1 |
| 98 | + while t_y2_data[i_y2] <= t_u and i_y2 < len_y2-1: |
| 99 | + i_y2 += 1 |
| 100 | + |
| 101 | + u_aligned = np.append(u_aligned, u_data[i_u]) |
| 102 | + y_aligned = np.append(y_aligned, y_data[i_y-1]) |
| 103 | + y2_aligned = np.append(y2_aligned, y2_data[i_y2-1]) |
| 104 | + t_aligned.append(t_u) |
| 105 | + |
| 106 | + return (t_aligned, u_aligned, y_aligned, y2_aligned) |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + import argparse |
| 110 | + import os |
| 111 | + |
| 112 | + parser = argparse.ArgumentParser( |
| 113 | + description='Extract data from a give .ulg file') |
| 114 | + |
| 115 | + parser.add_argument('logfile', help='Full ulog file path, name and extension', type=str) |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + logfile = os.path.abspath(args.logfile) # Convert to absolute path |
| 119 | + |
| 120 | + (t_aligned, u_aligned, y_aligned, y2_data) = getAllData(logfile) |
0 commit comments