/** * 跳判断相关脚本代码 */ function Event() { this.events = {}; } Event.prototype.addEventListener = function(type, listener) { this.events[type] = this.events[type] || []; this.events[type].push(listener); }; Event.prototype.trigger = function() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var type = args[0]; var params = args.slice(1); if (!!this.events[type]) { // console.log("type:",type); this.events[type].forEach(function(listener) { try { listener.apply(null, params); } catch (e) { console.error(e); } }); } }; var jumpOpts = { //是否上升的标志位 isDirectionUp: false, //持续上升次数 continueUpCount: 0, //上一点的持续上升的次数,为了记录波峰的上升次数 continueUpFormerCount: 0, continueDownCount: 0, continueDownFormerCount: 0, //上一点的状态,上升还是下降 lastStatus: false, //波峰值 peakOfWave: 0, //波谷值 valleyOfWave: 0, //检测到极快的波动的次数 timeOfPeakCount: 0, //开始添加 bUpdateTimeOfPeakCount: false, //开始更新的次数 startCount: 0, //停止跳 bStopJump: false, //上次传感器的值 gravityOld: 0, bUpState: false, //开始时间 startTime: 0, endTime: 0 } var ActionJump = function ActionJump() { this.jumpOpts = jumpOpts; this.peakOfWaveMaxValue = 0; this.peakOfWaveMaxValueCount = 0; this.valleyOfWaveMinValue = 0; this.valleyOfWaveMinValueCount = 0; this.highestCount = 0; //陀螺仪 this.oriGyroYArray = []; this.event = new Event(); this.frameCapacity = 10; this.frame = []; this.frameLength = 5; this.frameOffset = 0; for (var i = 0; i < this.frameCapacity; ++i) { var o = new Object(); o.maxValue = 0; o.gyroValue = 0; o.resultant = 0; o.index = -1; o.acc = null; o.gyro = null; this.frame.push(o); } this.frameLog = []; } ActionJump.prototype.addEventListener = function(type, listener) { this.event.addEventListener(type, listener); }; /** * 更新数据 */ ActionJump.prototype.updateJump = function() { let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; //使用三个轴的数据,计算重力轴的加速度。最后减去重力的加速度值 /** * 纠正后使用的轴向 * box["acc"] = { ax: -ay, ay: -ax, az: az }; box["gyro"] = { gx: -gy, gy: -gx, gz: gz }; */ //********加速计******** // let { // lAccX, // lAccY, // lAccZ // } = data.linearAcc; // let { // oAccX, // oAccY, // oAccZ // } = data.oriAcc; // let { // oGyroX, // oGyroY, // oGyroZ // } = data.oriGyro; // let { // bYAxis // } = data; // //oGyroX 在用的过程中方向相反,所以添加负号 // let _tempAxisData = bYAxis ? -oGyroX : oGyroY; //新设备直接使用oGyroY // this.detectorNewStep(data.resultant, lAccX, lAccY, lAccZ, oAccX, oAccY, oAccZ, data.runIndex, _tempAxisData); // oAccZ // this.detectorNewStep(data.resultant, -lAccY, -lAccX, -lAccZ, -oAccY, -oAccX, -oAccZ, data.runIndex, // _tempAxisData); this.detectorNewStep(data); }; /* *计算跳逻辑 */ // ActionJump.prototype.detectorNewStep = function(resultant, linearX, linearY, linearZ, oriX, oriY, oriZ, _runIndex, // _oGyroY) ActionJump.prototype.detectorNewStep = function(data) { let { oAccX, oAccY, oAccZ } = data.oriAcc; let { oGyroX, oGyroY, oGyroZ } = data.oriGyro; let { bYAxis } = data; //oGyroX 在用的过程中方向相反,所以添加负号 let _oGyroY = bYAxis ? -oGyroX : oGyroY; //新设备直接使用oGyroY let _judgmentValue = -oAccZ; //判断左右方向用z let resultant = data.resultant; //合加速度 let _runIndex = data.runIndex; //下标 //判断resultant 一个阀值 let limitResultant = 12; if (!this.jumpOpts.bStopJump) { if (resultant > limitResultant && !this.jumpOpts.bUpState) { //陀螺仪部分 // this.oriGyroYArray = []; //开始更新。加入时间判断 this.jumpOpts.startTime = new Date().getTime(); let _diffTime = this.jumpOpts.startTime - this.jumpOpts.endTime; //开始时间和结束时间太小的话,不能开始 if (_diffTime < 150) { return; } this.jumpOpts.bUpState = true; this.event.trigger('resultant', { type: "log", logType: 'normal', data: "开始时间:" + this.jumpOpts.startTime + ",resultant:" + resultant + ",bYAxis:" + bYAxis }); this.highestCount = 0; for (let i = 0; i < this.frame.length; i++) { this.frame[i].maxValue = 0; this.frame[i].gyroValue = 0; this.frame[i].resultant = 0; this.frame[i].index = -1; this.frame[i].acc = null; this.frame[i].gyro = null; } this.frameLog = []; } if (this.jumpOpts.bUpState) { let currTime = new Date().getTime(); //当前时间 let diffTime = currTime - this.jumpOpts.startTime; //当前时间减最初时间,得到当前时间差 if (diffTime > 500) { /** * 获取500毫秒内的数据 * 如果超过限定时间,重置一下数据 */ this.jumpOpts.startTime = currTime; this.highestCount = 0; for (let i = 0; i < this.frame.length; i++) { this.frame[i].maxValue = 0; this.frame[i].gyroValue = 0; this.frame[i].resultant = 0; this.frame[i].index = -1; this.frame[i].acc = null; this.frame[i].gyro = null; } this.frameLog = []; // console.error("1************************************************************"); // console.log(this.frame); // console.error("2************************************************************"); } // (this.frameOffset + this.frameLength) % this.frameCapacity let newFrame = this.frame[this.frameOffset]; newFrame.index = _runIndex; newFrame.acc = data.oriAcc; newFrame.gyro = data.oriGyro; if (_judgmentValue > 1) { newFrame.maxValue = _judgmentValue; if (_judgmentValue > this.peakOfWaveMaxValue) { this.peakOfWaveMaxValue += _judgmentValue; this.peakOfWaveMaxValueCount++; } } else if (_judgmentValue < -1) { newFrame.maxValue = _judgmentValue; if (_judgmentValue < this.valleyOfWaveMinValue) { this.valleyOfWaveMinValue += _judgmentValue; this.valleyOfWaveMinValueCount++; } } else { /** * 不符合条件设置为0 */ newFrame.maxValue = 0; } if (Math.abs(_oGyroY) > 5) { // this.oriGyroYArray.push(_oGyroY); newFrame.gyroValue = _oGyroY; } else { /** * 不符合条件设置为0 */ newFrame.gyroValue = 0; } newFrame.resultant = resultant; //拷贝一个数据 this.frameLog.push(JSON.parse(JSON.stringify(newFrame))); // console.log(JSON.parse(JSON.stringify(newFrame))); //出现极值后 // Math.abs(linearZ) < 7 && if (Math.abs(resultant) < 7) { this.highestCount++; if (this.highestCount >= 2) { //达到最高点, this.jumpOpts.bStopJump = true; this.jumpOpts.bUpdateTimeOfPeakCount = true; let _frameMaxValue = 0, _frameGyroValue = 0; // let isPlus = false; // if (this.peakOfWaveMaxValueCount >= this.valleyOfWaveMinValueCount) { // isPlus = true; // } console.log(this.frame); console.log(this.frameLog); this.sendLog(this.frameLog,'socket'); for (let i = 0; i < this.frame.length; i++) { _frameMaxValue += this.frame[i].maxValue; _frameGyroValue += this.frame[i].gyroValue; // if (isPlus && this.frame[i].maxValue > 0) { // _frameMaxValue += this.frame[i].maxValue; // _frameGyroValue += this.frame[i].gyroValue; // } else if (!isPlus && this.frame[i].maxValue < 0) { // _frameMaxValue += this.frame[i].maxValue; // _frameGyroValue += this.frame[i].gyroValue; // } } // console.log("frame:" + _frameMaxValue + " == " + _frameGyroValue); //后面通用使用这个类型传输数据 this.event.trigger('resultant', { type: "stateDataOfJump", currentMaxValue: _frameMaxValue, peakOfWaveMaxValue: this.peakOfWaveMaxValue, valleyOfWaveMinValue: this.valleyOfWaveMinValue, oGyroValue: _frameGyroValue , resultant: resultant, name: "highestCountEnd" }); // this.jumpOpts.bUpState = false; // this.jumpOpts.bStopJump = false; this.event.trigger('resultant', { type: "stop" }); this.resetAll(); } } if ((this.frameOffset += 1) >= this.frameCapacity) { this.frameOffset -= this.frameCapacity; } } } else if (this.jumpOpts.bUpdateTimeOfPeakCount) { // console.log("结束判断时候:" + resultant); this.jumpOpts.timeOfPeakCount++; //todo 如果直跳,可以调节更小的 limitTimeOfPeakCount 30 let limitTimeOfPeakCount = 10; if (this.jumpOpts.timeOfPeakCount >= limitTimeOfPeakCount) { this.jumpOpts.timeOfPeakCount = 0; this.jumpOpts.bStopJump = false; this.jumpOpts.bUpState = false; // this.event.trigger('resultant', { // type: "stop" // }); // this.resetAll(); this.jumpOpts.bUpdateTimeOfPeakCount = false; this.jumpOpts.endTime = new Date().getTime(); } } } ActionJump.prototype.setBUpState = function(value) { this.jumpOpts.bUpState = value; } //重置对应的参数 ActionJump.prototype.resetAll = function() { console.log("resetAll"); this.peakOfWaveMaxValue = 0; this.peakOfWaveMaxValueCount = 0; this.valleyOfWaveMinValue = 0; this.valleyOfWaveMinValueCount = 0; this.highestCount = 0; } /** * 日志 * @param {Object} data * @param {Object} logType */ ActionJump.prototype.sendLog = function(data,logType){ this.event.trigger('resultant', { type: "log", logType: logType, data: data }); } if (typeof module === "object" && typeof module.exports === "object") { module.exports = ActionJump; }