camera pose and roll pitch yaw
front 0.6 1.7 -0.23
Rotate 90 degrees to the left 0.1 -0.6 90 or 179,179, -88
down 140, 176, -88
up 34, -1, 88
using UnityEngine;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.IO;
using Unity.VisualScripting;
using UnityEditor.Profiling.Memory.Experimental;
using Unity.Jobs;
public class RobotVisualizer : MonoBehaviour
{
TcpClient client;
Thread receiveThread;
string serverIp = "127.0.0.1";
int serverPort = 8888;
Vector3 targetPosition;
Vector3 targetRotation;
float Roll_;
float Roll_2;
float Pitch_;
float Yaw_;
float bRoll_ = 0.0f;
float bPitch_ = 0.0f;
float bYaw_ = 0.0f;
bool positionUpdated = false;
// 텍스트 파일 경로
static string filePath = "C:\\\\Users\\\\cam_initi_data.txt";
// 텍스트 파일 읽어오기
static string content = ReadTextFile(filePath);
static string[] elements = content.Trim().Split();
//static float x_ = (float.Parse(elements[0]));
//static float y_ = (float.Parse(elements[1]));
//static float z_ = (float.Parse(elements[2]));
int flag = 2;
bool flag2 = true;
void Start()
{
client = new TcpClient(serverIp, serverPort);
receiveThread = new Thread(new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
// bRoll_ = Roll_;
// bPitch_ = Pitch_;
// bYaw_ = Yaw_;
// positionUpdated = false;
void Update()
{
if (positionUpdated)
{
if (Mathf.Abs(bRoll_ - Roll_) > 150 && flag == 2)
{
print("flag = 1 and bRoll_ - Roll_ : " + (bRoll_ - Roll_));
flag = 1;//바꾸기
}
else if (Mathf.Abs(bRoll_ - Roll_) > 150 && flag == 1)
{
print("flag = 2 and bRoll_ - Roll_ : " + (bRoll_ - Roll_));
flag = 2;//그대로
}
if (flag == 1)
{
Roll_2 = Roll_ + 180;
//bPitch_ 150
//Pitch_ 30
if (Pitch_ > 0)
{
Pitch_ = 180 - Pitch_;
}
else if (Pitch_ < 0)
{
Pitch_ = -180 - Pitch_;
}
//bYaw_: 150 bYaw : -30
//Yaw: 151 + 180 --> -29 Yaw : - 31 + 180 -> 149
//Yaw_ = Yaw_ - 180;
if (Yaw_ > 0)
{
Yaw_ = Yaw_ - 180;
}
else if (Yaw_ < 0)
{
Yaw_ = Yaw_ + 180;
}
print("flag = 1 : Rotate");
print("Roll_2 : " + Roll_2);
transform.position = targetPosition;
//transform.eulerAngles = targetRotation;
transform.eulerAngles = new Vector3(0, 0, 0);
print(" Pitch_ : " + Pitch_ + " Yaw_ : " + Yaw_ + " Roll_2 : " + -Roll_2);
transform.Rotate(0.0f, Yaw_, 0.0f, Space.World);
transform.Rotate(Pitch_, 0.0f, 0.0f, Space.Self);
transform.Rotate(0.0f, 0.0f, -Roll_2, Space.Self);
bRoll_ = Roll_2 - 180;
bPitch_ = Pitch_;
bYaw_ = Yaw_;
positionUpdated = false;
}
else
{
print("flag = 2 : Rotate");
transform.position = targetPosition;
// transform.eulerAngles = targetRotation;
transform.eulerAngles = new Vector3(0, 0, 0);
print(" Pitch_ : " + Pitch_ + " Yaw_ : " + Yaw_ + " Roll_ : " + -Roll_);
transform.Rotate(0.0f, Yaw_, 0.0f, Space.World);
transform.Rotate(Pitch_, 0.0f, 0.0f, Space.Self);
transform.Rotate(0.0f, 0.0f, -Roll_, Space.Self);
bRoll_ = Roll_;
bPitch_ = Pitch_;
bYaw_ = Yaw_;
positionUpdated = false;
}
}
else
{
transform.Rotate(0.0f, 0.0f, 0.0f, Space.World);
transform.Rotate(0.0f, 0.0f, 0.0f, Space.Self);
transform.Rotate(0.0f, 0.0f, 0.0f, Space.Self);
}
}
void ReceiveData()
{
NetworkStream stream = client.GetStream();
byte[] data = new byte[1024];
while (true)
{
int bytesReceived = stream.Read(data, 0, data.Length);
string message = Encoding.ASCII.GetString(data, 0, bytesReceived);
ParseData(message);
Thread.Sleep(100);
}
}
void ParseData(string data)
{
string[] values = data.Split(',');
Vector3 position = new Vector3(
-Convert.ToSingle(values[0]) + 0,
Convert.ToSingle(values[1]) + 0,
Convert.ToSingle(values[2]) + 0);
//Vector3 rotation = new Vector3(
// Convert.ToSingle(values[4]),
// -Convert.ToSingle(values[5]),
// -Convert.ToSingle(values[3]));
Roll_ = Convert.ToSingle(values[3]);
Pitch_ = Convert.ToSingle(values[4]);
Yaw_ = -Convert.ToSingle(values[5]);
targetPosition = position;
//targetRotation = rotation;
positionUpdated = true;
}
void OnApplicationQuit()
{
if (receiveThread != null)
receiveThread.Abort();
if (client != null)
client.Close();
}
static string ReadTextFile(string filePath)
{
try
{
// 파일의 모든 텍스트를 읽어옴
string content = File.ReadAllText(filePath);
return content;
}
catch (Exception ex)
{
// 예외 발생 시 처리
Console.WriteLine($"파일 읽기 오류: {ex.Message}");
return string.Empty;
}
}
}