Browse Source

修改命令请求方式:GRPC

zx 1 year ago
parent
commit
97351a8046
7 changed files with 84 additions and 752 deletions
  1. 4 1
      ControllWidget.py
  2. 31 50
      RobotData.py
  3. 1 1
      count.json
  4. 9 0
      message.proto
  5. 34 699
      message_pb2.py
  6. 3 1
      proto.sh
  7. 2 0
      test.py

+ 4 - 1
ControllWidget.py

@@ -8,6 +8,7 @@ from PyQt5.QtGui import QPixmap, QPainter, QResizeEvent, QCloseEvent, QPaintEven
 from PyQt5.QtCore import QSize, QTimer, Qt
 
 import Count
+import message_pb2
 from RobotData import Robot
 import dijkstra.Map as mp
 
@@ -29,13 +30,15 @@ class ControlFrame(QFrame):
         cmdTopic = config['cmdTopic']
         robotColor = config['robotColor']
 
+
         self.all_nodes_ = []
         for node in self.street_nodes_:
             self.all_nodes_.append(node)
         for node in self.space_nodes_:
             self.all_nodes_.append(node)
 
-        self.robot_ = Robot(self.label_)
+        rpc=config['rpc']
+        self.robot_ = Robot(rpc,self.label_)
         self.robot_.connect(id, ip, port, user, pawd)
         self.robot_.SetSubDataTopic(subtopics, self.messageArrived)
         self.robot_.SetCmdTopic(cmdTopic)

+ 31 - 50
RobotData.py

@@ -2,6 +2,8 @@ import copy
 import enum
 import time
 import message_pb2 as message
+import grpc
+import message_pb2_grpc as msg_rpc
 from mqtt_async import MqttAsync
 import google.protobuf.json_format as jtf
 from dijkstra.Map import DijikstraMap, SpaceNode, StreetNode
@@ -35,11 +37,12 @@ class TimeStatu:
 
 class Robot(MqttAsync):
 
-    def __init__(self, name=""):
+    def __init__(self,rpc_ipport,name=""):
         MqttAsync.__init__(self)
         self.timedRobotStatu_ = TimeStatu(message.RobotStatu, 0)
         self.timedNavStatu_ = TimeStatu(message.NavStatu, 0)
         self.dataTopic_ = {}
+        self.rpc_ipport_=rpc_ipport
         self.messageArrivedSignal = None
         self.currentNavData_ = None
         self.navCmdTopic_ = None
@@ -182,13 +185,6 @@ class Robot(MqttAsync):
         self.GeneratePath(begId, targetId, task_type=task_type)
         # self.ExecPathNodes(autoDirect)
         self.ExecNavtask(autoDirect)
-        while self.IsNavigating() == True:
-            '''if self.Connected() == False:
-                print("robot disconnected cancel task")
-                self.CancelNavTask()
-                return False'''
-            time.sleep(0.5)
-        print(" Nav completed!!!")
 
         if self.IsMainModeStatu():
             Count.TestCount().loadCountAdd()
@@ -292,12 +288,16 @@ class Robot(MqttAsync):
             print("Nav cmd is None")
             return False
 
-        count = 3
-        while self.ActionType() == ActType.eReady and count > 0:
-            self.publish(self.navCmdTopic_, jtf.MessageToJson(cmd))
-            count -= 1
-            time.sleep(1)
-        print("send nav cmd completed!!!")
+        channel=grpc.insecure_channel(self.rpc_ipport_)
+        stub = msg_rpc.NavExcutorStub(channel)
+
+        response = stub.Start(cmd)
+        print("client received: ", response)
+        if not response.ret==0:
+            print("nav failed :%s"%(response.info))
+        else:
+            print("nav completed !!!")
+
         return True
 
     def CreateNavPathNodesAction(self, path, autoDirect):
@@ -348,14 +348,9 @@ class Robot(MqttAsync):
         cmd.newActions.add().CopyFrom(action)
 
         print(cmd)
