jump-0.3.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. this.peakOfWaveMaxValue = 0;
  61. this.peakOfWaveMaxValueCount = 0;
  62. this.valleyOfWaveMinValue = 0;
  63. this.valleyOfWaveMinValueCount = 0;
  64. this.highestCount = 0;
  65. //陀螺仪
  66. this.oriGyroYArray = [];
  67. this.event = new Event();
  68. this.frameCapacity = 10;
  69. this.frame = [];
  70. this.frameLength = 5;
  71. this.frameOffset = 0;
  72. for (var i = 0; i < this.frameCapacity; ++i) {
  73. var o = new Object();
  74. o.maxValue = 0;
  75. o.gyroValue = 0;
  76. o.resultant = 0;
  77. o.index = -1;
  78. o.acc = null;
  79. o.gyro = null;
  80. this.frame.push(o);
  81. }
  82. this.frameLog = [];
  83. }
  84. ActionJump.prototype.addEventListener = function(type, listener) {
  85. this.event.addEventListener(type, listener);
  86. };
  87. /**
  88. * 更新数据
  89. */
  90. ActionJump.prototype.updateJump = function() {
  91. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  92. //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值
  93. /**
  94. * 纠正后使用的轴向
  95. * box["acc"] = {
  96. ax: -ay,
  97. ay: -ax,
  98. az: az
  99. };
  100. box["gyro"] = {
  101. gx: -gy,
  102. gy: -gx,
  103. gz: gz
  104. };
  105. */
  106. //********加速计********
  107. // let {
  108. // lAccX,
  109. // lAccY,
  110. // lAccZ
  111. // } = data.linearAcc;
  112. // let {
  113. // oAccX,
  114. // oAccY,
  115. // oAccZ
  116. // } = data.oriAcc;
  117. // let {
  118. // oGyroX,
  119. // oGyroY,
  120. // oGyroZ
  121. // } = data.oriGyro;
  122. // let {
  123. // bYAxis
  124. // } = data;
  125. // //oGyroX 在用的过程中方向相反,所以添加负号
  126. // let _tempAxisData = bYAxis ? -oGyroX : oGyroY; //新设备直接使用oGyroY
  127. // this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, _tempAxisData);
  128. // oAccZ
  129. // this.detectorNewStep(data.resultant, -lAccY, -lAccX, -lAccZ, -oAccY, -oAccX, -oAccZ, data.runIndex,
  130. // _tempAxisData);
  131. this.detectorNewStep(data);
  132. };
  133. /*
  134. *计算跳逻辑
  135. */
  136. // ActionJump.prototype.detectorNewStep = function(resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex,
  137. // _oGyroY)
  138. ActionJump.prototype.detectorNewStep = function(data) {
  139. let {
  140. oAccX,
  141. oAccY,
  142. oAccZ
  143. } = data.oriAcc;
  144. let {
  145. oGyroX,
  146. oGyroY,
  147. oGyroZ
  148. } = data.oriGyro;
  149. let {
  150. bYAxis
  151. } = data;
  152. //oGyroX 在用的过程中方向相反,所以添加负号
  153. let _oGyroY = bYAxis ? -oGyroX : oGyroY; //新设备直接使用oGyroY
  154. let _judgmentValue = -oAccZ; //判断左右方向用z
  155. let resultant = data.resultant; //合加速度
  156. let _runIndex = data.runIndex; //下标
  157. //判断resultant 一个阀值
  158. let limitResultant = 12;
  159. if (!this.jumpOpts.bStopJump) {
  160. if (resultant > limitResultant && !this.jumpOpts.bUpState) {
  161. //陀螺仪部分
  162. // this.oriGyroYArray = [];
  163. //开始更新。加入时间判断
  164. this.jumpOpts.startTime = new Date().getTime();
  165. let _diffTime = this.jumpOpts.startTime - this.jumpOpts.endTime; //开始时间和结束时间太小的话,不能开始
  166. if (_diffTime < 150) {
  167. return;
  168. }
  169. this.jumpOpts.bUpState = true;
  170. this.event.trigger('resultant', {
  171. type: "log",
  172. logType: 'normal',
  173. data: "开始时间:" + this.jumpOpts.startTime + ",resultant:" + resultant + ",bYAxis:" + bYAxis
  174. });
  175. this.highestCount = 0;
  176. for (let i = 0; i < this.frame.length; i++) {
  177. this.frame[i].maxValue = 0;
  178. this.frame[i].gyroValue = 0;
  179. this.frame[i].resultant = 0;
  180. this.frame[i].index = -1;
  181. this.frame[i].acc = null;
  182. this.frame[i].gyro = null;
  183. }
  184. this.frameLog = [];
  185. }
  186. if (this.jumpOpts.bUpState) {
  187. let currTime = new Date().getTime(); //当前时间
  188. let diffTime = currTime - this.jumpOpts.startTime; //当前时间减最初时间,得到当前时间差
  189. if (diffTime > 500) {
  190. /**
  191. * 获取500毫秒内的数据
  192. * 如果超过限定时间,重置一下数据
  193. */
  194. this.jumpOpts.startTime = currTime;
  195. this.highestCount = 0;
  196. for (let i = 0; i < this.frame.length; i++) {
  197. this.frame[i].maxValue = 0;
  198. this.frame[i].gyroValue = 0;
  199. this.frame[i].resultant = 0;
  200. this.frame[i].index = -1;
  201. this.frame[i].acc = null;
  202. this.frame[i].gyro = null;
  203. }
  204. this.frameLog = [];
  205. // console.error("1************************************************************");
  206. // console.log(this.frame);
  207. // console.error("2************************************************************");
  208. }
  209. // (this.frameOffset + this.frameLength) % this.frameCapacity
  210. let newFrame = this.frame[this.frameOffset];
  211. newFrame.index = _runIndex;
  212. newFrame.acc = data.oriAcc;
  213. newFrame.gyro = data.oriGyro;
  214. if (_judgmentValue > 1) {
  215. newFrame.maxValue = _judgmentValue;
  216. if (_judgmentValue > this.peakOfWaveMaxValue) {
  217. this.peakOfWaveMaxValue += _judgmentValue;
  218. this.peakOfWaveMaxValueCount++;
  219. }
  220. } else if (_judgmentValue < -1) {
  221. newFrame.maxValue = _judgmentValue;
  222. if (_judgmentValue < this.valleyOfWaveMinValue) {
  223. this.valleyOfWaveMinValue += _judgmentValue;
  224. this.valleyOfWaveMinValueCount++;
  225. }
  226. } else {
  227. /**
  228. * 不符合条件设置为0
  229. */
  230. newFrame.maxValue = 0;
  231. }
  232. if (Math.abs(_oGyroY) > 5) {
  233. // this.oriGyroYArray.push(_oGyroY);
  234. newFrame.gyroValue = _oGyroY;
  235. } else {
  236. /**
  237. * 不符合条件设置为0
  238. */
  239. newFrame.gyroValue = 0;
  240. }
  241. newFrame.resultant = resultant;
  242. //拷贝一个数据
  243. this.frameLog.push(JSON.parse(JSON.stringify(newFrame)));
  244. // console.log(JSON.parse(JSON.stringify(newFrame)));
  245. //出现极值后
  246. // Math.abs(linearZ) < 7 &&
  247. if (Math.abs(resultant) < 7) {
  248. this.highestCount++;
  249. if (this.highestCount >= 2) {
  250. //达到最高点,
  251. this.jumpOpts.bStopJump = true;
  252. this.jumpOpts.bUpdateTimeOfPeakCount = true;
  253. let _frameMaxValue = 0,
  254. _frameGyroValue = 0;
  255. // let isPlus = false;
  256. // if (this.peakOfWaveMaxValueCount >= this.valleyOfWaveMinValueCount) {
  257. // isPlus = true;
  258. // }
  259. console.log(this.frame);
  260. console.log(this.frameLog);
  261. this.sendLog(this.frameLog,'socket');
  262. for (let i = 0; i < this.frame.length; i++) {
  263. _frameMaxValue += this.frame[i].maxValue;
  264. _frameGyroValue += this.frame[i].gyroValue;
  265. // if (isPlus && this.frame[i].maxValue > 0) {
  266. // _frameMaxValue += this.frame[i].maxValue;
  267. // _frameGyroValue += this.frame[i].gyroValue;
  268. // } else if (!isPlus && this.frame[i].maxValue < 0) {
  269. // _frameMaxValue += this.frame[i].maxValue;
  270. // _frameGyroValue += this.frame[i].gyroValue;
  271. // }
  272. }
  273. // console.log("frame:" + _frameMaxValue + " == " + _frameGyroValue);
  274. //后面通用使用这个类型传输数据
  275. this.event.trigger('resultant', {
  276. type: "stateDataOfJump",
  277. currentMaxValue: _frameMaxValue,
  278. peakOfWaveMaxValue: this.peakOfWaveMaxValue,
  279. valleyOfWaveMinValue: this.valleyOfWaveMinValue,
  280. oGyroValue: _frameGyroValue ,
  281. resultant: resultant,
  282. name: "highestCountEnd"
  283. });
  284. // this.jumpOpts.bUpState = false;
  285. // this.jumpOpts.bStopJump = false;
  286. this.event.trigger('resultant', {
  287. type: "stop"
  288. });
  289. this.resetAll();
  290. }
  291. }
  292. if ((this.frameOffset += 1) >= this.frameCapacity) {
  293. this.frameOffset -= this.frameCapacity;
  294. }
  295. }
  296. } else if (this.jumpOpts.bUpdateTimeOfPeakCount) {
  297. // console.log("结束判断时候:" + resultant);
  298. this.jumpOpts.timeOfPeakCount++;
  299. //todo 如果直跳,可以调节更小的 limitTimeOfPeakCount 30
  300. let limitTimeOfPeakCount = 10;
  301. if (this.jumpOpts.timeOfPeakCount >= limitTimeOfPeakCount) {
  302. this.jumpOpts.timeOfPeakCount = 0;
  303. this.jumpOpts.bStopJump = false;
  304. this.jumpOpts.bUpState = false;
  305. // this.event.trigger('resultant', {
  306. // type: "stop"
  307. // });
  308. // this.resetAll();
  309. this.jumpOpts.bUpdateTimeOfPeakCount = false;
  310. this.jumpOpts.endTime = new Date().getTime();
  311. }
  312. }
  313. }
  314. ActionJump.prototype.setBUpState = function(value) {
  315. this.jumpOpts.bUpState = value;
  316. }
  317. //重置对应的参数
  318. ActionJump.prototype.resetAll = function() {
  319. console.log("resetAll");
  320. this.peakOfWaveMaxValue = 0;
  321. this.peakOfWaveMaxValueCount = 0;
  322. this.valleyOfWaveMinValue = 0;
  323. this.valleyOfWaveMinValueCount = 0;
  324. this.highestCount = 0;
  325. }
  326. /**
  327. * 日志
  328. * @param {Object} data
  329. * @param {Object} logType
  330. */
  331. ActionJump.prototype.sendLog = function(data,logType){
  332. this.event.trigger('resultant', {
  333. type: "log",
  334. logType: logType,
  335. data: data
  336. });
  337. }
  338. if (typeof module === "object" && typeof module.exports === "object") {
  339. module.exports = ActionJump;
  340. }