Skip to content

Commit

Permalink
Fix init command, throw exception for Ignus if Declaration is used, f…
Browse files Browse the repository at this point in the history
…ormatting
  • Loading branch information
visose committed Nov 22, 2024
1 parent 39d7723 commit d78a3f9
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 43 deletions.
26 changes: 16 additions & 10 deletions src/Robots/Commands/Command.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@

namespace Robots;

public abstract class Command(string? name = null) : TargetAttribute(name)
{
public static Command Default { get; } = new Commands.Custom("DefaultCommand");

protected Dictionary<Manufacturers, Func<RobotSystem, string>> _declarations = new(6);
protected Dictionary<Manufacturers, Func<RobotSystem, Target, string>> _commands = new(6);
protected Dictionary<Manufacturers, Func<RobotSystem, string>> _declarations = new(8);
protected Dictionary<Manufacturers, Func<RobotSystem, Target, string>> _commands = new(8);

protected virtual void ErrorChecking(RobotSystem robotSystem) { }

protected virtual void Populate() { }
public bool RunBefore { get; set; }

internal virtual IEnumerable<Command> Flatten()
{
if (this != Default)
yield return this;
}

internal string Declaration(Program program)
void Init(RobotSystem robot)
{
var robot = program.RobotSystem;
ErrorChecking(robot);
if (_commands.Count > 0 || _declarations.Count > 0)
return;

_declarations.Clear();
_commands.Clear();
ErrorChecking(robot);
Populate();
}

internal string Declaration(Program program)
{
var robot = program.RobotSystem;
Init(robot);

if (_declarations.TryGetValue(robot.Manufacturer, out var declaration))
return declaration(robot);
Expand All @@ -36,10 +44,8 @@ internal string Declaration(Program program)

internal string Code(Program program, Target target)
{
_declarations.Clear();
_commands.Clear();
Populate();
var robot = program.RobotSystem;
Init(robot);

if (_commands.TryGetValue(robot.Manufacturer, out var command))
return command(robot, target);
Expand Down
3 changes: 2 additions & 1 deletion src/Robots/Commands/Custom.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Robots.Commands;

namespace Robots.Commands;

public class Custom : Command
{
Expand Down
49 changes: 32 additions & 17 deletions src/Robots/PostProcessors/IgusPostProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static System.Math;

namespace Robots;
using System.Linq;

class IgusPostProcessor : IPostProcessor
{
Expand All @@ -19,7 +19,6 @@ class PostInstance

public PostInstance(SystemIgus system, Program program)
{

_system = system;
_program = program;
bool isMultiProgram = _program.MultiFileIndices.Count > 1;
Expand All @@ -42,8 +41,8 @@ public PostInstance(SystemIgus system, Program program)
groupCode.Add(SubModule(j));
}
}
Code = [groupCode];

Code = [groupCode];
}

List<string> MainModule()
Expand All @@ -55,12 +54,11 @@ List<string> MainModule()
code.Add("<Program>");
code.Add(" <Header RobotName=\"igus REBEL-6DOF\" RobotType=\"igus-REBEL/REBEL-6DOF-01\" " +
$"GripperType=\"{ToolName()}.xml\" Software=\"\" VelocitySetting=\"0\" />");
//_program.

if (is_multiProgram)
{
for (int i = 0; i < _program.MultiFileIndices.Count; i++)
code.Add($"<Sub Nr=\"{i + 1}\" File=\"{_program.Name}_{i + 1}.xml\" Descr=\"\" />");

}
else
{
Expand All @@ -74,19 +72,26 @@ List<string> MainModule()

List<string> SubModule(int index)
{
var code = new List<string>
{
List<string> code =
[
"<?xml version=\"1.0\" encoding=\"utf-8\"?>",
"<Program>",
"<Header RobotName=\"igus REBEL-6DOF\" RobotType=\"igus-REBEL/REBEL-6DOF-01\" " +
$"GripperType=\"{ToolName()}.xml\" Software=\"\" VelocitySetting=\"0\" />"
};
];

if (index == 0)
{
code.AddRange(TargetsCode(0, _program.MultiFileIndices[index + 1]));
}
else if (index == _program.MultiFileIndices.Count - 1)
{
code.AddRange(TargetsCode(_program.MultiFileIndices.Last(), _program.Targets.Count));
}
else
{
code.AddRange(TargetsCode(_program.MultiFileIndices[index], _program.MultiFileIndices[index + 1]));
}

code.Add("</Program>");

Expand All @@ -97,31 +102,32 @@ string ToolName()
{
var attributes = _program.Attributes;
var toolsNames = new List<string>();

foreach (var tool in attributes.OfType<Tool>().Where(t => !t.UseController))
{
if (toolsNames.Count == 0)
toolsNames.Add(tool.Name);
}

if (toolsNames.Count == 0)
return "";
else
return toolsNames[0];//We are only allowed to have a single tool
return toolsNames.Count switch
{
0 => "",
_ => toolsNames[0] //We are only allowed to have a single tool
};
}

List<string> TargetsCode(int startIndex, int endIndex)
{

var instructions = new List<string>();
List<string> instructions = [];
int lineCounter = 1;

for (int group = 0; group < _system.MechanicalGroups.Count; group++)
{
for (int j = startIndex; j < endIndex; j++)
{

var programTarget = _program.Targets[j].ProgramTargets[group];
var target = programTarget.Target;

if (j == 0)
{
foreach (var command in _program.InitCommands)
Expand All @@ -133,6 +139,7 @@ List<string> TargetsCode(int startIndex, int endIndex)
}

string moveText = "";

if (_system.MechanicalGroups[group].Externals.Count > 0)
{
_program.Warnings.Add("External axes not implemented in Staubli.");
Expand Down Expand Up @@ -188,6 +195,15 @@ List<string> TargetsCode(int startIndex, int endIndex)

}
}

foreach(var command in programTarget.Commands)
{
var declaration = command.Declaration(_program);

if (!string.IsNullOrWhiteSpace(declaration))
throw new NotImplementedException("Declaration of a command is not implemented for Igus robots.");
}

foreach (var command in programTarget.Commands.Where(c => c.RunBefore))
{
var instructionsStr = AddNumber(command.Code(_program, target), lineCounter);
Expand All @@ -206,6 +222,7 @@ List<string> TargetsCode(int startIndex, int endIndex)
}
}
}

return instructions;
}

Expand All @@ -223,9 +240,7 @@ string AddNumber(string input, int number)
{
_program.Errors.Add("Error at add_number function!");
return "Error at add_number function";

}
}

}
}
4 changes: 2 additions & 2 deletions src/Robots/Remotes/URRealTime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Net;
using System.Net;
using System.Net.Sockets;
using System.Text;

Expand Down Expand Up @@ -84,7 +84,7 @@ void UpdateBuffer()
using var client = GetClient();
var stream = client.GetStream();

stream.Read(_buffer, 0, _bufferLength);
_ = stream.Read(_buffer, 0, _bufferLength);
Array.Reverse(_buffer);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Robots/RobotArms/RobotIgus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ internal RobotIgus(string model, double payload, Plane basePlane, Mesh baseMesh,
public override double DegreeToRadian(double degree, int i)
{
double radian = degree.ToRadians();

if (i == 1 || i == 2)
radian -= HalfPI;

radian = -radian;
return radian;
}
Expand All @@ -24,8 +26,10 @@ public override double RadianToDegree(double radian, int i)
{
if (i != 0 && i != 5)
radian = -radian;

if (i == 1 || i == 2)
radian += HalfPI;

return radian.ToDegrees();
}

Expand Down
13 changes: 4 additions & 9 deletions src/Robots/RobotSystems/SystemIgus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,25 @@ internal override void SaveCode(IProgram program, string folder)

for (int i = 0; i < program.Code.Count; i++)
{

if (!multiProgram)
{
string filePath = Path.Combine(folder, $"{program.Name}.xml");
var code = program.Code[i][0];
var joinedCode = string.Join("\r\n", code);
File.WriteAllText(filePath, joinedCode);

}
else
{
for (int j = 0; j < program.Code[i].Count; j++)
{
string filePath;
if (j == 0)
filePath = Path.Combine(folder, $"{program.Name}.xml");
else
filePath = Path.Combine(folder, $"{program.Name}_{j:000}.xml");
string filePath = j == 0
? Path.Combine(folder, $"{program.Name}.xml")
: Path.Combine(folder, $"{program.Name}_{j:000}.xml");

var joinedCode = string.Join("\r\n", program.Code[i][j]);
File.WriteAllText(filePath, joinedCode);
}
}
}

}

}
2 changes: 1 addition & 1 deletion tests/Robots.Tests/ABBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ABBTests()
var targetB = new CartesianTarget(planeB, null, Motions.Linear, speed: speed);
var toolpath = new SimpleToolpath() { targetA, targetB };

