| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 | 
							- import o0 from "./o0.js"
 
- module.exports = {
 
-     SandbagAlgorithm: class{
 
-         constructor() {
 
- 			this.stableAcc = new o0.Vector2(0,0);
 
- 			this.stableGyr = new o0.Vector2(0,0);
 
- 			this.stableCount = 0;
 
- 			this.stableCountMax = 3000;
 
- 			this.frameCapacity = 6;
 
- 			this.frame = [];
 
- 			this.frameLength = 5;
 
- 			this.frameOffset = 0;
 
- 			this.frameHitCapacity = 11;
 
- 			this.frameHit = [];//打击后的n帧
 
- 			for(var i = 0;i<this.frameCapacity;++i){
 
- 				var o = new Object();
 
- 				o.acc = new o0.Vector2(0,0);
 
- 				o.gyr = new o0.Vector2(0,0);
 
- 				
 
- 				o.timeGap = 20;
 
- 				o.accFixed = 0;
 
- 				o.accSlope = 0;
 
- 				o.pos = new o0.Vector2(0,0);
 
- 				o.predict = new o0.Vector2(0,0);
 
- 				o.shake = 0;
 
- 				o.shakeFixed = 0;
 
- 				o.shakeSlope = 0;
 
- 				o.hit = 0;
 
- 				this.frame.push(o);
 
- 			}
 
- 		}
 
- 		//这个函数不建议外部调用
 
- 		GetDirection(){
 
-             var direction = new o0.Vector2(0,0);
 
- 			var directionDistance = 0.0;
 
- 			for (var fi = 0;fi < this.frameHit.length - 1;++fi) {
 
- 				for (var li = fi + 1;li < this.frameHit.length;++li) {
 
- 					var newDirection = this.frameHit[li].gyr - this.frameHit[fi].gyr;
 
- 					var newDirectionDistance = newDirection.length;
 
- 					if (directionDistance < newDirectionDistance) {
 
- 						directionDistance = newDirectionDistance;
 
- 						direction = newDirection.multiply(this.frameHit[this.frameHit.length -1].time - this.frameHit[0].time);
 
- 					}
 
- 					else {
 
- 						//cout << "false" << endl;
 
- 					}
 
- 				}
 
- 			}
 
- 			return direction;
 
- 		}
 
- 		// 输入俯视的平面坐标系下的xy轴坐标 的 加速计向量/陀螺仪向量。
 
- 		// timeGap 代表当前帧读取传感器与上一帧读取传感器 之间的时间差
 
- 		Update(accX, accY, gyrX,gyrY,timeGap,callback){
 
- 			var rawAcc = new o0.Vector2(accX,accY);
 
- 			var rawGyr = new o0.Vector2(gyrX,gyrY);
 
- 			let lastFrame = this.frame[(this.frameOffset + this.frameLength - 1) % this.frameCapacity];
 
- 			let last2Frame = this.frame[(this.frameOffset + this.frameLength - 2) % this.frameCapacity];
 
- 			let last3Frame = this.frame[(this.frameOffset + this.frameLength - 3) % this.frameCapacity];
 
- 			let last4Frame = this.frame[(this.frameOffset + this.frameLength - 4) % this.frameCapacity];
 
- 			let last5Frame = this.frame[(this.frameOffset + this.frameLength - 5) % this.frameCapacity];
 
- 			var newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
 
- 			newFrame.timeGap = timeGap;
 
- 			newFrame.acc = rawAcc.minus(this.stableAcc);
 
- 			newFrame.gyr = rawGyr.minus(this.stableGyr);
 
- 			if (this.stableCount < this.stableCountMax){
 
- 				this.stableCount += 1;
 
- 			}
 
- 			this.stableAcc = this.stableAcc.multiply((this.stableCount - 1.0) / this.stableCount).plus(rawAcc.multiply(1/this.stableCount));
 
- 			this.stableGyr = this.stableGyr.multiply((this.stableCount - 1.0) / this.stableCount).plus(rawGyr.multiply(1/this.stableCount));
 
- 			//////////////////////////////////////////////////////////////////////////////////
 
- 			newFrame.accFixed = newFrame.acc.length * 100;
 
- 			if(newFrame.accFixed < lastFrame.accFixed * 0.85){
 
- 				newFrame.accFixed = lastFrame.accFixed * 0.85;
 
- 			}
 
- 			lastFrame.accFixed = Math.max(lastFrame.accFixed, Math.min(newFrame.accFixed,last2Frame.accFixed), Math.min(newFrame.accFixed,last3Frame.accFixed));
 
- 			///////////////////////////////////////////////////////////////////////
 
- 			//newFrame.pos = lastFrame.pos.plus(lastFrame.acc.plus(newFrame.acc).multiply(timeGap/60)).multiply(Math.max(1-timeGap/200,0));
 
- 			newFrame.pos = lastFrame.pos.plus(newFrame.acc.multiply(timeGap/30)).multiply(Math.max(1-timeGap/1000,0));
 
- 			////////////////////////////////////////////
 
- 			newFrame.accSlope = Math.max(newFrame.accFixed-lastFrame.accFixed,0);
 
- 			var lastI = this.frame.length-1;
 
- 			var t2 = this.frame[lastI-1].timeGap;
 
- 			var t3 = this.frame[lastI].timeGap + t2;
 
- 			var t4 = newFrame.timeGap + t3;
 
- 			
 
- 			newFrame.predict = new o0.Vector2(
 
- 				new o0.QuadraticEquation(0,this.frame[lastI-2].pos.x,t2,this.frame[lastI-1].pos.x,t3,this.frame[lastI].pos.x).y(t4),
 
- 				new o0.QuadraticEquation(0,this.frame[lastI-2].pos.y,t2,this.frame[lastI-1].pos.y,t3,this.frame[lastI].pos.y).y(t4));/** */
 
- 			newFrame.shake = o0.distance2(newFrame.predict,newFrame.pos) * 100;
 
- 			if(isNaN(newFrame.shake)){
 
- 				newFrame.shake = 0.0;
 
- 			}
 
- 			
 
- 			newFrame.shakeFixed = lastFrame.shakeFixed * 0.85;
 
- 			if(newFrame.shake > newFrame.shakeFixed){
 
- 				newFrame.shakeFixed = newFrame.shake;
 
- 			}/* */
 
- 			lastFrame.shakeFixed = Math.max(lastFrame.shakeFixed, Math.min(newFrame.shakeFixed,last2Frame.shakeFixed), Math.min(newFrame.shakeFixed,last3Frame.shakeFixed));
 
- 			////////////////////////////////////////////////////////////////
 
- 			newFrame.shakeSlope = Math.max(newFrame.shakeFixed-lastFrame.shakeFixed,0);
 
- 			///////////////////////////////////////////////////////////////
 
- 			var direction = new o0.Vector2(0,0);
 
- 			if(lastFrame.hit==0
 
- 				&& last2Frame.hit==0
 
- 				&& last3Frame.hit==0
 
- 				&& last4Frame.hit==0
 
- 				&& last5Frame.hit==0
 
- 				&& (newFrame.accSlope >= 15 || lastFrame.accSlope >= 20)
 
- 				&& (newFrame.shakeSlope >= 20 || lastFrame.shakeSlope >= 40)){
 
- 				newFrame.hit = 1;
 
- 				if (this.frameHit.length < this.frameHitCapacity) {
 
- 					direction = this.GetDirection();
 
- 				}/**/
 
- 				this.frameHit = [];
 
- 				var o = new Object();
 
- 				o.time = 0;
 
- 				o.gyr = lastFrame.gyr;
 
- 				this.frameHit.push(o);
 
- 			}else{
 
- 				newFrame.hit = 0;
 
- 			}
 
- 			if (this.frameHit.length < this.frameHitCapacity) {
 
- 				var o = new Object();
 
- 				o.time = this.frameHit[this.frameHit.length - 1].time + newFrame.timeGap;
 
- 				o.gyr = newFrame.gyr; 
 
- 				if (this.frameHit.length == this.frameHitCapacity) {
 
- 					direction =  this.GetDirection();
 
- 				}
 
- 			}
 
- 			if ((this.frameOffset+=1) >= this.frameCapacity){
 
- 				this.frameOffset -= this.frameCapacity;
 
- 			}
 
- 			
 
- 			if(newFrame.hit != 0){
 
- 				console.log(newFrame.hit, direction);
 
- 			}
 
- 			return (newFrame.hit, direction);
 
- 		}
 
-         test(){
 
-             return "123123131";
 
-         }
 
-     }
 
- };
 
 
  |