RobotSimulator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MECF.Framework.Simulator.Core.Driver;
  8. namespace MECF.Framework.Simulator.Core.Robots
  9. {
  10. public enum RobotStateEnum
  11. {
  12. Idle,
  13. Homing,
  14. Picking,
  15. Placing,
  16. Exchanging,
  17. Approaching,
  18. Extending,
  19. Retracting,
  20. Errored,
  21. SVON,
  22. SVOFF
  23. }
  24. public class RobotStateEventArgs : EventArgs
  25. {
  26. public RobotStateEnum State { get; set; }
  27. public DateTime TimeStamp { get; set; }
  28. public RobotStateEventArgs(RobotStateEnum newState, DateTime time)
  29. {
  30. State = newState;
  31. TimeStamp = time;
  32. }
  33. }
  34. public abstract class RobotSimulator : SocketDeviceSimulator
  35. {
  36. public string ErrorMessage
  37. {
  38. get { return errorMessage; }
  39. set { errorMessage = value; }
  40. }
  41. public string RobotType
  42. {
  43. get { return robotType; }
  44. set { robotType = value; }
  45. }
  46. public virtual Dictionary<string, double> MoveTimes
  47. {
  48. get { return moveTimes; }
  49. set { moveTimes = value; }
  50. }
  51. public virtual ReadOnlyCollection<string> Arms
  52. {
  53. get { return arms; }
  54. }
  55. private string robotType;
  56. private event EventHandler<RobotStateEventArgs> RobotStateChange;
  57. protected RobotStateEventArgs robotStateArgs;
  58. private string errorMessage;
  59. private Dictionary<string, double> moveTimes;
  60. private ReadOnlyCollection<string> arms;
  61. protected Dictionary<string, string> errorLookup;
  62. protected int lastError;
  63. protected RobotSimulator(int port, int commandIndex, string lineDelimiter, char msgDelimiter, int cmdMaxLength = 4)
  64. : base(port,commandIndex,lineDelimiter,msgDelimiter,cmdMaxLength)
  65. {
  66. //this.robotType = parms["RobotType"].Value;
  67. robotStateArgs = new RobotStateEventArgs(RobotStateEnum.Idle, DateTime.Now);
  68. SetRobotState(RobotStateEnum.Idle);
  69. moveTimes = new Dictionary<string, double>();
  70. moveTimes["Realistic Delay"] = 1.0;
  71. arms = new ReadOnlyCollection<string>(new List<string>());
  72. errorLookup = new Dictionary<string, string>();
  73. lastError = 0;
  74. }
  75. protected string LookupError(string errorCode)
  76. {
  77. if (errorLookup.ContainsKey(errorCode) == false)
  78. return "Error not found";
  79. string errorMsg = errorLookup[errorCode];
  80. errorLookup.Remove(errorCode);
  81. return errorMsg;
  82. }
  83. public void AttachToRobotState(EventHandler<RobotStateEventArgs> target)
  84. {
  85. target(this, robotStateArgs);
  86. RobotStateChange += target;
  87. }
  88. protected void SetRobotState(RobotStateEnum newState)
  89. {
  90. robotStateArgs.State = newState;
  91. robotStateArgs.TimeStamp = DateTime.Now;
  92. if (RobotStateChange != null)
  93. RobotStateChange(this, robotStateArgs);
  94. }
  95. }
  96. }