Форум сайта python.su
Привет всем!
Объясните, пожалуйста, в какую сторону копать.
Написал на PyQt5 простенький джойстик управления “вперед-стоп-назад”:
from PyQt5.QtWidgets import QWidget from PyQt5.Qt import * import sys from enum import Enum class Direction(Enum): Up = 0 Down = 1 class Joystick(QWidget): def __init__(self, parent=None): super(Joystick, self).__init__(parent) self.setMinimumSize(1200, 600) self.movingOffset = QPointF(0, 0) self.grabCenter = False self.__maxDistance = 50 def paintEvent(self, event): painter = QPainter(self) bounds = QRectF(-self.__maxDistance, -self.__maxDistance, self.__maxDistance * 2, self.__maxDistance * 2).translated(self._center()) painter.drawEllipse(bounds) painter.setBrush(Qt.black) painter.drawEllipse(self._centerEllipse()) def _centerEllipse(self): if self.grabCenter: return QRectF(-20, -20, 40, 40).translated(self.movingOffset) return QRectF(-20, -20, 40, 40).translated(self._center()) def _center(self): return QPointF(self.width()/2, self.height()/2) def _boundJoystick(self, point): limitLine = QLineF(self._center(), point) if (limitLine.length() > self.__maxDistance): limitLine.setLength(self.__maxDistance) return limitLine.p2() def joystickDirection(self): if not self.grabCenter: return 0 normVector = QLineF(self._center(), self.movingOffset) currentDistance = normVector.length() angle = normVector.angle() distance = min(currentDistance / self.__maxDistance, 1.0) if 45 <= angle < 135: return (Direction.Up, distance) if 225 <= angle < 315: return (Direction.Down, distance) def mousePressEvent(self, ev): self.grabCenter = self._centerEllipse().contains(ev.pos()) return super().mousePressEvent(ev) def mouseReleaseEvent(self, event): self.grabCenter = False self.movingOffset = QPointF(0, 0) self.update() def mouseMoveEvent(self, event): if self.grabCenter: print("Moving") self.movingOffset = self._boundJoystick(event.pos()) self.update() print(self.joystickDirection()) if __name__ == '__main__': app = QApplication([]) app.setStyle(QStyleFactory.create("Cleanlooks")) mw = QMainWindow() mw.setWindowTitle('Joystick') cw = QWidget() ml = QGridLayout() cw.setLayout(ml) mw.setCentralWidget(cw) joystick = Joystick() ml.addWidget(joystick,0,0) mw.show() if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): QApplication.instance().exec_()
import device from device.enums import * import time dev = device.find_any() try: for i in range(1): dev.axis0.requested_state = AxisState.CLOSED_LOOP_CONTROL time.sleep(1) dev.axis0.controller.input_vel = 15 time.sleep(5) dev.axis0.controller.input_vel = 0 time.sleep(1) dev.axis0.requested_state = AxisState.IDLE time.sleep(1)
Отредактировано pyqter (Май 1, 2024 00:51:16)
Офлайн