jump.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. console.log("peakOfWave=",value,this.peakOfWaveMaxValue);
  148. } else if (bType == 'valleyOfWave') {
  149. this.valleyOfWaveArray.push(_temp);
  150. if (value < this.valleyOfWaveMinValue)
  151. this.valleyOfWaveMinValue = value;
  152. console.log("valleyOfWave=",value,this.valleyOfWaveMinValue);
  153. }
  154. this.highestCount = 0;
  155. //陀螺仪部分
  156. this.oriGyroYArray = [];
  157. this.jumpOpts.startCount = 0;
  158. }
  159. if (this.jumpOpts.bUpState) {
  160. this.oriGyroYArray.push(_oGyroY);
  161. this.jumpOpts.startCount++;
  162. if (this.jumpOpts.startCount >= 15) {
  163. //如果加过一定数量。判断没有触发,重置状态
  164. this.jumpOpts.bUpState = false;
  165. this.resetAll();
  166. this.jumpOpts.startCount = 0;
  167. let allOGyroValue = 0;
  168. for (let i = 0; i < this.oriGyroYArray.length; i++) {
  169. allOGyroValue += this.oriGyroYArray[i];
  170. }
  171. allOGyroValue /= this.oriGyroYArray.length;
  172. //这里相当于处理识别到跳,但是没有判断出什么动作。
  173. this.event.trigger('resultant', {
  174. type: "stateDataOfJump",
  175. currentMaxValue: 0,
  176. oGyroValue: allOGyroValue,
  177. resultant: resultant
  178. });
  179. }
  180. //出现极值后
  181. if (Math.abs(linearZ) < 7 && Math.abs(resultant) < 7) {
  182. this.highestCount++;
  183. if (this.highestCount >= 2) {
  184. //达到最高点,
  185. this.jumpOpts.bStopJump = 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. //目前测试预大于100 为旋转跳动
  198. // if (allOGyroValue > 0) {
  199. // console.log('right:', allOGyroValue);
  200. // } else {
  201. // console.log('left:', allOGyroValue);
  202. // }
  203. this.event.trigger('resultant', {
  204. type: "jump",
  205. acc: _currentMaxValue,
  206. value: resultant
  207. });
  208. this.event.trigger('resultant', {
  209. type: "curAngle",
  210. value: _currentMaxValue,
  211. resultant: resultant
  212. });
  213. this.event.trigger('resultant', {
  214. type: "rotate",
  215. value: allOGyroValue,
  216. resultant: resultant
  217. });
  218. //如果_currentMaxValue小于30判断原地跳
  219. // console.log("_currentMaxValue:", _currentMaxValue,allOGyroValue);
  220. //后面通用使用这个类型传输数据
  221. this.event.trigger('resultant', {
  222. type: "stateDataOfJump",
  223. currentMaxValue: _currentMaxValue,
  224. oGyroValue: allOGyroValue,
  225. resultant: resultant
  226. });
  227. // bUpdate = false;
  228. this.jumpOpts.bUpState = false;
  229. this.resetAll();
  230. console.log("resetAll:", this.jumpOpts.startCount);
  231. }
  232. }
  233. }
  234. } else {
  235. this.jumpOpts.timeOfPeakCount++;
  236. if (this.jumpOpts.timeOfPeakCount >= 30) {
  237. this.jumpOpts.timeOfPeakCount = 0;
  238. this.jumpOpts.bStopJump = false;
  239. this.event.trigger('resultant', {
  240. type: "stop"
  241. });
  242. console.log("timeOfPeakCount >=30");
  243. this.resetAll();
  244. }
  245. }
  246. // let result = Math.atan2(averX, averZ) * 180 / (Math.PI);
  247. // result = Math.round(result);
  248. // let curAngle = result > 0 ? result : (360 + result);
  249. // console.log("curAngle:", curAngle);
  250. this.event.trigger('resultant', {
  251. type: "bUpdateDraw",
  252. linearX: linearX,
  253. linearZ: linearZ,
  254. linearY: linearY,
  255. oriX: oriX,
  256. oriY: oriY,
  257. oriZ: oriZ
  258. });
  259. this.jumpOpts.gravityOld = _judgmentValue;
  260. }
  261. }
  262. ActionJump.prototype.detectorPeakOfWaveAndValleyOfWave = function(newValue, oldValue) {
  263. this.jumpOpts.lastStatus = this.jumpOpts.isDirectionUp;
  264. if (newValue >= oldValue) {
  265. this.jumpOpts.continueDownFormerCount = this.jumpOpts.continueDownCount;
  266. this.jumpOpts.continueDownCount = 0;
  267. this.jumpOpts.isDirectionUp = true;
  268. this.jumpOpts.continueUpCount++;
  269. } else {
  270. this.jumpOpts.continueUpFormerCount = this.jumpOpts.continueUpCount;
  271. this.jumpOpts.continueUpCount = 0;
  272. this.jumpOpts.isDirectionUp = false;
  273. this.jumpOpts.continueDownCount++;
  274. }
  275. if (!this.jumpOpts.isDirectionUp && this.jumpOpts.lastStatus && this.jumpOpts.continueUpFormerCount >= 2 && Math
  276. .abs(oldValue) >= 5) {
  277. this.jumpOpts.peakOfWave = oldValue;
  278. return {
  279. value: oldValue,
  280. bType: 'peakOfWave',
  281. bState: true
  282. };
  283. } else if (!this.jumpOpts.lastStatus && this.jumpOpts.isDirectionUp && this.jumpOpts.continueDownFormerCount >=
  284. 2 && Math.abs(oldValue) >= 5) {
  285. this.jumpOpts.valleyOfWave = oldValue;
  286. return {
  287. value: oldValue,
  288. bType: 'valleyOfWave',
  289. bState: true
  290. };
  291. } else {
  292. return {
  293. value: oldValue,
  294. bType: 'None',
  295. bState: false
  296. };
  297. }
  298. }
  299. //重置对应的参数
  300. ActionJump.prototype.resetAll = function() {
  301. // console.log('******************* resetAll ******************');
  302. this.peakOfWaveArray = [];
  303. this.peakOfWaveMaxValue = 0;
  304. this.valleyOfWaveArray = [];
  305. this.valleyOfWaveMinValue = 0;
  306. this.highestCount = 0;
  307. this.jumpOpts.continueDownFormerCount = 0;
  308. this.jumpOpts.continueDownCount = 0;
  309. this.jumpOpts.continueUpFormerCount = 0;
  310. this.jumpOpts.continueUpCount = 0;
  311. }
  312. if (typeof module === "object" && typeof module.exports === "object") {
  313. module.exports = ActionJump;
  314. }