jump-0.2.js 7.7 KB

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