AutoAdjustmentParameter.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.Remoting.Messaging;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Controls.Primitives;
  10. using Team.FFFeederService.Enums;
  11. using Team.FFFeederService.Interfaces;
  12. using Team.FFFeederService.Models;
  13. namespace Team.FFFeederService
  14. {
  15. /// <summary>
  16. /// Feeder自动调整参数
  17. /// </summary>
  18. public class AutoAdjustmentParameter : IDisposable
  19. {
  20. /// <summary>
  21. /// 动作的编号
  22. /// </summary>
  23. public int ActionIndex { get; set; }
  24. // 声明一个委托字段,其签名与A中的GetDataFromA方法匹配
  25. private readonly Func<Tuple<bool, float, float>> dataFetchDelegate;
  26. public FeederModel FeederModel { get; set; }
  27. private IVoiceCoilMotorFeeder Feeder;
  28. public VibrationFrequencyDetector detector;
  29. /// <summary>
  30. /// 振动参数改变
  31. /// </summary>
  32. public event Action<SingleActionParam> OnVibrationParamChanged;
  33. /// <summary>
  34. /// 构造函数
  35. /// </summary>
  36. /// <param name="actionParam">动作参数</param>
  37. /// <param name="rectangle">Feeder盘面的矩形</param>
  38. /// <param name="_dataFetchDelegate">回调函数,获取相机拍照后的结果</param>
  39. public AutoAdjustmentParameter(IVoiceCoilMotorFeeder feeder,int actionIndex,Func<Tuple<bool, float, float>> _dataFetchDelegate)
  40. {
  41. Feeder = feeder;
  42. ActionIndex= actionIndex;
  43. dataFetchDelegate = _dataFetchDelegate;
  44. FeederModel = Feeder.FeederModel;
  45. int feedermodel = 0;
  46. switch (Feeder.FeederModel)
  47. {
  48. case FeederModel.FF120:
  49. feedermodel = 0;
  50. break;
  51. case FeederModel.FF240:
  52. feedermodel = 0;
  53. break;
  54. case FeederModel.FF380:
  55. feedermodel = 1;
  56. break;
  57. case FeederModel.FF530:
  58. feedermodel = 2;
  59. break;
  60. case FeederModel.Unknown:
  61. feedermodel = 0;
  62. break;
  63. default:
  64. break;
  65. }
  66. detector=new VibrationFrequencyDetector();
  67. detector.Initialize(feedermodel, ActionIndex);
  68. // 订阅事件
  69. detector.OnExecuteFeedervibration += data =>
  70. {
  71. //Feeder振动参数设置并执行动作
  72. int action = data.inFedAct;
  73. SingleActionParam actionp = new SingleActionParam
  74. {
  75. Motor1 = new VoiceMotorParam
  76. {
  77. Amplitude = data.u16VoiMot0,
  78. Frequency = data.u16VoiMot1,
  79. Phase = data.u16VoiMot2,
  80. WaveShape = (WaveShape)data.u16VoiMot3,
  81. },
  82. Motor2 = new VoiceMotorParam
  83. {
  84. Amplitude = data.u16VoiMot4,
  85. Frequency = data.u16VoiMot5,
  86. Phase = data.u16VoiMot6,
  87. WaveShape = (WaveShape)data.u16VoiMot7,
  88. },
  89. Motor3 = new VoiceMotorParam
  90. {
  91. Amplitude = data.u16VoiMot8,
  92. Frequency = data.u16VoiMot9,
  93. Phase = data.u16VoiMotA,
  94. WaveShape = (WaveShape)data.u16VoiMotB,
  95. },
  96. Motor4 = new VoiceMotorParam
  97. {
  98. Amplitude = data.u16VoiMotC,
  99. Frequency = data.u16VoiMotD,
  100. Phase = data.u16VoiMotE,
  101. WaveShape = (WaveShape)data.u16VoiMotF,
  102. },
  103. DurationValue = data.u16DuartTim
  104. };
  105. OnVibrationParamChanged?.Invoke(actionp);
  106. ExecuteFeederVibration(action, actionp);
  107. };
  108. detector.OnExecutePhoto += () =>
  109. {
  110. /// 相机拍照获取物料位置
  111. return dataFetchDelegate?.Invoke();
  112. };
  113. }
  114. public async Task<bool> Execute(int amplitudeMax, int amplitudeMin, int frequencyMax, int frequencyMin,int duration, CancellationToken token,Action<int> callback)
  115. {
  116. //float currentX = 0, currentY=0;
  117. //float oldX = 0, oldY=0;
  118. //int counter = 0;
  119. int amplitude = amplitudeMin;
  120. int frequency = frequencyMin;
  121. await Feeder.OpenLightAsync();
  122. detector.OnExecuteResult += status =>
  123. {
  124. callback?.Invoke(status);
  125. };
  126. while (!token.IsCancellationRequested)
  127. {
  128. detector.StartTask();
  129. await Task.Delay(100);
  130. }
  131. detector.Dispose();
  132. return false;
  133. }
  134. /// <summary>
  135. /// 执行振动
  136. /// </summary>
  137. /// <param name="action">动作索引</param>
  138. /// <returns></returns>
  139. public async Task ExecuteAction(int action,int time)
  140. {
  141. await Feeder.StartAction(action, time);
  142. }
  143. /// <summary>
  144. /// 写入震动参数
  145. /// </summary>
  146. /// <param name="action"></param>
  147. /// <param name="actionp"></param>
  148. /// <returns></returns>
  149. public async Task WriteActionParm(int action, SingleActionParam actionp)
  150. {
  151. await Feeder.SetShakeParametersAsync(action, actionp);
  152. }
  153. public void ExecuteFeederVibration(int action, SingleActionParam actionp)
  154. {
  155. Task.Run(async () =>
  156. {
  157. await WriteActionParm(action, actionp);
  158. await ExecuteAction(action, actionp.DurationValue);
  159. await Task.Delay(actionp.DurationValue+1000);
  160. }).GetAwaiter().GetResult();
  161. }
  162. /// <summary>
  163. /// 设置寻频返回值0:停止 1 :启动 2:验证 3:结束
  164. /// </summary>
  165. /// <param name="state">0:停止 1 :启动 2:验证 3:结束</param>
  166. public void SetState(int state)
  167. {
  168. detector.SetLogicState(state);
  169. }
  170. public void Dispose()
  171. {
  172. if (detector!=null)
  173. {
  174. detector.Dispose();
  175. }
  176. }
  177. }
  178. }