我可以从充当客户端的通用机器人仿真软件(Polyscope)接收数据并将其显示在Unity中吗?

时间:2019-02-03 07:35:30

标签: c# unity3d client-server

我在称为polyscope的通用机器人仿真软件中仿真通用机器人手臂,并通过创建外部C#服务器以将位置命令发送到仿真器软件(Polyscope)中的手臂的外部C#server,使用websocket连接仿真手臂。一个新职位。

我想问问是否可以从机器人仿真软件中检索位置值并统一显示。我是否需要统一编写C#脚本以从通用机器人仿真客户端获取数据,还是可以将直接创建的服务器集成为统一服务器?还是不可能?

我正在共享通用机器人的代码。谁能让我知道从客户端检索值,以便当我统一播放场景时显示位置值吗?

通用机器人的服务器代码如下:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace URServer
{
class Program
{

static void Main(string[] args)
{

  // The IP address of the server (the PC on which this program is running)
  string sHostIpAddress = "127.0.0.1";
  // Standard port number
  int nPort = 21;

  // The following names are used in the PolyScope script for refencing the
  // three working points:
  // Name of an arbitrary work point 1
  const string csMsgPoint1 = "Point_1";
  // Name of an arbitrary work point 2
  const string csMsgPoint2 = "Point_2";
  // Name of an arbitrary work point 3
  const string csMsgPoint3 = "Point_3";

  Console.WriteLine("Opening IP Address: " + sHostIpAddress);
  IPAddress ipAddress = IPAddress.Parse(sHostIpAddress);        // Create the IP address
  Console.WriteLine("Starting to listen on port: " + nPort);
  TcpListener tcpListener = new TcpListener(ipAddress, nPort);  // Create the tcp Listener
  tcpListener.Start();                                          // Start listening

  // Keep on listening forever
  while (true)
  {
    TcpClient tcpClient = tcpListener.AcceptTcpClient();        // Accept the client
    Console.WriteLine("Accepted new client");
    NetworkStream stream = tcpClient.GetStream();               // Open the network stream
    while (tcpClient.Client.Connected)
    {
      // Create a byte array for the available bytes
      byte[] arrayBytesRequest = new byte[tcpClient.Available];
      // Read the bytes from the stream
      int nRead = stream.Read(arrayBytesRequest, 0, arrayBytesRequest.Length);
      if (nRead > 0)
      {
        // Convert the byte array into a string
        string sMsgRequest = ASCIIEncoding.ASCII.GetString(arrayBytesRequest);
        Console.WriteLine("Received message request: " + sMsgRequest);
        string sMsgAnswer = string.Empty;

        // Check which workpoint is requested
        if (sMsgRequest.Substring (0,7).Equals(csMsgPoint1))
        {
          // Some point in space for work point 1
          sMsgAnswer = "(0.4, 0, 0.5, 0, -3.14159, 0)";
        }
        else if (sMsgRequest.Substring(0, 7).Equals(csMsgPoint2))
        {
          // Some point in space for work point 2
          sMsgAnswer = "(0.3, 0.5, 0.5, 0, 3.14159, 0)";;
        }
        else if (sMsgRequest.Substring(0, 7).Equals(csMsgPoint3))
        {
          // Some point in space for work point 3
          sMsgAnswer = "(0, 0.6, 0.5, 0, 3.14159, 0)";
        }

        if (sMsgAnswer.Length > 0)
        {
          Console.WriteLine("Sending message answer: " + sMsgAnswer);
          // Convert the point into a byte array
          byte[] arrayBytesAnswer = ASCIIEncoding.ASCII.GetBytes(sMsgAnswer+'\n');
          // Send the byte array to the client
          stream.Write(arrayBytesAnswer, 0, arrayBytesAnswer.Length);
        }
      }
      else
      {
        if (tcpClient.Available == 0)
        {
          Console.WriteLine("Client closed the connection.");
          // No bytes read, and no bytes available, the client is closed.
          stream.Close();
          }
        }
      }
     }
    }
   }
  }

Universal机器人polyscope中的客户端代码如下

  Program
  BeforeStart
 open≔socket_open("127.0.0.1",21)
 Loop open≟ False
 open≔socket_open("127.0.0.1",21)
 targetPos≔p[0,0,0,0,0,0]
 counter≔0
 Robot Program
 sendToServer≔'send to server'
 socket_send_string(sendToServer)
 receiveFromServ≔socket_read_ascii_float(6)
 Loop receiveFromServ[0]≠6
 Wait: 0.3
 receiveFromServ≔socket_read_ascii_float(6)
 Loop counter<6
 targetPos[counter]=receiveFromServ[counter+1]
 counter≔counter+1
 MoveJ
 targetPos
 counter:=0   

1 个答案:

答案 0 :(得分:0)

UnderAutomation SDK具有.NET Standard版本,您可以放入Unity资产。然后,您可以使用以下代码连接到机器人并获得位置:

https://underautomation.com

private UR ur;

    void Start() {
      ur = new UR(); // Create a new UR instance
      ur.Connect("192.168.0.1"); // Connect to the robot
      // ...

      // Direct access to last received package
      JointDataPackageEventArgs _lastJointData = ur.JointData;

      // Attach a delegate to the event triggered when new package comes 
      ur.JointDataReceived += Ur_JointDataReceived;
    }

    private void Ur_JointDataReceived(object sender, JointDataPackageEventArgs e) {
      // e contains the incoming package

      // for example :
      var elbowPose = e.Elbow.Position;
    }

您还可以从Unity发送URScript:

// Remote execute a movej
ur.Send("movej([-1.5,-1.5,-2,-0.5,1.8,0],a=1.4, v=1.05, t=0, r=0)");

// Set digital output 2 to true
ur.Send(“set_digital_out(7,True)”);