jump.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * 跳判断相关脚本代码
  3. */
  4. let assign = function (target, ...varArgs) {
  5. if (target == null) {
  6. throw new TypeError('Cannot convert undefined or null to object');
  7. }
  8. if (!varArgs || varArgs.length <= 0) {
  9. return target;
  10. }
  11. // 深度合并对象
  12. function deepAssign(obj1, obj2) {
  13. for (let key in obj2) {
  14. obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
  15. deepAssign(obj1[key], obj2[key]) : obj1[key] = obj2[key];
  16. }
  17. return obj1;
  18. }
  19. varArgs.forEach(val => {
  20. target = deepAssign(target, val);
  21. });
  22. return target;
  23. };
  24. function Event() {
  25. this.events = {};
  26. }
  27. Event.prototype.addEventListener = function (type, listener) {
  28. this.events[type] = this.events[type] || [];
  29. this.events[type].push(listener);
  30. };
  31. Event.prototype.trigger = function () {
  32. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  33. args[_key] = arguments[_key];
  34. }
  35. var type = args[0];
  36. var params = args.slice(1);
  37. if (!!this.events[type]) {
  38. // console.log("type:",type);
  39. this.events[type].forEach(function (listener) {
  40. try {
  41. listener.apply(null, params);
  42. } catch (e) {
  43. console.error(e);
  44. }
  45. });
  46. }
  47. };
  48. var ActionJump = function ActionJump() {
  49. let jumpOpts = {
  50. //是否上升的标志位
  51. isDirectionUp: false,
  52. //持续上升次数
  53. continueUpCount: 0,
  54. //上一点的持续上升的次数,为了记录波峰的上升次数
  55. continueUpFormerCount: 0,
  56. continueDownCount: 0,
  57. continueDownFormerCount: 0,
  58. //上一点的状态,上升还是下降
  59. lastStatus: false,
  60. //波峰值
  61. peakOfWave: 0,
  62. //波谷值
  63. valleyOfWave: 0,
  64. //检测到极快的波动的次数
  65. timeOfPeakCount: 0,
  66. //停止跳
  67. bStopJump: false,
  68. //上次传感器的值
  69. gravityOld: 0,
  70. bUpState: false,
  71. }
  72. this.jumpOpts = jumpOpts;
  73. //其他波峰波谷参数相关数组记录
  74. this.peakOfWaveArray = [];
  75. this.peakOfWaveMaxValue = 0;
  76. this.valleyOfWaveArray = [];
  77. this.valleyOfWaveMinValue = 0;
  78. this.highestCount = 0;
  79. //陀螺仪
  80. this.oriGyroYArray = [];
  81. this.event = new Event();
  82. }
  83. ActionJump.prototype.addEventListener = function (type, listener) {
  84. this.event.addEventListener(type, listener);
  85. };
  86. ActionJump.prototype.updateJump = function () {
  87. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  88. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  89. //********加速计********
  90. let { lAccX, lAccY, lAccZ } = data.linearAcc;
  91. let { oAccX, oAccY, oAccZ } = data.oriAcc;
  92. let { oGyroX, oGyroY, oGyroZ } = data.oriGyro;
  93. this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, oGyroY);
  94. };
  95. /*
  96. * 检测步子,并开始计步
  97. * 1.传入数据
  98. * 2.如果检测到了波峰,并且符合时间差以及阈值的条件,则判定为1步
  99. * 3.符合时间差条件,波峰波谷差值大于initialValue,则将该差值纳入阈值的计算中
  100. * */
  101. ActionJump.prototype.detectorNewStep = function (resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex, _oGyroY) {
  102. let bUpdate = true;
  103. let _judgmentValue = oriZ;
  104. if (this.jumpOpts.gravityOld == 0) {
  105. this.jumpOpts.gravityOld = _judgmentValue;
  106. } else {
  107. if (!this.jumpOpts.bStopJump) {
  108. let { bState, bType, value } = this.detectorPeakOfWaveAndValleyOfWave(_judgmentValue, this.jumpOpts.gravityOld);
  109. if (bState) {
  110. this.jumpOpts.bUpState = true;
  111. let _temp = {
  112. type: bType,
  113. oldValue: value,
  114. value: resultant,
  115. lastIndex: _runIndex - 1
  116. };
  117. this.event.trigger('resultant', _temp);
  118. bUpdate = false;
  119. //记录最高点和最低点数组
  120. if (bType == 'peakOfWave') {
  121. this.peakOfWaveArray.push(_temp);
  122. if (value > this.peakOfWaveMaxValue)
  123. this.peakOfWaveMaxValue = value;
  124. } else if (bType == 'valleyOfWave') {
  125. this.valleyOfWaveArray.push(_temp);
  126. if (value < this.valleyOfWaveMinValue)
  127. this.valleyOfWaveMinValue = value;
  128. }
  129. this.highestCount = 0;
  130. //陀螺仪部分
  131. this.oriGyroYArray = [];
  132. }
  133. if (this.jumpOpts.bUpState) {
  134. this.oriGyroYArray.push(_oGyroY);
  135. //出现极值后
  136. if (Math.abs(linearZ) < 4 && Math.abs(resultant) < 6) {
  137. this.highestCount++;
  138. if (this.highestCount >= 2) {
  139. //达到最高点,
  140. this.jumpOpts.bStopJump = true;
  141. let _currentMaxValue = 0;
  142. if (Math.abs(this.peakOfWaveMaxValue) > Math.abs(this.valleyOfWaveMinValue)) {
  143. _currentMaxValue = this.peakOfWaveMaxValue;
  144. } else {
  145. _currentMaxValue = this.valleyOfWaveMinValue;
  146. }
  147. let allOGyroValue = 0;
  148. for (let i = 0; i < this.oriGyroYArray.length; i++) {
  149. allOGyroValue += this.oriGyroYArray[i];
  150. }
  151. allOGyroValue /= this.oriGyroYArray.length;
  152. //目前测试预大于100 为旋转跳动
  153. // if (allOGyroValue > 0) {
  154. // console.log('right:', allOGyroValue);
  155. // } else {
  156. // console.log('left:', allOGyroValue);
  157. // }
  158. this.event.trigger('resultant', {
  159. type: "jump",
  160. acc: _currentMaxValue,
  161. value: resultant
  162. });
  163. this.event.trigger('resultant', {
  164. type: "curAngle",
  165. value: _currentMaxValue,
  166. resultant: resultant
  167. });
  168. this.event.trigger('resultant', {
  169. type: "rotate",
  170. value: allOGyroValue,
  171. resultant: resultant
  172. });
  173. //如果_currentMaxValue小于30判断原地跳
  174. // console.log("_currentMaxValue:", _currentMaxValue,allOGyroValue);
  175. //后面通用使用这个类型传输数据
  176. this.event.trigger('resultant', {
  177. type: "stateDataOfJump",
  178. currentMaxValue: _currentMaxValue,
  179. oGyroValue: allOGyroValue,
  180. resultant: resultant
  181. });
  182. // bUpdate = false;
  183. this.jumpOpts.bUpState = false;
  184. this.resetAll();
  185. }
  186. }
  187. }
  188. } else {
  189. this.jumpOpts.timeOfPeakCount++;
  190. if (this.jumpOpts.timeOfPeakCount >= 30) {
  191. this.jumpOpts.timeOfPeakCount = 0;
  192. this.jumpOpts.bStopJump = false;
  193. this.event.trigger('resultant', {
  194. type: "stop"
  195. });
  196. this.resetAll();
  197. }
  198. }
  199. // let result = Math.atan2(averX, averZ) * 180 / (Math.PI);
  200. // result = Math.round(result);
  201. // let curAngle = result > 0 ? result : (360 + result);
  202. // console.log("curAngle:", curAngle);
  203. this.event.trigger('resultant', {
  204. type: "bUpdateDraw",
  205. linearX: linearX,
  206. linearZ: linearZ,
  207. linearY: linearY,
  208. oriX: oriX,
  209. oriY: oriY,
  210. oriZ: oriZ
  211. });
  212. this.jumpOpts.gravityOld = _judgmentValue;
  213. }
  214. }
  215. ActionJump.prototype.detectorPeakOfWaveAndValleyOfWave = function (newValue, oldValue) {
  216. this.jumpOpts.lastStatus = this.jumpOpts.isDirectionUp;
  217. if (newValue >= oldValue) {
  218. this.jumpOpts.continueDownFormerCount = this.jumpOpts.continueDownCount;
  219. this.jumpOpts.continueDownCount = 0;
  220. this.jumpOpts.isDirectionUp = true;
  221. this.jumpOpts.continueUpCount++;
  222. } else {
  223. this.jumpOpts.continueUpFormerCount = this.jumpOpts.continueUpCount;
  224. this.jumpOpts.continueUpCount = 0;
  225. this.jumpOpts.isDirectionUp = false;
  226. this.jumpOpts.continueDownCount++;
  227. }
  228. if (!this.jumpOpts.isDirectionUp && this.jumpOpts.lastStatus && this.jumpOpts.continueUpFormerCount >= 2 && Math.abs(oldValue) >= 5) {
  229. this.jumpOpts.peakOfWave = oldValue;
  230. return { value: oldValue, bType: 'peakOfWave', bState: true };
  231. } else if (!this.jumpOpts.lastStatus && this.jumpOpts.isDirectionUp && this.jumpOpts.continueDownFormerCount >= 2 && Math.abs(oldValue) >= 5) {
  232. this.jumpOpts.valleyOfWave = oldValue;
  233. return { value: oldValue, bType: 'valleyOfWave', bState: true };
  234. } else {
  235. return { value: oldValue, bType: 'None', bState: false };
  236. }
  237. }
  238. //重置对应的参数
  239. ActionJump.prototype.resetAll = function () {
  240. this.peakOfWaveArray = [];
  241. this.peakOfWaveMaxValue = 0;
  242. this.valleyOfWaveArray = [];
  243. this.valleyOfWaveMinValue = 0;
  244. this.highestCount = 0;
  245. this.jumpOpts.continueDownFormerCount = 0;
  246. this.jumpOpts.continueDownCount = 0;
  247. this.jumpOpts.continueUpFormerCount = 0;
  248. this.jumpOpts.continueUpCount = 0;
  249. }
  250. if (typeof module === "object" && typeof module.exports === "object") {
  251. module.exports = ActionJump;
  252. }