Linked Questions

Popular Questions

I am simulating a universal robot arm in the universal robot simulation software known as polyscope and connecting the simulated arm using websockets by creating an external C#server for sending position commands to the arm in the simulator software(Polyscope) to move to a new position.

I wanted to ask is it possible to retrieve the position values from the robot simulation software and display it in unity. Do I need to write a C# script in unity to get the data from the universal robot simulation client or can I integrate the server I have created directly to unity? Or is it not possible?

I am sharing the code which is from the universal robot. Can anyone please let me know retrieve the values from the client so that when I play the scene in unity, the position values are displayed?

The server code from the universal robot is as follows :

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();
          }
        }
      }
     }
    }
   }
  }

The client code in Universal robot polyscope is as follows

  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   

Related Questions