CalibrationCameraParamsViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using Team.FFFeederService.Interfaces;
  17. using TeamAAS_VP;
  18. using TeamAAS_VP.Core;
  19. using TeamAAS_VP.Interfaces;
  20. using TeamAAS_VP.Models;
  21. using TeamAAS_VP.Models.Calibration;
  22. using TeamAAS_VP.Resources.Languages;
  23. namespace TeamAAS_VP.ViewModels.Calibration
  24. {
  25. public class CalibrationCameraParamsViewModel : BindableBase, IConfirmNavigationRequest
  26. {
  27. IRegionManager _regionManager;
  28. IRegionNavigationService _regionNavigationService;
  29. IContainerProvider _container;
  30. IDialogService _dialogService;
  31. IEventAggregator _eventAggregator;
  32. IConfigService _configService;
  33. IRobotService _robotService;
  34. ICameraService _cameraService;
  35. IFeederService _feederService;
  36. ISystemDatabaseService _systemDatabaseService;
  37. #region 属性
  38. private CalibrationInfo _SelectCalibration;
  39. public CalibrationInfo SelectCalibration
  40. {
  41. get { return _SelectCalibration; }
  42. set { SetProperty(ref _SelectCalibration, value); }
  43. }
  44. private IRobot _Robot;
  45. public IRobot Robot
  46. {
  47. get { return _Robot; }
  48. set { SetProperty(ref _Robot, value); }
  49. }
  50. private IVoiceCoilMotorFeeder _Feeder;
  51. public IVoiceCoilMotorFeeder Feeder
  52. {
  53. get { return _Feeder; }
  54. set { SetProperty(ref _Feeder, value); }
  55. }
  56. private ICamera _Camera;
  57. public ICamera Camera
  58. {
  59. get { return _Camera; }
  60. set { SetProperty(ref _Camera, value); }
  61. }
  62. private bool _IsShowLight;
  63. public bool IsShowLight
  64. {
  65. get { return _IsShowLight; }
  66. set { SetProperty(ref _IsShowLight, value); }
  67. }
  68. private float _ExposureTime;
  69. /// <summary>
  70. /// 相机曝光时间
  71. /// </summary>
  72. public float ExposureTime
  73. {
  74. get { return _ExposureTime; }
  75. set
  76. {
  77. SetProperty(ref _ExposureTime, value);
  78. SelectCalibration.ExposureTime = value;
  79. if (Camera != null)
  80. {
  81. try
  82. {
  83. Camera.SetExposureTime(value);
  84. }
  85. catch (Exception)
  86. {
  87. }
  88. }
  89. }
  90. }
  91. private float _Gain;
  92. /// <summary>
  93. /// 相机增益
  94. /// </summary>
  95. public float Gain
  96. {
  97. get { return _Gain; }
  98. set
  99. {
  100. SetProperty(ref _Gain, value);
  101. SelectCalibration.Gain = value;
  102. if (Camera != null)
  103. {
  104. try
  105. {
  106. Camera.SetGain(value);
  107. }
  108. catch (Exception)
  109. {
  110. }
  111. }
  112. }
  113. }
  114. private ICogImage _Image;
  115. public ICogImage Image
  116. {
  117. get { return _Image; }
  118. set { SetProperty(ref _Image,value); }
  119. }
  120. #endregion
  121. #region 命令
  122. private DelegateCommand _LoadedCommand;
  123. public DelegateCommand LoadedCommand =>
  124. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  125. private DelegateCommand _NextCommand;
  126. public DelegateCommand NextCommand =>
  127. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  128. private DelegateCommand _BackCommand;
  129. public DelegateCommand BackCommand =>
  130. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  131. private DelegateCommand<double?> _LightValueChangedCommand;
  132. public DelegateCommand<double?> LightValueChangedCommand =>
  133. _LightValueChangedCommand ?? (_LightValueChangedCommand = new DelegateCommand<double?>(ExecuteLightValueChangedCommand));
  134. private DelegateCommand _StartGrabbingCommand;
  135. public DelegateCommand StartGrabbingCommand =>
  136. _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand));
  137. private DelegateCommand _StopGrabbingCommand;
  138. public DelegateCommand StopGrabbingCommand =>
  139. _StopGrabbingCommand ?? (_StopGrabbingCommand = new DelegateCommand(ExecuteStopGrabbingCommand));
  140. #endregion
  141. #region 事件
  142. #endregion
  143. public CalibrationCameraParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  144. IConfigService configService,IRobotService robotService,ICameraService cameraService,IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  145. {
  146. _regionManager = regionManager;
  147. _regionNavigationService = regionNavigationService;
  148. _container = container;
  149. _dialogService = dialogService;
  150. _eventAggregator = ea;
  151. _configService = configService;
  152. _robotService = robotService;
  153. _cameraService = cameraService;
  154. _feederService = feederService;
  155. _systemDatabaseService = systemDatabaseService;
  156. }
  157. #region 方法
  158. async void ExecuteLoadedCommand()
  159. {
  160. try
  161. {
  162. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  163. {
  164. IsAllowEdit = false;
  165. }
  166. else
  167. {
  168. IsAllowEdit = true;
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. LogHelper.WriteLogError(Lang.相机校准加载相机参数界面时出错, ex);
  174. MessageBox.Show(ex.Message);
  175. }
  176. }
  177. /// <summary>
  178. /// 下一步
  179. /// </summary>
  180. void ExecuteNextCommand()
  181. {
  182. SelectCalibration.ExposureTime = ExposureTime;
  183. SelectCalibration.Gain = Gain;
  184. if (Camera != null)
  185. {
  186. Camera.StopGrabbing();
  187. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  188. }
  189. NavigationParameters param = new NavigationParameters();
  190. param.Add("SelectCalibration", SelectCalibration);
  191. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  192. }
  193. /// <summary>
  194. /// 上一步
  195. /// </summary>
  196. void ExecuteBackCommand()
  197. {
  198. if (Camera != null)
  199. {
  200. Camera.StopGrabbing();
  201. Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
  202. }
  203. NavigationParameters param = new NavigationParameters();
  204. param.Add("SelectCalibration", SelectCalibration);
  205. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationBasics", param);
  206. }
  207. /// <summary>
  208. /// 光源亮度调整时
  209. /// </summary>
  210. async void ExecuteLightValueChangedCommand(double? lightvalue)
  211. {
  212. try
  213. {
  214. if (lightvalue == null) return;
  215. if (Feeder == null || !Feeder.IsConnected) return;
  216. await Feeder.SetLightValueAsync((ushort)lightvalue);
  217. }
  218. catch (Exception ex)
  219. {
  220. LogHelper.WriteLogError(Lang.相机校准光源亮度调整时出错, ex);
  221. MessageBox.Show(ex.Message);
  222. }
  223. }
  224. /// <summary>
  225. /// 停止采集
  226. /// </summary>
  227. void ExecuteStopGrabbingCommand()
  228. {
  229. if (Camera != null)
  230. Camera.StopGrabbing();
  231. }
  232. /// <summary>
  233. /// 开始采集
  234. /// </summary>
  235. void ExecuteStartGrabbingCommand()
  236. {
  237. if (Camera != null)
  238. Camera.StartGrabbing();
  239. }
  240. private void Camera_ImageCallbackEvent(ICogImage image,TimeSpan totaltime,string errormessage)
  241. {
  242. Image = image;
  243. }
  244. #endregion
  245. #region 继承
  246. /// <summary>
  247. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  248. /// </summary>
  249. /// <param name="navigationContext"></param>
  250. /// <param name="continuationCallback"></param>
  251. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  252. {
  253. continuationCallback(true);
  254. }
  255. /// <summary>
  256. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  257. /// </summary>
  258. /// <param name="navigationContext"></param>
  259. /// <exception cref="NotImplementedException"></exception>
  260. public async void OnNavigatedTo(NavigationContext navigationContext)
  261. {
  262. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  263. Camera = _cameraService.GetCamera(SelectCalibration.CameraID);
  264. if (SelectCalibration.ExposureTime == 0)
  265. {
  266. SelectCalibration.ExposureTime = 5000;
  267. }
  268. ExposureTime = SelectCalibration.ExposureTime;
  269. Gain = SelectCalibration.Gain;
  270. if (Camera != null)
  271. {
  272. Camera.SetExposureTime(ExposureTime);
  273. Camera.SetGain(Gain);
  274. Camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
  275. //Camera.Grab();
  276. }
  277. if (SelectCalibration.IsCreate && SelectCalibration.ToolBlock == null)
  278. {
  279. SelectCalibration.ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath);
  280. }
  281. else if (SelectCalibration.ToolBlock == null)
  282. {
  283. string prcPath = FilePath.CalibrationPath + "//" + SelectCalibration.Name + "//" + SelectCalibration.Name + ".vpp";
  284. if (File.Exists(prcPath))
  285. {
  286. SelectCalibration.ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(prcPath);
  287. }
  288. else
  289. {
  290. SelectCalibration.ToolBlock = VisionProFileHelper.LoadObjectFromFileSafe<CogToolBlock>(FilePath.CalibrationToolblockPath);
  291. }
  292. }
  293. if (SelectCalibration.RobotId != Guid.Empty)
  294. {
  295. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  296. Robot.EnterDebugMode = true;
  297. }
  298. if (SelectCalibration.FeederId != Guid.Empty)
  299. {
  300. Feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  301. if (Feeder.IsConnected)
  302. {
  303. await Feeder.OpenLightAsync();
  304. await Feeder.SetLightValueAsync((ushort)SelectCalibration.Brightness);
  305. }
  306. IsShowLight = true;
  307. }
  308. else
  309. {
  310. IsShowLight = false;
  311. }
  312. }
  313. /// <summary>
  314. /// 是否允许导航到此视图模型。
  315. /// </summary>
  316. /// <param name="navigationContext"></param>
  317. /// <returns></returns>
  318. public bool IsNavigationTarget(NavigationContext navigationContext)
  319. {
  320. return true;
  321. }
  322. /// <summary>
  323. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  324. /// </summary>
  325. /// <param name="navigationContext"></param>
  326. public void OnNavigatedFrom(NavigationContext navigationContext)
  327. {
  328. ExecuteStopGrabbingCommand();
  329. }
  330. #endregion
  331. private bool _IsAllowEdit = false;
  332. public bool IsAllowEdit
  333. {
  334. get { return _IsAllowEdit; }
  335. set { SetProperty(ref _IsAllowEdit, value); }
  336. }
  337. private void LoginChange(Models.User user)
  338. {
  339. if (user.userPart == Enums.UserPart.Operator)
  340. {
  341. IsAllowEdit = false;
  342. }
  343. else
  344. {
  345. IsAllowEdit = true;
  346. }
  347. }
  348. }
  349. }