public Sensor(int dir) { battery = DEFAULT_CAPACITY; Random rnd = new Random(); discharge = (uint)rnd.Next(1, 100); state = true; sdir = (SensorDirection)dir; }
//Constructor public Sensor() { battery = DEFAULT_CAPACITY; Random rnd = new Random(); discharge = 1;//(uint)rnd.Next(1,100); state = true; sdir = 0; }
private void OnOrientation(float x, float y, float z) { // �����ֻ���ת�Ƕ� orientation = 0; magnitude = x * x + y * y; if (magnitude * 4 >= z * z) { float angle = MathUtils.Atan2(-y, x) * MathUtils.RAD_TO_DEG; orientation = 90 - MathUtils.Round(angle); while (orientation >= 360) { orientation -= 360; } while (orientation < 0) { orientation += 360; } } // ���ֻ���ת�Ƕ�תΪ�ֻ����� if (CheckAngle(0, orientation) || CheckAngle(360, orientation)) { _all_direction = Config.TUP; _direction = SensorDirection.UP; } else if (CheckAngle(45, orientation)) { _all_direction = Config.LEFT; _direction = SensorDirection.LEFT; } else if (CheckAngle(90, orientation)) { _all_direction = Config.TLEFT; _direction = SensorDirection.LEFT; } else if (CheckAngle(135, orientation)) { _all_direction = Config.DOWN; _direction = SensorDirection.LEFT; } else if (CheckAngle(180, orientation)) { _all_direction = Config.TDOWN; _direction = SensorDirection.DOWN; } else if (CheckAngle(225, orientation)) { _all_direction = Config.RIGHT; _direction = SensorDirection.RIGHT; } else if (CheckAngle(270, orientation)) { _all_direction = Config.TRIGHT; _direction = SensorDirection.RIGHT; } else if (CheckAngle(315, orientation)) { _all_direction = Config.UP; _direction = SensorDirection.RIGHT; } else { _all_direction = Config.EMPTY; _direction = SensorDirection.EMPTY; } }
private void GravityToKey(SensorDirection d) { SetKeyUp(Key.LEFT); SetKeyUp(Key.RIGHT); SetKeyUp(Key.DOWN); SetKeyUp(Key.UP); if (d == SensorDirection.LEFT) { SetKeyDown(Key.LEFT); } else if (d == SensorDirection.RIGHT) { SetKeyDown(Key.RIGHT); } else if (d == SensorDirection.UP) { SetKeyDown(Key.UP); } else if (d == SensorDirection.DOWN) { SetKeyDown(Key.DOWN); } }
public void OnSensorChanged(LProcess.AccelerometerState e) { if (isLock || isClose || !isLoad) { return; } lock (this) { long curTime = JavaRuntime.CurrentTimeMillis(); if ((curTime - lastUpdate) > 30) { long diffTime = (curTime - lastUpdate); lastUpdate = curTime; currentX = e.Acceleration.X; currentY = e.Acceleration.Y; currentZ = e.Acceleration.Z; currenForce = Math.Abs(currentX + currentY + currentZ - lastX - lastY - lastZ) / diffTime * 10000; if (currenForce > 500) { OnShakeChanged(currenForce); } float absx = Math.Abs(currentX); float absy = Math.Abs(currentY); float absz = Math.Abs(currentZ); if (LSystem.SCREEN_LANDSCAPE) { // PS:横屏时上下朝向非常容易同左右混淆,暂未发现有效的解决方案(不是无法区分, // 而是横屏操作游戏时用户难以保持水平,而一晃就是【左摇右摆】……) ,所以现阶段横屏只取左右 double type = Math.Atan(currentY / currentZ); if (type <= -accelOffset) { if (landscapeUpdate > -accelOffset) { direction = SensorDirection.LEFT; } } else if (type >= accelOffset) { if (landscapeUpdate < accelOffset) { direction = SensorDirection.RIGHT; } } else { if (landscapeUpdate <= -accelOffset) { direction = SensorDirection.LEFT; } else if (landscapeUpdate >= accelOffset) { direction = SensorDirection.RIGHT; } } landscapeUpdate = type; } else { if (absx > absy && absx > absz) { if (currentX > accelOffset) { direction = SensorDirection.LEFT; } else { direction = SensorDirection.RIGHT; } } else if (absz > absx && absz > absy) { if (currentY < -accelOffset) { direction = SensorDirection.DOWN; } else { direction = SensorDirection.UP; } } } lastX = currentX; lastY = currentY; lastZ = currentZ; if (isGravityToKey) { GravityToKey(direction); } OnDirection(direction, currentX, currentY, currentZ); } } }
public virtual void OnDirection(SensorDirection direction, float x, float y, float z) { }
private void OnOrientation(float x, float y, float z) { // 换算手机翻转角度 orientation = 0; magnitude = x * x + y * y; if (magnitude * 4 >= z * z) { float angle = MathUtils.Atan2(-y, x) * MathUtils.RAD_TO_DEG; orientation = 90 - MathUtils.Round(angle); while (orientation >= 360) { orientation -= 360; } while (orientation < 0) { orientation += 360; } } // 将手机翻转角度转为手机朝向 if (CheckAngle(0, orientation) || CheckAngle(360, orientation)) { _all_direction = Config.TUP; _direction = SensorDirection.UP; } else if (CheckAngle(45, orientation)) { _all_direction = Config.LEFT; _direction = SensorDirection.LEFT; } else if (CheckAngle(90, orientation)) { _all_direction = Config.TLEFT; _direction = SensorDirection.LEFT; } else if (CheckAngle(135, orientation)) { _all_direction = Config.DOWN; _direction = SensorDirection.LEFT; } else if (CheckAngle(180, orientation)) { _all_direction = Config.TDOWN; _direction = SensorDirection.DOWN; } else if (CheckAngle(225, orientation)) { _all_direction = Config.RIGHT; _direction = SensorDirection.RIGHT; } else if (CheckAngle(270, orientation)) { _all_direction = Config.TRIGHT; _direction = SensorDirection.RIGHT; } else if (CheckAngle(315, orientation)) { _all_direction = Config.UP; _direction = SensorDirection.RIGHT; } else { _all_direction = Config.EMPTY; _direction = SensorDirection.EMPTY; } }
//Pre: next valid direction to move in the grid //Post: updated direction for the sensor to move with the robot. public void changeDirection(robotDirection newDir) { sdir = (SensorDirection)newDir; }