EquipmentAction.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. let assign = function (target, ...varArgs) {
  2. if (target == null) {
  3. throw new TypeError('Cannot convert undefined or null to object');
  4. }
  5. if (!varArgs || varArgs.length <= 0) {
  6. return target;
  7. }
  8. // 深度合并对象
  9. function deepAssign(obj1, obj2) {
  10. for (let key in obj2) {
  11. obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
  12. deepAssign(obj1[key], obj2[key]) : obj1[key] = obj2[key];
  13. }
  14. return obj1;
  15. }
  16. varArgs.forEach(val => {
  17. target = deepAssign(target, val);
  18. });
  19. return target;
  20. };
  21. function Event() {
  22. this.events = {};
  23. }
  24. Event.prototype.addEventListener = function (type, listener) {
  25. this.events[type] = this.events[type] || [];
  26. this.events[type].push(listener);
  27. };
  28. Event.prototype.trigger = function () {
  29. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  30. args[_key] = arguments[_key];
  31. }
  32. var type = args[0];
  33. var params = args.slice(1);
  34. if (!!this.events[type]) {
  35. // console.log("type:",type);
  36. this.events[type].forEach(function (listener) {
  37. try {
  38. listener.apply(null, params);
  39. } catch (e) {
  40. console.error(e);
  41. }
  42. });
  43. }
  44. };
  45. var EquipmentAction = function EquipmentAction() {
  46. let opts = {
  47. //合力计算
  48. oldResultantDifference: 0,
  49. resultantDifference: 0,
  50. bCalResultant: false,
  51. //最大的差值
  52. resultantMaxDifference: 0,
  53. //最大的加速度
  54. resultantMaxX: 0,
  55. resultantMaxZ: 0,
  56. resultantMaxY: 0,
  57. resultantMaxAddCount: 0,
  58. resultAddDifferenceCount: 0,
  59. xA: 0,
  60. oldxA: 0,
  61. zA: 0,
  62. oldzA: 0,
  63. yA: 0,
  64. oldyA: 0,
  65. //初始化
  66. initX: false,
  67. initZ: false,
  68. initY: false,
  69. mass: 5,
  70. //是否限制回弹,true 是限制回弹,false 是快速打击
  71. bLimitRebound: true
  72. }
  73. this.opts = opts;
  74. let runOpts = {
  75. ValueNum: 4,
  76. tempValue: [], //用于存放计算阈值的波峰波谷差值
  77. tempCount: 0,
  78. //是否上升的标志位
  79. isDirectionUp: false,
  80. //持续上升次数
  81. continueUpCount: 0,
  82. //上一点的持续上升的次数,为了记录波峰的上升次数
  83. continueUpFormerCount: 0,
  84. //上一点的状态,上升还是下降
  85. lastStatus: false,
  86. //波峰值
  87. peakOfWave: 0,
  88. //波谷值
  89. valleyOfWave: 0,
  90. //此次波峰的时间
  91. timeOfThisPeak: 0,
  92. //上次波峰的时间
  93. timeOfLastPeak: 0,
  94. //当前的时间
  95. timeOfNow: 0,
  96. //当前传感器的值
  97. gravityNew: 0,
  98. //上次传感器的值
  99. gravityOld: 0,
  100. //上传传感器的陀螺仪
  101. gyroOld: 0,
  102. //动态阈值需要动态的数据,这个值用于这些动态数据的阈值
  103. InitialValue: 1.3,
  104. //初始阈值
  105. ThreadValue: 2.0,
  106. //波峰波谷时间差
  107. TimeInterval: 250,
  108. //跳高
  109. jumpStepCount: 0,
  110. jumpTimeOfLastStep: 0,
  111. jumpTimeOfThisStep: 0,
  112. jumpAverageTimeOfEveryStep: 0,
  113. //跑步
  114. stepCount: 0,
  115. timeOfLastStep: 0,
  116. timeOfThisStep: 0,
  117. averageTimeOfEveryStep: 0,
  118. currentState: "idle"
  119. }
  120. this.runOpts = runOpts;
  121. let gyroOpts = {
  122. ValueNum: 4,
  123. tempValue: [], //用于存放计算阈值的波峰波谷差值
  124. tempCount: 0,
  125. //是否上升的标志位
  126. isDirectionUp: false,
  127. //持续上升次数
  128. continueUpCount: 0,
  129. //上一点的持续上升的次数,为了记录波峰的上升次数
  130. continueUpFormerCount: 0,
  131. //上一点的状态,上升还是下降
  132. lastStatus: false,
  133. //波峰值
  134. peakOfWave: 0,
  135. //波谷值
  136. valleyOfWave: 0,
  137. //此次波峰的时间
  138. timeOfThisPeak: 0,
  139. //上次波峰的时间
  140. timeOfLastPeak: 0,
  141. //当前的时间
  142. timeOfNow: 0,
  143. //当前传感器的值
  144. gravityNew: 0,
  145. //上次传感器的值
  146. gravityOld: 0,
  147. //动态阈值需要动态的数据,这个值用于这些动态数据的阈值
  148. InitialValue: 1.3,
  149. //初始阈值
  150. ThreadValue: 200.0,
  151. //波峰波谷时间差
  152. TimeInterval: 250,
  153. bJump: false,
  154. }
  155. this.gyroOpts = gyroOpts;
  156. this.event = new Event();
  157. // console.log("hit instanl:",this.opts);
  158. }
  159. EquipmentAction.prototype.addEventListener = function (type, listener) {
  160. this.event.addEventListener(type, listener);
  161. };
  162. /**
  163. * 更新两轴数据
  164. */
  165. EquipmentAction.prototype.updateAcc = function () {
  166. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  167. // this.opts = assign({}, this.opts, data);
  168. let bLimitRebound = data.bLimitRebound && this.opts.bLimitRebound;
  169. //使用两个轴的数据,其实就是不计算重力轴的加速度
  170. this.opts.xA = data.xA;
  171. this.opts.zA = data.zA;
  172. // console.log(bLimitRebound,data.bLimitRebound,this.opts.bLimitRebound);
  173. //这里防止第一次触发调用
  174. if (!this.opts.initX && this.opts.oldxA === 0) {
  175. this.opts.oldxA = data.xA;
  176. this.opts.initX = true;
  177. console.log("初始化oldxa");
  178. }
  179. if (!this.opts.initZ && this.opts.oldzA === 0) {
  180. this.opts.oldzA = data.zA;
  181. this.opts.initZ = true;
  182. console.log("初始化oldza");
  183. }
  184. let _differenceX = this.opts.xA - this.opts.oldxA;
  185. let _differenceZ = this.opts.zA - this.opts.oldzA;
  186. this.opts.resultantDifference = Math.abs(_differenceX) + Math.abs(_differenceZ);
  187. if (this.opts.oldResultantDifference != this.opts.resultantDifference && 0 !== this.opts.resultantDifference) {
  188. //5 默认值是12
  189. let _limitresultDifference = 12;
  190. //如果是回弹限制时候,判断值等于5
  191. if (bLimitRebound)
  192. _limitresultDifference = 5;
  193. if (this.opts.resultantDifference > _limitresultDifference) {
  194. if (!this.opts.bCalResultant) {
  195. //假如进入了一个计算周期
  196. this.opts.bCalResultant = true;
  197. }
  198. }
  199. if (this.opts.bCalResultant) {
  200. if (this.opts.resultantMaxX <= this.opts.xA)
  201. this.opts.resultantMaxX = this.opts.xA;
  202. if (this.opts.resultantMaxZ <= this.opts.zA)
  203. this.opts.resultantMaxZ = this.opts.zA;
  204. if (this.opts.resultantMaxDifference <= this.opts.resultantDifference) {
  205. this.opts.resultantMaxDifference = this.opts.resultantDifference;
  206. this.opts.resultantMaxAddCount = 0;
  207. } else {
  208. this.opts.resultantMaxAddCount++;
  209. //小于最大值的个数判断,超过限制,说明波动下滑
  210. // > 4
  211. if (this.opts.resultantMaxAddCount >= 4) {
  212. //这里区分两个版本,bLimitRebound:true一个是限制回弹版,bLimitRebound:false一个是快速打击版本
  213. if (bLimitRebound) {
  214. if (this.opts.resultantDifference <= 3) {
  215. this.opts.resultAddDifferenceCount++;
  216. // 当波动值小于3 次数到达5次,判断达到一个平稳状态
  217. if (this.opts.resultAddDifferenceCount >= 5) {
  218. let _resultantMaxAcc = Math.abs(this.opts.resultantMaxX) + Math.abs(this.opts.resultantMaxZ);
  219. this.opts.bCalResultant = false;
  220. this.opts.resultantMaxAddCount = 0;
  221. this.opts.resultAddDifferenceCount = 0;
  222. //成功判断打击直拳 todo return
  223. this.event.trigger('resultantHit', {
  224. type: "hit",
  225. acc: _resultantMaxAcc,
  226. power: Math.ceil(_resultantMaxAcc * this.opts.mass),
  227. mass: this.opts.mass
  228. });
  229. this.opts.oldResultantDifference = 0;
  230. this.opts.resultantDifference = 0;
  231. this.opts.resultantMaxDifference = 0;
  232. this.opts.resultantMaxX = 0;
  233. this.opts.resultantMaxZ = 0;
  234. // this.opts.oldxA = 0;
  235. // this.opts.oldzA = 0;
  236. }
  237. } else {
  238. this.opts.resultAddDifferenceCount = 0;
  239. }
  240. } else {
  241. let _resultantMaxAcc = Math.abs(this.opts.resultantMaxX) + Math.abs(this.opts.resultantMaxZ);
  242. this.opts.bCalResultant = false;
  243. this.opts.resultantMaxAddCount = 0;
  244. this.opts.resultAddDifferenceCount = 0;
  245. //成功判断打击直拳 todo return
  246. this.event.trigger('resultantHit', {
  247. type: "hit",
  248. acc: _resultantMaxAcc,
  249. power: Math.ceil(_resultantMaxAcc * this.opts.mass),
  250. mass: this.opts.mass
  251. });
  252. this.opts.oldResultantDifference = 0;
  253. this.opts.resultantDifference = 0;
  254. this.opts.resultantMaxDifference = 0;
  255. this.opts.resultantMaxX = 0;
  256. this.opts.resultantMaxZ = 0;
  257. // this.opts.oldxA = 0;
  258. // this.opts.oldzA = 0;
  259. }
  260. }
  261. }
  262. }
  263. this.opts.oldResultantDifference = this.opts.resultantDifference;
  264. }
  265. this.opts.oldxA = this.opts.xA;
  266. this.opts.oldzA = this.opts.zA;
  267. };
  268. /**
  269. * 更新三轴数据
  270. */
  271. EquipmentAction.prototype.updateTriaxialAcc = function () {
  272. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  273. // this.opts = assign({}, this.opts, data);
  274. let bLimitRebound = data.bLimitRebound && this.opts.bLimitRebound;
  275. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  276. this.opts.xA = data.xA;
  277. this.opts.zA = data.zA;
  278. this.opts.yA = data.yA;
  279. // console.log(bLimitRebound,data.bLimitRebound,this.opts.bLimitRebound);
  280. //这里防止第一次触发调用
  281. if (!this.opts.initX && this.opts.oldxA === 0) {
  282. this.opts.oldxA = data.xA;
  283. this.opts.initX = true;
  284. console.log("初始化oldxa");
  285. }
  286. if (!this.opts.initZ && this.opts.oldzA === 0) {
  287. this.opts.oldzA = data.zA;
  288. this.opts.initZ = true;
  289. console.log("初始化oldza");
  290. }
  291. if (!this.opts.initY && this.opts.oldyA === 0) {
  292. this.opts.oldyA = data.yA;
  293. this.opts.initY = true;
  294. console.log("初始化oldyA");
  295. }
  296. let _differenceX = this.opts.xA - this.opts.oldxA;
  297. let _differenceZ = this.opts.zA - this.opts.oldzA;
  298. let _differenceY = this.opts.yA - this.opts.oldyA;
  299. // let gravityNew = Math.sqrt(xA * xA + yA * yA + zA * zA);
  300. // this.opts.resultantDifference = Math.abs(_differenceX) + Math.abs(_differenceZ) + Math.abs(_differenceY);
  301. this.opts.resultantDifference = Math.sqrt(_differenceX * _differenceX + _differenceZ * _differenceZ + _differenceY *
  302. _differenceY);
  303. if (this.opts.oldResultantDifference != this.opts.resultantDifference && 0 !== this.opts.resultantDifference) {
  304. //5 默认值是12
  305. let _limitresultDifference = 12;
  306. //如果是回弹限制时候,判断值等于5
  307. if (bLimitRebound)
  308. _limitresultDifference = 5;
  309. if (this.opts.resultantDifference > _limitresultDifference) {
  310. if (!this.opts.bCalResultant) {
  311. //假如进入了一个计算周期
  312. this.opts.bCalResultant = true;
  313. }
  314. }
  315. if (this.opts.bCalResultant) {
  316. if (this.opts.resultantMaxDifference <= this.opts.resultantDifference) {
  317. this.opts.resultantMaxDifference = this.opts.resultantDifference;
  318. this.opts.resultantMaxAddCount = 0;
  319. } else {
  320. this.opts.resultantMaxAddCount++;
  321. //小于最大值的个数判断,超过限制,说明波动下滑
  322. // > 4
  323. if (this.opts.resultantMaxAddCount >= 4) {
  324. //这里区分两个版本,bLimitRebound:true一个是限制回弹版,bLimitRebound:false一个是快速打击版本
  325. if (bLimitRebound) {
  326. if (this.opts.resultantDifference <= 3) {
  327. this.opts.resultAddDifferenceCount++;
  328. // 当波动值小于3 次数到达5次,判断达到一个平稳状态
  329. if (this.opts.resultAddDifferenceCount >= 5) {
  330. // let _resultantMaxAcc = Math.abs(this.opts.resultantMaxX) + Math.abs(this.opts.resultantMaxZ) + Math.abs(this.opts.resultantMaxY);
  331. // console.log("this.opts.resultantMaxDifference:",this.opts.resultantMaxDifference);
  332. this.opts.bCalResultant = false;
  333. this.opts.resultantMaxAddCount = 0;
  334. this.opts.resultAddDifferenceCount = 0;
  335. //成功判断打击直拳 todo return
  336. this.event.trigger('resultantHit', {
  337. type: "hit",
  338. acc: this.opts.resultantMaxDifference,
  339. power: Math.ceil(this.opts.resultantMaxDifference * this.opts.mass),
  340. mass: this.opts.mass
  341. });
  342. this.opts.oldResultantDifference = 0;
  343. this.opts.resultantDifference = 0;
  344. this.opts.resultantMaxDifference = 0;
  345. this.opts.resultantMaxX = 0;
  346. this.opts.resultantMaxZ = 0;
  347. this.opts.resultantMaxY = 0;
  348. }
  349. } else {
  350. this.opts.resultAddDifferenceCount = 0;
  351. }
  352. } else {
  353. this.opts.bCalResultant = false;
  354. this.opts.resultantMaxAddCount = 0;
  355. this.opts.resultAddDifferenceCount = 0;
  356. //成功判断打击直拳 todo return
  357. this.event.trigger('resultantHit', {
  358. type: "hit",
  359. acc: this.opts.resultantMaxDifference,
  360. power: Math.ceil(this.opts.resultantMaxDifference * this.opts.mass),
  361. mass: this.opts.mass
  362. });
  363. this.opts.oldResultantDifference = 0;
  364. this.opts.resultantDifference = 0;
  365. this.opts.resultantMaxDifference = 0;
  366. this.opts.resultantMaxX = 0;
  367. this.opts.resultantMaxZ = 0;
  368. this.opts.resultantMaxY = 0;
  369. }
  370. }
  371. }
  372. }
  373. this.opts.oldResultantDifference = this.opts.resultantDifference;
  374. }
  375. this.opts.oldxA = this.opts.xA;
  376. this.opts.oldzA = this.opts.zA;
  377. this.opts.oldyA = this.opts.yA;
  378. };
  379. EquipmentAction.prototype.updateJumpAndRun = function () {
  380. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  381. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  382. //********加速计********
  383. let { ax, ay, az } = data.acc;
  384. ax = ax * 9.80665;
  385. ay = ay * 9.80665;
  386. az = az * 9.80665;
  387. let gravityNew = Math.sqrt(ax * ax + ay * ay + az * az);
  388. let { gx, gy, gz } = data.gyro;
  389. let gyroNew = Math.sqrt(gx * gx + gy * gy + gz * gz);
  390. this.detectorNewStep(gravityNew, gyroNew);
  391. };
  392. /*
  393. * 检测步子,并开始计步
  394. * 1.传入数据
  395. * 2.如果检测到了波峰,并且符合时间差以及阈值的条件,则判定为1步
  396. * 3.符合时间差条件,波峰波谷差值大于initialValue,则将该差值纳入阈值的计算中
  397. * */
  398. EquipmentAction.prototype.detectorNewStep = function (values, gyroValue) {
  399. if (this.gyroOpts.gravityOld == 0) {
  400. this.gyroOpts.gravityOld = gyroValue;
  401. } else {
  402. if (this.gyroDetectorPeak(gyroValue, this.gyroOpts.gravityOld)) {
  403. let step1 = this.gyroOpts.peakOfWave - this.gyroOpts.valleyOfWave;
  404. // if(step>10)
  405. // console.log("Gyro:", step);
  406. if (step1 > this.gyroOpts.ThreadValue) {
  407. this.gyroOpts.timeOfThisPeak = this.gyroOpts.timeOfNow;
  408. this.gyroOpts.bJump = true;
  409. }
  410. }
  411. }
  412. if (this.runOpts.gravityOld == 0) {
  413. this.runOpts.gravityOld = values;
  414. } else {
  415. if (this.detectorPeak(values, this.runOpts.gravityOld)) {
  416. this.runOpts.timeOfLastPeak = this.runOpts.timeOfThisPeak;
  417. this.runOpts.timeOfNow = new Date().getTime();
  418. let step2 = this.runOpts.peakOfWave - this.runOpts.valleyOfWave;
  419. if (this.runOpts.timeOfNow - this.runOpts.timeOfLastPeak >= this.runOpts.TimeInterval
  420. && step2 > this.runOpts.ThreadValue) {
  421. this.runOpts.timeOfThisPeak = this.runOpts.timeOfNow;
  422. this.countRunStep();
  423. }
  424. // if (this.runOpts.timeOfNow - this.runOpts.timeOfLastPeak >= this.runOpts.TimeInterval
  425. // && (this.runOpts.peakOfWave - this.runOpts.valleyOfWave >= this.runOpts.InitialValue)) {
  426. // this.runOpts.timeOfThisPeak = this.runOpts.timeOfNow;
  427. // this.runOpts.ThreadValue = this.peakValleyThread(this.runOpts.peakOfWave - this.runOpts.valleyOfWave);
  428. // }
  429. }
  430. }
  431. this.runOpts.gravityOld = values;
  432. this.gyroOpts.gravityOld = gyroValue;
  433. }
  434. EquipmentAction.prototype.countRunStep = function () {
  435. this.runOpts.timeOfLastStep = this.runOpts.timeOfThisStep;
  436. this.runOpts.timeOfThisStep = new Date().getTime();
  437. let diffValue = this.runOpts.timeOfThisStep - this.runOpts.timeOfLastStep;
  438. if (diffValue <= 2000) {
  439. if (this.gyroOpts.bJump) {
  440. let step = this.runOpts.peakOfWave - this.runOpts.valleyOfWave;
  441. this.event.trigger('resultant', {
  442. type: "jump",
  443. acc: step
  444. });
  445. this.resetJumpValue();
  446. // this.resSomeValue();
  447. }
  448. // else
  449. // {
  450. this.runOpts.averageTimeOfEveryStep += diffValue;
  451. this.runOpts.stepCount++;
  452. // if (this.runOpts.stepCount == 2)
  453. {
  454. this.runOpts.averageTimeOfEveryStep = this.runOpts.averageTimeOfEveryStep / 2;
  455. // this.runOpts.currentState = "runing";
  456. let step = this.runOpts.peakOfWave - this.runOpts.valleyOfWave;
  457. this.resSomeValue();
  458. this.event.trigger('resultant', {
  459. type: "runing",
  460. acc: step
  461. });
  462. }
  463. // }
  464. } else { //超时,说明没有运动了
  465. this.resSomeValue();
  466. console.log("超时", "averageTimeOfEveryStep: " + this.runOpts.averageTimeOfEveryStep);
  467. if (this.gyroOpts.bJump) {
  468. //判断是否跳
  469. let step = this.runOpts.peakOfWave - this.runOpts.valleyOfWave;
  470. this.event.trigger('resultant', {
  471. type: "jump",
  472. acc: step
  473. });
  474. this.resetJumpValue();
  475. }
  476. }
  477. }
  478. EquipmentAction.prototype.resSomeValue = function () {
  479. this.runOpts.stepCount = 0;
  480. this.runOpts.averageTimeOfEveryStep = 0;
  481. }
  482. EquipmentAction.prototype.resetJumpValue = function () {
  483. // this.runOpts.jumpStepCount = 0;
  484. // this.runOpts.jumpAverageTimeOfEveryStep = 0;
  485. this.gyroOpts.bJump = false;
  486. }
  487. /*
  488. * 检测波峰
  489. * 以下四个条件判断为波峰:
  490. * 1.目前点为下降的趋势:isDirectionUp为false
  491. * 2.之前的点为上升的趋势:lastStatus为true
  492. * 3.到波峰为止,持续上升大于等于2次
  493. * 4.波峰值大于20
  494. * 记录波谷值
  495. * 1.观察波形图,可以发现在出现步子的地方,波谷的下一个就是波峰,有比较明显的特征以及差值
  496. * 2.所以要记录每次的波谷值,为了和下次的波峰做对比
  497. * */
  498. EquipmentAction.prototype.detectorPeak = function (newValue, oldValue) {
  499. this.runOpts.lastStatus = this.runOpts.isDirectionUp;
  500. if (newValue >= oldValue) {
  501. this.runOpts.isDirectionUp = true;
  502. this.runOpts.continueUpCount++;
  503. } else {
  504. this.runOpts.continueUpFormerCount = this.runOpts.continueUpCount;
  505. this.runOpts.continueUpCount = 0;
  506. this.runOpts.isDirectionUp = false;
  507. }
  508. //this.runOpts.continueUpFormerCount >= 2 || oldValue >= 20
  509. if (!this.runOpts.isDirectionUp && this.runOpts.lastStatus &&
  510. (this.runOpts.continueUpFormerCount >= 2)) {
  511. this.runOpts.peakOfWave = oldValue;
  512. return true;
  513. } else if (!this.runOpts.lastStatus && this.runOpts.isDirectionUp) {
  514. this.runOpts.valleyOfWave = oldValue;
  515. return false;
  516. } else {
  517. return false;
  518. }
  519. }
  520. /*
  521. * 检测波峰
  522. * 以下四个条件判断为波峰:
  523. * 1.目前点为下降的趋势:isDirectionUp为false
  524. * 2.之前的点为上升的趋势:lastStatus为true
  525. * 3.到波峰为止,持续上升大于等于2次
  526. * 4.波峰值大于20
  527. * 记录波谷值
  528. * 1.观察波形图,可以发现在出现步子的地方,波谷的下一个就是波峰,有比较明显的特征以及差值
  529. * 2.所以要记录每次的波谷值,为了和下次的波峰做对比
  530. * */
  531. EquipmentAction.prototype.gyroDetectorPeak = function (newValue, oldValue) {
  532. this.gyroOpts.lastStatus = this.gyroOpts.isDirectionUp;
  533. if (newValue >= oldValue) {
  534. this.gyroOpts.isDirectionUp = true;
  535. this.gyroOpts.continueUpCount++;
  536. } else {
  537. this.gyroOpts.continueUpFormerCount = this.gyroOpts.continueUpCount;
  538. this.gyroOpts.continueUpCount = 0;
  539. this.gyroOpts.isDirectionUp = false;
  540. }
  541. //this.runOpts.continueUpFormerCount >= 2 || oldValue >= 20
  542. if (!this.gyroOpts.isDirectionUp && this.gyroOpts.lastStatus &&
  543. (this.gyroOpts.continueUpFormerCount >= 2)) {
  544. this.gyroOpts.peakOfWave = oldValue;
  545. return true;
  546. } else if (!this.gyroOpts.lastStatus && this.gyroOpts.isDirectionUp) {
  547. this.gyroOpts.valleyOfWave = oldValue;
  548. return false;
  549. } else {
  550. return false;
  551. }
  552. }
  553. /*
  554. * 阈值的计算
  555. * 1.通过波峰波谷的差值计算阈值
  556. * 2.记录4个值,存入tempValue[]数组中
  557. * 3.在将数组传入函数averageValue中计算阈值
  558. * */
  559. EquipmentAction.prototype.peakValleyThread = function (value) {
  560. let tempThread = this.runOpts.ThreadValue;
  561. if (this.runOpts.tempCount < this.runOpts.ValueNum) {
  562. this.runOpts.tempValue[this.runOpts.tempCount] = value;
  563. this.runOpts.tempCount++;
  564. } else {
  565. tempThread = this.averageValue(this.runOpts.tempValue, this.runOpts.ValueNum);
  566. for (let i = 1; i < this.runOpts.ValueNum; i++) {
  567. this.runOpts.tempValue[i - 1] = this.runOpts.tempValue[i];
  568. }
  569. this.runOpts.tempValue[this.runOpts.ValueNum - 1] = value;
  570. }
  571. return tempThread;
  572. }
  573. /*
  574. * 梯度化阈值
  575. * 1.计算数组的均值
  576. * 2.通过均值将阈值梯度化在一个范围里
  577. * */
  578. EquipmentAction.prototype.averageValue = function (value, n) {
  579. let ave = 0;
  580. for (let i = 0; i < n; i++) {
  581. ave += value[i];
  582. }
  583. ave = ave / this.runOpts.ValueNum;
  584. // console.log("ave:", ave);
  585. if (ave >= 8)
  586. ave = 4.3;
  587. else if (ave >= 7 && ave < 8)
  588. ave = 3.3;
  589. else if (ave >= 4 && ave < 7)
  590. ave = 2.3;
  591. else if (ave >= 3 && ave < 4)
  592. ave = 2.0;
  593. else {
  594. ave = 1.3;
  595. }
  596. return ave;
  597. }
  598. if (typeof module === "object" && typeof module.exports === "object") {
  599. module.exports = EquipmentAction;
  600. //export default EquipmentAction;//建议使用nodejs的module导出方式,如报错请使用export方式导出
  601. }