我正在使用add_attribute_listener从无人机获取数据,并想根据这些值更新我的小部件Qpainter。 我在这里附上我的代码:
import sys
import math
from dronekit import connect, VehicleMode, LocationGlobalRelative
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import DC_gimbal as backend #it is the drone connection script
from datetime import date
import time
def counterrotate(ox,oy,px,py,roll):
angle=math.radians(roll)
#counterclockwise
qx= ox+ math.cos(angle)*(px-ox)-math.sin(angle)*(py-oy)
qy= oy+ math.sin(angle)*(px-ox)+math.cos(angle)*(py-oy)
return qx,qy
def rotate(ox,oy,px,py,roll):
angle=math.radians(roll)
#clockwise
qx= ox+ math.cos(angle)*(px-ox)+math.sin(angle)*(py-oy)
qy= oy+ math.sin(angle)*(px-ox)+math.cos(angle)*(py-oy)
return qx,qy
def line_cord(x0,y0,x1,y1,ox,oy,pitch,roll):
x0,y0 = rotate(ox,oy,x0,y0, roll*5)
y0 -= pitch*20
x1, y1 = counterrotate(ox,oy,x1,y1, roll*5)
y1 -= pitch*20
return x0,y0,x1,y1
class Manager(QObject):
changedValue = pyqtSignal(tuple)
def __init__(self):
QObject.__init__(self)
filename = "Attitude_data_Manual_1232018_1158.txt" # sample file
res = self.read_content(filename)
self.results = zip(*res)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_value)
self.timer.start(50)
@pyqtSlot()
def update_value(self):
try:
self.changedValue.emit(next(self.results))
except StopIteration:
self.timer.stop()
def read_content(self, filename):
pitch = []
roll = []
yaw = []
with open(filename,'r') as f :
lines = f.readlines()[4:]
for s in lines:
a = s.split()
if len(a) !=14:
print("point reached")
continue
pitch.append(float(a[4]))
roll.append(float(a[6]))
yaw.append(float(a[8]))
return pitch, roll, yaw
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.v = None
@pyqtSlot(tuple)
def update_value(self, v):
self.v = v
self.update()
def paintEvent(self, event):
QWidget.paintEvent(self, event)
painter = QPainter(self)
r = self.rect()
font = QFont(self.font())
font.setPixelSize(15)
metrics = QFontMetricsF(font)
painter.setFont(font)
painter.setPen(self.palette().color(QPalette.Shadow))
if self.v:
xc=r.center().x()
yc= r.center().y()
pitch, roll, yaw = self.v
x0,y0,x1,y1 = line_cord(r.left()-200, r.center().y(), r.right()+200, r.center().y(),r.center().x(),r.center().y(),pitch,roll)
posx0 = QPoint(x0,y0)
posx1 = QPoint(x1, y1)
upper_polygon = QPolygonF([r.topLeft(), posx0, posx1, r.topRight()])
bottom_polygon = QPolygonF([posx0, posx1, r.bottomRight(), r.bottomLeft()])
painter.setBrush(QColor("skyblue"))
painter.drawPolygon(upper_polygon)
painter.setBrush(QColor("green"))
painter.drawPolygon(bottom_polygon)
painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y())
if __name__ == '__main__':
app = QApplication(sys.argv)
main()
backend.vehicle_initialise() # here vehicle object is initiated
manager = Manager()
huds_Up()
w = Widget()
manager.changedValue.connect(w.update_value)
w.show()
sys.exit(app.exec_())
当前,我正在处理示例文件。我正在从文件中获取俯仰,侧倾和偏航值。我正在使用该俯仰和滚动值来显示用户界面的更改。我想对无人机的实时值做同样的事情。 我了解了侦听器,但是在回调函数中我不能使用painter对象,因为它不能被paintevent继承。这是指向示例数据https://drive.google.com/file/d/10d65C_rLYKinf6wgB3C55T5xENj58y_l/view
的链接我想对通过以下方法获得的无人机值做同样的事情:
last_attitude_cache = None
def attitude_callback(self, attr_name, value):
# `attr_name` - the observed attribute (used if callback is used for multiple attributes)
# `self` - the associated vehicle object (used if a callback is different for multiple vehicles)
# `value` is the updated attribute value.
global last_attitude_cache
# Only publish when value changes
if value!=last_attitude_cache:
print " CALLBACK: Attitude changed to", value
last_attitude_cache=value
print "\nAdd `attitude` attribute callback/observer on `vehicle`"
vehicle.add_attribute_listener('attitude', attitude_callback)
这里的值是对象,它使我更改了无人机的实时值。其中value.pitch是无人机面的上下运动,value.roll是弧度,表示无人机倾斜了多少,而value.yaw是无人机的指南针值。如何在paintevent中使用这些无人机值来更改我的绘图?
由于我是与Qpainter UI集成的新型dronekit集成,因此提供的任何帮助都将受到高度赞赏。