我有一个部署,读取传感器的变量Arduino
,将从变量中获得的值,需要更新QWT widget
的值。
从未使用过Signal and Slots,然后无法实现解决方案。
可以帮助我吗?罗盘小部件的这个概念的形成是使用行words[2]
中的变量print "compass = "+ words[2]
的值更新的。 [sensor.py]
小部件必须更新以下代码,并以这种方式设置值:compass.setValue(35.0)
。[compass.py]这里,静态;
当两个不同的文件时,如何在words[2]
的值中连接变量compass
的值。
对不起我的英语。 的 compass.py
from PyQt4 import Qt, QtGui, QtCore
import PyQt4.Qwt5 as Qwt
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
def enumList(enum, sentinel):
return [enum(i) for i in range(sentinel)]
colorGroupList = enumList(
Qt.QPalette.ColorGroup, Qt.QPalette.NColorGroups)
colorRoleList = enumList(
Qt.QPalette.ColorRole, Qt.QPalette.NColorRoles)
handList = enumList(
Qwt.QwtAnalogClock.Hand, Qwt.QwtAnalogClock.NHands)
class Ui_Form(Qt.QFrame):
def setupUi(self, Form ):
compass = Qwt.QwtCompass()
palette = compass.palette()
#Cor Interna Bussola
palette.setColor(compass.backgroundRole(), Qt.Qt.darkCyan)
compass.setPalette(palette)
palette = Qt.QPalette()
for colorRole in colorRoleList:
palette.setColor(colorRole, Qt.QColor())
palette.setColor(
Qt.QPalette.Base,
#Alterando uma pouco a cor, mais forte ou mais fraco, depende do valor
compass.palette().color(compass.backgroundRole()).light(105))
palette.setColor(
#Cor de fundo do corculo da agulha
Qt.QPalette.Foreground,
palette.color(Qt.QPalette.Base))
compass.setLineWidth(2)
compass.setFrameShadow(Qwt.QwtCompass.Raised)
compass.setScaleTicks(0, 0, 2)
compass.setScaleOptions( Qwt.QwtDial.ScaleTicks | Qwt.QwtDial.ScaleLabel |
Qwt.QwtDial.ScaleBackbone)
compass.setLabelMap({0.0: "N",
45.0: "ne",
90.0: "E",
135.0: "se",
180.0: "S",
225.0: "so",
270.0: "O",
315.0: "no"})
compass.setScale(72,0, 0)
compass.setNeedle(Qwt.QwtCompassMagnetNeedle(
Qwt.QwtCompassMagnetNeedle.TriangleStyle,
Qt.Qt.white,
Qt.Qt.red))
compass.setValue(35.0)
newPalette = compass.palette()
for colorRole in colorRoleList:
if palette.color(colorRole).isValid():
for colorGroup in colorGroupList:
newPalette.setColor(
colorGroup, colorRole, palette.color(colorRole))
compass.setPalette(newPalette)
return compass
class WidgetBussola(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent=None)
self.ui = Ui_Form()
self.ui.setupUi(self)
palette = self.palette()
#Cor Interna Bussola
palette.setColor(self.backgroundRole(), Qt.Qt.darkCyan)
self.setPalette(palette)
#Posicionamento do Widget
layout = Qt.QGridLayout(self)
layout.addWidget(self.ui.setupUi(1),1,1)
layout.setColumnStretch(1,1)
if __name__ == '__main__':
import sys
# BUSSOLA
app = QtGui.QApplication(sys.argv)
bussola = WidgetBussola()
bussola.show()
sys.exit(app.exec_())
sensor.py
from visual import *
import serial
import string
import math
from time import time
grad2rad = 3.141592/180.0
# Check your COM port and baud rate
ser = serial.Serial(port='COM3',baudrate=115200, timeout=1)
# Main scene
scene=display(title="9DOF Razor IMU test")
scene.range=(1.2,1.2,1.2)
#scene.forward = (0,-1,-0.25)
scene.forward = (1,0,-0.25)
scene.up=(0,0,1)
# Second scene (Roll, Pitch, Yaw)
scene2 = display(title='9DOF Razor IMU test',x=0, y=0, width=500, height=200,center=(0,0,0), background=(0,0,0))
scene2.range=(1,1,1)
scene.width=500
scene.y=200
scene2.select()
#Roll, Pitch, Yaw
cil_roll = cylinder(pos=(-0.4,0,0),axis=(0.2,0,0),radius=0.01,color=color.red)
cil_roll2 = cylinder(pos=(-0.4,0,0),axis=(-0.2,0,0),radius=0.01,color=color.red)
cil_pitch = cylinder(pos=(0.1,0,0),axis=(0.2,0,0),radius=0.01,color=color.green)
cil_pitch2 = cylinder(pos=(0.1,0,0),axis=(-0.2,0,0),radius=0.01,color=color.green)
#cil_course = cylinder(pos=(0.6,0,0),axis=(0.2,0,0),radius=0.01,color=color.blue)
#cil_course2 = cylinder(pos=(0.6,0,0),axis=(-0.2,0,0),radius=0.01,color=color.blue)
arrow_course = arrow(pos=(0.6,0,0),color=color.cyan,axis=(-0.2,0,0), shaftwidth=0.02, fixedwidth=1)
#Roll,Pitch,Yaw labels
label(pos=(-0.4,0.3,0),text="Roll",box=0,opacity=0)
label(pos=(0.1,0.3,0),text="Pitch",box=0,opacity=0)
label(pos=(0.55,0.3,0),text="Yaw",box=0,opacity=0)
label(pos=(0.6,0.22,0),text="N",box=0,opacity=0,color=color.yellow)
label(pos=(0.6,-0.22,0),text="S",box=0,opacity=0,color=color.yellow)
label(pos=(0.38,0,0),text="W",box=0,opacity=0,color=color.yellow)
label(pos=(0.82,0,0),text="E",box=0,opacity=0,color=color.yellow)
label(pos=(0.75,0.15,0),height=7,text="NE",box=0,color=color.yellow)
label(pos=(0.45,0.15,0),height=7,text="NW",box=0,color=color.yellow)
label(pos=(0.75,-0.15,0),height=7,text="SE",box=0,color=color.yellow)
label(pos=(0.45,-0.15,0),height=7,text="SW",box=0,color=color.yellow)
L1 = label(pos=(-0.4,0.22,0),text="-",box=0,opacity=0)
L2 = label(pos=(0.1,0.22,0),text="-",box=0,opacity=0)
L3 = label(pos=(0.7,0.3,0),text="-",box=0,opacity=0)
# Main scene objects
scene.select()
# Reference axis (x,y,z)
arrow(color=color.green,axis=(1,0,0), shaftwidth=0.02, fixedwidth=1)
arrow(color=color.green,axis=(0,-1,0), shaftwidth=0.02 , fixedwidth=1)
arrow(color=color.green,axis=(0,0,-1), shaftwidth=0.02, fixedwidth=1)
# labels
label(pos=(0,0,0.8),text="9DOF Razor IMU test",box=0,opacity=0)
label(pos=(1,0,0),text="X",box=0,opacity=0)
label(pos=(0,-1,0),text="Y",box=0,opacity=0)
label(pos=(0,0,-1),text="Z",box=0,opacity=0)
# IMU object
platform = box(length=1, height=0.05, width=1, color=color.red)
p_line = box(length=1,height=0.08,width=0.1,color=color.yellow)
plat_arrow = arrow(color=color.green,axis=(1,0,0), shaftwidth=0.06, fixedwidth=1)
f = open("Serial"+str(time())+".txt", 'w')
roll=0
pitch=0
yaw=0
while 1:
line = ser.readline()
line = line.replace("!ANG:","") # Delete "!ANG:"
f.write(line) # Write to the output log file
words = string.split(line,",") # Fields split
if len(words) > 2:
try:
roll = float(words[0])*grad2rad
pitch = float(words[1])*grad2rad
yaw = float(words[2])*grad2rad
print "Bussula=" + words[2]
except:
print "Invalid line"
axis=(cos(pitch)*cos(yaw),-cos(pitch)*sin(yaw),sin(pitch))
up=(sin(roll)*sin(yaw)+cos(roll)*sin(pitch)*cos(yaw),sin(roll)*cos(yaw)-cos(roll)*sin(pitch)*sin(yaw),-cos(roll)*cos(pitch))
platform.axis=axis
platform.up=up
platform.length=1.0
platform.width=0.65
plat_arrow.axis=axis
plat_arrow.up=up
plat_arrow.length=0.8
p_line.axis=axis
p_line.up=up
cil_roll.axis=(0.2*cos(roll),0.2*sin(roll),0)
cil_roll2.axis=(-0.2*cos(roll),-0.2*sin(roll),0)
cil_pitch.axis=(0.2*cos(pitch),0.2*sin(pitch),0)
cil_pitch2.axis=(-0.2*cos(pitch),-0.2*sin(pitch),0)
arrow_course.axis=(0.2*sin(yaw),0.2*cos(yaw),0)
L1.text = str(float(words[0]))
L2.text = str(float(words[1]))
L3.text = str(float(words[2]))
ser.close
f.close
感谢您的帮助。