python - Update variable from sensor in Widget with Signal and Slot -
i have deployment reading of variable of sensor arduino
, value obtained in variable , need update value of qwt widget
.
never worked signal , slots, unable implement solution.
could help me?, formation of concept compass widget updated value of variable words[2]
in line print "compass = "+ words[2]
. [sensor.py]
the widget has updated next code, , value set in way: compass.setvalue(35.0)
.[compass.py] here, statically;
how connect value of variable words[2]
, in value of compass
, when 2 different files.
excuse me english. compass.py
from pyqt4 import qt, qtgui, qtcore import pyqt4.qwt5 qwt try: _fromutf8 = qtcore.qstring.fromutf8 except attributeerror: def _fromutf8(s): homecoming s try: _encoding = qtgui.qapplication.unicodeutf8 def _translate(context, text, disambig): homecoming qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror: def _translate(context, text, disambig): homecoming qtgui.qapplication.translate(context, text, disambig) def enumlist(enum, sentinel): homecoming [enum(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() colorrole in colorrolelist: palette.setcolor(colorrole, qt.qcolor()) palette.setcolor( qt.qpalette.base, #alterando uma pouco cor, mais forte ou mais fraco, depende valor compass.palette().color(compass.backgroundrole()).light(105)) palette.setcolor( #cor de fundo 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() colorrole in colorrolelist: if palette.color(colorrole).isvalid(): colorgroup in colorgrouplist: newpalette.setcolor( colorgroup, colorrole, palette.color(colorrole)) compass.setpalette(newpalette) homecoming 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 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 time import time grad2rad = 3.141592/180.0 # check com port , 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) # sec 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 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
thanks help.
as far have seen, can achieved through threading. in nutshell, gui part (pyqt) runs main thread, while processing part (the 1 receives values arduino) runs in thread.
take @ page, paul's posted code. can implement similar. pyqt , threads
python arduino pyqt signals signals-slots
No comments:
Post a Comment