def distance_error(): # logging.basicConfig(level=logging.DEBUG) arduno_port = connect_ardunio() try_time = 10 logs = np.zeros(try_time) sampling_n = 1 voltage = 12.00 # (v) current = voltage / 470 # (i) channel = 2 with GPIB_Manager(0) as gb1: for i in range(try_time): print("times : {}".format(i)) reference = get_signal(arduno_port, sampling_n) gb1.write_data('v', voltage, channel) gb1.write_data('i', current, channel) # print(current) gb1.turn_on(channel) time.sleep(1) signal = get_signal(arduno_port, sampling_n) print("The signal is : {}".format(signal)) print("The reference is : {}".format(reference)) gb1.turn_off(channel) distance = signal2distance(np.abs(signal - reference)) logging.debug("distance is {}".format(distance)) logs[i] = distance print("The average of distance: {}".format(logs.mean())) print("The std of distance: {}".format(logs.std()))
def build_experiment(filename='distance_10.csv', result={}): port = connect_ardunio() result['distance'] = [] result['signal'] = [] result['ref_std'] = [] result['ref_mean'] = [] result['mean'] = [] result['std'] = [] with serial.Serial(port, 19200, timeout=1) as ser: with GPIB_Manager(0) as g_m: while True: content = input("Please enter the x, y :") if content == 's': break position = content.split(',') if len(position) < 2: print("Wrong key, try again") continue x, y = float(position[0]), float(position[1]) distance = (x ** 2 + y ** 2 + z ** 2) ** (1 / 2) result['distance'].append(distance) ref_d = get_signal(ser, 10) while ref_d['std'] > 1: print("sampling failed") ref_d = get_signal(ser, 10) print("ref : {}".format(ref_d['mean'])) result['ref_mean'].append(ref_d['mean']) result['ref_std'].append(ref_d['std']) g_m.write_data('v', voltage, 1) g_m.write_data('i', current, 1) g_m.turn_on(1) time.sleep(1) sig_d = get_signal(ser, 10) while sig_d['std'] > 1: print("sampling failed") sig_d = get_signal(ser, 10) print("sig : {}".format(sig_d['mean'])) result['mean'].append(sig_d['mean']) result['std'].append(sig_d['std']) result['signal'].append(abs(sig_d['mean'] - ref_d['mean'])) g_m.turn_off(1) k = input("With position x, y : {}, {}\nsave data ? ".format(x, y)) if len(k) > 0: print("give up this data") continue for i, j in result.items(): print("{} content is len of {}".format(i, len(j))) df = pd.DataFrame.from_dict(result) df.to_csv(filename)
def main(): # parameter setting arduno_port = connect_ardunio() sender_position = np.array(([(-5, 0, 0), (5, 0, 0), (0, -5, 0)])) sender_n = sender_position.shape[0] # print(sender_n) distance = np.zeros(3) amplitude = np.zeros(sender_n) p_e = Position_estimation(sender_position, distance) sampling_n = 100 voltage = 12.00 # (v) current = voltage / 470 #(i) #starting with GPIB_Manager(0) as gb1: with GPIB_Manager(1) as gb2: while True: # Get reference data reference = get_signal(arduno_port, sampling_n) print("The reference signal is {}".format(reference)) # Get the data when sender gibe the signal for i in range(sender_n): channel = i + 1 if channel > 2: gb = gb2 channel = channel - 2 else: gb = gb1 gb.write_data('v', voltage, channel) gb.write_data('i', current, channel) # print(current) gb.turn_on(channel) time.sleep(0.1) signal = get_signal(arduno_port, sampling_n) print("channel : {}, signal get : {}".format(i, signal)) amplitude[i] = signal gb.turn_off(channel) time.sleep(0.1) print("Change channel") amplitude = np.subtract(amplitude, reference) amplitude = np.absolute(amplitude) #change the signal to distance distance = signal2distance(amplitude) position = p_e.renew_distance(distance) print("The position now : {}".format(position))
def distance_test(): arduno_port = connect_ardunio() sampling_n = 1 voltage = 12.00 # (v) current = voltage / 470 # (i) channel = 1 with GPIB_Manager(1) as gb1: while True: reference = get_signal(arduno_port, sampling_n) gb1.write_data('v', voltage, channel) gb1.write_data('i', current, channel) # print(current) gb1.turn_on(channel) time.sleep(channel) signal = get_signal(arduno_port, sampling_n) print("The signal is : {}".format(signal)) print("The reference is : {}".format(reference)) gb1.turn_off(channel) print(signal2distance(np.abs(signal - reference)))
def PDF_draw(times, filename='distance_10.csv'): port = connect_ardunio() result = {} result['distance'] = [] result['signal'] = [] head = time.time() with serial.Serial(port, 19200, timeout=1) as ser: with GPIB_Manager(0) as g_m: while True: o_d = get_signal(ser, 10) while o_d['std'] > 1: print("sampling fail") o_d = get_signal(ser, 10) ref = o_d['mean'] print("The ref signal is {}".format(ref)) print("The std of ref is {}".format(o_d['std'])) g_m.write_data('v', voltage, 1) g_m.write_data('i', current, 1) g_m.turn_on(1) time.sleep(1) r_d = get_signal(ser, 10) while r_d['std'] > 1: print("sampling fail") r_d = get_signal(ser, 10) signal = r_d['mean'] print("The signal is {}".format(signal)) print("The std of signal is {}".format(r_d['std'])) g_m.turn_off(1) result['signal'].append(abs(signal - ref)) if len(result['signal']) > times: break end = time.time() print("cost time: {}".format(end - head)) data = np.asarray(result['signal']) np.savetxt(filename, data, delimiter=',') PDF_data(filename)