_program = new Program("TestProgram", robot, new[] { toolpath });
_program = new Program("TestProgram", robot, [toolpath]);
}

[Test]
Expand Down
5 changes: 3 additions & 2 deletions tests/Robots.Tests/Performance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ void PerfTestAbb()

Log("Toolpath");

var program = new Program("TestProgram", robot, new[] { toolpath }, stepSize: 0.02);
var program = new Program("TestProgram", robot, [toolpath], stepSize: 0.02);

Log("Program"); // 486

double expected = 3.7851345985264309;
Debug.Assert(program.Duration == expected, "Test failed");
}

#pragma warning disable IDE0051
void PerfTestUR()
{
_watch.Restart();
Expand All @@ -79,7 +80,7 @@ void PerfTestUR()

Log("Toolpath");

var program = new Program("URTest", robot, new[] { toolpath }, stepSize: 0.01);
var program = new Program("URTest", robot, [toolpath], stepSize: 0.01);

Log("Program");

Expand Down
2 changes: 1 addition & 1 deletion tests/Robots.Tests/URTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public URTests()
var targetB = new CartesianTarget(planeB, null, Motions.Linear, speed: speed);
var toolpath = new SimpleToolpath() { targetA, targetB };

_program = new Program("URTest", robot, new[] { toolpath });
_program = new Program("URTest", robot, [toolpath]);
}

[Test]
Expand Down

0 comments on commit d78a3f9

Please sign in to comment.