def full_calibrate(self): timestamp = time.strftime('%m-%d_%H%M') fileName = "data\\dmc_calib_" + timestamp self.calibrate_both_spins(fileName) self.calibrate_all_straight(fileName) for i in range(2): serial.write("cmd write_motor_settings") print("!!!\nDone calibrating!\n!!!")
def full_calibrate(self): #first, calibrate droplet spin for one direction. This will give us motor sign and relative values for the three motors. #then calibrate directions 0, 2, and 4 (or 1, 3, and 5) timestamp = time.strftime('%m-%d_%H%M') fileName = "data\\dmc_calib_" + timestamp self.calibrate_both_spins(fileName) self.calibrate_all_straight(fileName) for i in range(2): serial.write("cmd write_motor_settings") print("!!!\nDone calibrating!\n!!!")
def process_and_transmit_line(line): byte_strs = [str(line[i])+str(line[i+1]) for i in range(1,len(line)-1,2)] byte_sum=0 for b in byte_strs: byte_sum+=int(b,16) byte_sum-=int(byte_strs[-1],16) if(format(0xFF&((0xFF^(byte_sum & 0xFF))+1),'02X') != byte_strs[-1]): print("checksum mismatch?") print("\t" + format(0xFF&((0xFF^(byte_sum & 0xFF))+1),'02X')) print("\t" + byte_strs[-1]) dat = ''.join([chr(int(b,16)) for b in byte_strs]) #print(dat) Serial.write(dat)
def __init__(self): self.rr = RR_API() self.rr.Connect("localhost") port_h = serial.open_serial_port("COM9") serial.set_global_port(port_h)
if id == "ROOT": id = rootID str = '"{0}"->{{{1:.0f}, {2:.0f}, {3:.1f}}}'.format(id, xPositions[i], yPositions[i], orientations[i]) print(str,end="") file.write(str) if i is not (numFids-1): print(", ",end="") file.write(", ") print("}},") file.write('}},\n') timeStr = time.strftime("%d%m%Y_%H%M%S") root = "ROOT" with open("rnbCalibData_{0}.txt".format(timeStr),'w') as f: with serial.Wrapper("COM3") as port: while True: while port.inWaiting() > 0: data = port.readLine() data = data.strip() idMatch = re.match("My ID is: ([0-9A-F]{4})", data) dataMatch = re.match('\{"([0-9A-F]{4})", "([0-9A-F]{4})", \{\{-?\d+,-?\d+,-?\d+,-?\d+,-?\d+,-?\d+\},\{-?\d+,-?\d+,-?\d+,-?\d+,-?\d+,-?\d+\},\{-?\d+,-?\d+,-?\d+,-?\d+,-?\d+,-?\d+\},\{-?\d+,-?\d+,-?\d+,-?\d+,-?\d+,-?\d+\},\{-?\d+,-?\d+,-?\d+,-?\d+,-?\d+,-?\d+\},\{-?\d+,-?\d+,-?\d+,-?\d+,-?\d+,-?\d+\}\}\},', data) if idMatch: root = idMatch.group(1) elif dataMatch: root = dataMatch.group(2) print(data[:-2],end=', ') f.write(data[:-2]) f.write(', ') printRRData(f, root) else:
def __del__(self): serial.cleanup() self.rr.close() print("Finished cleaning up myself.")
def calibrate_droplet_straight(self, direction, fName=None): alpha = 1. #reflection coefficient gamma = 2. #expansion coefficient rho = -0.5 #contraction coefficient sigma = 0.5 #shrink coefficient x_0 = np.array([0,0]) x_1 = np.array([100,-100]) x_2 = np.array([-100,100]) spx = np.array([x_0, x_1, x_2]) old_spx = np.zeros((3,2)) if fName is None: settings_history_fName = 'dmc_data_%s.csv'%("cw" if cw_q else "ccw") simplex_history_fName = 'dmc_data_%s.csv'%("cw" if cw_q else "ccw") else: settings_history_fName = fName + "_%s_hist.csv"%(direction) simplex_history_fName = fName + "_%s_spx.csv"%(direction) settings_history_file = open(settings_history_fName,'w') settings_history_file.write('mot0set, mot1set, mot2set, radius, sign, distance_traveled\n') simplex_history_file = open(simplex_history_fName,'w') simplex_history_file.write('spx00, spx01, spx02, rad0, spx10, spx11, spx12, rad1, spx20, spx21, spx22, rad2, spx30, spx31, spx32, rad3\n') finished = False def fun(x): (radius, travel_dir, dist_traveled) =self.test_straight_settings(x, direction, settings_history_file) return -radius - travel_dir*dist_traveled try: while(True): spx = np.array(sorted(spx, key=fun)) if str(spx) == str(old_spx): finished = True old_spx = copy.deepcopy(spx) func_values = [fun(point) for point in spx] output_array = np.array(map(lambda spx_point, radius: np.append(spx_point, radius), spx, func_values)).flatten().tolist() simplex_history_file.write(",".join(map(str, output_array))+"\n") simplex_history_file.flush() print("spx: %s"%(str(spx))) x_o = np.mean(spx[:-1],0).astype(int) x_r = (x_o + alpha*(x_o - spx[-1])).astype(int) if (fun(x_r)<fun(spx[-2])) and (fun(x_r)>=fun(spx[0])): print("Reflecting.") spx[-1] = x_r elif fun(x_r)<fun(spx[0]): x_e = (x_o + gamma*(x_o-spx[-1])).astype(int) if fun(x_e)<fun(x_r): print("Expanding.") spx[-1] = x_e else: print("Reflecting.") spx[-1] = x_r else: x_c = (x_o + rho*(x_o-spx[-1])).astype(int) if fun(x_c)<fun(spx[-1]): print("Contracting.") spx[-1] = x_c else: for i in range(1,3): spx[i] = (spx[0] + sigma*(spx[i]-spx[0])).astype(int) if finished: set_motors(i, *spx[0]) print("We're done straightening direction %d"%direction) settings_history_file.close() simplex_history_file.close() return cw_q except KeyboardInterrupt: print("Calibration interrupted by user.") settings_history_file.close() simplex_history_file.close() serial.cleanup()
def set_motors(direction, mot0val, mot1val, mot2val): serial.write("cmd set_motors %d %d %d %d"%(direction, mot0val, mot1val, mot2val)) #Once more, just to be sure. serial.write("cmd set_motors %d %d %d %d"%(direction, mot0val, mot1val, mot2val))
def set_motors(direction, mot0val, mot1val, mot2val): serial.write("cmd set_motors %d %d %d %d" % (direction, mot0val, mot1val, mot2val)) #Once more, just to be sure. serial.write("cmd set_motors %d %d %d %d" % (direction, mot0val, mot1val, mot2val))
def set_led(r, g, b): serial.write("cmd set_led rgb %d %d %d" % (r, g, b))
def __init__(self): self.rr = RoboRealmInterface() port_h = serial.open_serial_port("COM10") serial.set_global_port(port_h)
import MySerialWrapper as Serial import time PROGRAM_DELAY = 6.0 #seconds programTime = PROGRAM_DELAY-0.50 bytespersec = Serial.baud/8 secondsPerLine = (21./bytespersec) #21 is the no. of bytes of each line lineCount = int(PROGRAM_DELAY*0.9/secondsPerLine) #need to set the two lines below based on your system. port = Serial.open_serial_port("COM7") hexFilePath = "C:\Users\Kailaash\Documents\Atmel Studio\Droplets\Droplets\Debug\Droplets.hex" Serial.set_global_port(port) #count = lineCount now = 0 def process_and_transmit_line(line): byte_strs = [str(line[i])+str(line[i+1]) for i in range(1,len(line)-1,2)] byte_sum=0 for b in byte_strs: byte_sum+=int(b,16) byte_sum-=int(byte_strs[-1],16) if(format(0xFF&((0xFF^(byte_sum & 0xFF))+1),'02X') != byte_strs[-1]): print("checksum mismatch?") print("\t" + format(0xFF&((0xFF^(byte_sum & 0xFF))+1),'02X')) print("\t" + byte_strs[-1]) dat = ''.join([chr(int(b,16)) for b in byte_strs]) #print(dat) Serial.write(dat)
def set_led(r, g, b): serial.write("cmd set_led rgb %d %d %d"%(r, g, b))
def calibrate_droplet_straight(self, direction, fName=None): alpha = 1. #reflection coefficient gamma = 2. #expansion coefficient rho = -0.5 #contraction coefficient sigma = 0.5 #shrink coefficient x_0 = np.array([0, 0]) x_1 = np.array([100, -100]) x_2 = np.array([-100, 100]) spx = np.array([x_0, x_1, x_2]) old_spx = np.zeros((3, 2)) if fName is None: settings_history_fName = 'dmc_data_%s.csv' % ("cw" if cw_q else "ccw") simplex_history_fName = 'dmc_data_%s.csv' % ("cw" if cw_q else "ccw") else: settings_history_fName = fName + "_%s_hist.csv" % (direction) simplex_history_fName = fName + "_%s_spx.csv" % (direction) settings_history_file = open(settings_history_fName, 'w') settings_history_file.write( 'mot0set, mot1set, mot2set, radius, sign, distance_traveled\n') simplex_history_file = open(simplex_history_fName, 'w') simplex_history_file.write( 'spx00, spx01, spx02, rad0, spx10, spx11, spx12, rad1, spx20, spx21, spx22, rad2, spx30, spx31, spx32, rad3\n' ) finished = False def fun(x): (radius, travel_dir, dist_traveled) = self.test_straight_settings( x, direction, settings_history_file) return -radius - travel_dir * dist_traveled try: while (True): spx = np.array(sorted(spx, key=fun)) if str(spx) == str(old_spx): finished = True old_spx = copy.deepcopy(spx) func_values = [fun(point) for point in spx] output_array = np.array( map(lambda spx_point, radius: np.append(spx_point, radius), spx, func_values)).flatten().tolist() simplex_history_file.write(",".join(map(str, output_array)) + "\n") simplex_history_file.flush() print("spx: %s" % (str(spx))) x_o = np.mean(spx[:-1], 0).astype(int) x_r = (x_o + alpha * (x_o - spx[-1])).astype(int) if (fun(x_r) < fun(spx[-2])) and (fun(x_r) >= fun(spx[0])): print("Reflecting.") spx[-1] = x_r elif fun(x_r) < fun(spx[0]): x_e = (x_o + gamma * (x_o - spx[-1])).astype(int) if fun(x_e) < fun(x_r): print("Expanding.") spx[-1] = x_e else: print("Reflecting.") spx[-1] = x_r else: x_c = (x_o + rho * (x_o - spx[-1])).astype(int) if fun(x_c) < fun(spx[-1]): print("Contracting.") spx[-1] = x_c else: for i in range(1, 3): spx[i] = (spx[0] + sigma * (spx[i] - spx[0])).astype(int) if finished: set_motors(i, *spx[0]) print("We're done straightening direction %d" % direction) settings_history_file.close() simplex_history_file.close() return cw_q except KeyboardInterrupt: print("Calibration interrupted by user.") settings_history_file.close() simplex_history_file.close() serial.cleanup()
def move_steps(direction, num_steps): serial.write("cmd move_steps %d %d"%(direction, num_steps))
def move_steps(direction, num_steps): serial.write("cmd move_steps %d %d" % (direction, num_steps))
def stop_walk(): serial.write("cmd stop_walk")