diff --git a/RASDK.Arm.TestForms/Form1.Designer.cs b/RASDK.Arm.TestForms/Form1.Designer.cs
index c344964..51e4cba 100644
--- a/RASDK.Arm.TestForms/Form1.Designer.cs
+++ b/RASDK.Arm.TestForms/Form1.Designer.cs
@@ -45,16 +45,17 @@ private void InitializeComponent()
this.numericUpDownJogZ = new System.Windows.Forms.NumericUpDown();
this.textBoxPort = new System.Windows.Forms.TextBox();
this.comboBoxArmType = new System.Windows.Forms.ComboBox();
+ this.buttonCheckConnect = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownJogXY)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownJogZ)).BeginInit();
this.SuspendLayout();
//
// buttonConnect
//
- this.buttonConnect.Location = new System.Drawing.Point(6, 30);
- this.buttonConnect.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonConnect.Location = new System.Drawing.Point(8, 38);
+ this.buttonConnect.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonConnect.Name = "buttonConnect";
- this.buttonConnect.Size = new System.Drawing.Size(79, 27);
+ this.buttonConnect.Size = new System.Drawing.Size(105, 34);
this.buttonConnect.TabIndex = 0;
this.buttonConnect.Text = "Connect";
this.buttonConnect.UseVisualStyleBackColor = true;
@@ -62,10 +63,10 @@ private void InitializeComponent()
//
// buttonDisconnect
//
- this.buttonDisconnect.Location = new System.Drawing.Point(6, 60);
- this.buttonDisconnect.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonDisconnect.Location = new System.Drawing.Point(8, 75);
+ this.buttonDisconnect.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonDisconnect.Name = "buttonDisconnect";
- this.buttonDisconnect.Size = new System.Drawing.Size(79, 27);
+ this.buttonDisconnect.Size = new System.Drawing.Size(105, 34);
this.buttonDisconnect.TabIndex = 0;
this.buttonDisconnect.Text = "Disconnect";
this.buttonDisconnect.UseVisualStyleBackColor = true;
@@ -73,19 +74,19 @@ private void InitializeComponent()
//
// textBoxIp
//
- this.textBoxIp.Location = new System.Drawing.Point(6, 9);
- this.textBoxIp.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.textBoxIp.Location = new System.Drawing.Point(8, 11);
+ this.textBoxIp.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.textBoxIp.Name = "textBoxIp";
- this.textBoxIp.Size = new System.Drawing.Size(102, 22);
+ this.textBoxIp.Size = new System.Drawing.Size(135, 25);
this.textBoxIp.TabIndex = 1;
- this.textBoxIp.Text = "192.168.100.111";
+ this.textBoxIp.Text = "192.168.0.1";
//
// buttonHoming
//
- this.buttonHoming.Location = new System.Drawing.Point(6, 90);
- this.buttonHoming.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonHoming.Location = new System.Drawing.Point(8, 151);
+ this.buttonHoming.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonHoming.Name = "buttonHoming";
- this.buttonHoming.Size = new System.Drawing.Size(79, 27);
+ this.buttonHoming.Size = new System.Drawing.Size(105, 34);
this.buttonHoming.TabIndex = 0;
this.buttonHoming.Text = "Homing";
this.buttonHoming.UseVisualStyleBackColor = true;
@@ -93,10 +94,10 @@ private void InitializeComponent()
//
// buttonMove1
//
- this.buttonMove1.Location = new System.Drawing.Point(6, 120);
- this.buttonMove1.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonMove1.Location = new System.Drawing.Point(8, 189);
+ this.buttonMove1.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonMove1.Name = "buttonMove1";
- this.buttonMove1.Size = new System.Drawing.Size(79, 27);
+ this.buttonMove1.Size = new System.Drawing.Size(105, 34);
this.buttonMove1.TabIndex = 0;
this.buttonMove1.Text = "Unblock";
this.buttonMove1.UseVisualStyleBackColor = true;
@@ -104,10 +105,10 @@ private void InitializeComponent()
//
// buttonMove2
//
- this.buttonMove2.Location = new System.Drawing.Point(6, 149);
- this.buttonMove2.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonMove2.Location = new System.Drawing.Point(8, 225);
+ this.buttonMove2.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonMove2.Name = "buttonMove2";
- this.buttonMove2.Size = new System.Drawing.Size(79, 27);
+ this.buttonMove2.Size = new System.Drawing.Size(105, 34);
this.buttonMove2.TabIndex = 0;
this.buttonMove2.Text = "Blocked";
this.buttonMove2.UseVisualStyleBackColor = true;
@@ -115,10 +116,10 @@ private void InitializeComponent()
//
// buttonJogXP
//
- this.buttonJogXP.Location = new System.Drawing.Point(226, 74);
- this.buttonJogXP.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonJogXP.Location = new System.Drawing.Point(301, 92);
+ this.buttonJogXP.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonJogXP.Name = "buttonJogXP";
- this.buttonJogXP.Size = new System.Drawing.Size(25, 24);
+ this.buttonJogXP.Size = new System.Drawing.Size(33, 30);
this.buttonJogXP.TabIndex = 2;
this.buttonJogXP.Text = "X+";
this.buttonJogXP.UseVisualStyleBackColor = true;
@@ -127,10 +128,10 @@ private void InitializeComponent()
//
// buttonJogXM
//
- this.buttonJogXM.Location = new System.Drawing.Point(124, 74);
- this.buttonJogXM.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonJogXM.Location = new System.Drawing.Point(165, 92);
+ this.buttonJogXM.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonJogXM.Name = "buttonJogXM";
- this.buttonJogXM.Size = new System.Drawing.Size(25, 24);
+ this.buttonJogXM.Size = new System.Drawing.Size(33, 30);
this.buttonJogXM.TabIndex = 2;
this.buttonJogXM.Text = "X-";
this.buttonJogXM.UseVisualStyleBackColor = true;
@@ -139,10 +140,10 @@ private void InitializeComponent()
//
// buttonJogYP
//
- this.buttonJogYP.Location = new System.Drawing.Point(176, 43);
- this.buttonJogYP.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonJogYP.Location = new System.Drawing.Point(235, 54);
+ this.buttonJogYP.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonJogYP.Name = "buttonJogYP";
- this.buttonJogYP.Size = new System.Drawing.Size(25, 24);
+ this.buttonJogYP.Size = new System.Drawing.Size(33, 30);
this.buttonJogYP.TabIndex = 2;
this.buttonJogYP.Text = "Y+";
this.buttonJogYP.UseVisualStyleBackColor = true;
@@ -151,10 +152,10 @@ private void InitializeComponent()
//
// buttonJogZM
//
- this.buttonJogZM.Location = new System.Drawing.Point(279, 99);
- this.buttonJogZM.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonJogZM.Location = new System.Drawing.Point(372, 124);
+ this.buttonJogZM.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonJogZM.Name = "buttonJogZM";
- this.buttonJogZM.Size = new System.Drawing.Size(25, 24);
+ this.buttonJogZM.Size = new System.Drawing.Size(33, 30);
this.buttonJogZM.TabIndex = 2;
this.buttonJogZM.Text = "Z-";
this.buttonJogZM.UseVisualStyleBackColor = true;
@@ -163,10 +164,10 @@ private void InitializeComponent()
//
// buttonJogYM
//
- this.buttonJogYM.Location = new System.Drawing.Point(176, 106);
- this.buttonJogYM.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonJogYM.Location = new System.Drawing.Point(235, 132);
+ this.buttonJogYM.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonJogYM.Name = "buttonJogYM";
- this.buttonJogYM.Size = new System.Drawing.Size(25, 24);
+ this.buttonJogYM.Size = new System.Drawing.Size(33, 30);
this.buttonJogYM.TabIndex = 2;
this.buttonJogYM.Text = "Y-";
this.buttonJogYM.UseVisualStyleBackColor = true;
@@ -175,10 +176,10 @@ private void InitializeComponent()
//
// buttonJogZP
//
- this.buttonJogZP.Location = new System.Drawing.Point(279, 47);
- this.buttonJogZP.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.buttonJogZP.Location = new System.Drawing.Point(372, 59);
+ this.buttonJogZP.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.buttonJogZP.Name = "buttonJogZP";
- this.buttonJogZP.Size = new System.Drawing.Size(25, 24);
+ this.buttonJogZP.Size = new System.Drawing.Size(33, 30);
this.buttonJogZP.TabIndex = 2;
this.buttonJogZP.Text = "Z+";
this.buttonJogZP.UseVisualStyleBackColor = true;
@@ -188,10 +189,10 @@ private void InitializeComponent()
// numericUpDownJogXY
//
this.numericUpDownJogXY.DecimalPlaces = 3;
- this.numericUpDownJogXY.Location = new System.Drawing.Point(158, 79);
- this.numericUpDownJogXY.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.numericUpDownJogXY.Location = new System.Drawing.Point(211, 99);
+ this.numericUpDownJogXY.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.numericUpDownJogXY.Name = "numericUpDownJogXY";
- this.numericUpDownJogXY.Size = new System.Drawing.Size(60, 22);
+ this.numericUpDownJogXY.Size = new System.Drawing.Size(80, 25);
this.numericUpDownJogXY.TabIndex = 3;
this.numericUpDownJogXY.Value = new decimal(new int[] {
10,
@@ -202,10 +203,10 @@ private void InitializeComponent()
// numericUpDownJogZ
//
this.numericUpDownJogZ.DecimalPlaces = 3;
- this.numericUpDownJogZ.Location = new System.Drawing.Point(262, 79);
- this.numericUpDownJogZ.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.numericUpDownJogZ.Location = new System.Drawing.Point(349, 99);
+ this.numericUpDownJogZ.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.numericUpDownJogZ.Name = "numericUpDownJogZ";
- this.numericUpDownJogZ.Size = new System.Drawing.Size(60, 22);
+ this.numericUpDownJogZ.Size = new System.Drawing.Size(80, 25);
this.numericUpDownJogZ.TabIndex = 3;
this.numericUpDownJogZ.Value = new decimal(new int[] {
5,
@@ -215,10 +216,10 @@ private void InitializeComponent()
//
// textBoxPort
//
- this.textBoxPort.Location = new System.Drawing.Point(114, 9);
- this.textBoxPort.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
+ this.textBoxPort.Location = new System.Drawing.Point(152, 11);
+ this.textBoxPort.Margin = new System.Windows.Forms.Padding(1);
this.textBoxPort.Name = "textBoxPort";
- this.textBoxPort.Size = new System.Drawing.Size(40, 22);
+ this.textBoxPort.Size = new System.Drawing.Size(52, 25);
this.textBoxPort.TabIndex = 4;
this.textBoxPort.Text = "5890";
//
@@ -229,17 +230,28 @@ private void InitializeComponent()
"HIWIN",
"TM Robot",
"CoppeliaSim"});
- this.comboBoxArmType.Location = new System.Drawing.Point(166, 9);
- this.comboBoxArmType.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
+ this.comboBoxArmType.Location = new System.Drawing.Point(221, 11);
+ this.comboBoxArmType.Margin = new System.Windows.Forms.Padding(1);
this.comboBoxArmType.Name = "comboBoxArmType";
- this.comboBoxArmType.Size = new System.Drawing.Size(72, 20);
+ this.comboBoxArmType.Size = new System.Drawing.Size(95, 23);
this.comboBoxArmType.TabIndex = 5;
//
+ // buttonCheckConnect
+ //
+ this.buttonCheckConnect.Location = new System.Drawing.Point(8, 113);
+ this.buttonCheckConnect.Name = "buttonCheckConnect";
+ this.buttonCheckConnect.Size = new System.Drawing.Size(105, 34);
+ this.buttonCheckConnect.TabIndex = 6;
+ this.buttonCheckConnect.Text = "Check";
+ this.buttonCheckConnect.UseVisualStyleBackColor = true;
+ this.buttonCheckConnect.Click += new System.EventHandler(this.buttonCheckConnect_Click);
+ //
// Form1
//
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+ this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(400, 216);
+ this.ClientSize = new System.Drawing.Size(548, 283);
+ this.Controls.Add(this.buttonCheckConnect);
this.Controls.Add(this.comboBoxArmType);
this.Controls.Add(this.textBoxPort);
this.Controls.Add(this.numericUpDownJogZ);
@@ -256,7 +268,7 @@ private void InitializeComponent()
this.Controls.Add(this.buttonHoming);
this.Controls.Add(this.buttonDisconnect);
this.Controls.Add(this.buttonConnect);
- this.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+ this.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1);
this.Name = "Form1";
this.Text = "RASDK.Arm Test";
((System.ComponentModel.ISupportInitialize)(this.numericUpDownJogXY)).EndInit();
@@ -284,6 +296,7 @@ private void InitializeComponent()
private System.Windows.Forms.NumericUpDown numericUpDownJogZ;
private System.Windows.Forms.TextBox textBoxPort;
private System.Windows.Forms.ComboBox comboBoxArmType;
+ private System.Windows.Forms.Button buttonCheckConnect;
}
}
diff --git a/RASDK.Arm.TestForms/Form1.cs b/RASDK.Arm.TestForms/Form1.cs
index d3a39d8..20371ad 100644
--- a/RASDK.Arm.TestForms/Form1.cs
+++ b/RASDK.Arm.TestForms/Form1.cs
@@ -45,6 +45,14 @@ public Form1()
comboBoxArmType.SelectedIndex = 0;
}
+ private void buttonCheckConnect_Click(object sender, EventArgs e)
+ {
+ _arm = _arm ?? RoboticArmFactory();
+
+ var connected = _arm.Connected;
+ _message.Show($"Connected: {connected}.");
+ }
+
private void buttonConnect_Click(object sender, EventArgs e)
{
_arm = _arm ?? RoboticArmFactory();
diff --git a/RASDK.Arm/Hiwin/Exception.cs b/RASDK.Arm/Hiwin/Exception.cs
new file mode 100644
index 0000000..b6489de
--- /dev/null
+++ b/RASDK.Arm/Hiwin/Exception.cs
@@ -0,0 +1,22 @@
+namespace RASDK.Arm.Hiwin
+{
+ public class HiwinRobotControlException : System.Exception
+ {
+ public HiwinRobotControlException() : base()
+ { }
+
+ public HiwinRobotControlException(string message) : base(message)
+ { }
+
+ public HiwinRobotControlException(int code) : base($"HIWIN robot error code: {code}.")
+ { }
+
+ public HiwinRobotControlException(int code, string message)
+ : base($"HIWIN robot error code: {code}. {message}.")
+ { }
+
+ public HiwinRobotControlException(string message, System.Exception innerException)
+ : base(message, innerException)
+ { }
+ }
+}
\ No newline at end of file
diff --git a/RASDK.Arm/Hiwin/ReturnCodeCheck.cs b/RASDK.Arm/Hiwin/ReturnCodeCheck.cs
deleted file mode 100644
index 7f2da68..0000000
--- a/RASDK.Arm/Hiwin/ReturnCodeCheck.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using System.Runtime.CompilerServices;
-using RASDK.Basic;
-using RASDK.Basic.Message;
-
-namespace RASDK.Arm.Hiwin
-{
- internal static class ReturnCodeCheck
- {
- ///
- /// 檢查指令是否成果。
- ///
- /// 執行指令回傳的代碼。
- /// 訊息處理器。
- /// 要忽略的錯誤代碼。
- /// 代表成功的代碼。
- ///
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsSuccessful(int code, MessageHandler message = null, int ignoreCode = 0, int successCode = 0)
- {
- bool successful;
- if ((code == ignoreCode) || (code == successCode))
- {
- successful = true;
- }
- else
- {
- successful = false;
- message?.Show($"上銀手臂錯誤。\r\n錯誤代碼:{code}", LoggingLevel.Error);
- }
- return successful;
- }
- }
-}
\ No newline at end of file
diff --git a/RASDK.Arm/Hiwin/RoboticArm.cs b/RASDK.Arm/Hiwin/RoboticArm.cs
index 3bd7341..3ae16d3 100644
--- a/RASDK.Arm/Hiwin/RoboticArm.cs
+++ b/RASDK.Arm/Hiwin/RoboticArm.cs
@@ -18,7 +18,7 @@ namespace RASDK.Arm.Hiwin
///
public class RoboticArm : RASDK.Arm.RoboticArm
{
- private static volatile bool _waiting;
+ private static volatile bool _moving;
private readonly string _ip;
private int _id;
@@ -34,6 +34,7 @@ public RoboticArm(MessageHandler message, string ip = Default.Ip) : base(message
_message.LogMethodStart(nameof(RoboticArm), paramNames, paramVals, "上銀機械手臂建構子", LoggingLevel.Info);
_ip = ip;
+ _moving = false;
_message.LogMethodEnd(nameof(RoboticArm));
}
@@ -67,19 +68,45 @@ public override double[] GetNowPosition(CoordinateType coordinate = CoordinateTy
break;
default:
- throw new ArgumentException("Unknown coordinator type.");
+ throw new ArgumentException("Unknown coordinator type.", nameof(coordinate));
}
var returnCode = action(_id, position);
- var noError = ReturnCodeCheck.IsSuccessful(returnCode, _message);
+ if (!IsSuccessfulReturnCode(returnCode))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(GetNowPosition));
+ }
_message.LogMethodEnd(nameof(GetNowPosition),
$"{position[0]},{position[1]},{position[2]},{position[3]},{position[4]},{position[5]}",
$"錯誤代碼:{returnCode}",
- noError ? LoggingLevel.Trace : LoggingLevel.Warn);
+ LoggingLevel.Trace);
return position;
}
+ ///
+ /// 檢查指令是否成功。
+ ///
+ /// 執行指令回傳的代碼。
+ /// 要忽略的錯誤代碼。
+ /// 代表成功的代碼。
+ /// 是否成功無錯誤。
+ private bool IsSuccessfulReturnCode(int code, bool logging = true, int ignoreCode = 0, int successCode = 0)
+ {
+ if (code == ignoreCode ||
+ code == successCode)
+ {
+ return true; // Successful.
+ }
+
+ if (logging)
+ {
+ _message?.Log($"上銀手臂錯誤,錯誤代碼:{code}。", LoggingLevel.Error);
+ }
+
+ return false; // Otherwise, not successful.
+ }
+
#region Motion
///
@@ -88,7 +115,13 @@ public override double[] GetNowPosition(CoordinateType coordinate = CoordinateTy
public override void Abort()
{
_message.LogMethodStart(nameof(Abort), "", "");
- HRobot.motion_abort(_id);
+
+ var returnCode = HRobot.motion_abort(_id);
+ if (!IsSuccessfulReturnCode(returnCode))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(Abort));
+ }
+
_message.LogMethodEnd(nameof(Abort));
}
@@ -99,7 +132,9 @@ public override void Abort()
/// 座標系類型。
/// 是否需要等待動作完成 (阻塞)。
///
- public override void Homing(bool slowly = true, CoordinateType coordinate = CoordinateType.Descartes, bool needWait = true)
+ public override void Homing(bool slowly = true,
+ CoordinateType coordinate = CoordinateType.Descartes,
+ bool needWait = true)
{
var paramNames = new List { nameof(slowly), nameof(coordinate), nameof(needWait) };
var paramVals = new List { slowly.ToString(), coordinate.ToString(), needWait.ToString() };
@@ -126,11 +161,26 @@ public override void Homing(bool slowly = true, CoordinateType coordinate = Coor
throw new ArgumentException("Unknown coordinator type.");
}
- WaitForMotionComplete(needWait, retuenCode);
+ if (IsSuccessfulReturnCode(retuenCode))
+ {
+ if (needWait)
+ {
+ WaitForMotionComplete(); // Blocking.
- if (slowly)
+ if (slowly)
+ {
+ Speed = oriSpeedValue;
+ }
+ }
+ }
+ else
{
- Speed = oriSpeedValue;
+ if (slowly)
+ {
+ Speed = oriSpeedValue;
+ }
+
+ throw new HiwinRobotControlException(retuenCode, nameof(Homing));
}
_message.LogMethodEnd(nameof(Homing));
@@ -150,11 +200,15 @@ public override void Jog(string axis)
if (CheckJogArg(axis))
{
- HRobot.jog(_id, 0, ParseAxis(axis), ParseDirection(axis));
+ var returnCode = HRobot.jog(_id, 0, ParseAxis(axis), ParseDirection(axis));
+ if (!IsSuccessfulReturnCode(returnCode))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(Jog));
+ }
}
else
{
- throw new ArgumentException($"Input regex: {_inputRegexPattern}");
+ throw new ArgumentException($"Input regex should be: {_inputRegexPattern}, but: {axis}", nameof(axis));
}
_message.LogMethodEnd(nameof(Jog));
@@ -217,7 +271,15 @@ public override void MoveAbsolute(double j1X,
break;
}
- WaitForMotionComplete(addParams.NeedWait, returnCode);
+ if (!IsSuccessfulReturnCode(returnCode))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(MoveRelative));
+ }
+
+ if (addParams.NeedWait)
+ {
+ WaitForMotionComplete(); // Blocking.
+ }
_message.LogMethodEnd(nameof(MoveAbsolute));
}
@@ -279,7 +341,15 @@ public override void MoveRelative(double j1X,
break;
}
- WaitForMotionComplete(addParams.NeedWait, returnCode);
+ if (!IsSuccessfulReturnCode(returnCode))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(MoveRelative));
+ }
+
+ if (addParams.NeedWait)
+ {
+ WaitForMotionComplete(); // Blocking.
+ }
_message.LogMethodEnd(nameof(MoveRelative));
}
@@ -301,18 +371,34 @@ private int GetSmoothTypeCode(SmoothType smoothType, MotionType motionType)
return code;
}
- private void WaitForMotionComplete(bool needWait, int returnCode)
+ ///
+ /// Blocking.
+ ///
+ private void WaitForMotionComplete()
{
- _message.LogMethodStart(nameof(WaitForMotionComplete),
- new List { nameof(needWait), nameof(returnCode) },
- new List { needWait.ToString(), returnCode.ToString() });
+ _message.LogMethodStart(nameof(WaitForMotionComplete), "void", "void");
- if (needWait && ReturnCodeCheck.IsSuccessful(returnCode))
+ uint i = 0;
+ do
{
- _waiting = true;
- while (_waiting)
- { /* Do nothing. */ }
+ i++;
+ if (i > 2000000)
+ {
+ i = 0;
+
+ // Active update motion state.
+ var motionState = HRobot.get_motion_state(_id);
+ if (motionState == 1) // Idle.
+ {
+ _moving = false;
+ }
+ else if (motionState == 2 || motionState == 4 || motionState == 5)
+ {
+ _moving = true;
+ }
+ }
}
+ while (_moving);
_message.LogMethodEnd(nameof(WaitForMotionComplete));
}
@@ -323,8 +409,6 @@ private void WaitForMotionComplete(bool needWait, int returnCode)
private static readonly HRobot.CallBackFun _callBackFun = EventFun;
- // FIXME: HRobot.network_get_state(_id) always return 0, so Connected always false.
-
///
/// 是否已連線。
///
@@ -332,16 +416,15 @@ public override bool Connected
{
get
{
- bool connected;
try
{
- connected = HRobot.network_get_state(_id) == 1;
+ // FIXME: HRobot.network_get_state(_id) always return 0, so Connected always false.
+ return HRobot.network_get_state(_id) == 1;
}
- catch (Exception)
- {
- connected = false;
- }
- return connected;
+ catch (Exception ex)
+ { /* Do nothing. */ }
+
+ return false;
}
}
@@ -454,7 +537,7 @@ private static void EventFun(UInt16 cmd, UInt16 rlt, ref UInt16 Msg, int len)
$"Joint:{infos[20]},{infos[21]},{infos[22]},{infos[23]},{infos[24]},{infos[25]}\r\n");
// Motion state=1: Idle.
- _waiting = (infos[8] != "1");
+ _moving = (infos[8] != "1");
break;
case 4011 when rlt != 0:
@@ -548,7 +631,8 @@ public override double Acceleration
acc = HRobot.get_acc_dec_ratio(_id);
if (acc == -1)
{
- _message.Show("取得手臂加速度時出錯。", LoggingLevel.Error);
+ _message?.Log("取得手臂加速度時出錯。", LoggingLevel.Error);
+ throw new HiwinRobotControlException($"取得手臂加速度時出錯。");
}
_message.LogMethodEnd($"get {nameof(Acceleration)}", acc.ToString(), "");
@@ -561,15 +645,18 @@ public override double Acceleration
if (value < 1 || value > 100)
{
- _message.Show($"手臂加速度應為1% ~ 100%之間。輸入值為:{value}",
- LoggingLevel.Warn);
+ _message?.Log($"手臂加速度應為1% ~ 100%之間。輸入值為:{value}", LoggingLevel.Warn);
+ throw new HiwinRobotControlException($"手臂加速度應為1% ~ 100%之間。輸入值為:{value}");
}
else
{
var returnCode = HRobot.set_acc_dec_ratio(_id, (int)value);
// 執行HRobot.set_acc_dec_ratio時會固定回傳錯誤代碼4000。
- ReturnCodeCheck.IsSuccessful(returnCode, _message, 4000);
+ if (!IsSuccessfulReturnCode(returnCode, true, 4000))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(Acceleration));
+ }
}
_message.LogMethodEnd($"set {nameof(Acceleration)}");
@@ -590,7 +677,8 @@ public override double Speed
var speed = HRobot.get_override_ratio(_id);
if (speed == -1)
{
- _message.Show("取得手臂速度時出錯。", LoggingLevel.Error);
+ _message?.Log("取得手臂速度時出錯。", LoggingLevel.Error);
+ throw new HiwinRobotControlException($"取得手臂速度時出錯。");
}
_message.LogMethodEnd($"get {nameof(Speed)}", speed.ToString(), "");
@@ -603,13 +691,17 @@ public override double Speed
if (value < 1 || value > 100)
{
- _message.Show($"手臂速度應為1% ~ 100%之間。輸入值為:{value}",
- LoggingLevel.Warn);
+ _message?.Log($"手臂速度應為1% ~ 100%之間。輸入值為:{value}", LoggingLevel.Warn);
+ throw new HiwinRobotControlException($"手臂速度應為1% ~ 100%之間。輸入值為:{value}");
}
else
{
var returnCode = HRobot.set_override_ratio(_id, (int)value);
- ReturnCodeCheck.IsSuccessful(returnCode, _message);
+
+ if (!IsSuccessfulReturnCode(returnCode))
+ {
+ throw new HiwinRobotControlException(returnCode, nameof(Speed));
+ }
}
_message.LogMethodEnd($"set {nameof(Speed)}");
@@ -634,7 +726,6 @@ public override void SetRobotOutput(int index, bool value)
/// Get robot output(RO).
///
///
- ///
public override bool GetRobotOutput(int index)
{
return HRobot.get_robot_output(_id, index) == 1;
@@ -644,7 +735,6 @@ public override bool GetRobotOutput(int index)
/// Get robot input(RI).
///
///
- ///
public override bool GetRobotInput(int index)
{
return HRobot.get_robot_input(_id, index) == 1;
diff --git a/RASDK.Arm/RASDK.Arm.csproj b/RASDK.Arm/RASDK.Arm.csproj
index 290a13c..0bd0348 100644
--- a/RASDK.Arm/RASDK.Arm.csproj
+++ b/RASDK.Arm/RASDK.Arm.csproj
@@ -72,9 +72,9 @@
+
-