##def fine_search():
##    center=global_search()
##    steps=[(x+center) for x in range(-6,6,1) if (x+center)>=0 and (x+center)<=100]
##    pi.hardware_PWM(PWM_GPIO,PWM_freq,steps[0]*10000)
##    sleep(0.1)
##    sharp=[]
##    src=vs.read()
##    for i in steps[1::1]:
##        pi.hardware_PWM(PWM_GPIO,PWM_freq,i*10000)
##        sharp.append([sum([item for item in p.map(findsharpness,slicer(src),1) if item is not None]),i-1])
##        src=vs.read()
##    sharp.append([sum([item for item in p.map(findsharpness,slicer(src),1) if item is not None]),i])
##    src=vs.read()
##    for i in steps[len(steps)-2::-1]:
##        pi.hardware_PWM(PWM_GPIO,PWM_freq,i*10000)
##        sharp[i+1-center][0]=int((sharp[i+1-center][0]+sum([item for item in p.map(findsharpness,slicer(src),1) if item is not None]))/1000)
##    sharp[0][0]=int((sharp[0][0]+sum([item for item in p.map(findsharpness,slicer(vs.read()),1) if item is not None]))/1000)    
##    return max(sharp)[1]

##def smart_search(steps=11,init_max_ratio=0.5,pos=0,stepsize=5):
##    global last_duty
##    Abs=0
##    direction=cmp(stepsize,0)
##    max_ratio_sum=init_max_ratio*steps
##    max_ratio=init_max_ratio
##    transition(pos)
##    actions=[]
##
##    sharp=sum([item for item in p.map(findsharpness,slicer(Y),1)])
##    actions.append(("sharp: ",sharp))
##    pos=pos+abs(stepsize)*direction
##    actions.append(("size: ",stepsize))
##    actions.append(("pos: ",pos))
##    pi.hardware_PWM(PWM_GPIO,PWM_freq,(pos)*10000)
##    
##
##    ness=sum([item for item in p.map(findsharpness,slicer(Y),1)])
##    actions.append(("ness: ",ness))
##    for it in range(0,(steps-1)//2):
##        
##        ratio=(ness-sharp)/sharp
##        actions.append(("ratio: ",ratio))
##        Abs=abs(ratio)
##        max_ratio_sum=max_ratio_sum+Abs-init_max_ratio
##        actions.append(("max_ratio_sum: ",max_ratio_sum))
##        max_ratio=max_ratio_sum/steps
##        actions.append(("max_ratio: ",max_ratio))
##        if Abs>=max_ratio:
##            stepsize=1
##        else:
##            stepsize=np.ceil(9*(1-Abs/max_ratio))
##
##        
##        direction=direction*cmp(ratio,0)
##        pos=pos+stepsize*direction
##        if pos>100:
##            pos=100
##            
##        elif pos<0:
##            pos=0
##            
##
##        actions.append(("size: ",stepsize))
##        actions.append(("pos: ",pos))    
##        pi.hardware_PWM(PWM_GPIO,PWM_freq,(pos)*10000)
##        
##        sharp=sum([item for item in p.map(findsharpness,slicer(Y),1)])
##        actions.append(("sharp: ",sharp))
##
##
##        ratio=(sharp-ness)/ness
##        actions.append(("ratio: ",ratio))
##        Abs=abs(ratio)
##        max_ratio_sum=max_ratio_sum+Abs-init_max_ratio
##        actions.append(("max_ratio_sum: ",max_ratio_sum))
##        max_ratio=max_ratio_sum/steps
##        actions.append(("max_ratio: ",max_ratio))
##        if Abs>=max_ratio:
##            stepsize=1
##        else:
##            stepsize=cmp(ratio,0)*np.ceil(9*(1-Abs/max_ratio))
##
##        
##        direction=direction*cmp(ratio,0)
##        pos=pos+stepsize*direction
##        if pos>100:
##            pos=100
##            
##        elif pos<0:
##            pos=0
##            
##            
##        actions.append(("size: ",stepsize))
##        actions.append(("pos: ",pos))    
##
##        pi.hardware_PWM(PWM_GPIO,PWM_freq,(pos)*10000)
##        
##        ness=sum([item for item in p.map(findsharpness,slicer(Y),1)])
##        actions.append(("ness: ",ness))
##
##    ratio=(ness-sharp)/sharp
##    actions.append(("ratio: ",ratio))
##    Abs=abs(ratio)
##    max_ratio_sum=max_ratio_sum+Abs-init_max_ratio
##    actions.append(("max_ratio_sum: ",max_ratio_sum))
##    max_ratio=max_ratio_sum/steps
##    actions.append(("max_ratio: ",max_ratio))
##    if Abs>=max_ratio:
##        stepsize=1
##    else:
##        stepsize=cmp(ratio,0)*np.ceil(9*(1-Abs/max_ratio))
##
##    
##    direction=direction*cmp(ratio,0)
##    pos=pos+stepsize*direction
##    if pos>100:
##        pos=100
##        
##    elif pos<0:
##        pos=0
##        
##        
##    actions.append(("size: ",stepsize))
##    actions.append(("pos: ",pos))        
##        
##
##
##
##
##    last_duty=pos
##
##
##
##    return actions


##def fine_search(gls):
##    global last_duty
##    m=gls.index(max(gls))
##    #transition(m*5)
##    endpos=m*5-5
##    startpos=m*5+5
##    if endpos<0:
##        endpos=0
##    if startpos>100:
##        startpos=100
##    transition(startpos)
##    #pi.hardware_PWM(PWM_GPIO,PWM_freq,startpos*10000)
##    sleep(1)
##    shots=[]
##    r=range(startpos-1,endpos-1,-2)
##    src=Y
##    for i in r:
##        pi.hardware_PWM(PWM_GPIO,PWM_freq,i*10000)
##        shots.append(findsharpness_prior(src))
##        src=Y
##    shots.append(findsharpness_prior(src))
##    r=[startpos]+r
##    last_duty=i
##    return(r,p.map(findsharpness_post,shots))
