using RobotNet10.CANOpen.CiA402; using RobotNet10.CANOpen.CiA402.Enums; using RobotNet10.CANOpen.CiA402.Models; namespace RobotNet10.RobotApp.Devices; /// /// Interface cho CiA402 Servo - cung cấp tất cả chức năng điều khiển động cơ servo theo chuẩn CiA402 /// Tập trung vào phần điều khiển động cơ, không bao gồm các chức năng liên quan đến CAN/PDO /// public interface ICiA402Servo { // Cached values (thread-safe, read-only) /// /// Statusword hiện tại (real-time, thread-safe) /// Statusword CachedStatusword { get; } /// /// Actual position hiện tại (thread-safe) /// int CachedPosition { get; } /// /// Actual velocity hiện tại (thread-safe) /// int CachedVelocity { get; } /// /// Actual torque hiện tại (thread-safe) /// short CachedTorque { get; } // Events /// /// Event được kích hoạt khi Statusword thay đổi /// event EventHandler? StatuswordChanged; /// /// Event được kích hoạt khi Position thay đổi /// event EventHandler? PositionChanged; /// /// Event được kích hoạt khi Velocity thay đổi /// event EventHandler? VelocityChanged; // Statusword & Controlword /// /// Get statusword hiện tại /// Task GetStatuswordAsync(CancellationToken ct = default); /// /// Set controlword để điều khiển motor /// Task SetControlwordAsync(Controlword controlword, CancellationToken ct = default); /// /// Get drive state từ statusword /// Task GetStateAsync(CancellationToken ct = default); // Operation Mode /// /// Set operation mode (Profile Position, Profile Velocity, Profile Torque, etc.) /// Task SetOperationModeAsync(OperationMode mode, CancellationToken ct = default); /// /// Get operation mode hiện tại /// Task GetOperationModeAsync(CancellationToken ct = default); // State Machine Control /// /// Reset fault trên motor /// Task FaultResetAsync(CancellationToken ct = default); /// /// Shutdown command /// Task ShutdownAsync(CancellationToken ct = default); /// /// Switch on command /// Task SwitchOnAsync(CancellationToken ct = default); /// /// Enable operation command /// Task EnableOperationAsync(CancellationToken ct = default); /// /// Disable operation command /// Task DisableOperationAsync(CancellationToken ct = default); /// /// Quick stop command /// Task QuickStopAsync(CancellationToken ct = default); /// /// Enable motor với automatic state transitions /// Task EnableAsync(CancellationToken ct = default); /// /// Disable motor /// Task DisableAsync(CancellationToken ct = default); // Position Control /// /// Get actual position hiện tại /// Task GetActualPositionAsync(CancellationToken ct = default); /// /// Set target position /// Task SetTargetPositionAsync(int position, CancellationToken ct = default); /// /// Set profile speed /// Task SetProfileSpeedAsync(uint velocity, CancellationToken ct = default); /// /// Set profile velocity /// Task SetProfileVelocityAsync(uint velocity, CancellationToken ct = default); /// /// Set profile acceleration /// Task SetProfileAccelerationAsync(uint acceleration, CancellationToken ct = default); /// /// Set profile deceleration /// Task SetProfileDecelerationAsync(uint deceleration, CancellationToken ct = default); /// /// Get profile speed (0x6081) từ drive /// Task GetProfileSpeedAsync(CancellationToken ct = default); /// /// Get profile acceleration (0x6083) từ drive /// Task GetProfileAccelerationAsync(CancellationToken ct = default); /// /// Get profile deceleration (0x6084) từ drive /// Task GetProfileDecelerationAsync(CancellationToken ct = default); /// /// Start position move (set new setpoint bit) /// Task StartPositionMoveAsync(CancellationToken ct = default); /// /// Move to position với Profile Position mode /// Task MoveToPositionAsync(int position, uint velocity = 1000, uint acceleration = 5000, uint deceleration = 5000, CancellationToken ct = default); // Velocity Control /// /// Get actual velocity hiện tại /// Task GetActualVelocityAsync(CancellationToken ct = default); /// /// Set target velocity /// Task SetTargetVelocityAsync(int velocity, CancellationToken ct = default); /// /// Target velocity với Profile Velocity mode /// Task TargetVelocityAsync(int targetVelocity, uint acceleration = 5000, uint deceleration = 5000, CancellationToken ct = default); /// /// Profile velocity với Profile Velocity mode /// Task ProfileVelocityAsync(int targetVelocity, uint acceleration = 5000, uint deceleration = 5000, CancellationToken ct = default); // Torque Control /// /// Get actual torque hiện tại /// Task GetActualTorqueAsync(CancellationToken ct = default); /// /// Set target torque /// Task SetTargetTorqueAsync(short torque, CancellationToken ct = default); /// /// Run torque với Profile Torque mode /// Task RunTorqueAsync(short torque, CancellationToken ct = default); // Homing /// /// Set homing method /// Task SetHomingMethodAsync(byte method, CancellationToken ct = default); /// /// Set homing speed (speed during search for switch, sub-index 1 of 0x6099). /// Task SetHomingSpeedAsync(int speed, CancellationToken ct = default); /// /// Set homing offset (0x607C). Offset applied after homing is complete. /// Task SetHomingOffsetAsync(int offset, CancellationToken ct = default); /// /// Đọc lại homing method từ drive (0x6098) để kiểm tra đã ghi xuống chưa. /// Task GetHomingMethodAsync(CancellationToken ct = default); /// /// Đọc lại homing speed từ drive (0x6099 sub-index 1) để kiểm tra đã ghi xuống chưa. /// Task GetHomingSpeedAsync(CancellationToken ct = default); /// /// Đọc lại homing offset từ drive (0x607C) để kiểm tra đã ghi xuống chưa. /// Task GetHomingOffsetAsync(CancellationToken ct = default); /// /// Start homing procedure /// Task StartHomingAsync(byte method, int speed, CancellationToken ct = default); // Target Position Checking /// /// Kiểm tra xem motor đã đến vị trí target chưa /// /// Tolerance cho position comparison (encoder counts). Chỉ dùng nếu không có TargetReached bit. /// True để dùng Statusword.TargetReached bit (recommended), False để so sánh position bool IsAtTarget(int tolerance = 100, bool useStatusword = true); /// /// Đợi cho đến khi motor đến vị trí target /// Task WaitUntilAtTargetAsync(int tolerance = 100, bool useStatusword = true, int checkIntervalMs = 10, CancellationToken ct = default); // Error and Status Information /// /// Kiểm tra xem motor có đang ở trạng thái Fault không /// Task IsInFaultStateAsync(CancellationToken ct = default); /// /// Đọc Error Register từ device /// Task GetErrorRegisterAsync(CancellationToken ct = default); /// /// Đọc Pre-defined Error Field từ device (danh sách các error codes gần đây) /// Task GetErrorHistoryAsync(CancellationToken ct = default); /// /// Đọc error code mới nhất từ Error History /// Task GetLatestErrorCodeAsync(CancellationToken ct = default); /// /// Reset fault trên motor (nếu motor đang ở trạng thái Fault) /// Task TryFaultResetAsync(CancellationToken ct = default); /// /// Kiểm tra xem motor có đang enabled (Operation Enabled state) không /// Task IsEnabledAsync(CancellationToken ct = default); /// /// Kiểm tra xem motor có đang ready (Ready to Switch On hoặc Switched On) không /// Task IsReadyAsync(CancellationToken ct = default); } #region Event Args /// /// Event args cho StatuswordChanged event /// public class StatuswordChangedEventArgs(Statusword statusword, DriveState oldState, DriveState newState) : EventArgs { public Statusword Statusword { get; } = statusword; public DriveState OldState { get; } = oldState; public DriveState NewState { get; } = newState; } /// /// Event args cho PositionChanged event /// public class PositionChangedEventArgs(int position, int oldPosition) : EventArgs { public int Position { get; } = position; public int OldPosition { get; } = oldPosition; public int Delta => Position - OldPosition; } /// /// Event args cho VelocityChanged event /// public class VelocityChangedEventArgs(int velocity, int oldVelocity) : EventArgs { public int Velocity { get; } = velocity; public int OldVelocity { get; } = oldVelocity; } #endregion