我试图在绘制传入数据的同时连续运行一个控制步进电机的功能。我可以运行该函数,但是当我尝试通过键盘中断来停止它时,线程无法识别它。
这是代码:
def forward():
global delay
while True:
try:
cor = (-2.3*readadc(cor_adc)) + 1841
target = readadc(target_adc)
deltaP = target - cor
deltaP_list.append(deltaP)
if math.fabs((deltaP_list[-1] - deltaP_list[-2])) >= 10:
delay=(-.125628140704*math.fabs(deltaP)) + 30.1256281407
if delay < 5:
delay=5
print "delay", delay
print "diff", math.fabs((deltaP_list[-1] - deltaP_list[-2]))
print "Delta P", math.fabs(deltaP)
for i in range(0, int(50)):
setStep(1, 0, 0, 0)
time.sleep(int(delay) / 1000.0)
setStep(1, 1, 0, 0)
time.sleep(int(delay) / 1000.0)
setStep(0, 1, 0, 0)
time.sleep(int(delay) / 1000.0)
setStep(0, 1, 1, 0)
time.sleep(int(delay) / 1000.0)
setStep(0, 0, 1, 0)
time.sleep(int(delay) / 1000.0)
setStep(0, 0, 1, 1)
time.sleep(int(delay) / 1000.0)
setStep(0, 0, 0, 1)
time.sleep(int(delay) / 1000.0)
setStep(1, 0, 0, 1)
time.sleep(int(delay) / 1000.0)
except KeyboardInterrupt:
print('end')
break
Thread(target=forward).start()
xAchse=pylab.arange(0,50,1)
yAchse=pylab.array([0]*50)
fig = pylab.figure(1)
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_title("Linear Algorithm")
ax.set_xlabel("Time")
ax.set_ylabel("mmHg")
ax.axis([0,50,0,300])
line1=ax.plot(xAchse,yAchse,'-')
line2=ax.plot(xAchse,yAchse,'-')
manager = pylab.get_current_fig_manager()
def build_plot(arg):
cor = (-2.3*readadc(cor_adc)) + 1841
target = readadc(target_adc)
cor_list.append(cor)
target_list.append(target)
def RealtimePloter(arg):
CurrentXAxis=pylab.arange(0,50,1)
line1[0].set_data(CurrentXAxis,pylab.array(cor_list[-50:]))
line2[0].set_data(CurrentXAxis,pylab.array(target_list[-50:]))
ax.axis([CurrentXAxis.min(),CurrentXAxis.max(),0, 300])
manager.canvas.draw()
#manager.show()
timer = fig.canvas.new_timer(interval=1)
timer.add_callback(RealtimePloter, ())
timer2 = fig.canvas.new_timer(interval=1)
timer2.add_callback(build_plot, ())
timer.start()
timer2.start()
pylab.show()