ActionJump.js 9.3 KB

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