最近我接到了一个在raspberrypi2上开发GUI来控制机器的项目。我对qtgui和Qthreads没有太多的经验,所以请耐心等待。在
我使用Qt4和Python开发GUI和底层程序。在
这个程序运行得相当顺利,在我的电脑上没有任何问题。我使用线程,一切正常,直到我用控制机器的RaspberryPi2来尝试它。在
它有一个Qthread,它从摄像机抓取一个帧并用标签显示出来。这可以正常工作,但不能实时更新(更新速度非常慢)。 我有几个按钮控制机器轴的移动,运行在一个Qthread中,还想将另一个Qthread与一个程序结合起来,这个程序对机器有一组特定的指令。在
这一切似乎都在工作,但机器的反应非常慢(有时按下按钮时执行一个命令最多需要5秒钟)。我已经对它进行了调试并追溯到GUI。在
另请注意:当程序运行时,raspberrypi2cpu使用率约为30-35%。。。。所以它不会耗尽CPU。在
有人能告诉我我做错了什么吗。我怀疑它和线程执行和队列有关。在
我附上了以下代码示例:
class IO_Worker(QtCore.QThread):
def MoveLeft(self):
print "MovingLeft"
RPIO.output(4,0) # this is pin 7 the direction switch (lowwest)
RPIO.output(15,0) # this is pin 10 the middle toggle switch
RPIO.output(18,0) # this is pin 12 the top toggle switchdirno=4
time.sleep(.05)
RPIO.output(23,1) # next three lines press blue Go button for x stage
time.sleep(0.2)
RPIO.output(23,0)
def MoveRight(self):
print "MovingRight"
RPIO.output(4,1) # this is pin 7 the direction switch (lowwest)
RPIO.output(15,0) # this is pin 10 the middle toggle switch
RPIO.output(18,0) # this is pin 12 the top toggle switchdirno=4
time.sleep(.05)
RPIO.output(23,1) # next three lines press blue Go button for x stage
time.sleep(0.2)
RPIO.output(23,0)
class frameGrabber(QtCore.QThread):
def __init__(self, cameraID=0):
QtCore.QThread.__init__(self)
self.cameraID = cameraID
def run(self):
cap = cv2.VideoCapture(self.cameraID)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,320)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,180)
while(True):
time.sleep(0.25)
ret, frame = cap.read()
height, width, depth = frame.shape
#print "H:",height,"W:",width
frame = QtGui.QImage(frame.tostring(),width,height, QtGui.QImage.Format_RGB888).rgbSwapped()
self.emit(QtCore.SIGNAL('webcam_frame(QImage)'), frame)
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
## Initialise IO (Raspberry Pi GPIO)
self.InitIO(self.ui)
## SetupSignals
self.SetupSignals()
class InitIO(QtCore.QThread):
def __init__(self,ui):
print "IO Initialised"
RPIO.setmode(RPIO.BCM)
RPIO.cleanup()
RPIO.setup(21,RPIO.OUT,initial=1)
RPIO.setup(20,RPIO.OUT,initial=0) # LSB
RPIO.setup(16,RPIO.OUT,initial=0)
RPIO.setup(12,RPIO.OUT,initial=0) #
RPIO.setup(7,RPIO.OUT,initial=0) # MSB
RPIO.setup(26,RPIO.IN,pull_up_down=RPIO.PUD_UP)
def RightAxisMovement(self):
print "Moving Up"
IO_WorkerThread = IO_Worker()
IO_WorkerThread.MoveRight()
def LeftAxisMovement(self):
IO_WorkerThread = IO_Worker()
IO_WorkerThread.MoveLeft()
def ProcessCamFrame(self,frame):
#print "Frame update", frame
pix = QtGui.QPixmap(frame)
pix = pix.scaled(self.ui.Video_Out.size())
self.ui.Video_Out.setPixmap(pix)
def SetupSignals(self):
##############################################################
## Start Camera Thread
##############################################################
camWorker = frameGrabber()
camWorker.start()
QtCore.QObject.connect(camWorker,QtCore.SIGNAL("webcam_frame(QImage)"),self.ProcessCamFrame)
IO_WorkerThread = IO_Worker()
IO_WorkerThread.start()
##############################################################
## Listen to Movement Button Class
##############################################################
self.ui.pButton_Left.clicked.connect(self.LeftAxisMovement)
self.ui.pButton_Right.clicked.connect(self.RightAxisMovement)
##---------------------------------------------------------------------
if __name__== "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
目前没有回答
相关问题 更多 >
编程相关推荐