检测机器人晃动设备的左或右的方向吗?机器人、方向、设备

2023-09-03 22:39:39 作者:没心没肺^也不愈

我想使用的SensorManager但对于检测向左或右方向振动装置的,我不知道如何检测震动装置是在朝着正确的方向或左方向。我有以下问题:

I want to use SensorManager but for detecting Shaking of device in left or right direction, I don't know how to detect shaking of device is in right direction or in left direction. I have following question:

如何实现设备的使用摇晃的SensorManager?

How to implement shaking of device using SensorManager?

如何检测在哪个方向摇动装置是?

How can I detect in which direction shaking of device is?

推荐答案

我已经试过这一个,发现了这样的一种方式。该方法是,我监测X轴的值。如果阈值超过极限,X值超过我们给的价值,那么我们将得到的我们动摇了手机向左或向右敬酒消息。我曾尝试这样的:

I have tried this one and found out one way of doing this. The method is, I am monitoring the value of X axis. If the threshold exceeds the limit and the X value exceeds the value we are giving, then we will get a Toast message that on which we shaken the phone either left or right. I have tried like this:

public class ShakeActivity extends Activity implements SensorListener {
        // For shake motion detection.
        private SensorManager sensorMgr;
        private long lastUpdate = -1;
        private float x, y, z;
        private float last_x, last_y, last_z;
        private static final int SHAKE_THRESHOLD = 800;

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        // start motion detection
        sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        boolean accelSupported = sensorMgr.registerListener(this,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);

        if (!accelSupported) {
            // on accelerometer on this device
            sensorMgr.unregisterListener(this,
                    SensorManager.SENSOR_ACCELEROMETER);
        }
        }

        protected void onPause() {
        if (sensorMgr != null) {
            sensorMgr.unregisterListener(this,
                    SensorManager.SENSOR_ACCELEROMETER);
            sensorMgr = null;
            }
        super.onPause();
        }

        public void onAccuracyChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub
        }

        public void onSensorChanged(int sensor, float[] values) {
        if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            // only allow one update every 100ms.
            if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = values[SensorManager.DATA_X];
            y = values[SensorManager.DATA_Y];
            z = values[SensorManager.DATA_Z];

            if(Round(x,4)>10.0000){
                Log.d("sensor", "X Right axis: " + x);
                Toast.makeText(this, "Right shake detected", Toast.LENGTH_SHORT).show();
            }
            else if(Round(x,4)<-10.0000){
                Log.d("sensor", "X Left axis: " + x);
                Toast.makeText(this, "Left shake detected", Toast.LENGTH_SHORT).show();
            }

            float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;

            // Log.d("sensor", "diff: " + diffTime + " - speed: " + speed);
            if (speed > SHAKE_THRESHOLD) {
                //Log.d("sensor", "shake detected w/ speed: " + speed);
                //Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
            }
            last_x = x;
            last_y = y;
            last_z = z;
            }
        }
        }

        public static float Round(float Rval, int Rpl) {
        float p = (float)Math.pow(10,Rpl);
        Rval = Rval * p;
        float tmp = Math.round(Rval);
        return (float)tmp/p;
        }
    }