mirror of
https://github.com/Team4388/2019-Virtual-Field.git
synced 2026-06-09 00:28:01 -06:00
Compatability with Robopipe v2.0.0
This commit is contained in:
@@ -9,16 +9,22 @@ using UnityEngine;
|
||||
|
||||
public class robotCommunication : MonoBehaviour
|
||||
{
|
||||
public bool isData = false;
|
||||
public static int portNumber = 4388;
|
||||
public int bytesAvailable;
|
||||
List<string> keys = new List<string>();
|
||||
public SortedList<string, float> robotValues =
|
||||
new SortedList<string, float>
|
||||
{
|
||||
{ "encoder", 0.0f },
|
||||
{ "navX", 0.0f },
|
||||
{ "Left Pos Inches", 0.0f },
|
||||
{ "Yaw Angle Deg", 0.0f },
|
||||
{ "Right Pos Inches", 0.0f },
|
||||
{ "Elevator Pos Ticks", 0.0f },
|
||||
};
|
||||
|
||||
public string input;
|
||||
public float encoder, navX;
|
||||
|
||||
public string input, request = "";
|
||||
|
||||
IPEndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portNumber);
|
||||
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
@@ -29,24 +35,36 @@ public class robotCommunication : MonoBehaviour
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
Console.WriteLine("Connecting...");
|
||||
clientSocket.Connect(serverAddress);
|
||||
Console.WriteLine("Connected");
|
||||
foreach (var valuePair in robotValues)
|
||||
{
|
||||
request += valuePair.Key;
|
||||
keys.Add(valuePair.Key);
|
||||
request += ",";
|
||||
}
|
||||
request = request.Substring(0,request.Length-1);
|
||||
byte[] byteRequest = Encoding.UTF8.GetBytes(request);
|
||||
clientSocket.Send(byteRequest);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
bytesAvailable = clientSocket.Available;
|
||||
if (bytesAvailable == 12)
|
||||
if (bytesAvailable > 10)
|
||||
{
|
||||
byte[] byteInput = new byte[100];
|
||||
clientSocket.Receive(byteInput);
|
||||
input = Encoding.UTF8.GetString(byteInput);
|
||||
string[] values = input.Split('|');
|
||||
robotValues["encoder"] = float.Parse(values[0], CultureInfo.InvariantCulture.NumberFormat);
|
||||
robotValues["navX"] = float.Parse(values[1], CultureInfo.InvariantCulture.NumberFormat);
|
||||
int i = 0;
|
||||
foreach (string key in keys)
|
||||
{
|
||||
robotValues[keys[i]] = float.Parse(values[i], CultureInfo.InvariantCulture.NumberFormat);
|
||||
i++;
|
||||
}
|
||||
clientSocket.Send(new byte[] { 100 });
|
||||
isData = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user