jump-0.2.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * 跳判断相关脚本代码
  3. */
  4. function Event() {
  5. this.events = {};
  6. }
  7. Event.prototype.addEventListener = function(type, listener) {
  8. this.events[type] = this.events[type] || [];
  9. this.events[type].push(listener);
  10. };
  11. Event.prototype.trigger = function() {
  12. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  13. args[_key] = arguments[_key];
  14. }
  15. var type = args[0];
  16. var params = args.slice(1);
  17. if (!!this.events[type]) {
  18. // console.log("type:",type);
  19. this.events[type].forEach(function(listener) {
  20. try {
  21. listener.apply(null, params);
  22. } catch (e) {
  23. console.error(e);
  24. }
  25. });
  26. }
  27. };
  28. var jumpOpts = {
  29. //是否上升的标志位
  30. isDirectionUp: false,
  31. //持续上升次数
  32. continueUpCount: 0,
  33. //上一点的持续上升的次数,为了记录波峰的上升次数
  34. continueUpFormerCount: 0,
  35. continueDownCount: 0,
  36. continueDownFormerCount: 0,
  37. //上一点的状态,上升还是下降
  38. lastStatus: false,
  39. //波峰值
  40. peakOfWave: 0,
  41. //波谷值
  42. valleyOfWave: 0,
  43. //检测到极快的波动的次数
  44. timeOfPeakCount: 0,
  45. //开始添加
  46. bUpdateTimeOfPeakCount: false,
  47. //开始更新的次数
  48. startCount: 0,
  49. //停止跳
  50. bStopJump: false,
  51. //上次传感器的值
  52. gravityOld: 0,
  53. bUpState: false,
  54. //开始时间
  55. startTime: 0,
  56. endTime: 0
  57. }
  58. var ActionJump = function ActionJump() {
  59. this.jumpOpts = jumpOpts;
  60. //其他波峰波谷参数相关数组记录
  61. this.peakOfWaveArray = [];
  62. this.peakOfWaveMaxValue = 0;
  63. this.valleyOfWaveArray = [];
  64. this.valleyOfWaveMinValue = 0;
  65. this.peakOfWaveArrayValue = [];
  66. this.valleyOfWaveArrayValue = [];
  67. this.peakOfWaveArrayValueLinear = [];
  68. this.valleyOfWaveArrayValueLinear = [];
  69. this.highestCount = 0;
  70. //陀螺仪
  71. this.oriGyroYArray = [];
  72. this.isJumpTop = false;
  73. this.event = new Event();
  74. this.frameCapacity = 6;
  75. this.frame = [];
  76. this.frameLength = 5;
  77. this.frameOffset = 0;
  78. this.frameHitCapacity = 11;
  79. this.frameHit = []; //打击后的n帧
  80. for (var i = 0; i < this.frameCapacity; ++i) {
  81. var o = new Object();
  82. // o.acc = [0, 0, 0];
  83. // o.gyr = [0, 0, 0];
  84. o.maxValue = 0;
  85. o.gyroValue = 0;
  86. o.resultant = 0;
  87. this.frame.push(o);
  88. }
  89. }
  90. ActionJump.prototype.addEventListener = function(type, listener) {
  91. this.event.addEventListener(type, listener);
  92. };
  93. ActionJump.prototype.updateJump = function() {
  94. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  95. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  96. //********加速计********
  97. let {
  98. lAccX,
  99. lAccY,
  100. lAccZ
  101. } = data.linearAcc;
  102. let {
  103. oAccX,
  104. oAccY,
  105. oAccZ
  106. } = data.oriAcc;
  107. let {
  108. oGyroX,
  109. oGyroY,
  110. oGyroZ
  111. } = data.oriGyro;
  112. let {
  113. bYAxis
  114. } = data;
  115. let _tempAxisData = bYAxis ? oGyroY : oGyroX;
  116. this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, _tempAxisData);
  117. };
  118. /*
  119. * 检测步子,并开始计步
  120. * 1.传入数据
  121. * 2.如果检测到了波峰,并且符合时间差以及阈值的条件,则判定为1步
  122. * 3.符合时间差条件,波峰波谷差值大于initialValue,则将该差值纳入阈值的计算中
  123. * */
  124. ActionJump.prototype.detectorNewStep = function(resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex,
  125. _oGyroY) {
  126. let _judgmentValue = oriZ;
  127. //判断resultant 一个阀值
  128. let limitResultant = 20;
  129. if (!this.jumpOpts.bStopJump) {
  130. if (resultant > limitResultant && !this.jumpOpts.bUpState) {
  131. // console.log("开始判断时候:" + resultant);
  132. this.jumpOpts.bUpState = true;
  133. this.isJumpTop = false;
  134. this.highestCount = 0;
  135. //陀螺仪部分
  136. this.oriGyroYArray = [];
  137. this.peakOfWaveArrayValue = [];
  138. this.valleyOfWaveArrayValue = [];
  139. this.peakOfWaveArrayValueLinear = [];
  140. this.valleyOfWaveArrayValueLinear = [];
  141. this.jumpOpts.startCount = 0;
  142. //开始更新。加入时间判断
  143. this.jumpOpts.startTime = new Date().getTime();
  144. this.event.trigger('resultant', {
  145. type: "log",
  146. logType:'normal',
  147. data: "开始时间:"+this.jumpOpts.startTime
  148. });
  149. }
  150. if (this.jumpOpts.bUpState) {
  151. let newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
  152. if (_judgmentValue > 2) {
  153. newFrame.maxValue = _judgmentValue;
  154. if (_judgmentValue > this.peakOfWaveMaxValue)
  155. this.peakOfWaveMaxValue += _judgmentValue;
  156. } else if (_judgmentValue < -2) {
  157. newFrame.maxValue = _judgmentValue;
  158. if (_judgmentValue < this.valleyOfWaveMinValue)
  159. this.valleyOfWaveMinValue += _judgmentValue;
  160. }
  161. if (Math.abs(_oGyroY) > 5){
  162. this.oriGyroYArray.push(_oGyroY);
  163. newFrame.gyroValue = _oGyroY;
  164. }
  165. newFrame.resultant = resultant;
  166. let currTime = new Date().getTime(); //当前时间
  167. let diffTime = currTime - this.jumpOpts.startTime; //当前时间减最初时间,得到当前时间差
  168. // if (diffTime > 200){
  169. // this.jumpOpts.bUpState = false;
  170. // this.event.trigger('resultant', {
  171. // type: "log",
  172. // logType:'normal',
  173. // data: "超时:"+diffTime
  174. // });
  175. // return;
  176. // };
  177. //出现极值后
  178. // Math.abs(linearZ) < 7 &&
  179. if (Math.abs(resultant) < 7) {
  180. this.event.trigger('resultant', {
  181. type: "log",
  182. logType:'normal',
  183. data: '出现极值后:'+ resultant+",时间:"+diffTime
  184. });
  185. // if (diffTime < 150){
  186. // this.jumpOpts.bUpState = false;
  187. // return;
  188. // };
  189. // this.event.trigger('resultant', {
  190. // type: "log",
  191. // logType:'normal',
  192. // data: "************触发成功************"
  193. // });
  194. this.highestCount++;
  195. if (this.highestCount >= 1) {
  196. //达到最高点,
  197. this.jumpOpts.bStopJump = true;
  198. this.jumpOpts.bUpdateTimeOfPeakCount = true;
  199. let _currentMaxValue = 0;
  200. if (Math.abs(this.peakOfWaveMaxValue) > Math.abs(this.valleyOfWaveMinValue)) {
  201. _currentMaxValue = this.peakOfWaveMaxValue;
  202. } else {
  203. _currentMaxValue = this.valleyOfWaveMinValue;
  204. }
  205. let allOGyroValue = 0;
  206. for (let i = 0; i < this.oriGyroYArray.length; i++) {
  207. allOGyroValue += this.oriGyroYArray[i];
  208. }
  209. allOGyroValue /= this.oriGyroYArray.length;
  210. let _frameMaxValue = 0,_frameGyroValue = 0;
  211. for (let i = 0; i < this.frame.length; i++) {
  212. _frameMaxValue += this.frame[i].maxValue;
  213. _frameGyroValue += this.frame[i].gyroValue;
  214. }
  215. console.log("frame:"+_frameMaxValue + " == "+ _frameGyroValue);
  216. //后面通用使用这个类型传输数据
  217. this.event.trigger('resultant', {
  218. type: "stateDataOfJump",
  219. currentMaxValue: _frameMaxValue,
  220. peakOfWaveMaxValue: this.peakOfWaveMaxValue,
  221. valleyOfWaveMinValue: this.valleyOfWaveMinValue,
  222. oGyroValue: _frameGyroValue/this.frame.length,
  223. resultant: resultant,
  224. name: "highestCountEnd"
  225. });
  226. // this.jumpOpts.bUpState = false;
  227. // this.jumpOpts.bStopJump = false;
  228. this.event.trigger('resultant', {
  229. type: "stop"
  230. });
  231. this.resetAll();
  232. }
  233. }
  234. if ((this.frameOffset += 1) >= this.frameCapacity) {
  235. this.frameOffset -= this.frameCapacity;
  236. }
  237. }
  238. }else if (this.jumpOpts.bUpdateTimeOfPeakCount ) {
  239. // console.log("结束判断时候:" + resultant);
  240. this.jumpOpts.timeOfPeakCount++;
  241. //todo 如果直跳,可以调节更小的 limitTimeOfPeakCount 30
  242. let limitTimeOfPeakCount = 10;
  243. if (this.jumpOpts.timeOfPeakCount >= limitTimeOfPeakCount) {
  244. this.jumpOpts.timeOfPeakCount = 0;
  245. this.jumpOpts.bStopJump = false;
  246. this.jumpOpts.bUpState = false;
  247. // this.event.trigger('resultant', {
  248. // type: "stop"
  249. // });
  250. // this.resetAll();
  251. this.jumpOpts.bUpdateTimeOfPeakCount = false;
  252. // console.log("timeOfPeakCount >= " + limitTimeOfPeakCount);
  253. }
  254. }
  255. }
  256. ActionJump.prototype.setBUpState = function(value){
  257. this.jumpOpts.bUpState = value;
  258. }
  259. //重置对应的参数
  260. ActionJump.prototype.resetAll = function() {
  261. // console.log('******************* resetAll ******************');
  262. this.peakOfWaveArray = [];
  263. this.peakOfWaveMaxValue = 0;
  264. this.valleyOfWaveArray = [];
  265. this.valleyOfWaveMinValue = 0;
  266. this.highestCount = 0;
  267. this.jumpOpts.continueDownFormerCount = 0;
  268. this.jumpOpts.continueDownCount = 0;
  269. this.jumpOpts.continueUpFormerCount = 0;
  270. this.jumpOpts.continueUpCount = 0;
  271. }
  272. if (typeof module === "object" && typeof module.exports === "object") {
  273. module.exports = ActionJump;
  274. }