Skip to content

Commit 28af060

Browse files
Friday update
1 parent b98cd24 commit 28af060

File tree

7 files changed

+202
-301
lines changed

7 files changed

+202
-301
lines changed

Manus Interfacing/Manus_interpreter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ public Manus_hand_obj get_hand(device_type_t side)
254254
}
255255
}
256256

257+
public Manus_hand_obj get_hand(int side)
258+
{
259+
if (side == 0)
260+
{
261+
return hands[0];
262+
}
263+
else
264+
{
265+
return hands[1];
266+
}
267+
}
268+
257269
//Add the calcuations for the arm calculations on the manus API
258270
private void add_arm_calc(ref manus_hand_t hand, ref ik_body_t body_side, ref ik_profile_t my_profile, ref Manus_hand_obj my_hand)
259271
{

Utillity Scripts/Manus_To_TCP.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Original System: Manus_To_TCP.cs
4+
// Subsystem: Human-Robot Interaction
5+
// Workfile: Unity workspace?
6+
// Revision: 1.0 - 6/29/2018
7+
// Author: Esteban Segarra
8+
//
9+
// Description
10+
// ===========
11+
// Data phraser from Manus to TPC server.
12+
//
13+
///////////////////////////////////////////////////////////////////////////////
14+
15+
using System.Collections;
16+
using System.Collections.Generic;
17+
using UnityEngine;
18+
using manus_interface;
19+
20+
using TPC_Server;
21+
22+
public class Manus_To_TCP : MonoBehaviour {
23+
TCP_Server server;
24+
Manus_interpreter interpreter;
25+
26+
// Use this for initialization
27+
void Start () {
28+
server = this.GetComponent<TCP_Server>();
29+
GameObject locator = GameObject.Find("Manus_VR_Driver");
30+
interpreter = locator.GetComponent<Manus_interpreter>();
31+
32+
}
33+
34+
// Update is called once per frame
35+
void Update () {
36+
string converted_string_manus = consodiliate_strings();
37+
server.IPC_comms_message = converted_string_manus;
38+
}
39+
40+
41+
string consodiliate_strings()
42+
{
43+
Manus_hand_obj device0 = interpreter.get_hand(0);
44+
Manus_hand_obj device1 = interpreter.get_hand(1);
45+
try
46+
{
47+
string stringed_array = null;
48+
double[] raw_values_manus = device0.get_raw_hand().ToArray() ;
49+
raw_values_manus += device1.get_raw_hand().ToArray();
50+
51+
return stringed_array;
52+
}
53+
catch
54+
{
55+
return "Problem with return type";
56+
}
57+
return "null";
58+
}
59+
60+
61+
62+
}
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Utillity Scripts/TCPTestServer.cs

Lines changed: 0 additions & 121 deletions
This file was deleted.

Utillity Scripts/TCP_Server.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//Code Adopt from https://gist.github.com/danielbierwirth/0636650b005834204cb19ef5ae6ccedb
2+
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Net;
7+
using System.Net.Sockets;
8+
using System.Text;
9+
using System.Threading;
10+
using UnityEngine;
11+
12+
namespace TPC_Server
13+
{
14+
public class TCP_Server : MonoBehaviour
15+
{
16+
#region private members
17+
/// <summary>
18+
/// TCPListener to listen for incomming TCP connection
19+
/// requests.
20+
/// </summary>
21+
private TcpListener tcpListener;
22+
/// <summary>
23+
/// Background thread for TcpServer workload.
24+
/// </summary>
25+
private Thread tcpListenerThread;
26+
/// <summary>
27+
/// Create handle to connected tcp client.
28+
/// </summary>
29+
private TcpClient connectedTcpClient;
30+
#endregion
31+
public string IPC_comms_message = "null_msg";
32+
public string IPC_output = "null";
33+
34+
// Use this for initialization
35+
void Start()
36+
{
37+
// Start TcpServer background thread
38+
tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests));
39+
tcpListenerThread.IsBackground = true;
40+
tcpListenerThread.Start();
41+
}
42+
43+
// Update is called once per frame
44+
void Update()
45+
{
46+
//if (Input.GetKeyDown(KeyCode.Space))
47+
//{
48+
SendMessage(IPC_comms_message);
49+
//}
50+
}
51+
52+
/// <summary>
53+
/// Runs in background TcpServerThread; Handles incomming TcpClient requests
54+
/// </summary>
55+
private void ListenForIncommingRequests()
56+
{
57+
try
58+
{
59+
// Create listener on localhost port 8052.
60+
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 27015);
61+
tcpListener.Start();
62+
Debug.Log("Server is listening");
63+
Byte[] bytes = new Byte[1024];
64+
while (true)
65+
{
66+
connectedTcpClient = tcpListener.AcceptTcpClient();
67+
{
68+
// Get a stream object for reading
69+
using (NetworkStream stream = connectedTcpClient.GetStream())
70+
{
71+
int length;
72+
// Read incomming stream into byte arrary.
73+
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
74+
{
75+
var incommingData = new byte[length];
76+
Array.Copy(bytes, 0, incommingData, 0, length);
77+
// Convert byte array to string message.
78+
string clientMessage = Encoding.ASCII.GetString(incommingData);
79+
Debug.Log("client message received as: " + clientMessage);
80+
IPC_output = clientMessage;
81+
}
82+
}
83+
}
84+
}
85+
}
86+
catch (SocketException socketException)
87+
{
88+
Debug.Log("SocketException " + socketException.ToString());
89+
}
90+
}
91+
/// <summary>
92+
/// Send message to client using socket connection.
93+
/// </summary>
94+
private void SendMessage(string serverMessage)
95+
{
96+
if (connectedTcpClient == null)
97+
{
98+
return;
99+
}
100+
101+
try
102+
{
103+
104+
// Get a stream object for writing.
105+
NetworkStream stream = connectedTcpClient.GetStream();
106+
if (stream.CanWrite)
107+
{
108+
//Debug.Log("Hi, I'm a robot!");
109+
//string serverMessage = "This is a message from your server.";
110+
// Convert string message to byte array.
111+
byte[] serverMessageAsByteArray = Encoding.ASCII.GetBytes(serverMessage);
112+
// Write byte array to socketConnection stream.
113+
stream.Write(serverMessageAsByteArray, 0, serverMessageAsByteArray.Length);
114+
Debug.Log("Server sent his message - should be received by client");
115+
}
116+
///Debug.Log("Hi, I'm a job!");
117+
}
118+
catch (SocketException socketException)
119+
{
120+
Debug.Log("Socket exception: " + socketException);
121+
}
122+
}
123+
}
124+
}

Utillity Scripts/UR5_transmission_serivce.cs.meta renamed to Utillity Scripts/TCP_Server.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)