| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.Remoting.Messaging;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Controls.Primitives;
- using Team.FFFeederService.Enums;
- using Team.FFFeederService.Interfaces;
- using Team.FFFeederService.Models;
- namespace Team.FFFeederService
- {
- /// <summary>
- /// Feeder自动调整参数
- /// </summary>
- public class AutoAdjustmentParameter : IDisposable
- {
- /// <summary>
- /// 动作的编号
- /// </summary>
- public int ActionIndex { get; set; }
- // 声明一个委托字段,其签名与A中的GetDataFromA方法匹配
- private readonly Func<Tuple<bool, float, float>> dataFetchDelegate;
- public FeederModel FeederModel { get; set; }
- private IVoiceCoilMotorFeeder Feeder;
- public VibrationFrequencyDetector detector;
- /// <summary>
- /// 振动参数改变
- /// </summary>
- public event Action<SingleActionParam> OnVibrationParamChanged;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="actionParam">动作参数</param>
- /// <param name="rectangle">Feeder盘面的矩形</param>
- /// <param name="_dataFetchDelegate">回调函数,获取相机拍照后的结果</param>
- public AutoAdjustmentParameter(IVoiceCoilMotorFeeder feeder,int actionIndex,Func<Tuple<bool, float, float>> _dataFetchDelegate)
- {
- Feeder = feeder;
- ActionIndex= actionIndex;
- dataFetchDelegate = _dataFetchDelegate;
- FeederModel = Feeder.FeederModel;
- int feedermodel = 0;
- switch (Feeder.FeederModel)
- {
- case FeederModel.FF120:
- feedermodel = 120;
- break;
- case FeederModel.FF240:
- feedermodel = 240;
- break;
- case FeederModel.FF380:
- feedermodel = 380;
- break;
- case FeederModel.FF530:
- feedermodel = 530;
- break;
- case FeederModel.Unknown:
- feedermodel = 380;
- break;
- default:
- break;
- }
- detector=new VibrationFrequencyDetector();
- detector.Initialize(feedermodel, ActionIndex);
- // 订阅事件
- detector.OnExecuteFeedervibration += data =>
- {
- //Feeder振动参数设置并执行动作
- int action = data.inFedAct;
- SingleActionParam actionp = new SingleActionParam
- {
- Motor1 = new VoiceMotorParam
- {
- Amplitude = data.u16VoiMot0,
- Frequency = data.u16VoiMot1,
- Phase = data.u16VoiMot2,
- WaveShape = (WaveShape)data.u16VoiMot3,
- },
- Motor2 = new VoiceMotorParam
- {
- Amplitude = data.u16VoiMot4,
- Frequency = data.u16VoiMot5,
- Phase = data.u16VoiMot6,
- WaveShape = (WaveShape)data.u16VoiMot7,
- },
- Motor3 = new VoiceMotorParam
- {
- Amplitude = data.u16VoiMot8,
- Frequency = data.u16VoiMot9,
- Phase = data.u16VoiMotA,
- WaveShape = (WaveShape)data.u16VoiMotB,
- },
- Motor4 = new VoiceMotorParam
- {
- Amplitude = data.u16VoiMotC,
- Frequency = data.u16VoiMotD,
- Phase = data.u16VoiMotE,
- WaveShape = (WaveShape)data.u16VoiMotF,
- },
- DurationValue = data.u16DuartTim
- };
- OnVibrationParamChanged?.Invoke(actionp);
- ExecuteFeederVibration(action, actionp);
- };
- detector.OnExecutePhoto += () =>
- {
- /// 相机拍照获取物料位置
- return dataFetchDelegate?.Invoke();
- };
- }
- public async Task<bool> Execute(int amplitudeMax, int amplitudeMin, int frequencyMax, int frequencyMin,int duration, CancellationToken token,Action<int> callback)
- {
-
- float currentX = 0, currentY=0;
- float oldX = 0, oldY=0;
- int counter = 0;
- int amplitude = amplitudeMin;
- int frequency = frequencyMin;
- await Feeder.OpenLightAsync();
- detector.OnExecuteResult += status =>
- {
- callback?.Invoke(status);
- };
- while (!token.IsCancellationRequested)
- {
- detector.StartTask();
- await Task.Delay(100);
- }
- detector.Dispose();
- return false;
- }
- /// <summary>
- /// 执行振动
- /// </summary>
- /// <param name="action">动作索引</param>
- /// <returns></returns>
- public async Task ExecuteAction(int action,int time)
- {
- await Feeder.StartAction(action, time);
- }
- /// <summary>
- /// 写入震动参数
- /// </summary>
- /// <param name="action"></param>
- /// <param name="actionp"></param>
- /// <returns></returns>
- public async Task WriteActionParm(int action, SingleActionParam actionp)
- {
- await Feeder.SetShakeParametersAsync(action, actionp);
- }
- public void ExecuteFeederVibration(int action, SingleActionParam actionp)
- {
- Task.Run(async () =>
- {
- await WriteActionParm(action, actionp);
- await ExecuteAction(action, actionp.DurationValue);
- await Task.Delay(actionp.DurationValue+1000);
- }).GetAwaiter().GetResult();
- }
- /// <summary>
- /// 设置寻频返回值0:停止 1 :启动 2:验证 3:结束
- /// </summary>
- /// <param name="state">0:停止 1 :启动 2:验证 3:结束</param>
- public void SetState(int state)
- {
- detector.SetLogicState(state);
- }
- public void Dispose()
- {
- if (detector!=null)
- {
- detector.Dispose();
- }
- }
- }
- }
|