Initial commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using MudBlazor.Extensions;
|
||||
using RobotNet.VDA5050.Factsheet;
|
||||
using RobotNet.VDA5050.Type;
|
||||
using RobotNet10.RobotApp.Interfaces;
|
||||
using RobotNet10.RobotApp.Services.Exceptions;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace RobotNet10.RobotApp.Services.Robot.Actions;
|
||||
|
||||
[RobotAction(ActionType.ROTATE,
|
||||
[ActionScope.INSTANT, ActionScope.NODE, ActionScope.EDGE],
|
||||
[BlockingType.HARD],
|
||||
"Xoay robot tại chỗ.",
|
||||
"Robot đã xoay tại chỗ.")]
|
||||
public class RobotRotateAction(IServiceProvider ServiceProvider) : RobotAction(ServiceProvider)
|
||||
{
|
||||
private static readonly ReadOnlyCollection<ActionParameter> ActionParameters = new List<ActionParameter>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Key = "angle",
|
||||
Description = "Góc xoay của robot. (rad)",
|
||||
ValueDataType = ValueDataType.FLOAT,
|
||||
IsOptional = false,
|
||||
}
|
||||
}.AsReadOnly();
|
||||
|
||||
private double Angle = 0;
|
||||
private INavigation? RobotNavigation;
|
||||
private ILocalization? Localization;
|
||||
|
||||
private const int MAX_TIMEOUT = 60000 * 2;
|
||||
private int CountTimeout = 0;
|
||||
protected override Task StartAction()
|
||||
{
|
||||
RobotNavigation = ServiceProvider.GetRequiredService<INavigation>();
|
||||
Localization = ServiceProvider.GetRequiredService<ILocalization>();
|
||||
RobotNavigation.Rotate(Angle);
|
||||
CountTimeout = 0;
|
||||
return base.StartAction();
|
||||
}
|
||||
|
||||
protected override Task StopAction()
|
||||
{
|
||||
RobotNavigation?.CancelMovement();
|
||||
return base.StopAction();
|
||||
}
|
||||
|
||||
protected override Task ExecuteAction()
|
||||
{
|
||||
if (RobotNavigation is null)
|
||||
{
|
||||
SetStatus(ActionEvent.FAILED);
|
||||
ResultDescription = "Module Navigation is not existed";
|
||||
}
|
||||
else if (RobotNavigation.State == NavigationState.Completed && Localization is not null && Math.Abs(Localization.Theta - Angle) * 180 / Math.PI < 5)
|
||||
{
|
||||
SetStatus(ActionEvent.FINISHED);
|
||||
ResultDescription = string.IsNullOrEmpty(ActionAttribute.ResultDescription) ? ResultDescription : ActionAttribute.ResultDescription;
|
||||
}
|
||||
else if (RobotNavigation.State == NavigationState.Error)
|
||||
{
|
||||
SetStatus(ActionEvent.FAILED);
|
||||
ResultDescription = $"Action Handle [{Type} - {Id}] - angle: {Angle} is failed";
|
||||
}
|
||||
else if (CountTimeout++ > MAX_TIMEOUT / ActionInterval)
|
||||
{
|
||||
RobotNavigation?.CancelMovement();
|
||||
SetStatus(ActionEvent.FAILED);
|
||||
ResultDescription = "Timeout action";
|
||||
}
|
||||
return base.ExecuteAction();
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (ActionParameters.Count > 0)
|
||||
{
|
||||
foreach (var parameterStore in ActionParameters)
|
||||
{
|
||||
if (!parameterStore.IsOptional)
|
||||
{
|
||||
if (Action is null || Action.ActionParameters is null || !Action.ActionParameters.Any(a => a.Key == parameterStore.Key)) throw new ActionException($"Thiếu tham số bắt buộc '{parameterStore.Key}' cho action {Type}.");
|
||||
}
|
||||
}
|
||||
Parameters = Action is null || Action.ActionParameters is null ? [] : Action.ActionParameters;
|
||||
}
|
||||
|
||||
var anglePara = Parameters.FirstOrDefault(p => p.Key == "angle") ?? throw new ActionException($"Action {Type} không tìm thấy parameter key 'angle'");
|
||||
var angleParse = double.TryParse(anglePara.Value, out double angleData);
|
||||
if (!angleParse) throw new ActionException($"Action {Type} có parameter 'angle' không đúng kiểu dữ liệu");
|
||||
Angle = angleData;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user