Files
HDRobotics/Assets/Scripts/Model/RobotMoveStep.cs

144 lines
4.7 KiB
C#
Raw Normal View History

2025-11-06 20:14:27 +09:00
using System.Text.RegularExpressions;
using UnityEngine;
public class RobotMoveStep
{
public string FullLine { get; private set; }
public RobotPose Pose { get; private set; }
2025-11-06 20:14:27 +09:00
public int StepNumber { get; private set; }
2025-11-13 21:23:28 +09:00
public int Speed { get; private set; }
public int Accu { get; private set; }
public int Tool { get; private set; }
2025-11-06 20:14:27 +09:00
/// <summary>
/// <20><>ǥ<EFBFBD><C7A5> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>̵<EFBFBD> <20><><EFBFBD><EFBFBD> (<28><>: "move P,spd=50%,accu=3,tool=1")
/// </summary>
public string MoveCommand { get; private set; }
// Regex<65><78> S<<3C><>ȣ><3E><> move <<3C><><EFBFBD><EFBFBD>> [<5B><>ǥ]<5D><> <20>и<EFBFBD>
// <20>׷<EFBFBD> 1: (S([0-9]+)) -> "S1", "1"
// <20>׷<EFBFBD> 3: (move.*?) -> "move P,spd=50%,accu=3,tool=1"
private static readonly Regex StepRegex = new Regex(@"^(S([0-9]+))\s+(move.*?)(\s+\[.*\])", RegexOptions.Compiled);
2025-11-13 21:23:28 +09:00
private static readonly Regex SpeedRegex = new Regex(@"spd=([0-9]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex AccuRegex = new Regex(@"accu=([0-9]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex ToolRegex = new Regex(@"tool=([0-9]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
2025-11-06 20:14:27 +09:00
public RobotMoveStep(string line, string coordString)
{
this.FullLine = line;
string[] values = coordString.Split(',');
2025-11-06 20:14:27 +09:00
if (values.Length >= 6)
{
this.Pose = new RobotPose(
float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]),
float.Parse(values[3]), float.Parse(values[4]), float.Parse(values[5])
);
}
2025-11-06 20:14:27 +09:00
else
{
Debug.LogError($"<22><>ǥ <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD>: {coordString}");
this.Pose = new RobotPose(0, 0, 0, 0, 0, 0);
}
Match match = StepRegex.Match(line);
if (match.Success)
{
this.StepNumber = int.Parse(match.Groups[2].Value);
this.MoveCommand = match.Groups[3].Value.Trim();
2025-11-13 21:23:28 +09:00
Match speedMatch = SpeedRegex.Match(this.MoveCommand);
Match accuMatch = AccuRegex.Match(this.MoveCommand);
Match toolMatch = ToolRegex.Match(this.MoveCommand);
if (speedMatch.Success)
this.Speed = int.Parse(speedMatch.Groups[1].Value);
else
Debug.LogWarning($"'spd='<27><> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD>: {this.MoveCommand}");
if (accuMatch.Success)
this.Accu = int.Parse(accuMatch.Groups[1].Value);
else
Debug.LogWarning($"'accu='<27><> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD>: {this.MoveCommand}");
if (toolMatch.Success)
this.Tool = int.Parse(toolMatch.Groups[1].Value);
else
Debug.LogWarning($"'tool='<27><> ã<><C3A3> <20><> <20><><EFBFBD><EFBFBD>: {this.MoveCommand}");
2025-11-06 20:14:27 +09:00
}
else
{
Debug.LogError($"<22><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>Ľ<EFBFBD> <20><><EFBFBD><EFBFBD>: {line}");
this.StepNumber = -1;
this.MoveCommand = "move P,spd=50%,accu=3,tool=0"; // <20><EFBFBD><E2BABB>
2025-11-13 21:23:28 +09:00
this.Speed = 50; // <20><EFBFBD><E2BABB>
2025-11-06 20:14:27 +09:00
}
}
/// <summary>
/// DeleteStep <20><>, S<<3C><>ȣ><3E><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD>ϱ<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>Լ<EFBFBD>
/// </summary>
public void UpdateStepNumber(int newStepNumber, string newMoveCommand = null)
{
this.StepNumber = newStepNumber;
if (newMoveCommand != null)
{
this.MoveCommand = newMoveCommand;
}
// FullLine<6E><65> <20><> <20><>ȣ<EFBFBD><C8A3> Pose<73><65> <20>ٽ<EFBFBD> <20><><EFBFBD><EFBFBD>
this.FullLine = $"S{this.StepNumber} {this.MoveCommand} {this.Pose.ToString()}";
}
/// <summary>
/// UpdateStep <20><>, Pose<73><65> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD>ϱ<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>Լ<EFBFBD>
/// </summary>
public void UpdatePose(RobotPose newPose)
{
this.Pose = newPose;
// FullLine<6E><65> <20><> Pose<73><65> <20>ٽ<EFBFBD> <20><><EFBFBD><EFBFBD>
this.FullLine = $"S{this.StepNumber} {this.MoveCommand} {this.Pose.ToString()}";
}
}
public class RobotPose
{
public float x, y, z, rx, ry, rz;
public RobotPose(float x, float y, float z, float rx, float ry, float rz)
{
this.x = x; this.y = y; this.z = z;
this.rx = rx; this.ry = ry; this.rz = rz;
}
2025-11-06 20:14:27 +09:00
/// <summary>
/// RobotData(Model)<29><> RobotPose(Program)<29><> <20><>ȯ
/// </summary>
public RobotPose(RobotData data)
{
this.x = data.x; this.y = data.y; this.z = data.z;
this.rx = data.rx; this.ry = data.ry; this.rz = data.rz;
}
/// <summary>
/// RobotPose(Program)<29><> RobotData(Model)<29><> <20><>ȯ
/// </summary>
public RobotData ToRobotData()
{
return new RobotData
{
x = this.x,
y = this.y,
z = this.z,
rx = this.rx,
ry = this.ry,
rz = this.rz
};
}
/// <summary>
/// API<50><49><EFBFBD><EFBFBD> <20><EFBFBD>ϴ<EFBFBD> <20><>ǥ <20><><EFBFBD>ڿ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>ȯ
/// </summary>
public override string ToString()
{
return $"[{x},{y},{z},{rx},{ry},{rz},\"robot\"]";
}
}