jump.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 jumpOpts = {
  49. //是否上升的标志位
  50. isDirectionUp: false,
  51. //持续上升次数
  52. continueUpCount: 0,
  53. //上一点的持续上升的次数,为了记录波峰的上升次数
  54. continueUpFormerCount: 0,
  55. continueDownCount: 0,
  56. continueDownFormerCount: 0,
  57. //上一点的状态,上升还是下降
  58. lastStatus: false,
  59. //波峰值
  60. peakOfWave: 0,
  61. //波谷值
  62. valleyOfWave: 0,
  63. //检测到极快的波动的次数
  64. timeOfPeakCount: 0,
  65. //开始添加
  66. bUpdateTimeOfPeakCount: false,
  67. //开始更新的次数
  68. startCount: 0,
  69. //停止跳
  70. bStopJump: false,
  71. //上次传感器的值
  72. gravityOld: 0,
  73. bUpState: false,
  74. }
  75. var ActionJump = function ActionJump() {
  76. this.jumpOpts = jumpOpts;
  77. //其他波峰波谷参数相关数组记录
  78. this.peakOfWaveArray = [];
  79. this.peakOfWaveMaxValue = 0;
  80. this.valleyOfWaveArray = [];
  81. this.valleyOfWaveMinValue = 0;
  82. this.highestCount = 0;
  83. //陀螺仪
  84. this.oriGyroYArray = [];
  85. this.isJumpTop = false;
  86. this.event = new Event();
  87. }
  88. ActionJump.prototype.addEventListener = function(type, listener) {
  89. this.event.addEventListener(type, listener);
  90. };
  91. ActionJump.prototype.updateJump = function() {
  92. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  93. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  94. //********加速计********
  95. let {
  96. lAccX,
  97. lAccY,
  98. lAccZ
  99. } = data.linearAcc;
  100. let {
  101. oAccX,
  102. oAccY,
  103. oAccZ
  104. } = data.oriAcc;
  105. let {
  106. oGyroX,
  107. oGyroY,
  108. oGyroZ
  109. } = data.oriGyro;
  110. let {
  111. bYAxis
  112. } = data;
  113. let _tempAxisData = bYAxis ? oGyroY : oGyroX;
  114. this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, _tempAxisData);
  115. };
  116. /*
  117. * 检测步子,并开始计步
  118. * 1.传入数据
  119. * 2.如果检测到了波峰,并且符合时间差以及阈值的条件,则判定为1步
  120. * 3.符合时间差条件,波峰波谷差值大于initialValue,则将该差值纳入阈值的计算中
  121. * */
  122. ActionJump.prototype.detectorNewStep = function(resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex,
  123. _oGyroY) {
  124. let _judgmentValue = oriZ;
  125. if (this.jumpOpts.gravityOld == 0) {
  126. this.jumpOpts.gravityOld = _judgmentValue;
  127. } else {
  128. if (!this.jumpOpts.bStopJump) {
  129. let {
  130. bState,
  131. bType,
  132. value
  133. } = this.detectorPeakOfWaveAndValleyOfWave(_judgmentValue, this.jumpOpts.gravityOld);
  134. if (bState) {
  135. if (!this.jumpOpts.bUpState) {
  136. this.jumpOpts.bUpState = true;
  137. this.isJumpTop = false;
  138. this.highestCount = 0;
  139. //陀螺仪部分
  140. this.oriGyroYArray = [];
  141. this.jumpOpts.startCount = 0;
  142. }
  143. // let _temp = {
  144. // type: bType,
  145. // oldValue: value,
  146. // // value: resultant,
  147. // lastIndex: _runIndex - 1
  148. // };
  149. // this.event.trigger('resultant', _temp);
  150. //记录最高点和最低点数组
  151. if (bType == 'peakOfWave') {
  152. this.peakOfWaveArray.push(value);
  153. // if (value > this.peakOfWaveMaxValue)
  154. this.peakOfWaveMaxValue += value;
  155. } else if (bType == 'valleyOfWave') {
  156. this.valleyOfWaveArray.push(value);
  157. // if (value < this.valleyOfWaveMinValue)
  158. this.valleyOfWaveMinValue += value;
  159. }
  160. }
  161. if (this.jumpOpts.bUpState) {
  162. if(Math.abs(_oGyroY)>10)
  163. this.oriGyroYArray.push(_oGyroY);
  164. // this.jumpOpts.startCount++;
  165. // if (this.jumpOpts.startCount >= 7 ) {
  166. // console.log("startCount peakOfWaveArray", JSON.stringify(this.peakOfWaveArray));
  167. // console.log("startCount valleyOfWaveArray", JSON.stringify(this.valleyOfWaveArray));
  168. // if (this.peakOfWaveArray.length !== 0 || this.valleyOfWaveArray.length !== 0) {
  169. // let _currentMaxValue = 0;
  170. // if (this.peakOfWaveMaxValue > 5) {
  171. // _currentMaxValue = this.peakOfWaveMaxValue;
  172. // } else if (this.valleyOfWaveMinValue < -5) {
  173. // _currentMaxValue = this.valleyOfWaveMinValue;
  174. // }
  175. // let allOGyroValue = 0;
  176. // for (let i = 0; i < this.oriGyroYArray.length; i++) {
  177. // allOGyroValue += this.oriGyroYArray[i];
  178. // }
  179. // allOGyroValue /= this.oriGyroYArray.length;
  180. // //这里相当于处理识别到跳,但是没有判断出什么动作。
  181. // this.event.trigger('resultant', {
  182. // type: "stateDataOfJump",
  183. // currentMaxValue: _currentMaxValue,
  184. // oGyroValue: allOGyroValue,
  185. // resultant: resultant,
  186. // name: "startCountEnd"
  187. // });
  188. // }
  189. // // 如果加过一定数量。判断没有触发,重置状态
  190. // this.jumpOpts.bUpState = false;
  191. // this.jumpOpts.bStopJump = true;
  192. // this.jumpOpts.bUpdateTimeOfPeakCount = true;
  193. // this.resetAll();
  194. // }
  195. //出现极值后
  196. if (Math.abs(linearZ) < 7 && Math.abs(resultant) < 7) {
  197. // this.isJumpTop = true;
  198. this.highestCount++;
  199. if (this.highestCount >= 2) {
  200. //达到最高点,
  201. this.jumpOpts.bStopJump = true;
  202. this.jumpOpts.bUpdateTimeOfPeakCount = true;
  203. // this.isJumpTop = false;
  204. let _currentMaxValue = 0;
  205. // console.log("highestCount peakOfWaveArray", JSON.stringify(this.peakOfWaveArray));
  206. // console.log("highestCount valleyOfWaveArray", JSON.stringify(this.valleyOfWaveArray));
  207. // console.log("达到最高点时候数值 Max:", this.peakOfWaveMaxValue, " min:", this.valleyOfWaveMinValue);
  208. //(Math.abs(this.peakOfWaveMaxValue) > 5 && Math.abs(this.valleyOfWaveMinValue) - Math
  209. // .abs(this.peakOfWaveMaxValue) < 10) ||
  210. if (Math.abs(this.peakOfWaveMaxValue) > Math.abs(this.valleyOfWaveMinValue)) {
  211. _currentMaxValue = this.peakOfWaveMaxValue;
  212. } else {
  213. _currentMaxValue = this.valleyOfWaveMinValue;
  214. }
  215. let allOGyroValue = 0;
  216. for (let i = 0; i < this.oriGyroYArray.length; i++) {
  217. allOGyroValue += this.oriGyroYArray[i];
  218. }
  219. allOGyroValue /= this.oriGyroYArray.length;
  220. //目前测试预大于100 为旋转跳动
  221. // if (allOGyroValue > 0) {
  222. // console.log('right:', allOGyroValue);
  223. // } else {
  224. // console.log('left:', allOGyroValue);
  225. // }
  226. this.event.trigger('resultant', {
  227. type: "jump",
  228. acc: _currentMaxValue,
  229. value: resultant
  230. });
  231. this.event.trigger('resultant', {
  232. type: "curAngle",
  233. value: _currentMaxValue,
  234. resultant: resultant
  235. });
  236. this.event.trigger('resultant', {
  237. type: "rotate",
  238. value: allOGyroValue,
  239. resultant: resultant
  240. });
  241. //如果_currentMaxValue小于30判断原地跳
  242. // console.log("_currentMaxValue:", _currentMaxValue,allOGyroValue);
  243. //后面通用使用这个类型传输数据
  244. this.event.trigger('resultant', {
  245. type: "stateDataOfJump",
  246. currentMaxValue: _currentMaxValue,
  247. peakOfWaveMaxValue: this.peakOfWaveMaxValue,
  248. valleyOfWaveMinValue: this.valleyOfWaveMinValue,
  249. oGyroValue: allOGyroValue,
  250. resultant: resultant,
  251. name: "highestCountEnd"
  252. });
  253. this.jumpOpts.bUpState = false;
  254. // this.resetAll();
  255. }
  256. }
  257. }
  258. } else if (this.jumpOpts.bUpdateTimeOfPeakCount) {
  259. this.jumpOpts.timeOfPeakCount++;
  260. if (this.jumpOpts.timeOfPeakCount >= 30) {
  261. this.jumpOpts.timeOfPeakCount = 0;
  262. this.jumpOpts.bStopJump = false;
  263. this.event.trigger('resultant', {
  264. type: "stop"
  265. });
  266. console.log("timeOfPeakCount >=30");
  267. this.resetAll();
  268. this.jumpOpts.bUpdateTimeOfPeakCount = false;
  269. }
  270. }
  271. // let result = Math.atan2(averX, averZ) * 180 / (Math.PI);
  272. // result = Math.round(result);
  273. // let curAngle = result > 0 ? result : (360 + result);
  274. // console.log("curAngle:", curAngle);
  275. this.event.trigger('resultant', {
  276. type: "bUpdateDraw",
  277. linearX: linearX,
  278. linearZ: linearZ,
  279. linearY: linearY,
  280. oriX: oriX,
  281. oriY: oriY,
  282. oriZ: oriZ
  283. });
  284. this.jumpOpts.gravityOld = _judgmentValue;
  285. }
  286. }
  287. ActionJump.prototype.detectorPeakOfWaveAndValleyOfWave = function(newValue, oldValue) {
  288. this.jumpOpts.lastStatus = this.jumpOpts.isDirectionUp;
  289. if (newValue >= oldValue) {
  290. this.jumpOpts.continueDownFormerCount = this.jumpOpts.continueDownCount;
  291. this.jumpOpts.continueDownCount = 0;
  292. this.jumpOpts.isDirectionUp = true;
  293. this.jumpOpts.continueUpCount++;
  294. } else {
  295. this.jumpOpts.continueUpFormerCount = this.jumpOpts.continueUpCount;
  296. this.jumpOpts.continueUpCount = 0;
  297. this.jumpOpts.isDirectionUp = false;
  298. this.jumpOpts.continueDownCount++;
  299. }
  300. if (!this.jumpOpts.isDirectionUp && this.jumpOpts.lastStatus && this.jumpOpts.continueUpFormerCount >= 2 && Math
  301. .abs(oldValue) > 4) {
  302. this.jumpOpts.peakOfWave = oldValue;
  303. return {
  304. value: oldValue,
  305. bType: 'peakOfWave',
  306. bState: true
  307. };
  308. } else if (!this.jumpOpts.lastStatus && this.jumpOpts.isDirectionUp && this.jumpOpts.continueDownFormerCount >=
  309. 2 && Math.abs(oldValue) > 4) {
  310. this.jumpOpts.valleyOfWave = oldValue;
  311. return {
  312. value: oldValue,
  313. bType: 'valleyOfWave',
  314. bState: true
  315. };
  316. } else {
  317. return {
  318. value: oldValue,
  319. bType: 'None',
  320. bState: false
  321. };
  322. }
  323. }
  324. //重置对应的参数
  325. ActionJump.prototype.resetAll = function() {
  326. // console.log('******************* resetAll ******************');
  327. this.peakOfWaveArray = [];
  328. this.peakOfWaveMaxValue = 0;
  329. this.valleyOfWaveArray = [];
  330. this.valleyOfWaveMinValue = 0;
  331. this.highestCount = 0;
  332. this.jumpOpts.continueDownFormerCount = 0;
  333. this.jumpOpts.continueDownCount = 0;
  334. this.jumpOpts.continueUpFormerCount = 0;
  335. this.jumpOpts.continueUpCount = 0;
  336. // this.jumpOpts.gravityOld = 0;
  337. }
  338. if (typeof module === "object" && typeof module.exports === "object") {
  339. module.exports = ActionJump;
  340. }