MotionAxis.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using Prism.Mvvm;
  2. using System;
  3. namespace SKMC.Api.Motion.Model
  4. {
  5. /// <summary>
  6. /// 电机模型
  7. /// </summary>
  8. public class MotionAxis : BindableBase
  9. {
  10. private long _id;
  11. public long Id
  12. {
  13. get { return _id; }
  14. set { _id = value; RaisePropertyChanged(); }
  15. }
  16. private string _type;
  17. /// <summary>
  18. /// 电机类型, Closed: 闭环, Open: 开环
  19. /// </summary>
  20. public string Type
  21. {
  22. get { return _type; }
  23. set { _type = value; }
  24. }
  25. private string _code;
  26. private short level;
  27. /// <summary>
  28. /// 安全等级, 0: 低压电机, 1: 高压电机
  29. /// </summary>
  30. public short Level
  31. {
  32. get { return level; }
  33. set { level = value; }
  34. }
  35. /// <summary>
  36. /// 电机码
  37. /// </summary>
  38. public string Code
  39. {
  40. get { return _code; }
  41. set { _code = value; RaisePropertyChanged(); }
  42. }
  43. private string catalog;
  44. /// <summary>
  45. /// 所属(模块)分类
  46. /// </summary>
  47. public string Catalog
  48. {
  49. get { return catalog; }
  50. set { catalog = value; RaisePropertyChanged(); }
  51. }
  52. private short _axisNo;
  53. /// <summary>
  54. /// 电机序号
  55. /// </summary>
  56. public short AxisNo
  57. {
  58. get { return _axisNo; }
  59. set { _axisNo = value; RaisePropertyChanged(); }
  60. }
  61. private char _axisDir;
  62. public char AxisDir
  63. {
  64. get { return _axisDir; }
  65. set { _axisDir = value; RaisePropertyChanged(); }
  66. }
  67. /// <summary>
  68. /// 默认的安全检查函数, 用于电机调试时
  69. /// </summary>
  70. public Func<bool> SafeChecker { get; set; }
  71. private MotionAxisStatus _axisSts;
  72. /// <summary>
  73. /// 电机状态
  74. /// </summary>
  75. public MotionAxisStatus AxisSts
  76. {
  77. get { return _axisSts; }
  78. set { _axisSts = value; RaisePropertyChanged(); }
  79. }
  80. /// <summary>
  81. /// 到位时允许的偏差 (mm)
  82. /// </summary>
  83. public double InRange { get; set; }
  84. /// <summary>
  85. /// 异常时是否可自动恢复(0:不恢复 1:恢复)
  86. /// </summary>
  87. public short AutoRecovery { get; set; }
  88. /// <summary>
  89. /// 是否已停止
  90. /// </summary>
  91. public bool IsStopped => _axisSts.IsBusy == 0 && _axisSts.IsDone == 1;
  92. /// <summary>
  93. /// 是否上使能
  94. /// </summary>
  95. public bool IsEnabled => _axisSts.IsEnable == 1;
  96. /// <summary>
  97. /// 是否已回零
  98. /// </summary>
  99. public bool IsHomed
  100. {
  101. get => _axisSts.IsHomed == 1;
  102. set => _axisSts.IsHomed = value ? (byte)1 : (byte)0;
  103. }
  104. private string _name;
  105. /// <summary>
  106. /// 名称/描述
  107. /// </summary>
  108. public string Name
  109. {
  110. get { return _name; }
  111. set { _name = value; RaisePropertyChanged(); }
  112. }
  113. private string _ptag;
  114. /// <summary>
  115. /// 正方向运动标签, 默认→
  116. /// </summary>
  117. public string PTag
  118. {
  119. get { return _ptag; }
  120. set { _ptag = value; }
  121. }
  122. private string _ntag;
  123. /// <summary>
  124. /// 负方向运动标签, 默认←
  125. /// </summary>
  126. public string NTag
  127. {
  128. get { return _ntag; }
  129. set { _ntag = value; }
  130. }
  131. private short _homeMod;
  132. /// <summary>
  133. /// 驱动器回零模式
  134. /// </summary>
  135. public short HomeMod
  136. {
  137. get { return _homeMod; }
  138. set { _homeMod = value; RaisePropertyChanged(); }
  139. }
  140. private short _homeDir;
  141. /// <summary>
  142. /// 回零方向
  143. /// </summary>
  144. public short HomeDir
  145. {
  146. get { return _homeDir; }
  147. set { _homeDir = value; RaisePropertyChanged(); }
  148. }
  149. private int _homeLowVel;
  150. /// <summary>
  151. /// 回零低速
  152. /// </summary>
  153. public int HomeLowVel
  154. {
  155. get { return _homeLowVel; }
  156. set { _homeLowVel = value; RaisePropertyChanged(); }
  157. }
  158. private int _homeHighVel;
  159. /// <summary>
  160. /// 回零高速
  161. /// </summary>
  162. public int HomeHighVel
  163. {
  164. get { return _homeHighVel; }
  165. set { _homeHighVel = value; RaisePropertyChanged(); }
  166. }
  167. public string SerialNo { get => $"[{AxisNo}]{AxisDir}: {Code}"; }
  168. public string SerialName { get => $"[{Code}]: {Name}"; }
  169. private int _gearScale;
  170. /// <summary>
  171. /// 电子齿轮比
  172. /// </summary>
  173. public int GearScale
  174. {
  175. get { return _gearScale; }
  176. set { _gearScale = value; }
  177. }
  178. private double _distanceR;
  179. /// <summary>
  180. /// 电机每旋转一圈机构的移动距离, 可以是螺距导程, 也可以是传动轮周长
  181. /// </summary>
  182. public double DistanceR
  183. {
  184. get { return _distanceR; }
  185. set { _distanceR = value; }
  186. }
  187. private short _unitType;
  188. // 单位类型, 0表示电机步距, 1表示实际距离
  189. public short UnitType
  190. {
  191. get { return _unitType; }
  192. set { _unitType = value; }
  193. }
  194. private string _unitName;
  195. // 单位名称用于显示, 例如mm, °等
  196. public string UnitName
  197. {
  198. get { return _unitName; }
  199. set { _unitName = value; }
  200. }
  201. private string _moveType;
  202. // 移动类型, L直线, R旋转
  203. public string MoveType
  204. {
  205. get { return _moveType; }
  206. set { _moveType = value; }
  207. }
  208. /// <summary>
  209. /// 距离(mm)对应的电机运动单位
  210. /// </summary>
  211. public double DistanceUnit
  212. {
  213. get => _gearScale / _distanceR;
  214. }
  215. /// <summary>
  216. /// 当前ENC值对应的实际距离mm
  217. /// </summary>
  218. public double Distance
  219. {
  220. get => _axisSts.ENC / DistanceUnit;
  221. }
  222. /// <summary>
  223. /// 离上一次记录的实际距离 mm
  224. /// </summary>
  225. public double WatchDistance => _axisSts.WatchPos / DistanceUnit;
  226. private double _prfUnit;
  227. /// <summary>
  228. /// 运动目标值, 实际单位mm或者角度等
  229. /// </summary>
  230. public double PrfUnit
  231. {
  232. get { return _prfUnit; }
  233. set
  234. {
  235. _prfUnit = value / DistanceUnit;
  236. AxisSts.PRF = value;
  237. RaisePropertyChanged();
  238. }
  239. }
  240. private double _encUnit;
  241. /// <summary>
  242. /// 运动反馈值, 实际单位mm或者角度等
  243. /// </summary>
  244. public double EncUnit
  245. {
  246. get { return _encUnit; }
  247. set
  248. {
  249. _encUnit = value / DistanceUnit;
  250. AxisSts.ENC = value;
  251. if (PercentOn) Percent = (_encUnit - NegativeUnit) / (PositiveUnit - NegativeUnit);
  252. RaisePropertyChanged();
  253. }
  254. }
  255. /// <summary>
  256. /// 是否使用位置百分比
  257. /// </summary>
  258. public bool PercentOn { get; set; }
  259. /// <summary>
  260. /// 负限位位置(mm)
  261. /// </summary>
  262. public double NegativeUnit { get; set; }
  263. /// <summary>
  264. /// 正限位位置(mm)
  265. /// </summary>
  266. public double PositiveUnit { get; set; }
  267. private double percent;
  268. /// <summary>
  269. /// 位置百分比, 负限位为0%, 正限位为100%
  270. /// </summary>
  271. public double Percent
  272. {
  273. get { return percent; }
  274. set { percent = value; RaisePropertyChanged(); }
  275. }
  276. /// <summary>
  277. /// 电机停止(中断)
  278. /// </summary>
  279. public bool IsInterrupt { get; set; }
  280. /// <summary>
  281. /// 被占用的handler
  282. /// </summary>
  283. public int? LockHandler { get; set; }
  284. /// <summary>
  285. /// 当前位置是否在偏移量之内
  286. /// </summary>
  287. /// <param name="targetVal"></param>
  288. /// <returns></returns>
  289. public bool IsEncInRange(double targetVal)
  290. {
  291. if (_encUnit == targetVal) return true;
  292. else if (_encUnit > targetVal) return _encUnit - targetVal <= InRange;
  293. else return targetVal - _encUnit <= InRange;
  294. }
  295. /// <summary>
  296. /// 当前位置是否在偏移量之内
  297. /// </summary>
  298. /// <param name="targetVal"></param>
  299. /// <param name="offset"></param>
  300. /// <returns></returns>
  301. public bool IsEncInRange(double targetVal, double offset)
  302. {
  303. if (_encUnit == targetVal) return true;
  304. else if (_encUnit > targetVal) return _encUnit - targetVal <= offset;
  305. else return targetVal - _encUnit <= offset;
  306. }
  307. }
  308. }