RobotSimulator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  22. public class RobotStateEventArgs : EventArgs
  23. {
  24. public RobotStateEnum State { get; set; }
  25. public DateTime TimeStamp { get; set; }
  26. public RobotStateEventArgs(RobotStateEnum newState, DateTime time)
  27. {
  28. State = newState;
  29. TimeStamp = time;
  30. }
  31. }
  32. public abstract class RobotSimulator : SocketDeviceSimulator
  33. {
  34. public string ErrorMessage
  35. {
  36. get { return errorMessage; }
  37. set { errorMessage = value; }
  38. }
  39. public string RobotType
  40. {
  41. get { return robotType; }
  42. set { robotType = value; }
  43. }
  44. public virtual Dictionary<string, double> MoveTimes
  45. {
  46. get { return moveTimes; }
  47. set { moveTimes = value; }
  48. }
  49. public virtual ReadOnlyCollection<string> Arms
  50. {
  51. get { return arms; }
  52. }
  53. private string robotType;
  54. private event EventHandler<RobotStateEventArgs> RobotStateChange;
  55. protected RobotStateEventArgs robotStateArgs;
  56. private string errorMessage;
  57. private Dictionary<string, double> moveTimes;
  58. private ReadOnlyCollection<string> arms;
  59. protected Dictionary<string, string> errorLookup;
  60. protected int lastError;
  61. protected RobotSimulator(int port, int commandIndex, string lineDelimiter, char msgDelimiter)
  62. : base(port,commandIndex,lineDelimiter,msgDelimiter)
  63. {
  64. //this.robotType = parms["RobotType"].Value;
  65. robotStateArgs = new RobotStateEventArgs(RobotStateEnum.Idle, DateTime.Now);
  66. SetRobotState(RobotStateEnum.Idle);
  67. moveTimes = new Dictionary<string, double>();
  68. moveTimes["Realistic Delay"] = 1.0;
  69. arms = new ReadOnlyCollection<string>(new List<string>());
  70. errorLookup = new Dictionary<string, string>();
  71. lastError = 0;
  72. }
  73. protected string LookupError(string errorCode)
  74. {
  75. if (errorLookup.ContainsKey(errorCode) == false)
  76. return "Error not found";
  77. string errorMsg = errorLookup[errorCode];
  78. errorLookup.Remove(errorCode);
  79. return errorMsg;
  80. }
  81. public void AttachToRobotState(EventHandler<RobotStateEventArgs> target)
  82. {
  83. target(this, robotStateArgs);
  84. RobotStateChange += target;
  85. }
  86. protected void SetRobotState(RobotStateEnum newState)
  87. {
  88. robotStateArgs.State = newState;
  89. robotStateArgs.TimeStamp = DateTime.Now;
  90. if (RobotStateChange != null)
  91. RobotStateChange(this, robotStateArgs);
  92. }
  93. }
  94. }