jump-0.2.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * 跳判断相关脚本代码
  3. */
  4. function Event() {
  5. this.events = {};
  6. }
  7. Event.prototype.addEventListener = function(type, listener) {
  8. this.events[type] = this.events[type] || [];
  9. this.events[type].push(listener);
  10. };
  11. Event.prototype.trigger = function() {
  12. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  13. args[_key] = arguments[_key];
  14. }
  15. var type = args[0];
  16. var params = args.slice(1);
  17. if (!!this.events[type]) {
  18. // console.log("type:",type);
  19. this.events[type].forEach(function(listener) {
  20. try {
  21. listener.apply(null, params);
  22. } catch (e) {
  23. console.error(e);
  24. }
  25. });
  26. }
  27. };
  28. var jumpOpts = {
  29. //是否上升的标志位
  30. isDirectionUp: false,
  31. //持续上升次数
  32. continueUpCount: 0,
  33. //上一点的持续上升的次数,为了记录波峰的上升次数
  34. continueUpFormerCount: 0,
  35. continueDownCount: 0,
  36. continueDownFormerCount: 0,
  37. //上一点的状态,上升还是下降
  38. lastStatus: false,
  39. //波峰值
  40. peakOfWave: 0,
  41. //波谷值
  42. valleyOfWave: 0,
  43. //检测到极快的波动的次数
  44. timeOfPeakCount: 0,
  45. //开始添加
  46. bUpdateTimeOfPeakCount: false,
  47. //开始更新的次数
  48. startCount: 0,
  49. //停止跳
  50. bStopJump: false,
  51. //上次传感器的值
  52. gravityOld: 0,
  53. bUpState: false,
  54. //开始时间
  55. startTime: 0,
  56. endTime: 0
  57. }
  58. var ActionJump = function ActionJump() {
  59. this.jumpOpts = jumpOpts;
  60. this.peakOfWaveMaxValue = 0;
  61. this.valleyOfWaveMinValue = 0;
  62. this.highestCount = 0;
  63. //陀螺仪
  64. this.oriGyroYArray = [];
  65. this.event = new Event();
  66. this.frameCapacity = 6;
  67. this.frame = [];
  68. this.frameLength = 5;
  69. this.frameOffset = 0;
  70. for (var i = 0; i < this.frameCapacity; ++i) {
  71. var o = new Object();
  72. o.maxValue = 0;
  73. o.gyroValue = 0;
  74. o.resultant = 0;
  75. this.frame.push(o);
  76. }
  77. }
  78. ActionJump.prototype.addEventListener = function(type, listener) {
  79. this.event.addEventListener(type, listener);
  80. };
  81. /**
  82. * 更新数据
  83. */
  84. ActionJump.prototype.updateJump = function() {
  85. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  86. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  87. //********加速计********
  88. let {
  89. lAccX,
  90. lAccY,
  91. lAccZ
  92. } = data.linearAcc;
  93. let {
  94. oAccX,
  95. oAccY,
  96. oAccZ
  97. } = data.oriAcc;
  98. let {
  99. oGyroX,
  100. oGyroY,
  101. oGyroZ
  102. } = data.oriGyro;
  103. let {
  104. bYAxis
  105. } = data;
  106. let _tempAxisData = bYAxis ? oGyroY : oGyroX;
  107. this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, _tempAxisData);
  108. };
  109. /*
  110. *计算跳逻辑
  111. */
  112. ActionJump.prototype.detectorNewStep = function(resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex,
  113. _oGyroY) {
  114. let _judgmentValue = oriZ;
  115. //判断resultant 一个阀值
  116. let limitResultant = 20;
  117. if (!this.jumpOpts.bStopJump) {
  118. if (resultant > limitResultant && !this.jumpOpts.bUpState) {
  119. this.jumpOpts.bUpState = true;
  120. this.highestCount = 0;
  121. //陀螺仪部分
  122. // this.oriGyroYArray = [];
  123. //开始更新。加入时间判断
  124. this.jumpOpts.startTime = new Date().getTime();
  125. this.event.trigger('resultant', {
  126. type: "log",
  127. logType: 'normal',
  128. data: "开始时间:" + this.jumpOpts.startTime
  129. });
  130. for (let i = 0; i < this.frame.length; i++) {
  131. this.frame[i].maxValue = 0;
  132. this.frame[i].gyroValue = 0;
  133. this.frame[i].resultant = 0;
  134. }
  135. }
  136. if (this.jumpOpts.bUpState) {
  137. let currTime = new Date().getTime(); //当前时间
  138. let diffTime = currTime - this.jumpOpts.startTime; //当前时间减最初时间,得到当前时间差
  139. // if (diffTime > 500) {
  140. // //如果超时重置一下参数
  141. // this.jumpOpts.startTime = new Date().getTime();
  142. // for (let i = 0; i < this.frame.length; i++) {
  143. // this.frame[i].maxValue = 0;
  144. // this.frame[i].gyroValue = 0;
  145. // this.frame[i].resultant = 0;
  146. // }
  147. // };
  148. let newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
  149. if (_judgmentValue > 2) {
  150. newFrame.maxValue = _judgmentValue;
  151. if (_judgmentValue > this.peakOfWaveMaxValue)
  152. this.peakOfWaveMaxValue += _judgmentValue;
  153. } else if (_judgmentValue < -2) {
  154. newFrame.maxValue = _judgmentValue;
  155. if (_judgmentValue < this.valleyOfWaveMinValue)
  156. this.valleyOfWaveMinValue += _judgmentValue;
  157. }
  158. if (Math.abs(_oGyroY) > 5) {
  159. // this.oriGyroYArray.push(_oGyroY);
  160. newFrame.gyroValue = _oGyroY;
  161. }
  162. newFrame.resultant = resultant;
  163. //出现极值后
  164. // Math.abs(linearZ) < 7 &&
  165. if (Math.abs(resultant) < 7) {
  166. this.event.trigger('resultant', {
  167. type: "log",
  168. logType: 'normal',
  169. data: '出现极值后:' + resultant + ",时间:" + diffTime
  170. });
  171. // if (diffTime < 150){
  172. // this.jumpOpts.bUpState = false;
  173. // return;
  174. // };
  175. // this.event.trigger('resultant', {
  176. // type: "log",
  177. // logType:'normal',
  178. // data: "************触发成功************"
  179. // });
  180. this.highestCount++;
  181. if (this.highestCount >= 2) {
  182. //达到最高点,
  183. this.jumpOpts.bStopJump = true;
  184. this.jumpOpts.bUpdateTimeOfPeakCount = true;
  185. // let _currentMaxValue = 0;
  186. // if (Math.abs(this.peakOfWaveMaxValue) > Math.abs(this.valleyOfWaveMinValue)) {
  187. // _currentMaxValue = this.peakOfWaveMaxValue;
  188. // } else {
  189. // _currentMaxValue = this.valleyOfWaveMinValue;
  190. // }
  191. // let allOGyroValue = 0;
  192. // for (let i = 0; i < this.oriGyroYArray.length; i++) {
  193. // allOGyroValue += this.oriGyroYArray[i];
  194. // }
  195. // allOGyroValue /= this.oriGyroYArray.length;
  196. let _frameMaxValue = 0,
  197. _frameGyroValue = 0;
  198. for (let i = 0; i < this.frame.length; i++) {
  199. _frameMaxValue += this.frame[i].maxValue;
  200. _frameGyroValue += this.frame[i].gyroValue;
  201. }
  202. console.log("frame:" + _frameMaxValue + " == " + _frameGyroValue);
  203. //后面通用使用这个类型传输数据
  204. this.event.trigger('resultant', {
  205. type: "stateDataOfJump",
  206. currentMaxValue: _frameMaxValue,
  207. peakOfWaveMaxValue: this.peakOfWaveMaxValue,
  208. valleyOfWaveMinValue: this.valleyOfWaveMinValue,
  209. oGyroValue: _frameGyroValue / this.frame.length,
  210. resultant: resultant,
  211. name: "highestCountEnd"
  212. });
  213. // this.jumpOpts.bUpState = false;
  214. // this.jumpOpts.bStopJump = false;
  215. this.event.trigger('resultant', {
  216. type: "stop"
  217. });
  218. this.resetAll();
  219. }
  220. }
  221. if ((this.frameOffset += 1) >= this.frameCapacity) {
  222. this.frameOffset -= this.frameCapacity;
  223. }
  224. }
  225. } else if (this.jumpOpts.bUpdateTimeOfPeakCount) {
  226. // console.log("结束判断时候:" + resultant);
  227. this.jumpOpts.timeOfPeakCount++;
  228. //todo 如果直跳,可以调节更小的 limitTimeOfPeakCount 30
  229. let limitTimeOfPeakCount = 10;
  230. if (this.jumpOpts.timeOfPeakCount >= limitTimeOfPeakCount) {
  231. this.jumpOpts.timeOfPeakCount = 0;
  232. this.jumpOpts.bStopJump = false;
  233. this.jumpOpts.bUpState = false;
  234. // this.event.trigger('resultant', {
  235. // type: "stop"
  236. // });
  237. // this.resetAll();
  238. this.jumpOpts.bUpdateTimeOfPeakCount = false;
  239. // console.log("timeOfPeakCount >= " + limitTimeOfPeakCount);
  240. }
  241. }
  242. }
  243. ActionJump.prototype.setBUpState = function(value) {
  244. this.jumpOpts.bUpState = value;
  245. }
  246. //重置对应的参数
  247. ActionJump.prototype.resetAll = function() {
  248. this.peakOfWaveMaxValue = 0;
  249. this.valleyOfWaveMinValue = 0;
  250. this.highestCount = 0;
  251. }
  252. if (typeof module === "object" && typeof module.exports === "object") {
  253. module.exports = ActionJump;
  254. }