I am trying to create a script in unity that will load and run the robots program. My issue is that when I am executing a stop then play command, the dashboard response always comes back RUNNING, when it is stopped in the real world. Excuse the messy code as I have been trying many different things to fix this issue (delays to the functions).
public class UR_Control_Dashboard : MonoBehaviour
{
// TCP/IP Communication for Dashboard Server
private TcpClient dashboard_client = new TcpClient();
private NetworkStream dashboard_stream = null;
private bool exit_thread = false;
public string programName;
// Method to send a command to the Dashboard Server
public void SendDashboardCommand(string command)
{
try
{
if (dashboard_client.Connected == false)
{
// Connect to the robot's Dashboard Server on port 29999
dashboard_client.Connect(ur_data_processing.UR_Control_Data.ip_address, 29999);
}
// Send the command
dashboard_stream = dashboard_client.GetStream();
byte[] command_bytes = System.Text.Encoding.ASCII.GetBytes(command + "\n");
dashboard_stream.Write(command_bytes, 0, command_bytes.Length);
// Read the response (optional, can be omitted if you don't need the response)
byte[] response = new byte[256];
dashboard_stream.Read(response, 0, response.Length);
string response_str = System.Text.Encoding.ASCII.GetString(response);
Debug.Log("Dashboard Response: " + response_str);
}
catch (SocketException e)
{
Debug.LogException(e);
}
}
// Method to load and run a program
public void RunProgram(string programName)
{
// Load the program
SendDashboardCommand("load " + programName + ".urp");
//Debug.LogWarning("Loaded Program");
// Start the program
SendDashboardCommand("robotmode");
StartCoroutine(PlayProgramAfterDelay());
}
public void CheckRobotStatus()
{
SendDashboardCommand("robotmode");
// You should get a response from the robot that indicates if it's ready
// Adjust the logic based on the response to ensure it's ready to run the next program
}
private IEnumerator PlayProgramAfterDelay()
{
yield return new WaitForSeconds(10);
SendDashboardCommand("robotmode"); // Adjust delay if necessary
SendDashboardCommand("play");
}
public void OnRunRobot1()
{
SendDashboardCommand("stop");
StartCoroutine(PlayProgramAfterDelay());
//RunProgram("test");
//SendDashboardCommand("play");
//Debug.LogWarning("Loaded RunProgram");
}
public void OnRunRobot2()
{
SendDashboardCommand("stop");
SendDashboardCommand("robotmode");
RunProgram("robottestnew");
}
public void Start()
{
// Start thread or any other control logic
exit_thread = false;
// Example to run a program from Unity
//RunProgram("robotblocktestnew");
}
public void Destroy()
{
if (dashboard_client.Connected == true)
{
// Disconnect communication
dashboard_stream.Dispose();
dashboard_client.Close();
}
Thread.Sleep(100);
}
}
The base for this code was taken from rparak (Roman Parak) · GitHub.