0

我试图在绘制传入数据的同时连续运行一个控制步进电机的功能。我可以运行该函数,但是当我尝试通过键盘中断来停止它时,线程无法识别它。

这是代码:

              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()
4

1 回答 1

0

没时间了!

我相信您的线程实例不会听的原因interrupt是因为它非常忙于运行该forward()方法!

试试看是否追加:

import time #somewhere in your code
time.sleep(0)

在你的方法结束时,会让线程听!“睡眠”零秒等于检查信号。

阅读更多关于它的信息

于 2013-08-20T06:16:36.520 回答