入力データをプロットしながら、同時にステッピング モーターを制御する機能を継続的に実行しようとしています。関数を実行できますが、キーボード割り込みで停止しようとすると、スレッドが認識しません。
コードは次のとおりです。
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()