-        published = False
-        while self.IsNavigating() == False:
-            if not self.ActionType() == ActType.eReady:
-                published = True
-            if published == False:
-                self.publish(self.navCmdTopic_, jtf.MessageToJson(cmd))
-            time.sleep(1)
-        print("send nav cmd completed!!!")
+        channel=grpc.insecure_channel(self.rpc_ipport_)
+        stub = msg_rpc.NavExcutorStub(channel)
+        response = stub.Start(cmd)
         return True
 
     '''
@@ -447,17 +442,9 @@ class Robot(MqttAsync):
         if mode == 2:
             action.wheelbase = wheelbase
         cmd.newActions.add().CopyFrom(action)
-        loop = 3
-        while loop > 0:
-            if mode == 2:
-                if self.IsMainModeStatu():
-                    return True
-            if mode == 1:
-                if self.IsMainModeStatu() == False:
-                    return True
-            self.publish(self.navCmdTopic_, jtf.MessageToJson(cmd))
-            loop -= 1
-            time.sleep(0.5)
+        channel=grpc.insecure_channel(self.rpc_ipport_)
+        stub = msg_rpc.NavExcutorStub(channel)
+        response = stub.Start(cmd)
         return False
 
     def IsClampClosed(self):
@@ -504,13 +491,9 @@ class Robot(MqttAsync):
         act = message.NewAction()
         act.type = 6
         cmd.newActions.add().CopyFrom(act)
-        published = False
-        while self.IsClampClosed() == False:
-            if self.ActionType() == ActType.eClampClose:
-                published = True
-            if published == False:
-                self.publish(self.navCmdTopic_, jtf.MessageToJson(cmd))
-            time.sleep(0.5)
+        channel=grpc.insecure_channel(self.rpc_ipport_)
+        stub = msg_rpc.NavExcutorStub(channel)
+        response = stub.Start(cmd)
         return True
 
     def ClampOpen(self):
@@ -524,21 +507,19 @@ class Robot(MqttAsync):
         act = message.NewAction()
         act.type = 7
         cmd.newActions.add().CopyFrom(act)
-        published = False
-        while self.IsClampOpened() == False:
-            if self.ActionType() == ActType.eClampOpen:
-                published = True
-            if published == False:
-                self.publish(self.navCmdTopic_, jtf.MessageToJson(cmd))
-            time.sleep(0.5)
+        channel=grpc.insecure_channel(self.rpc_ipport_)
+        stub = msg_rpc.NavExcutorStub(channel)
+        response = stub.Start(cmd)
         return True
 
     def CancelNavTask(self):
         cmd = message.NavCmd()
         cmd.action = 3
 
-        while self.IsNavigating() == True:
-            self.publish(self.navCmdTopic_, jtf.MessageToJson(cmd))
-            time.sleep(1)
+        channel=grpc.insecure_channel(self.rpc_ipport_)
+        stub = msg_rpc.NavExcutorStub(channel)
+
+        response = stub.Cancel(cmd)
+        print("client received: ", response)
         print(" Cancel task completed!!!")
         return True

+ 1 - 1
count.json

@@ -1 +1 @@
-{"load_count": 531, "single_count": 1254}
+{"load_count": 538, "single_count": 1277}

+ 9 - 0
message.proto

@@ -67,6 +67,15 @@ message NavCmd
   string key=2;
   repeated NewAction newActions=5;
 }
+message NavResponse{
+  int32 ret=1;
+  string info=2;
+}
+
+service NavExcutor{
+  rpc Start(NavCmd) returns(NavResponse){}
+  rpc Cancel(NavCmd) returns(NavResponse){}
+}
 
 message NavStatu
 {

File diff suppressed because it is too large
+ 34 - 699
message_pb2.py


+ 3 - 1
proto.sh

@@ -1,2 +1,4 @@
 
-protoc -I=./ message.proto --python_out=./
+#protoc -I=./ message.proto --python_out=./ --grpc_python_out=./
+
+python -m grpc_tools.protoc -I. --python_out=. --pyi_out=. --grpc_python_out=. message.proto

+ 2 - 0
test.py

@@ -64,6 +64,7 @@ class MainWindow(QMainWindow):
         self.count_frame_=ControllWidget.CountFrame()
         self.LoadMap()
         config1={"label":"AgvMain",
+                 "rpc":"127.0.0.1:9090",
                  "street_nodes":self.ui_nodes["street_nodes"],
                  "space_nodes":self.ui_nodes["space_nodes"],
                  # "mqtt":["pyui1","192.168.0.224",1883,"admin","zx123456"],
@@ -75,6 +76,7 @@ class MainWindow(QMainWindow):
                  "robotColor":[0.7,0.2,0.3]}
 
         config2={"label":"AGV2",
+                 "rpc":"127.0.0.1:9091",
                  "street_nodes":self.ui_nodes["street_nodes"],
                  "space_nodes":self.ui_nodes["space_nodes"],
                  # "mqtt":["pyui2","192.168.0.224",1883,"admin","zx123456"],