AtmRobot1.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using Aitex.Core.Common;
  2. using Aitex.Core.UI.MVVM;
  3. using Aitex.Sorter.UI.Controls;
  4. using Aitex.Sorter.UI.Controls.Common;
  5. using MECF.Framework.Common.CommonData;
  6. using MECF.Framework.Common.Equipment;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Animation;
  17. namespace EfemUI.Controls
  18. {
  19. /// <summary>
  20. /// AtmRobot1.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class AtmRobot1 : UserControl, INotifyPropertyChanged
  23. {
  24. protected readonly int MoveTime = 300;
  25. private const int AnimationTimeout = 3000; // seconds
  26. public int RotateAngle
  27. {
  28. get { return (int)GetValue(RotateAngleProperty); }
  29. set { SetValue(RotateAngleProperty, value); }
  30. }
  31. // Using a DependencyProperty as the backing store for RotateAngel. This enables animation, styling, binding, etc...
  32. public static readonly DependencyProperty RotateAngleProperty =
  33. DependencyProperty.Register("RotateAngel", typeof(int), typeof(AtmRobot1), new PropertyMetadata(0));
  34. public ModuleName Station
  35. {
  36. get { return (ModuleName)GetValue(StationProperty); }
  37. set { SetValue(StationProperty, value); }
  38. }
  39. // Using a DependencyProperty as the backing store for Station. This enables animation, styling, binding, etc...
  40. public static readonly DependencyProperty StationProperty =
  41. DependencyProperty.Register("Station", typeof(ModuleName), typeof(AtmRobot1), new PropertyMetadata(ModuleName.Robot));
  42. public ICommand Command
  43. {
  44. get { return (ICommand)GetValue(CommandProperty); }
  45. set { SetValue(CommandProperty, value); }
  46. }
  47. // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
  48. public static readonly DependencyProperty CommandProperty =
  49. DependencyProperty.Register("Command", typeof(ICommand), typeof(AtmRobot1), new PropertyMetadata(null));
  50. public string RobotBladeTarget
  51. {
  52. get { return (string)GetValue(RobotBladeTargetProperty); }
  53. set { SetValue(RobotBladeTargetProperty, value); }
  54. }
  55. public static readonly DependencyProperty RobotBladeTargetProperty =
  56. DependencyProperty.Register("RobotBladeTarget", typeof(string), typeof(AtmRobot1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  57. public string RobotBlade2Target
  58. {
  59. get { return (string)GetValue(RobotBlade2TargetProperty); }
  60. set { SetValue(RobotBlade2TargetProperty, value); }
  61. }
  62. public static readonly DependencyProperty RobotBlade2TargetProperty =
  63. DependencyProperty.Register("RobotBlade2Target", typeof(string), typeof(AtmRobot1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  64. public Dictionary<string, StationPosition> StationPosition
  65. {
  66. get { return (Dictionary<string, StationPosition>)GetValue(StationPositionProperty); }
  67. set { SetValue(StationPositionProperty, value); }
  68. }
  69. // Using a DependencyProperty as the backing store for StationPosition2. This enables animation, styling, binding, etc...
  70. public static readonly DependencyProperty StationPositionProperty =
  71. DependencyProperty.Register("StationPosition", typeof(Dictionary<string, StationPosition>), typeof(AtmRobot1), new PropertyMetadata(null, StationPositionChangedCallback));
  72. public bool ShowDock
  73. {
  74. get { return (bool)GetValue(ShowDockProperty); }
  75. set { SetValue(ShowDockProperty, value); }
  76. }
  77. // Using a DependencyProperty as the backing store for ShowDock. This enables animation, styling, binding, etc...
  78. public static readonly DependencyProperty ShowDockProperty =
  79. DependencyProperty.Register("ShowDock", typeof(bool), typeof(AtmRobot1), new PropertyMetadata(false));
  80. public WaferInfo Wafer1
  81. {
  82. get { return (WaferInfo)GetValue(Wafer1Property); }
  83. set { SetValue(Wafer1Property, value); }
  84. }
  85. // Using a DependencyProperty as the backing store for Wafer1. This enables animation, styling, binding, etc...
  86. public static readonly DependencyProperty Wafer1Property =
  87. DependencyProperty.Register("Wafer1", typeof(WaferInfo), typeof(AtmRobot1), new PropertyMetadata(null));
  88. public RobotMoveInfo MoveInfo
  89. {
  90. get { return (RobotMoveInfo)GetValue(MoveInfoProperty); }
  91. set { SetValue(MoveInfoProperty, value); }
  92. }
  93. public static readonly DependencyProperty MoveInfoProperty =
  94. DependencyProperty.Register("MoveInfo", typeof(RobotMoveInfo), typeof(AtmRobot1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  95. public WaferInfo Wafer2
  96. {
  97. get { return (WaferInfo)GetValue(Wafer2Property); }
  98. set { SetValue(Wafer2Property, value); }
  99. }
  100. // Using a DependencyProperty as the backing store for Wafer2. This enables animation, styling, binding, etc...
  101. public static readonly DependencyProperty Wafer2Property =
  102. DependencyProperty.Register("Wafer2", typeof(WaferInfo), typeof(AtmRobot1), new PropertyMetadata(null));
  103. private string CurrentPosition
  104. {
  105. get; set;
  106. }
  107. private List<MenuItem> menu;
  108. public List<MenuItem> Menu
  109. {
  110. get
  111. {
  112. return menu;
  113. }
  114. set
  115. {
  116. menu = value;
  117. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Menu"));
  118. }
  119. }
  120. public ICommand MoveCommand
  121. {
  122. get
  123. {
  124. return new DelegateCommand<string>(MoveTo);
  125. }
  126. }
  127. private AnimationQueue queue;
  128. public event PropertyChangedEventHandler PropertyChanged;
  129. static void StationPositionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  130. {
  131. var self = (AtmRobot1)d;
  132. var positions = (Dictionary<string, StationPosition>)e.NewValue;
  133. self.Menu = positions.Select(x => new MenuItem() { Header = x.Key, Command = self.MoveCommand, CommandParameter = x.Key }).ToList();
  134. }
  135. public AtmRobot1()
  136. {
  137. #if DEBUG
  138. System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;
  139. #endif
  140. InitializeComponent();
  141. root.DataContext = this;
  142. queue = new AnimationQueue("Robot Animation");
  143. queue.StatusUpdated += Queue_StatusUpdated;
  144. CurrentPosition = "ArmA.System";
  145. canvas1.Rotate(155);
  146. canvas2.Rotate(240);
  147. canvas3.Rotate(236);
  148. canvas21.Rotate(25);
  149. canvas22.Rotate(120);
  150. canvas23.Rotate(484);
  151. }
  152. protected override void OnRender(DrawingContext drawingContext)
  153. {
  154. base.OnRender(drawingContext);
  155. if (DesignerProperties.GetIsInDesignMode(this))
  156. {
  157. return;
  158. }
  159. //if (RobotBladeTarget != null)
  160. //{
  161. // queue.EnqueueStatus(new AnimationParameter() { Target = RobotBladeTarget });
  162. //}
  163. //if (RobotBlade2Target != null)
  164. //{
  165. // queue.EnqueueStatus(new AnimationParameter() { Target = RobotBlade2Target });
  166. //}
  167. if (MoveInfo != null)
  168. {
  169. queue.EnqueueStatus(new AnimationParameter() { Target = MoveInfo.BladeTarget});// Arm = MoveInfo.ArmTarget.ToString();
  170. }
  171. }
  172. private void Queue_StatusUpdated(object sender, StatusUpdateArgs e)
  173. {
  174. //string TargetPosition = CurrentPosition.Split('.')[0] + "." + e.Parameter.Target;
  175. string TargetPosition = e.Parameter.Target;
  176. LogMsg(string.Format("Target: {0} - {1}", CurrentPosition, TargetPosition));
  177. var nextEvent = new AutoResetEvent(false);
  178. var next = new Action(() =>
  179. {
  180. nextEvent.Set();
  181. });
  182. var wait = new Action(() =>
  183. {
  184. if (!nextEvent.WaitOne(AnimationTimeout))
  185. {
  186. LogMsg("Aniamtion time out!");
  187. }
  188. });
  189. var done = new Action(() =>
  190. {
  191. CurrentPosition = TargetPosition;
  192. e.Event.Set();
  193. });
  194. var needMove = CurrentPosition != TargetPosition;
  195. if (needMove)
  196. {
  197. Invoke(() => MoveRobot(TargetPosition, next));
  198. wait();
  199. }
  200. done();
  201. }
  202. private void MoveRobot(string target, Action onComplete = null)
  203. {
  204. MoveToStart(CurrentPosition, () => TranslateTo(target, () =>
  205. //RetractArm(() =>
  206. MoveToStart(target, () => MoveToEnd(target, onComplete))));
  207. }
  208. private void RetractArm(Action onComplete = null)
  209. {
  210. canvas2.Rotate(180, true, 50, 0, 0, onComplete);
  211. canvas3.Rotate(180, true);
  212. canvas2.Rotate(180, true);
  213. canvas3.Rotate(180, true);
  214. }
  215. private void MoveToStart(string station, Action onComplete = null)
  216. {
  217. //var position = StationPosition[station];
  218. StationPosition positionA = station.Contains("ArmA") ? StationPosition[station] : StationPosition[station.Replace("ArmB", "ArmA")];
  219. StationPosition positionB = station.Contains("ArmB") ? StationPosition[station] : StationPosition[station.Replace("ArmA", "ArmB")];
  220. canvas1.Rotate(positionA.StartPosition.Root, true, MoveTime, 0, 0, onComplete);
  221. canvas2.Rotate(positionA.StartPosition.Arm, true, MoveTime);
  222. canvas3.Rotate(positionA.StartPosition.Hand, true, MoveTime);
  223. canvas21.Rotate(positionB.StartPosition.Root, true, MoveTime);
  224. canvas22.Rotate(positionB.StartPosition.Arm, true, MoveTime);
  225. canvas23.Rotate(positionB.StartPosition.Hand, true, MoveTime);
  226. CurrentPosition = station;
  227. }
  228. private void MoveToEnd(string station, Action onComplete = null)
  229. {
  230. var position = StationPosition[station];
  231. if (station.Contains("ArmA"))
  232. {
  233. canvas1.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete);
  234. canvas2.Rotate(position.EndPosition.Arm, true, MoveTime);
  235. canvas3.Rotate(position.EndPosition.Hand, true, MoveTime);
  236. }
  237. if (station.Contains("ArmB"))
  238. {
  239. canvas21.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete);
  240. canvas22.Rotate(position.EndPosition.Arm, true, MoveTime);
  241. canvas23.Rotate(position.EndPosition.Hand, true, MoveTime);
  242. }
  243. CurrentPosition = station;
  244. }
  245. private void TranslateTo(string station, Action onComplete = null)
  246. {
  247. var position = StationPosition[station];
  248. if (position.StartPosition.X.HasValue)
  249. {
  250. Translate(position.StartPosition.X.Value, onComplete);
  251. }
  252. else
  253. {
  254. onComplete?.Invoke();
  255. }
  256. }
  257. private void Translate(int x, Action onComplete = null)
  258. {
  259. var storyBoard = new Storyboard();
  260. var oldX = translate.X;
  261. var animation = new DoubleAnimation(oldX, x, TimeSpan.FromMilliseconds(MoveTime));
  262. storyBoard.Children.Add(animation);
  263. Storyboard.SetTarget(animation, root);
  264. var propertyChain = new DependencyProperty[]
  265. {
  266. Canvas.RenderTransformProperty,
  267. TransformGroup.ChildrenProperty,
  268. TranslateTransform.XProperty
  269. };
  270. string thePath = "(0).(1)[1].(2)";
  271. PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
  272. Storyboard.SetTargetProperty(animation, myPropertyPath);
  273. storyBoard.Completed += (s, e) =>
  274. {
  275. //InvalidateVisual();
  276. if (onComplete != null)
  277. {
  278. onComplete();
  279. }
  280. };
  281. storyBoard.Begin();
  282. }
  283. private void MoveTo(string target)
  284. {
  285. MoveRobot(target);
  286. }
  287. private void Invoke(Action action)
  288. {
  289. Dispatcher.Invoke(action);
  290. }
  291. private void LogMsg(string msg)
  292. {
  293. var source = "Robot";
  294. Console.WriteLine("{0} {1}", source, msg);
  295. }
  296. }
  297. //public enum BladeStatusLP
  298. //{
  299. // Extend,
  300. // Retract
  301. //}
  302. //public class Robot2Position
  303. //{
  304. // public int? X;
  305. // public int Root;
  306. // public int Arm;
  307. // public int Hand;
  308. //}
  309. //public class StationPosition2
  310. //{
  311. // public Robot2Position StartPosition;
  312. // public Robot2Position EndPosition;
  313. //}
  314. }