def follow_wp_path_opt_take_measurements( self, num_plot_points=250, model_type='log', b_take_meas=False, b_log_data=False, tolmm=10, tolrad=10e-3, start_wp=[1000, 1000, 0.5 * np.pi], sample_size=32): """ :param b_take_meas: :param start_wp: :param sample_size: :return: """ if b_take_meas is True: # dont start SDR if not RSS measurements will be taken self.start_RfEar() self.__oRf.set_samplesize(sample_size) sample_size = self.__oRf.get_samplesize() wplist_filename = hc_tools.select_file() print(wplist_filename) wp_append_list = [] with open(wplist_filename, 'r') as wpfile: for i, line in enumerate(wpfile): # print('line = ' + line) # print(line.split(',')) temp_list = line.split(',') wp_append_list.append(map(float, temp_list[0:-1])) # print(str(np.asarray(wp_append_list))) wp_list = np.asarray(wp_append_list) if b_log_data: measdata_filename = hc_tools.save_as_dialog() # print(measdata_filename) num_wp = len(wp_list) print('Number of way points: ' + str(num_wp)) start_time = t.time() data_list = [] meas_counter = 0 time_elapsed = 0.0 tolx_mm = tolmm # mm toly_mm = tolmm # mm tola_rad = tolrad # rad b_ekf = True if b_ekf is True: # init EKF EKF = estimator.ExtendedKalmanFilter(model_type) import estimator_plot_tools EKF_plotter = estimator_plot_tools.EKF_Plot( EKF.get_tx_pos(), model_type) # follow wp sequence for wp in wp_list: print('wp in list = ' + str(wp)) # go to wp self.set_target_wp(wp) self.start_moving_gantry_to_target() not_arrived_at_wp = True print('Moving to wp = ' + str(wp)) # following sequence while not_arrived_at_wp: meas_counter += 1 time_elapsed = t.time() - start_time pos_x_mm, pos_y_mm, pos_a_rad = self.get_gantry_pos_xya_mmrad() if b_ekf is True: EKF.ekf_prediction() EKF.ekf_update(-85) # EKF.check_valid_position_estimate() # print(EKF.get_x_est()) EKF_plotter.add_x_est_to_plot(EKF.get_x_est()) EKF_plotter.update_meas_circles(EKF.get_z_meas(), EKF.get_tx_alpha(), EKF.get_tx_gamma(), True, EKF.get_y_est()) EKF_plotter.plot_gantry_pos([pos_x_mm, pos_y_mm]) EKF_plotter.plot_ekf_pos_live(True, num_plot_points) # EKF_plotter.add_p_cov_to_plot(EKF.get_p_mat()) #EKF_plotter.plot_p_cov(num_plot_points) if b_take_meas is True: # taking measurements freq_den_max, pxx_den_max = self.__oRf.get_rss_peaks() data_row = np.append([ meas_counter, time_elapsed, pos_x_mm, pos_y_mm, pos_a_rad ], pxx_den_max) else: data_row = [ meas_counter, time_elapsed, pos_x_mm, pos_y_mm, pos_a_rad ] # add new data to list data_list.append(data_row) # arrived at wp? -> go to next if self.confirm_arrived_at_target_wp(tolx_mm, toly_mm, tola_rad): not_arrived_at_wp = False meas_freq = meas_counter / time_elapsed print('Logging with avg. ' + str(meas_freq) + ' Hz') if b_log_data: with open(measdata_filename, 'w') as measfile: measfile.write('Measurement file for trajectory following\n') measfile.write('Measurement was taken on ' + t.ctime() + '\n') measfile.write('### begin grid settings\n') if b_take_meas is True: measfile.write('sample size = ' + str(sample_size) + ' [*1024]\n') measfile.write('avg. meas frequency = ' + str(meas_freq) + ' Hz\n') measfile.write('start_point =' + str(start_wp) + '\n') measfile.write('wp_list =' + str(wp_list) + '\n') if b_take_meas is True: measfile.write( 'data format = [meas_counter, time_elapsed, pos_x_mm, pos_y_mm, pos_a_rad], pxx_den_max\n' ) else: measfile.write( 'data format = [meas_counter, time_elapsed, pos_x_mm, pos_y_mm, pos_a_rad]\n' ) measfile.write('### begin data log\n') data_mat = np.asarray(data_list) for row in data_mat: row_string = '' for i in range(len(row)): row_string += str(row[i]) + ',' row_string += '\n' measfile.write(row_string) measfile.close() return True
def main(simulate_meas, x_start=None, measfile_rel_path=None, cal_param_file=None, make_plot=False): """ executive programm :param x_start: :param measfile_rel_path: :param cal_param_file: :param make_plot: :param simulate_meas: :return: """ '''load measurement data''' meas_data, alpha = est_to.get_meas_values( simulate_meas, measfile_rel_path) # possibly write this data into class # print('meas_data:\n' + str(meas_data)) '''initialize values for EKF''' EKF = Extended_Kalman_Filter( alpha=alpha, x_start=x_start ) # initialize object ->check initial values in __init__ function est_to.read_measfile_header( object=EKF, analyze_tx=[1, 2], measfile_path=measfile_rel_path) # write params from header in object # EKF.set_num_meas(10) EKF.set_cal_params(cal_param_file=cal_param_file) EKF.set_tx_param() EKF.set_alpha(alpha) EKF.set_initial_values() '''EKF loop''' num_meas = EKF.get_num_meas() x_est_list = [] x_est_list.append(x_start) y_est_list = [] rss_meas = [] p_mat_list = [] psi_list = [] theta_cap_list = [] for i in range(num_meas): print('Passage number:' + str(i + 1) + '\n') # print('meas_data num :' + str(i) + str(meas_data[i][:]) + '\n') # setting depth value from wp position to simulate depth sensor EKF.set_pos_real(meas_data[i][0:3]) EKF.ekf_prediction() EKF.ekf_update(meas_data[i][:]) # print('x_est = ') # print(str(EKF.get_x_est()) + '\n') '''make list for plotting''' x = EKF.get_x_est()[0][0] y = EKF.get_x_est()[1][0] z = EKF.get_x_est()[2][0] x_est_list.append([x, y, z]) # for plotting purposes # x_est_list.append(EKF.get_x_est()) '''make list for plotting''' sigma_x = EKF.get_p_mat()[0][0] sigma_y = EKF.get_p_mat()[1][1] sigma_z = EKF.get_p_mat()[2][2] p_mat_list.append([sigma_x, sigma_y, sigma_z]) # for plotting purposes # x_est_list.append(EKF.get_x_est()) y_tild_list = EKF.get_y_tild() y_est_list.append(EKF.get_y_est().tolist()) theta_cap_list.append(EKF.get_theta_cap()) meas = meas_data[i][:] rss_meas.append(meas[3:3 + EKF.get_tx_num()].tolist()) print('\n* * * * * *\n' 'estimator.py stopped!\n' '* * * * * *\n') ''' Plott functions from Jonas ''' print('Start plotting results.\n') '''Erstellung der X und Y Koordinatenlisten zum einfachen und effizienteren Plotten''' x_n_x = [None] * num_meas x_n_y = [None] * num_meas x_n_z = [None] * num_meas x_est_x = [None] * len(x_est_list) x_est_y = [None] * len(x_est_list) x_est_z = [None] * len(x_est_list) tx_pos_x = [None] * len(EKF.get_tx_pos()) tx_pos_y = [None] * len(EKF.get_tx_pos()) tx_pos_z = [None] * len(EKF.get_tx_pos()) for i in range(0, num_meas): x_n_x[i] = meas_data[i, 0] # position of waypoints x_n_y[i] = meas_data[i, 1] # position of waypoints x_n_z[i] = meas_data[i, 2] # position of waypoints for i in range(0, len(x_est_list)): x_est_x[i] = x_est_list[i][0] x_est_y[i] = x_est_list[i][1] x_est_z[i] = x_est_list[i][2] for i in range(0, len(EKF.get_tx_pos())): tx_pos_x[i] = EKF.get_tx_pos()[i][0] tx_pos_y[i] = EKF.get_tx_pos()[i][1] tx_pos_z[i] = EKF.get_tx_pos()[i][2] fig = plt.figure(2, figsize=(10, 2.5)) # fig = plt.figure(1, figsize=(25, 12)) '''Fehlerplot ueber Koordinaten''' plot_fehlerplotueberkoordinaten = False if plot_fehlerplotueberkoordinaten: plt.subplot(144) x_est_fehler = [None] * len(x_est_x) for i in range(3, len(x_n_x)): x_est_fehler[i] = est_to.get_distance_1d(x_est_x[i], x_n_x[i - 1]) # plt.plot(x_est_fehler) xmax = max(x_est_fehler) for i in range(3, len(x_n_y)): x_est_fehler[i] = est_to.get_distance_1d(x_est_y[i], x_n_y[i - 1]) # plt.plot(x_est_fehler) ymax = max([max(x_est_fehler), xmax]) a = np.asarray(x_est_list[3]) b = meas_data[3 - 1, 0:3] for i in range(3, len(x_est_list)): x_est_fehler[i] = est_to.get_distance_2d(np.asarray(x_est_list[i]), meas_data[i - 1, 0:3]) # plt.plot(x_est_fehler) ymax = max([max(x_est_fehler), ymax]) # maximum error x_est_fehler_ges_mean = [np.mean(x_est_fehler[3:])] * len(x_est_x) x_est_fehler_ges_sdt = np.std(x_est_fehler[3:]) # just for 2D error?? if plot_fehlerplotueberkoordinaten: plt.plot(x_est_fehler_ges_mean, '--') plt.xlabel('Messungsnummer') plt.ylabel('Fehler') plt.legend([ 'Fehler X-Koordinate', 'Fehler Y-Koordinate', '(Gesamt-) Abstandsfehler', ('Mittlerer Gesamtfehler = ' + str(np.round(x_est_fehler_ges_mean[0], 1))) ], loc=0) plt.ylim(0, ymax + 300) '''Analyse der Einzelmessungen fuer einfacheres Tuning''' analyze_individual_meas = False if analyze_individual_meas: ekf_plotter = ept.EKF_Plot(EKF.get_tx_pos(), bplot_circles=True) # Einzelanalyse der Punkte mit Kreisen direct_terms = [None] * EKF.get_tx_num() for i in range(EKF.get_tx_num()): direct_terms[i] = np.log10(EKF.get_D_0_rx() * EKF.get_D_0_tx()) for itx in range(len(x_n_x)): msg_x_est_temp = x_est_list[itx] # print('x= ' + str(msg_x_est)) msg_yaw_rad = 0 msg_z_meas = meas_data[itx, 3:(3 + EKF.get_tx_num())] msg_y_est = meas_data[itx, 3:(3 + EKF.get_tx_num())] msg_next_wp = meas_data[itx, 0:3] # print('wp=' + str(msg_next_wp)) ekf_plotter.add_x_est_to_plot(msg_x_est_temp, msg_yaw_rad) ekf_plotter.update_next_wp(msg_next_wp) ekf_plotter.update_meas_circles(msg_z_meas, EKF.get_tx_lambda(), EKF.get_tx_gamma(), direct_terms, msg_y_est, b_plot_yest=True) ekf_plotter.plot_ekf_pos_live(b_yaw=False, b_next_wp=True) plt.show() # Hier Breakpoint hinsetzen fuer Analyse der punkte '''Strecke im Scatterplot''' plot_3d = False if plot_3d: # ax = fig.add_subplot(121, projection='3d') ax = fig.add_subplot(111, projection='3d') ax.scatter(tx_pos_x, tx_pos_y, tx_pos_z, marker="*", c='k', s=100, depthshade=True, zorder=0) ax.scatter(x_n_x, x_n_y, x_est_z, marker="^", c='c', s=25, depthshade=True, zorder=1) ax.scatter(x_est_x, x_est_y, x_est_z, marker="o", c='r', s=100, depthshade=True, zorder=2) xmin = float( min([min(x_n_x), min(tx_pos_x), min(x_n_y), min(tx_pos_y)])) - 100.0 xmax = float( max([max(x_n_x), max(tx_pos_x), max(x_n_y), max(tx_pos_y)])) + 100.0 ymin = float(min([min(x_n_y), min(tx_pos_y)])) - 100.0 ymax = float(max([max(x_n_y), max(tx_pos_y)])) + 100.0 if ymax < xmax: ymax += 0.5 * xmax ymin = ymax - xmax zmin = float(min([min(tx_pos_z), x_est_z])) zmax = float(max([max(tx_pos_z), x_est_z])) if zmax < xmax: zmax += 0.5 * xmax zmin = zmax - xmax elif zmax < ymax: zmax += 0.5 * ymax zmin = zmax - ymax ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) ax.set_zlim(zmin, zmax) ax.set_xlabel('X - Achse', fontsize=20) ax.set_ylabel('Y - Achse', fontsize=20) ax.set_zlabel('Z - Achse', fontsize=20) ax.grid() ax.legend( ['Transmitter Antennen', 'Wahre Position', 'Geschaetzte Position'], loc=3) # best:0, or=1, ol=2, ul=3, ur=4 ax.set_title('Plot der wahren und geschaetzten Punkte', fontsize=25) # plt.show() else: ''' here begins the tracking plot ''' plt.subplot(221) # plt.subplot(111) plt.plot(x_n_x, x_n_y, marker=".", c='c') plt.plot(x_est_x, x_est_y, marker=".", c='r') plt.scatter(tx_pos_x, tx_pos_y, marker="*", c='k', s=100) xmin = float(min([min(x_n_x), min(tx_pos_x), min(x_est_x)])) - 100.0 xmax = float(max([max(x_n_x), max(tx_pos_x), max(x_est_x)])) + 100.0 ymin = float(min([min(x_n_y), min(tx_pos_y), min(x_est_y)])) - 150.0 # - 350 ymax = float(max([max(x_n_y), max(tx_pos_y), max(x_est_y)])) + 100.0 ymax2 = ymin + ((ymax - ymin) / 2) + ((xmax - xmin) / 2) ymin2 = ymin + ((ymax - ymin) / 2) - ((xmax - xmin) / 2) plt.axis([xmin, xmax, ymin, ymax]) plt.xlabel('X - Achse [mm]') plt.ylabel('Y - Achse [mm]') plt.grid() # add annotations # plt.legend(['$x_text{wahr}', '$x_text{R,est}', '$x_text{Ti}'], loc=3, ncol=3) # best:0, or=1, ol=2, ul=3, ur=4 # plt.title('Plot der wahren und geschaetzten Punkte', fontsize=25) # plt.annotate('Z-Koordinate Sendeantennen: ' + str(tx_h[0]) + 'mm', xy=(xmin+50, ymax-50), xytext=(xmin+50, ymax-50)) # plt.annotate('Z-Koordinate Empfaengerantenne: ' + str(h_mauv) + 'mm', xy=(xmin+50, ymax-160), xytext=(xmin+50, ymax-160)) plt.annotate('Durchschnittlicher Fehler: ' + str(np.round(x_est_fehler_ges_mean[0], 0)) + ' +- ' + str(np.round(x_est_fehler_ges_sdt)) + 'mm', xy=(xmin + 50, ymax - 350), xytext=(xmin + 50, ymax - 350)) plt.annotate('$x_text{0,est,wahr}', xy=(x_est_x[0], x_est_y[0]), xytext=(x_est_x[0] - 50, x_est_y[0] - 100)) # plt.annotate('$x_text{0,wahr}', xy=(x_n_x[0], x_n_y[0]), xytext=(x_n_x[0]-50, x_n_y[0]-100)) # plt.show() '''Strecke im Linienplot''' plot_streckeimlinienplot = True if plot_streckeimlinienplot: # x_est_fehler = [None] * len((x_est_x)) for i in range(len(x_est_x)): x_est_fehler[i] = est_to.get_distance_1d(x_est_x[i], x_est_y[i]) # plt.figure(figsize=(12, 12)) plt.subplot(222) plt.plot(range(0, num_meas), x_n_x, c='c') plt.plot(x_est_x, c='r') plt.plot(range(0, num_meas), x_n_y, c='m') plt.plot(x_est_y, c='y') p_est_x = [None] * len(p_mat_list) p_est_y = [None] * len(p_mat_list) p_est_z = [None] * len(p_mat_list) #plot variance next to coordinate # for i in range(0, len(p_mat_list)): # p_est_x[i] = p_mat_list[i][0] # p_est_y[i] = p_mat_list[i][1] # p_est_z[i] = p_mat_list[i][2] # # x_plot = np.asarray(range(0, num_meas)) # plt.fill_between(x_plot, np.asarray(x_est_x[:-1]) + np.asarray(p_est_x) * 0.1, # np.asarray(x_est_x[:-1]) - np.asarray(p_est_x) * 0.1, alpha=0.5) # plt.fill_between(x_plot, np.asarray(x_est_y[:-1]) + np.asarray(p_est_y) * 0.1, # np.asarray(x_est_y[:-1]) - np.asarray(p_est_y) * 0.1, alpha=0.5) plt.xlabel('Messungsnummer') plt.ylabel('Koordinate [mm]') plt.legend(['x_true', 'x_est', 'y_true', 'y_est'], loc=1) ymin = min([min(x_n_x), min(x_n_y), min(x_est_x), min(x_est_y)]) ymax = max([max(x_n_x), max(x_n_y), max(x_est_x), max(x_est_y)]) plt.ylim(ymin - 100, ymax + 100) plt.xticks(np.arange(0, len(x_n_x), step=5)) plt.grid() plot_z = True if plot_z: # plt.figure(figsize=(12, 12)) plt.subplot(223) plt.plot(range(0, num_meas), x_n_z, c='c') plt.plot(x_est_z, c='r') plt.xlabel('Messungsnummer') plt.ylabel('Koordinate [mm]') plt.legend(['z_true', 'z_est'], loc=1) ymin = min([min(x_n_z), min(x_est_z)]) ymax = max([max(x_n_z), max(x_est_z)]) plt.ylim(ymin - 100, ymax + 100) plt.xticks(np.arange(0, len(x_n_x), step=5)) plt.grid() plt.annotate('Durchschnittlicher Fehler: ' + str(np.round(x_est_fehler_ges_mean[0], 0)) + ' +- ' + str(np.round(x_est_fehler_ges_sdt)) + 'mm', xy=(xmin + 50, ymax - 350), xytext=(xmin + 50, ymax - 350)) plot_meas_sym = False if plot_meas_sym: # plt.figure(figsize=(12, 12)) plt.subplot(224) # make list for every Tx y_est_0 = [] y_est_1 = [] y_est_2 = [] y_est_3 = [] y_est_4 = [] rss_0 = [] rss_1 = [] rss_2 = [] rss_3 = [] rss_4 = [] for i in range(EKF.get_num_meas()): y_est_0.append(y_est_list[i][0]) y_est_1.append(y_est_list[i][1]) # y_est_2.append(y_est_list[i][2]) # y_est_3.append(y_est_list[i][3]) # y_est_4.append(y_est_list[i][4]) rss_0.append(rss_meas[i][0]) rss_1.append(rss_meas[i][1]) # rss_2.append(rss_meas[i][2]) # rss_3.append(rss_meas[i][3]) # rss_4.append(rss_meas[i][4]) plt.plot(range(1, (num_meas + 1)), y_est_0, '.--', c='r') plt.plot(range(1, (num_meas + 1)), y_est_1, '.--', c='y') # plt.plot(range(1, (num_meas + 1)), y_est_2, '.--', c='m') plt.plot(range(1, (num_meas + 1)), rss_0, '.-', c='c') plt.plot(range(1, (num_meas + 1)), rss_1, '.-', c='g') # plt.plot(range(1, (num_meas + 1)), rss_2, '.-', c='b') ymin = min([min(y_est_0), min(rss_0), min(y_est_1), min(rss_1)]) ymax = max([max(y_est_0), max(rss_0), max(y_est_1), max(rss_1)]) # plt.axis([xmin, xmax, ymin, ymax]) plt.xlabel('Messungsnummer') plt.ylabel('RSS') # plt.legend(['RSS_est_T1', 'RSS_est_T2', 'RSS_est_T3', 'RSS_true_T1', 'RSS_true_T2', 'RSS_true_T3'], loc=1) plt.legend(['RSS_est_T1', 'RSS_est_T2', 'RSS_true_T1', 'RSS_true_T2'], loc=1) plt.ylim(ymin - 2, ymax + 2) plt.xticks(np.arange(0, len(y_est_0) + 1, step=5)) plt.grid() plt.subplot(223) ymin = min([min(y_est_0), min(rss_0), min(y_est_1), min(rss_1)]) ymax = max([max(y_est_0), max(rss_0), max(y_est_1), max(rss_1)]) # plt.axis([xmin, xmax, ymin, ymax]) plt.xlabel('Messungsnummer') plt.ylabel('RSS') # plt.plot(range(1, (num_meas + 1)), y_est_3, '.--', c='y') # plt.plot(range(1, (num_meas + 1)), y_est_4, '.--', c='b') # # plt.plot(range(1, (num_meas + 1)), rss_3, '.-', c='g') # plt.plot(range(1, (num_meas + 1)), rss_4, '.-', c='c') plt.legend(['RSS_est_T4', 'RSS_est_T5', 'RSS_true_T4', 'RSS_true_T5'], loc=1) plt.ylim(ymin - 2, ymax + 2) plt.xticks(np.arange(0, len(y_est_0) + 1, step=5)) plt.grid() '''Fehlerhistogramm''' plot_fehlerhistogramm = False if plot_fehlerhistogramm: plt.subplot(248) plt.hist(x_est_fehler[3:], 30, (0, 800)) # 800 plt.xlabel('Fehler') plt.ylabel('Anzahl der Vorkommnisse') plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.4, hspace=None) plt.show() fig_1 = plt.figure(1, figsize=(25, 12)) '''Theta''' fig_1.add_subplot(131) return True
import socket_server import estimator import estimator_plot_tools import numpy as np import time as t """ TX position StillWasserBecker """ #tx_pos = [[520.0, 430.0], [1540.0, 430.0], [2570.0, 430.0], [2570.0, 1230.0], [1540.0, 1230.0], [530.0, 1230.0]] """ TX position - origin at Beacon #1 """ tx_pos = tx_6pos = [[0, 0], [1000, 0], [2000, 0], [2000, 900], [1000, 900], [0, 900]] ekf_plotter = estimator_plot_tools.EKF_Plot(tx_pos, bplot_circles=False) """ WARNING!!!!!! IF YOU CHANGE THE ALPHA + GAMMA VALUES IN EKF YOU HAVE!!!! to change them here MANUALLY!!! """ tx_alpha = [0.01149025464796399, 0.016245419273983631, 0.011352095690562954, 0.012125937076390217, 0.0092717529591962722, 0.01295918160582895] tx_gamma = [-0.49471304043015696, -1.2482393190627841, -0.17291318936462172, -0.61587988305564456, 0.99831151034040444, 0.85711994311461936] """ WARNING!!!!!! IF YOU CHANGE THE ALPHA + GAMMA VALUES IN EKF YOU HAVE!!!! to change them here MANUALLY!!! """ server_ip = '192.168.0.100' # thinkpad ethernet #server_ip = '192.168.0.101' # thinkpad wifi-intern server_port = 50008 soc_server = socket_server.SocServer(server_ip, server_port)
phi_cap_t = [0.0]*tx_num theta_cap_t = [0.0]*tx_num psi_low_t = [0.0]*tx_num theta_low_t = [0.0]*tx_num '''Einstellungen fuer Messwerterzeugung''' messung_benutzen = True if messung_benutzen: '''Laden der Messdatei''' plotdata_mat = analyze_measdata_from_file(measfilename='wp_list_2018_08_10_grid_meas_d505025_measurement_h500.txt') extra_plotting = False direct_terms = [0.0] * tx_num if extra_plotting: '''Setup fuer Kreisplots''' ekf_plotter = ept.EKF_Plot(tx_pos, bplot_circles=True) for i in range(tx_num): direct_terms[i] = np.log10(directivity_r * directivities_t[i]) '''Setup fuer ytild Plot''' fig_ytild_p = plt.figure(42) sub1_ytild = fig_ytild_p.add_subplot(121) linspace_ploty_txnum = np.linspace(1, tx_num, tx_num) ydata_ploty = np.linspace(-20, 20, tx_num) line1sub1, = sub1_ytild.plot(linspace_ploty_txnum, ydata_ploty, 'r-') # Returns a tuple of line objects, thus the comma plt.grid() '''Setup fuer P Plot''' sub2_pmat = fig_ytild_p.add_subplot(122) linspace_plotp = [1, 2]