jump.js 9.0 KB

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