jump-0.2.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. //oGyroX 在用的过程中方向相反,所以添加负号
  107. let _tempAxisData = bYAxis ? oGyroY : -oGyroY;//-oGyroX; 直接解析数据时候,调整
  108. this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, _tempAxisData);
  109. };
  110. /*
  111. *计算跳逻辑
  112. */
  113. ActionJump.prototype.detectorNewStep = function(resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex,
  114. _oGyroY) {
  115. let _judgmentValue = oriZ;
  116. //判断resultant 一个阀值
  117. let limitResultant = 20;
  118. if (!this.jumpOpts.bStopJump) {
  119. if (resultant > limitResultant && !this.jumpOpts.bUpState) {
  120. this.jumpOpts.bUpState = true;
  121. this.highestCount = 0;
  122. //陀螺仪部分
  123. // this.oriGyroYArray = [];
  124. //开始更新。加入时间判断
  125. this.jumpOpts.startTime = new Date().getTime();
  126. this.event.trigger('resultant', {
  127. type: "log",
  128. logType: 'normal',
  129. data: "开始时间:" + this.jumpOpts.startTime
  130. });
  131. for (let i = 0; i < this.frame.length; i++) {
  132. this.frame[i].maxValue = 0;
  133. this.frame[i].gyroValue = 0;
  134. this.frame[i].resultant = 0;
  135. }
  136. }
  137. if (this.jumpOpts.bUpState) {
  138. let currTime = new Date().getTime(); //当前时间
  139. let diffTime = currTime - this.jumpOpts.startTime; //当前时间减最初时间,得到当前时间差
  140. // if (diffTime > 500) {
  141. // //如果超时重置一下参数
  142. // this.jumpOpts.startTime = new Date().getTime();
  143. // for (let i = 0; i < this.frame.length; i++) {
  144. // this.frame[i].maxValue = 0;
  145. // this.frame[i].gyroValue = 0;
  146. // this.frame[i].resultant = 0;
  147. // }
  148. // };
  149. let newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
  150. if (_judgmentValue > 2) {
  151. newFrame.maxValue = _judgmentValue;
  152. if (_judgmentValue > this.peakOfWaveMaxValue)
  153. this.peakOfWaveMaxValue += _judgmentValue;
  154. } else if (_judgmentValue < -2) {
  155. newFrame.maxValue = _judgmentValue;
  156. if (_judgmentValue < this.valleyOfWaveMinValue)
  157. this.valleyOfWaveMinValue += _judgmentValue;
  158. }
  159. if (Math.abs(_oGyroY) > 5) {
  160. // this.oriGyroYArray.push(_oGyroY);
  161. newFrame.gyroValue = _oGyroY;
  162. }
  163. newFrame.resultant = resultant;
  164. //出现极值后
  165. // Math.abs(linearZ) < 7 &&
  166. if (Math.abs(resultant) < 7) {
  167. this.event.trigger('resultant', {
  168. type: "log",
  169. logType: 'normal',
  170. data: '出现极值后:' + resultant + ",时间:" + diffTime
  171. });
  172. // if (diffTime < 150){
  173. // this.jumpOpts.bUpState = false;
  174. // return;
  175. // };
  176. // this.event.trigger('resultant', {
  177. // type: "log",
  178. // logType:'normal',
  179. // data: "************触发成功************"
  180. // });
  181. this.highestCount++;
  182. if (this.highestCount >= 2) {
  183. //达到最高点,
  184. this.jumpOpts.bStopJump = true;
  185. this.jumpOpts.bUpdateTimeOfPeakCount = true;
  186. // let _currentMaxValue = 0;
  187. // if (Math.abs(this.peakOfWaveMaxValue) > Math.abs(this.valleyOfWaveMinValue)) {
  188. // _currentMaxValue = this.peakOfWaveMaxValue;
  189. // } else {
  190. // _currentMaxValue = this.valleyOfWaveMinValue;
  191. // }
  192. // let allOGyroValue = 0;
  193. // for (let i = 0; i < this.oriGyroYArray.length; i++) {
  194. // allOGyroValue += this.oriGyroYArray[i];
  195. // }
  196. // allOGyroValue /= this.oriGyroYArray.length;
  197. let _frameMaxValue = 0,
  198. _frameGyroValue = 0;
  199. for (let i = 0; i < this.frame.length; i++) {
  200. _frameMaxValue += this.frame[i].maxValue;
  201. _frameGyroValue += this.frame[i].gyroValue;
  202. }
  203. // console.log("frame:" + _frameMaxValue + " == " + _frameGyroValue);
  204. //后面通用使用这个类型传输数据
  205. this.event.trigger('resultant', {
  206. type: "stateDataOfJump",
  207. currentMaxValue: _frameMaxValue,
  208. peakOfWaveMaxValue: this.peakOfWaveMaxValue,
  209. valleyOfWaveMinValue: this.valleyOfWaveMinValue,
  210. oGyroValue: _frameGyroValue / this.frame.length,
  211. resultant: resultant,
  212. name: "highestCountEnd"
  213. });
  214. // this.jumpOpts.bUpState = false;
  215. // this.jumpOpts.bStopJump = false;
  216. this.event.trigger('resultant', {
  217. type: "stop"
  218. });
  219. this.resetAll();
  220. }
  221. }
  222. if ((this.frameOffset += 1) >= this.frameCapacity) {
  223. this.frameOffset -= this.frameCapacity;
  224. }
  225. }
  226. } else if (this.jumpOpts.bUpdateTimeOfPeakCount) {
  227. // console.log("结束判断时候:" + resultant);
  228. this.jumpOpts.timeOfPeakCount++;
  229. //todo 如果直跳,可以调节更小的 limitTimeOfPeakCount 30
  230. let limitTimeOfPeakCount = 10;
  231. if (this.jumpOpts.timeOfPeakCount >= limitTimeOfPeakCount) {
  232. this.jumpOpts.timeOfPeakCount = 0;
  233. this.jumpOpts.bStopJump = false;
  234. this.jumpOpts.bUpState = false;
  235. // this.event.trigger('resultant', {
  236. // type: "stop"
  237. // });
  238. // this.resetAll();
  239. this.jumpOpts.bUpdateTimeOfPeakCount = false;
  240. // console.log("timeOfPeakCount >= " + limitTimeOfPeakCount);
  241. }
  242. }
  243. }
  244. ActionJump.prototype.setBUpState = function(value) {
  245. this.jumpOpts.bUpState = value;
  246. }
  247. //重置对应的参数
  248. ActionJump.prototype.resetAll = function() {
  249. this.peakOfWaveMaxValue = 0;
  250. this.valleyOfWaveMinValue = 0;
  251. this.highestCount = 0;
  252. }
  253. if (typeof module === "object" && typeof module.exports === "object") {
  254. module.exports = ActionJump;
  255. }