jump-0.1.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. var ActionJump = function ActionJump() {
  56. this.jumpOpts = jumpOpts;
  57. //其他波峰波谷参数相关数组记录
  58. this.peakOfWaveArray = [];
  59. this.peakOfWaveMaxValue = 0;
  60. this.valleyOfWaveArray = [];
  61. this.valleyOfWaveMinValue = 0;
  62. this.peakOfWaveArrayValue = [];
  63. this.valleyOfWaveArrayValue = [];
  64. this.peakOfWaveArrayValueLinear = [];
  65. this.valleyOfWaveArrayValueLinear = [];
  66. this.highestCount = 0;
  67. //陀螺仪
  68. this.oriGyroYArray = [];
  69. this.isJumpTop = false;
  70. this.event = new Event();
  71. this.frameCapacity = 6;
  72. this.frame = [];
  73. this.frameLength = 5;
  74. this.frameOffset = 0;
  75. this.frameHitCapacity = 11;
  76. this.frameHit = [];//打击后的n帧
  77. for (var i = 0; i < this.frameCapacity; ++i) {
  78. var o = new Object();
  79. // o.acc = [0, 0, 0];
  80. // o.gyr = [0, 0, 0];
  81. o.resultant = 0;
  82. this.frame.push(o);
  83. }
  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 _judgmentValue = oriZ;
  122. // if (this.jumpOpts.gravityOld == 0) {
  123. // this.jumpOpts.gravityOld = _judgmentValue;
  124. // } else {
  125. if (!this.jumpOpts.bStopJump) {
  126. // let {
  127. // bState,
  128. // bType,
  129. // value
  130. // } = this.detectorPeakOfWaveAndValleyOfWave(_judgmentValue, this.jumpOpts.gravityOld);
  131. // let lastFrame = this.frame[(this.frameOffset + this.frameLength - 1) % this.frameCapacity];
  132. // let last2Frame = this.frame[(this.frameOffset + this.frameLength - 2) % this.frameCapacity];
  133. // let last3Frame = this.frame[(this.frameOffset + this.frameLength - 3) % this.frameCapacity];
  134. // let last4Frame = this.frame[(this.frameOffset + this.frameLength - 4) % this.frameCapacity];
  135. // let last5Frame = this.frame[(this.frameOffset + this.frameLength - 5) % this.frameCapacity];
  136. let newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
  137. newFrame.resultant = resultant;
  138. if (resultant > 20 && !this.jumpOpts.bUpState) {
  139. this.jumpOpts.bUpState = true;
  140. this.isJumpTop = false;
  141. this.highestCount = 0;
  142. //陀螺仪部分
  143. this.oriGyroYArray = [];
  144. this.peakOfWaveArrayValue = [];
  145. this.valleyOfWaveArrayValue = [];
  146. this.peakOfWaveArrayValueLinear = [];
  147. this.valleyOfWaveArrayValueLinear = [];
  148. this.jumpOpts.startCount = 0;
  149. console.log("开始:", JSON.stringify(this.frame), '当前:', resultant);
  150. }
  151. if (this.jumpOpts.bUpState) {
  152. if (_judgmentValue > 2) {
  153. if (_judgmentValue > this.peakOfWaveMaxValue)
  154. this.peakOfWaveMaxValue += _judgmentValue;
  155. this.peakOfWaveArrayValue.push(_judgmentValue);
  156. }
  157. else if (_judgmentValue < -2) {
  158. if (_judgmentValue < this.valleyOfWaveMinValue)
  159. this.valleyOfWaveMinValue += _judgmentValue;
  160. this.valleyOfWaveArrayValue.push(_judgmentValue);
  161. }
  162. if (linearZ > 2)
  163. this.peakOfWaveArrayValueLinear.push(linearZ);
  164. else if (linearZ < -2)
  165. this.valleyOfWaveArrayValueLinear.push(linearZ);
  166. if (Math.abs(_oGyroY) > 10)
  167. this.oriGyroYArray.push(_oGyroY);
  168. //出现极值后
  169. // Math.abs(linearZ) < 7 &&
  170. if (Math.abs(resultant) < 9.8) {
  171. console.log('出现极值后:', linearZ, resultant);
  172. // this.isJumpTop = true;
  173. this.highestCount++;
  174. if (this.highestCount >= 1) {
  175. //达到最高点,
  176. this.jumpOpts.bStopJump = true;
  177. this.jumpOpts.bUpdateTimeOfPeakCount = true;
  178. // this.isJumpTop = false;
  179. let _currentMaxValue = 0;
  180. // console.log("highestCount peakOfWaveArray", JSON.stringify(this.peakOfWaveArray));
  181. // console.log("highestCount valleyOfWaveArray", JSON.stringify(this.valleyOfWaveArray));
  182. let allPeakOfWave = 0;
  183. for (let i = 0; i < this.peakOfWaveArrayValue.length; i++) {
  184. allPeakOfWave += this.peakOfWaveArrayValue[i];
  185. }
  186. let allValleyOfWave = 0;
  187. for (let i = 0; i < this.valleyOfWaveArrayValue.length; i++) {
  188. allValleyOfWave += this.valleyOfWaveArrayValue[i];
  189. }
  190. let allPeakOfWaveLinear = 0;
  191. for (let i = 0; i < this.peakOfWaveArrayValueLinear.length; i++) {
  192. allPeakOfWaveLinear += this.peakOfWaveArrayValueLinear[i];
  193. }
  194. let allValleyOfWaveLinear = 0;
  195. for (let i = 0; i < this.valleyOfWaveArrayValueLinear.length; i++) {
  196. allValleyOfWaveLinear += this.valleyOfWaveArrayValueLinear[i];
  197. }
  198. // console.log("highestCount peakOfWaveArrayValue", JSON.stringify(this.peakOfWaveArrayValue));
  199. // console.log("highestCount valleyOfWaveArrayValue", JSON.stringify(this.valleyOfWaveArrayValue));
  200. console.log("ori总值 peak:", allPeakOfWave, " valley:", allValleyOfWave);
  201. console.log("linear总值 peak:", allPeakOfWaveLinear, " valley:", allValleyOfWaveLinear);
  202. // console.log("达到最高点时候数值 Max:", this.peakOfWaveMaxValue, " min:", this.valleyOfWaveMinValue);
  203. //(Math.abs(this.peakOfWaveMaxValue) > 5 && Math.abs(this.valleyOfWaveMinValue) - Math
  204. // .abs(this.peakOfWaveMaxValue) < 10) ||
  205. if (Math.abs(this.peakOfWaveMaxValue) > Math.abs(this.valleyOfWaveMinValue)) {
  206. _currentMaxValue = this.peakOfWaveMaxValue;
  207. } else {
  208. _currentMaxValue = this.valleyOfWaveMinValue;
  209. }
  210. let allOGyroValue = 0;
  211. for (let i = 0; i < this.oriGyroYArray.length; i++) {
  212. allOGyroValue += this.oriGyroYArray[i];
  213. }
  214. allOGyroValue /= this.oriGyroYArray.length;
  215. this.event.trigger('resultant', {
  216. type: "jump",
  217. acc: _currentMaxValue,
  218. value: resultant
  219. });
  220. this.event.trigger('resultant', {
  221. type: "curAngle",
  222. value: _currentMaxValue,
  223. resultant: resultant
  224. });
  225. this.event.trigger('resultant', {
  226. type: "rotate",
  227. value: allOGyroValue,
  228. resultant: resultant
  229. });
  230. //后面通用使用这个类型传输数据
  231. this.event.trigger('resultant', {
  232. type: "stateDataOfJump",
  233. currentMaxValue: _currentMaxValue,
  234. peakOfWaveMaxValue: this.peakOfWaveMaxValue,
  235. valleyOfWaveMinValue: this.valleyOfWaveMinValue,
  236. oGyroValue: allOGyroValue,
  237. resultant: resultant,
  238. name: "highestCountEnd"
  239. });
  240. this.jumpOpts.bUpState = false;
  241. }
  242. }
  243. }
  244. if ((this.frameOffset += 1) >= this.frameCapacity) {
  245. this.frameOffset -= this.frameCapacity;
  246. }
  247. } else if (this.jumpOpts.bUpdateTimeOfPeakCount) {
  248. this.jumpOpts.timeOfPeakCount++;
  249. //todo 如果直跳,可以调节更小的 limitTimeOfPeakCount
  250. let limitTimeOfPeakCount = 40;
  251. if (this.jumpOpts.timeOfPeakCount >= limitTimeOfPeakCount) {
  252. this.jumpOpts.timeOfPeakCount = 0;
  253. this.jumpOpts.bStopJump = false;
  254. this.event.trigger('resultant', {
  255. type: "stop"
  256. });
  257. console.log("timeOfPeakCount >= " + limitTimeOfPeakCount);
  258. this.resetAll();
  259. this.jumpOpts.bUpdateTimeOfPeakCount = false;
  260. }
  261. }
  262. this.event.trigger('resultant', {
  263. type: "bUpdateDraw",
  264. linearX: linearX,
  265. linearZ: linearZ,
  266. linearY: linearY,
  267. oriX: oriX,
  268. oriY: oriY,
  269. oriZ: oriZ
  270. });
  271. }
  272. ActionJump.prototype.detectorPeakOfWaveAndValleyOfWave = function (newValue, oldValue) {
  273. this.jumpOpts.lastStatus = this.jumpOpts.isDirectionUp;
  274. if (newValue >= oldValue) {
  275. this.jumpOpts.continueDownFormerCount = this.jumpOpts.continueDownCount;
  276. this.jumpOpts.continueDownCount = 0;
  277. this.jumpOpts.isDirectionUp = true;
  278. this.jumpOpts.continueUpCount++;
  279. } else {
  280. this.jumpOpts.continueUpFormerCount = this.jumpOpts.continueUpCount;
  281. this.jumpOpts.continueUpCount = 0;
  282. this.jumpOpts.isDirectionUp = false;
  283. this.jumpOpts.continueDownCount++;
  284. }
  285. if (!this.jumpOpts.isDirectionUp && this.jumpOpts.lastStatus && this.jumpOpts.continueUpFormerCount >= 2 && Math
  286. .abs(oldValue) > 4) {
  287. this.jumpOpts.peakOfWave = oldValue;
  288. return {
  289. value: oldValue,
  290. bType: 'peakOfWave',
  291. bState: true
  292. };
  293. } else if (!this.jumpOpts.lastStatus && this.jumpOpts.isDirectionUp && this.jumpOpts.continueDownFormerCount >=
  294. 2 && Math.abs(oldValue) > 4) {
  295. this.jumpOpts.valleyOfWave = oldValue;
  296. return {
  297. value: oldValue,
  298. bType: 'valleyOfWave',
  299. bState: true
  300. };
  301. } else {
  302. return {
  303. value: oldValue,
  304. bType: 'None',
  305. bState: false
  306. };
  307. }
  308. }
  309. //重置对应的参数
  310. ActionJump.prototype.resetAll = function () {
  311. // console.log('******************* resetAll ******************');
  312. this.peakOfWaveArray = [];
  313. this.peakOfWaveMaxValue = 0;
  314. this.valleyOfWaveArray = [];
  315. this.valleyOfWaveMinValue = 0;
  316. this.highestCount = 0;
  317. this.jumpOpts.continueDownFormerCount = 0;
  318. this.jumpOpts.continueDownCount = 0;
  319. this.jumpOpts.continueUpFormerCount = 0;
  320. this.jumpOpts.continueUpCount = 0;
  321. // this.jumpOpts.gravityOld = 0;
  322. }
  323. if (typeof module === "object" && typeof module.exports === "object") {
  324. module.exports = ActionJump;
  325. }