LinMotAxis.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.Util;
  7. using MECF.Framework.Common.Device.LinMot;
  8. using CyberX8_Core;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using Aitex.Core.RT.Routine;
  16. using MECF.Framework.Common.ToolLayout;
  17. using System.Diagnostics;
  18. using Aitex.Core.RT.SCCore;
  19. namespace CyberX8_RT.Devices.LinMot
  20. {
  21. public class LinMotAxis : BaseDevice, IDevice
  22. {
  23. #region 常量
  24. /// <summary>
  25. /// 控制字
  26. /// </summary>
  27. private const string STATUS_WORD = "StatusWord";
  28. private const string HOME = "Home";
  29. private const string SWITCH_ON = "SwitchOn";
  30. private const string DIRECTION = "Direction";
  31. private const string POSITION = "Position";
  32. private const string SCAN_COUNT = "ScanCount";
  33. /// <summary>
  34. /// 下一指令判定条件与_startPosition的距离
  35. /// </summary>
  36. private const double _nextInterval = 2;
  37. #endregion
  38. #region 内部变量
  39. /// <summary>
  40. /// Home状态
  41. /// </summary>
  42. private bool _isHomed;
  43. /// <summary>
  44. /// SwitchOn状态
  45. /// </summary>
  46. private bool _isSwitchOn;
  47. /// <summary>
  48. /// Disabled状态
  49. /// </summary>
  50. private bool _isDisabled;
  51. /// <summary>
  52. /// 错误状态
  53. /// </summary>
  54. private bool _isError;
  55. /// <summary>
  56. /// 致命错误状态
  57. /// </summary>
  58. private bool _isFatalError;
  59. /// <summary>
  60. /// 运动状态
  61. /// </summary>
  62. private bool _isMotorOn;
  63. /// <summary>
  64. /// 是否到达目标
  65. /// </summary>
  66. private bool _inTagertPosition = false;
  67. /// <summary>
  68. /// 操作当前状态
  69. /// </summary>
  70. private RState _status;
  71. /// <summary>
  72. /// 当前操作指令
  73. /// </summary>
  74. private LinMotOperation _currentOperation = LinMotOperation.None;
  75. /// <summary>
  76. /// 状态字
  77. /// </summary>
  78. private short _statusWord;
  79. /// <summary>
  80. /// LinMotId
  81. /// </summary>
  82. private byte _linmotId;
  83. /// <summary>
  84. /// 错误代码
  85. /// </summary>
  86. private short _errorCode;
  87. /// <summary>
  88. /// Reset Routine
  89. /// </summary>
  90. private LinMotResetRoutine _resetRoutine;
  91. /// <summary>
  92. /// Stop Motor Routine
  93. /// </summary>
  94. private LinMotStopMotorRoutine _stopMotorRoutine;
  95. /// <summary>
  96. /// Start Curve
  97. /// </summary>
  98. private LinMotStartContinueCurveRoutine _startContinueCurveRoutine;
  99. /// <summary>
  100. /// Start VAI
  101. /// </summary>
  102. private LinMotStartVAIPositionRoutine _startVAIPositionRoutine;
  103. /// <summary>
  104. /// Curve速度
  105. /// </summary>
  106. private int _curverSpeed = 100;
  107. /// <summary>
  108. /// Curver指定的Id
  109. /// </summary>
  110. private byte _curveId = 1;
  111. /// <summary>
  112. /// 指令发送时间(用于空闲时发送读取状态指令)
  113. /// </summary>
  114. private DateTime _sendTime = DateTime.Now;
  115. /// <summary>
  116. /// 运动时间(用于判定运动指令发送LinMot并没有执行上报超时)
  117. /// </summary>
  118. private DateTime _motorTime = DateTime.Now;
  119. /// <summary>
  120. /// 设备参数对象
  121. /// </summary>
  122. private LinMotDevice _linMotDeviceParam;
  123. /// <summary>
  124. /// 开始时的方向
  125. /// </summary>
  126. private string _startDirection = "";
  127. /// <summary>
  128. /// 当前GAI GO To Position方向
  129. /// </summary>
  130. private string _direction = "";
  131. /// <summary>
  132. /// 当前位置
  133. /// </summary>
  134. private double _currentPosition =0;
  135. /// <summary>
  136. /// 当前位置(类型为整形)
  137. /// </summary>
  138. private int _currentIntPosition = 0;
  139. /// 用于prewet页面UI的位置数据
  140. /// </summary>
  141. private double _currentPrewetUIPostion = 0;
  142. /// <summary>
  143. /// <summary>
  144. /// 判定方向位置
  145. /// </summary>
  146. private double _judgePosition = 0;
  147. /// <summary>
  148. /// GAI运动前的位置
  149. /// </summary>
  150. private double _startPosition = 0;
  151. /// <summary>
  152. /// 状态字时间
  153. /// </summary>
  154. private DateTime _statusWordDateTime=DateTime.Now;
  155. /// <summary>
  156. /// linmot stop watch
  157. /// </summary>
  158. private Stopwatch _linmotStopWatch = new Stopwatch();
  159. /// <summary>
  160. /// 状态字上升沿信号
  161. /// </summary>
  162. private R_TRIG _statusTrigger = new R_TRIG();
  163. #endregion
  164. #region 属性
  165. /// <summary>
  166. /// 操作当前状态
  167. /// </summary>
  168. public RState Status { get { return _status; } }
  169. /// <summary>
  170. /// Home
  171. /// </summary>
  172. public bool IsHomed { get { return IsConnectd && _isHomed; } }
  173. /// <summary>
  174. /// SwitchOn状态
  175. /// </summary>
  176. public bool IsSwitchOn { get { return IsConnectd && _isSwitchOn; } }
  177. /// <summary>
  178. /// 错误
  179. /// </summary>
  180. public bool IsError { get { return _isError; } }
  181. /// <summary>
  182. /// Disabled状态
  183. /// </summary>
  184. public bool IsDisabled { get { return IsConnectd&&_isDisabled; } }
  185. /// <summary>
  186. /// 运动状态
  187. /// </summary>
  188. public bool IsMotorOn
  189. {
  190. get
  191. {
  192. if(_isMotorOn)
  193. {
  194. return true;
  195. }
  196. else
  197. {
  198. if (_linmotStopWatch.ElapsedMilliseconds <= 2000)
  199. {
  200. return true;
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 是否已经到达目标
  211. /// </summary>
  212. public bool InTargetPosition { get { return _inTagertPosition; } }
  213. /// <summary>
  214. /// 当前位置
  215. /// </summary>
  216. public double CurrentPosition { get { return _currentPosition; } }
  217. /// <summary>
  218. /// 方向
  219. /// </summary>
  220. public string Direction { get { return _direction; } }
  221. /// <summary>
  222. /// 连接状态
  223. /// </summary>
  224. public bool IsConnectd { get { return LinMotDeviceConfigManager.Instance.GetDeviceConnect(Module.ToString()); } }
  225. /// <summary>
  226. /// 扫描次数
  227. /// </summary>
  228. public int ScanCount { get { return _startVAIPositionRoutine.CurrentScan; } }
  229. /// <summary>
  230. /// 当前位置
  231. /// </summary>
  232. public int CurrentIntPosition { get { return _currentIntPosition; } }
  233. /// <summary>
  234. /// 最后是否SwitchOff
  235. /// </summary>
  236. public bool LastSwitchOff { get; set; } = false;
  237. /// <summary>
  238. /// curve速度
  239. /// </summary>
  240. public int CurveSpeed
  241. {
  242. get { return _curverSpeed; }
  243. }
  244. /// <summary>
  245. /// 错误代码
  246. /// </summary>
  247. public short ErrorCode
  248. {
  249. get { return _errorCode; }
  250. }
  251. #endregion
  252. /// <summary>
  253. /// 构造函数
  254. /// </summary>
  255. /// <param name="moduleName"></param>
  256. /// <param name="name"></param>
  257. public LinMotAxis(string moduleName) : base(moduleName, moduleName, moduleName, moduleName)
  258. {
  259. InitialData();
  260. InitializeOperation();
  261. InitializeRoute();
  262. _linmotStopWatch.Restart();
  263. }
  264. /// <summary>
  265. /// 初始化Routine
  266. /// </summary>
  267. private void InitializeRoute()
  268. {
  269. _resetRoutine = new LinMotResetRoutine(Module,this);
  270. _stopMotorRoutine=new LinMotStopMotorRoutine(Module,this);
  271. _startContinueCurveRoutine=new LinMotStartContinueCurveRoutine(Module,this);
  272. _startVAIPositionRoutine=new LinMotStartVAIPositionRoutine(Module,this);
  273. }
  274. /// <summary>
  275. /// 初始化OP
  276. /// </summary>
  277. private void InitializeOperation()
  278. {
  279. OP.Subscribe($"{Module}.Reset",(cmd,args)=> { return ResetOperation(cmd, LastSwitchOff); });
  280. OP.Subscribe($"{Module}.StartCurve", StartCurveOperation);
  281. OP.Subscribe($"{Module}.StartPosition", StartPosition);
  282. OP.Subscribe($"{Module}.Stop", StopOperation);
  283. OP.Subscribe($"{Module}.Abort", AbortOperation);
  284. OP.Subscribe($"{Module}.UpdateSpeedData",UpdateSpeedDataOperation);
  285. LinMotDeviceConfigManager.Instance.InitialDevice(Module.ToString());
  286. LinMotDeviceConfigManager.Instance.SubscribeModuleVariable(Module, UpdateStatusWord);
  287. }
  288. /// <summary>
  289. /// 初始化数据
  290. /// </summary>
  291. private void InitialData()
  292. {
  293. _linMotDeviceParam = LinMotDeviceConfigManager.Instance.GetLinMotDevice(Module);
  294. if (_linMotDeviceParam == null)
  295. {
  296. LOG.WriteLog(eEvent.ERR_LINMOT, Module.ToString(), "Get Address Id Error"); ;
  297. return;
  298. }
  299. if (byte.TryParse(_linMotDeviceParam.Address, out var tmpLinmotId))
  300. {
  301. _linmotId = tmpLinmotId;
  302. }
  303. DATA.Subscribe($"{Module}.{STATUS_WORD}", () => IsConnectd?_statusWord:0,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  304. DATA.Subscribe($"{Module}.{HOME}", () => IsHomed,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  305. DATA.Subscribe($"{Module}.{SWITCH_ON}", () => IsSwitchOn, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  306. DATA.Subscribe($"{Module}.{DIRECTION}", () => _direction,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  307. DATA.Subscribe($"{Module}.{POSITION}", () => _currentPosition, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  308. DATA.Subscribe($"{Module}.{SCAN_COUNT}",()=>ScanCount,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  309. DATA.Subscribe($"{Module}.Speed", () => _curverSpeed, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  310. DATA.Subscribe($"{Module}.IsConnectd", () => IsConnectd, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  311. DATA.Subscribe($"{Module}.CurrentPrewetUIPostion", () => _currentPrewetUIPostion, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  312. DATA.Subscribe($"{Module}.LinmotDeviceData", () => _linMotDeviceParam.LinMotDeviceData, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  313. }
  314. /// <summary>
  315. /// 更新状态字
  316. /// </summary>
  317. /// <param name="cmd"></param>
  318. /// <param name="args"></param>
  319. /// <returns></returns>
  320. private void UpdateStatusWord(short statusWord,short errorCode,int position)
  321. {
  322. _statusWordDateTime=DateTime.Now;
  323. _statusTrigger.CLK = _statusWord != statusWord;
  324. if (_statusTrigger.Q)
  325. {
  326. LOG.WriteLog(eEvent.INFO_LINMOT, Module, $"statusword is {statusWord}");
  327. }
  328. _statusWord = statusWord;
  329. UpdateStatusWord(statusWord);
  330. short tmpErrorCode = errorCode;
  331. NotifyError(tmpErrorCode);
  332. //ContinueGAIGotoPosition();
  333. double tmpPosition = (double)position / 10000;
  334. if (Math.Abs(tmpPosition - _currentPosition) >= 0.1)
  335. {
  336. _currentPosition = tmpPosition;
  337. bool enableLog = SC.GetValue<bool>("Log.EnableLinmotLog");
  338. if (enableLog)
  339. {
  340. LOG.WriteLog(eEvent.INFO_LINMOT, Module, $"motor status {_isMotorOn} position {_currentPosition}");
  341. }
  342. }
  343. _currentIntPosition = position;
  344. if (_linMotDeviceParam.LinMotDeviceData != null && _linMotDeviceParam.LinMotDeviceData.TopPosition - _linMotDeviceParam.LinMotDeviceData.BottomPosition != 0)
  345. {
  346. double proportion = _linMotDeviceParam.LinMotDeviceData.TopPosition - _linMotDeviceParam.LinMotDeviceData.BottomPosition;
  347. _currentPrewetUIPostion = 150 - 90 * Math.Abs(position / proportion); //150是prewet页面底部位置,90是页面顶部和底部距离之差
  348. }
  349. JudgeDirection();
  350. }
  351. /// <summary>
  352. /// 更新StatusWord
  353. /// </summary>
  354. /// <param name="statusWord"></param>
  355. private void UpdateStatusWord(short statusWord)
  356. {
  357. bool isHomed = (statusWord & 0b0100000000000) >> 11 == 1;//11位为Home
  358. if (isHomed != _isHomed)
  359. {
  360. _isHomed = isHomed;
  361. }
  362. _isDisabled = (statusWord & 0b0100) >> 2 == 0;
  363. _isSwitchOn = (statusWord & 0x01) == 1;
  364. _isError = ((statusWord & 0b01000) >> 3 == 1) || ((statusWord & 0b01000000000000) >> 12 == 1);
  365. _isFatalError = ((statusWord & 0b01000000000000) >> 12 == 1);
  366. bool isMotorOn = ((statusWord & 0b010000000000000) >> 13 == 1);//13是否正在运动
  367. if (isMotorOn != _isMotorOn)
  368. {
  369. _isMotorOn = isMotorOn;
  370. if (!_isMotorOn)
  371. {
  372. _linmotStopWatch.Restart();
  373. }
  374. }
  375. _inTagertPosition = (statusWord & 0b010000000000) >> 10 == 1;//10位是否到达目标
  376. }
  377. /// <summary>
  378. /// 通知错误
  379. /// </summary>
  380. /// <param name="tmpErrorCode"></param>
  381. private void NotifyError(short tmpErrorCode)
  382. {
  383. if (tmpErrorCode != _errorCode)
  384. {
  385. if (tmpErrorCode != 0)
  386. {
  387. string errorMsg = LinMotErrorCodeManager.Instance.GetErrorCodeText(tmpErrorCode);
  388. string str = string.IsNullOrEmpty(errorMsg) ? tmpErrorCode.ToString() : errorMsg;
  389. if (!_isFatalError)
  390. {
  391. LOG.WriteLog(eEvent.ERR_LINMOT, Module, $"{str},please reset error");
  392. }
  393. else
  394. {
  395. LOG.WriteLog(eEvent.ERR_LINMOT, Module, $"{str},meets fatal error,please reboot {Module}");
  396. }
  397. _currentOperation = LinMotOperation.None;
  398. _status = RState.Failed;
  399. }
  400. _errorCode = tmpErrorCode;
  401. }
  402. }
  403. /// <summary>
  404. /// 判定方向
  405. /// </summary>
  406. private void JudgeDirection()
  407. {
  408. if(_linMotDeviceParam!=null&&_linMotDeviceParam.LinMotDeviceData!=null&&_currentOperation==LinMotOperation.StartVAIGoToPosition)
  409. {
  410. double interval = _currentPosition - _judgePosition;
  411. if(Math.Abs(interval)>=1)
  412. {
  413. string tmpDirection = "";
  414. if(interval>0)
  415. {
  416. tmpDirection = "down";
  417. }
  418. else
  419. {
  420. tmpDirection = "up";
  421. }
  422. if(tmpDirection!=_direction)
  423. {
  424. //_sendNextGAIPosition = false;
  425. _direction = tmpDirection;
  426. }
  427. _judgePosition = _currentPosition;
  428. }
  429. }
  430. }
  431. /// <summary>
  432. /// Reset
  433. /// </summary>
  434. /// <param name="cmd"></param>
  435. /// <param name="args"></param>
  436. /// <returns></returns>
  437. public bool ResetOperation(string cmd, bool lastNeedSwitchoff)
  438. {
  439. if(_status==RState.Running)
  440. {
  441. LOG.WriteLog(eEvent.ERR_LINMOT,Module.ToString(), $"{Module} current execute {_currentOperation},cannot Reset");
  442. return false;
  443. }
  444. if(!LinMotDeviceConfigManager.Instance.GetDeviceConnect(Module))
  445. {
  446. LOG.WriteLog(eEvent.ERR_LINMOT, Module.ToString(), $"device is not connected");
  447. return false;
  448. }
  449. _currentOperation = LinMotOperation.Reset;
  450. _status= _resetRoutine.Start(lastNeedSwitchoff);
  451. _direction = "";
  452. return _status==RState.Running;
  453. }
  454. /// <summary>
  455. /// 开始Curve
  456. /// </summary>
  457. /// <param name="cmd"></param>
  458. /// <param name="args"></param>
  459. /// <returns></returns>
  460. public bool StartCurveOperation(string cmd, object[] args)
  461. {
  462. if (_status == RState.Running)
  463. {
  464. EV.PostAlarmLog(Module.ToString(), eEvent.INFO_LINMOT, $"{Module} current execute {_currentOperation},cannot curve");
  465. return false;
  466. }
  467. _motorTime = DateTime.Now;
  468. object[] param= (object[])args[1];
  469. int speed = (int)param[0];
  470. return StartCurve(speed);
  471. }
  472. /// <summary>
  473. /// 开始Curve
  474. /// </summary>
  475. /// <param name="speed"></param>
  476. /// <returns></returns>
  477. public bool StartCurve(int speed)
  478. {
  479. _currentOperation = LinMotOperation.StartCurve;
  480. _curverSpeed = speed;
  481. _status= _startContinueCurveRoutine.Start(_curverSpeed);
  482. LOG.WriteLog(eEvent.INFO_LINMOT, Module, $"Start Curve With Speed {_curverSpeed}%");
  483. return _status==RState.Running;
  484. }
  485. /// <summary>
  486. /// 开始Curve速度
  487. /// </summary>
  488. /// <param name="speed"></param>
  489. /// <returns></returns>
  490. public bool ChangeCurveSpeed(int speed)
  491. {
  492. _curverSpeed= speed;
  493. return WriteRamIntValue(0xCB, 0x14, speed * 100);
  494. }
  495. /// <summary>
  496. /// 开始移动(GAI Goto Position)
  497. /// </summary>
  498. /// <param name="cmd"></param>
  499. /// <param name="args"></param>
  500. /// <returns></returns>
  501. public bool StartPosition(string cmd, object[] args)
  502. {
  503. if (_linMotDeviceParam == null || _linMotDeviceParam.LinMotDeviceData == null)
  504. {
  505. LOG.WriteLog(eEvent.ERR_LINMOT, Module.ToString(), $"{Module} config error");
  506. return false;
  507. }
  508. if (_status == RState.Running)
  509. {
  510. LOG.WriteLog(eEvent.ERR_LINMOT,Module.ToString(), $"{Module} current execute {_currentOperation},cannot Start VAI Go To Position");
  511. return false;
  512. }
  513. _currentOperation = LinMotOperation.StartVAIGoToPosition;
  514. if (_linMotDeviceParam.LinMotDeviceData.TopPosition < 0)
  515. {
  516. _direction = "up";
  517. }
  518. else
  519. {
  520. _direction = "down";
  521. }
  522. _startDirection = _direction;
  523. _judgePosition = _currentPosition;
  524. _startPosition = _currentPosition;
  525. int totalScan = (int)args[0] ;
  526. _status= _startVAIPositionRoutine.Start(totalScan, _linMotDeviceParam.LinMotDeviceData);
  527. return _status==RState.Running;
  528. }
  529. /// <summary>
  530. /// SwitchOff
  531. /// </summary>
  532. /// <param name="cmd"></param>
  533. /// <param name="args"></param>
  534. /// <returns></returns>
  535. private bool AbortOperation(string cmd, object[] args)
  536. {
  537. if(_currentOperation==LinMotOperation.StartVAIGoToPosition)
  538. {
  539. _startVAIPositionRoutine.Abort();
  540. }
  541. _currentOperation = LinMotOperation.None;
  542. _status = RState.End;
  543. _direction = "";
  544. LOG.WriteLog(eEvent.INFO_LINMOT, Module, "switch off");
  545. return SendOperation(LinMotOperation.SwitchOff);
  546. }
  547. /// <summary>
  548. /// 更新linmot速度参数
  549. /// </summary>
  550. /// <param name="cmd"></param>
  551. /// <param name="args"></param>
  552. /// <returns></returns>
  553. private bool UpdateSpeedDataOperation(string cmd, object[] args)
  554. {
  555. string name = args[0].ToString();
  556. string direction = args[1].ToString();
  557. double maxSpeed = (double)args[2];
  558. int maxAcc = (int)args[3];
  559. int maxDec = (int)args[4];
  560. LinMotDeviceConfigManager.Instance.UpdateSpeedData(name, direction, maxSpeed,maxAcc,maxDec);
  561. LOG.WriteLog(eEvent.INFO_LINMOT, Module, $"{Module} Speed related paramater was updated");
  562. return true;
  563. }
  564. /// <summary>
  565. /// 发送Curve指令
  566. /// </summary>
  567. private void SendCurveCommand()
  568. {
  569. _motorTime = DateTime.Now;
  570. _sendTime = DateTime.Now;
  571. LinMotDeviceConfigManager.Instance.SendCurveOperation(Module.ToString(), _linmotId, _curveId,(ushort)_curverSpeed);
  572. }
  573. /// <summary>
  574. /// 发送GAI GoToPosition
  575. /// </summary>
  576. /// <param name="position"></param>
  577. /// <param name="speed"></param>
  578. /// <param name="accel"></param>
  579. /// <param name="decel"></param>
  580. public bool SendVAIGoToPosition(int position,int speed,int accel,int decel)
  581. {
  582. _motorTime = DateTime.Now;
  583. _sendTime = DateTime.Now;
  584. return LinMotDeviceConfigManager.Instance.SendVAIGotoPosition(Module.ToString(), _linmotId, position, speed,accel, decel);
  585. }
  586. /// <summary>
  587. /// Parameter Write RAM
  588. /// </summary>
  589. /// <param name="lowByte"></param>
  590. /// <param name="highByte"></param>
  591. /// <param name="intValue"></param>
  592. /// <returns></returns>
  593. public bool WriteRamIntValue(byte lowByte,byte highByte,int intValue)
  594. {
  595. _sendTime = DateTime.Now;
  596. return LinMotDeviceConfigManager.Instance.SendWriteRamIntValue(Module.ToString(), _linmotId, lowByte, highByte, intValue);
  597. }
  598. /// <summary>
  599. /// 发送Command Table
  600. /// </summary>
  601. /// <param name="entryId"></param>
  602. /// <returns></returns>
  603. public bool SendCommandTableOperation(short entryId)
  604. {
  605. _motorTime = DateTime.Now;
  606. _sendTime = DateTime.Now;
  607. return LinMotDeviceConfigManager.Instance.SendCommandTable(Module.ToString(), _linmotId, entryId);
  608. }
  609. /// <summary>
  610. /// 发送GAI GoToPositionAfterActualCommand
  611. /// </summary>
  612. /// <param name="position"></param>
  613. /// <param name="speed"></param>
  614. /// <param name="accel"></param>
  615. /// <param name="decel"></param>
  616. public bool SendGAIGoToPositionAfterActualCommand(int position, int speed, int accel, int decel)
  617. {
  618. _motorTime = DateTime.Now;
  619. _sendTime = DateTime.Now;
  620. return LinMotDeviceConfigManager.Instance.SendVAIGotoPositionAfterActualCommand(Module.ToString(), _linmotId,position, speed, accel, decel);
  621. }
  622. /// <summary>
  623. /// 停止
  624. /// </summary>
  625. /// <param name="cmd"></param>
  626. /// <param name="args"></param>
  627. /// <returns></returns>
  628. public bool StopOperation(string cmd, object[] args)
  629. {
  630. if (_currentOperation==LinMotOperation.StopMotor)
  631. {
  632. LOG.WriteLog(eEvent.WARN_LINMOT, Module, "current operation is stop motor,cannot repet stop motor");
  633. return false;
  634. }
  635. if (_currentOperation==LinMotOperation.StartVAIGoToPosition)
  636. {
  637. _startVAIPositionRoutine.Abort();
  638. }
  639. _status = RState.Running;
  640. _currentOperation = LinMotOperation.StopMotor;
  641. _stopMotorRoutine.Start(LastSwitchOff);
  642. _direction = "";
  643. return true;
  644. }
  645. /// <summary>
  646. /// 定时器
  647. /// </summary>
  648. /// <returns></returns>
  649. public bool OnTimer()
  650. {
  651. if (_status == RState.Running)
  652. {
  653. IRoutine routine = GetCurrentRoutine();
  654. if (routine != null)
  655. {
  656. RState rsState = routine.Monitor();
  657. if (rsState == RState.Failed || rsState == RState.Timeout)
  658. {
  659. _status = RState.Failed;
  660. _currentOperation = LinMotOperation.None;
  661. }
  662. else if (rsState == RState.End)
  663. {
  664. _status = RState.End;
  665. _currentOperation = LinMotOperation.None;
  666. }
  667. }
  668. }
  669. SendReadStatusCommand();
  670. if(DateTime.Now.Subtract(_statusWordDateTime).TotalSeconds>=2)
  671. {
  672. _statusWord = 0;
  673. UpdateStatusWord(_statusWord);
  674. }
  675. return true;
  676. }
  677. /// <summary>
  678. /// 当前Routine;
  679. /// </summary>
  680. /// <returns></returns>
  681. private IRoutine GetCurrentRoutine()
  682. {
  683. switch(_currentOperation)
  684. {
  685. case LinMotOperation.StartVAIGoToPosition:
  686. return _startVAIPositionRoutine;
  687. case LinMotOperation.StartCurve:
  688. return _startContinueCurveRoutine;
  689. case LinMotOperation.Reset:
  690. return _resetRoutine;
  691. case LinMotOperation.StopMotor:
  692. return _stopMotorRoutine;
  693. default:
  694. return null;
  695. }
  696. }
  697. /// <summary>
  698. /// 监控
  699. /// </summary>
  700. public void Monitor()
  701. {
  702. }
  703. /// <summary>
  704. /// 发送读取状态命令
  705. /// </summary>
  706. private void SendReadStatusCommand()
  707. {
  708. if (LinMotDeviceConfigManager.Instance.GetDeviceConnect(Module.ToString()))
  709. {
  710. if (DateTime.Now.Subtract(_sendTime).TotalMilliseconds >= 300)
  711. {
  712. SendOperation(LinMotOperation.ReadStatus);
  713. }
  714. }
  715. else
  716. {
  717. _statusWord = 0;
  718. UpdateStatusWord(_statusWord);
  719. }
  720. }
  721. /// <summary>
  722. /// 发送消息
  723. /// </summary>
  724. /// <param name="operation"></param>
  725. public bool SendOperation(LinMotOperation operation)
  726. {
  727. _sendTime = DateTime.Now;
  728. return LinMotDeviceConfigManager.Instance.SendOperation(Module.ToString(), _linmotId,operation);
  729. }
  730. /// <summary>
  731. /// 掉电
  732. /// </summary>
  733. /// <returns></returns>
  734. public bool SwitchOff()
  735. {
  736. return SendOperation(LinMotOperation.SwitchOff);
  737. }
  738. /// <summary>
  739. /// 中止
  740. /// </summary>
  741. public void AbortCurrentRoutine()
  742. {
  743. StopOperation("", null);
  744. }
  745. public bool Initialize()
  746. {
  747. return true;
  748. }
  749. public void Terminate()
  750. {
  751. StopOperation("", null);
  752. }
  753. public void Reset()
  754. {
  755. }
  756. }
  757. }