1
0

calculation-context.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const SCALE_MAX = 32767
  2. const ATT_COEF = 0.85
  3. const TWO_PI = 2 * 3.1415926
  4. const DEFAULT_DRIVER_PARAMS = {
  5. carrierFrequencyKHz: 16,
  6. baseVoltage: 5.0,
  7. opAmpGain: 4,
  8. samplingResistorMohm: 100,
  9. busVoltageDividerRatio: 996.8 / 6.8,
  10. analogInputDividerRatio: 200 / 268
  11. }
  12. let currentDriverParams = {
  13. ...DEFAULT_DRIVER_PARAMS
  14. }
  15. const sharedInputValues = {}
  16. const FAULT_CODE_MAP = {
  17. 0x01: '硬件过流',
  18. 0x02: '软件过流',
  19. 0x03: '风扇过流',
  20. 0x04: '电流偏置校准失败',
  21. 0x05: '缺相',
  22. 0x06: '驱动器上桥短路',
  23. 0x07: '驱动器下桥短路',
  24. 0x08: '相间短路',
  25. 0x09: '启动堵转',
  26. 0x0A: '运行堵转',
  27. 0x0B: '过功率',
  28. 0x0C: '过压',
  29. 0x0D: '欠压',
  30. 0x0E: '芯片欠压',
  31. 0x0F: 'NTC过温',
  32. 0x10: '电机过温',
  33. 0x11: 'IPM过温',
  34. 0x12: '芯片过温',
  35. 0x13: '串口丢失',
  36. 0x14: 'PWM丢失'
  37. }
  38. function toFiniteNumber(value, fallback = 0) {
  39. if (typeof value === 'string') {
  40. const text = value.trim()
  41. const directValue = Number(text)
  42. if (Number.isFinite(directValue)) return directValue
  43. const match = text.match(/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?/i)
  44. const textValue = match ? Number(match[0]) : NaN
  45. return Number.isFinite(textValue) ? textValue : fallback
  46. }
  47. const numberValue = Number(value)
  48. return Number.isFinite(numberValue) ? numberValue : fallback
  49. }
  50. function getSharedInputValues() {
  51. return {
  52. ...sharedInputValues
  53. }
  54. }
  55. function getDriverParams() {
  56. return {
  57. ...currentDriverParams
  58. }
  59. }
  60. function updateDriverParams(params = {}) {
  61. currentDriverParams = {
  62. ...currentDriverParams,
  63. ...Object.keys(params).reduce((result, key) => {
  64. const value = toFiniteNumber(params[key], NaN)
  65. if (Number.isFinite(value) && value > 0) {
  66. result[key] = value
  67. }
  68. return result
  69. }, {})
  70. }
  71. }
  72. function getSharedInputDefault() {
  73. return 0
  74. }
  75. function setSharedInputValue(name, value, fallback = getSharedInputDefault(name)) {
  76. sharedInputValues[name] = toFiniteNumber(value, fallback)
  77. }
  78. function setSharedInputValues(registers) {
  79. registers.forEach((item) => {
  80. setSharedInputValue(item.name, item.inputValue, getSharedInputDefault(item.name))
  81. })
  82. }
  83. function mergeInputValues(registers = []) {
  84. return registers.reduce((result, item) => {
  85. const fallback = Object.prototype.hasOwnProperty.call(result, item.name) ? result[item.name] : getSharedInputDefault(item.name)
  86. result[item.name] = toFiniteNumber(item.inputValue, fallback)
  87. return result
  88. }, getSharedInputValues())
  89. }
  90. function getFaultText(code) {
  91. const numberValue = Number(code)
  92. if (!Number.isFinite(numberValue) || numberValue === 0) return '无故障'
  93. return FAULT_CODE_MAP[numberValue] || '未知故障'
  94. }
  95. module.exports = {
  96. ATT_COEF,
  97. DEFAULT_DRIVER_PARAMS,
  98. SCALE_MAX,
  99. TWO_PI,
  100. getFaultText,
  101. getDriverParams,
  102. getSharedInputValues,
  103. getSharedInputDefault,
  104. mergeInputValues,
  105. setSharedInputValues,
  106. updateDriverParams,
  107. toFiniteNumber
  108. }