123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # ===============================================================================
- #
- # test.py
- #
- # Test program for the simple GL Viewer.
- #
- # Copyright (c) 2011, Arne Schmitz <arne.schmitz@gmx.net>
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are met:
- # * Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # * Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- # * Neither the name of the <organization> nor the
- # names of its contributors may be used to endorse or promote products
- # derived from this software without specific prior written permission.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #
- # ===============================================================================
- import math
- import time
- import sys
- from PyQt5.QtGui import *
- from PyQt5.QtWidgets import *
- from PyQt5.QtCore import *
- from MapGLWidget import MapGLWidget
- import json
- import dijkstra.Map as mp
- import ControllWidget
- import JointContrallerWidget
- import ManualOperationWidget
- import RobotData as RD
- import message_pb2 as message
- import google.protobuf.json_format as jtf
- import uuid
- from custom_define import RobotName
- # ===============================================================================
- class MainWindow(QMainWindow):
- """docstring for Mainwindow"""
- djks_map_ = mp.DijikstraMap()
- ui_nodes = {}
- ui_nodes["street_nodes"] = []
- ui_nodes["space_nodes"] = []
- def __init__(self, parent=None):
- super(MainWindow, self).__init__(parent)
- self.basic()
- self.count_frame_ = ControllWidget.CountFrame()
- self.LoadMap()
- self.isLocalTest = True
- self.isAutoDispatch = False
- rpc_1 = "192.168.0.70:9090"
- mqtt_1 = ["pyui1", "192.168.0.70", 1883, "admin", "zx123456"]
- rpc_2 = "192.168.0.71:9090"
- mqtt_2 = ["pyui2", "192.168.0.71", 1883, "admin", "zx123456"]
- if self.isLocalTest:
- rpc_1 = "127.0.0.1:9090"
- mqtt_1 = ["pyui-main", "127.0.0.1", 1883, "admin", "zx123456"]
- rpc_2 = "127.0.0.1:9091"
- mqtt_2 = ["pyui-child", "127.0.0.1", 1883, "admin", "zx123456"]
- config1 = {"label": RobotName.strAGVMain,
- "rpc": rpc_1,
- "street_nodes": self.ui_nodes["street_nodes"],
- "space_nodes": self.ui_nodes["space_nodes"],
- "mqtt": mqtt_1,
- "subTopics": {"agv_main/agv_statu": message.RobotStatu.__name__,
- "agv_main/nav_statu": message.NavStatu.__name__},
- "cmdTopic": "agv_main/nav_cmd",
- "robotColor": [0.7, 0.2, 0.3]}
- config2 = {"label": RobotName.strAGV2,
- "rpc": rpc_2,
- # "rpc": "127.0.0.1:9091",
- "street_nodes": self.ui_nodes["street_nodes"],
- "space_nodes": self.ui_nodes["space_nodes"],
- "mqtt": mqtt_2,
- "subTopics": {"agv_child/agv_statu": message.RobotStatu.__name__,
- "agv_child/nav_statu": message.NavStatu.__name__},
- "cmdTopic": "agv_child/nav_cmd",
- "robotColor": [0.4, 0.2, 0.8]}
- self.Controller1 = ControllWidget.ControlFrame(config1)
- self.Controller2 = ControllWidget.ControlFrame(config2)
- robot_dict = {self.Controller1.robot_.name_: self.Controller1.robot_,
- self.Controller2.robot_.name_: self.Controller2.robot_}
- self.ManualOperationWidget = ManualOperationWidget.ManualOperationWidget(robot_dict)
- splitter_main = self.split_()
- self.setCentralWidget(splitter_main)
- self.gl.SetRobot1Instance(self.Controller1.robot_)
- self.gl.SetRobot2Instance(self.Controller2.robot_)
- self.timer_ = QTimer()
- self.timer_.timeout.connect(self.Update)
- self.timer_.start(100)
- def LoadMap(self):
- self.gl = MapGLWidget() # 将opengl例子嵌入GUI
- map = self.load_map("./map.json")
- for node in map['street_nodes'].items():
- [id, point] = node
- street_node = mp.StreetNode(id, point[0], point[1])
- self.djks_map_.AddVertex(street_node)
- self.gl.AddNode([id, "street_node", point])
- self.ui_nodes["street_nodes"].append(id)
- for node in map['space_nodes'].items():
- [id, point] = node
- [x, y, yaw] = point
- space_node = mp.SpaceNode(id, point[0], point[1], yaw)
- self.djks_map_.AddVertex(space_node)
- glNode = [id, "space_node", [x, y]]
- self.gl.AddNode(glNode)
- self.ui_nodes["space_nodes"].append(id)
- for road in map['roads'].items():
- self.gl.AddRoad(road)
- [_, points] = road
- for pt1 in points:
- for pt2 in points:
- if not pt1 == pt2:
- self.djks_map_.AddEdge(pt1, pt2)
- def load_map(self, file):
- with open(file, 'r', encoding='utf-8') as fp:
- map = json.load(fp)
- return map
- def Update(self):
- self.gl.update()
- if self.isAutoDispatch:
- self.Controller1.setVisible(False)
- self.Controller2.setVisible(False)
- else:
- self.Controller1.setVisible(True)
- self.Controller2.setVisible(True)
- # 窗口基础属性
- def basic(self):
- # 设置标题,大小,图标
- self.setWindowTitle("GT")
- self.resize(1100, 650)
- self.setWindowIcon(QIcon("./image/Gt.png"))
- # 居中显示
- screen = QDesktopWidget().geometry()
- self_size = self.geometry()
- self.move(int((screen.width() - self_size.width()) / 2), int((screen.height() - self_size.height()) / 2))
- def closeEvent(self, QCloseEvent):
- self.gl.close()
- # 分割窗口
- def split_(self):
- splitter_main = QSplitter(Qt.Horizontal)
- splitter = QSplitter(Qt.Vertical)
- splitter.addWidget(self.count_frame_)
- splitter.addWidget(self.Controller1)
- splitter.addWidget(self.Controller2)
- splitter.addWidget(self.ManualOperationWidget)
- splitter.setStretchFactor(0, 1)
- splitter.setStretchFactor(1, 3)
- splitter.setStretchFactor(2, 3)
- splitter.setStretchFactor(3, 1)
- splitter_main.addWidget(splitter)
- splitter_main.addWidget(self.gl)
- splitter_main.setStretchFactor(0, 1)
- splitter_main.setStretchFactor(2, 4)
- return splitter_main
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- win = MainWindow()
- win.show()
- sys.exit(app.exec_())
- # ===============================================================================
- # Main
- # ===============================================================================
- '''app = QApplication(sys.argv)
- mainWindow = MapGLWidget()
- mainWindow.show()
- mainWindow.raise_() # Need this at least on OS X, otherwise the window ends up in background
- sys.exit(app.exec_())'''
- # ===============================================================================
- #
- # Local Variables:
- # mode: Python
- # indent-tabs-mode: nil
- # End:
- #
- # ===============================================================================
|