Android Accelerometer Sensor, Anomal Values ​​/ Glitches

Sinovli telefonda (Nexus S) akselerometr uchun Sensor API ishlatishga harakat qilaman. Skameykada o'tirib telefonda hech narsa qilmasdan men juda g'alati o'qiyman (rasm biriktirilgan). Ushbu turdagi xatti-harakatlarning birontasini ko'rganmisiz? Kalitlarni o'qishda shaffoflik ? U bilan qanday muomala qilish haqida har qanday fikr bormi?

   package com.example.m6;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
//import android.widget.Toast;

public class HelloAndroidActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager; 
    private TextView tv;
    private float accel[] = {0,0,0};
    private float gyro[] = {0,0,0};


    InetAddress addr; 
    int port         = 1337;
    DatagramSocket s = null;
    String out       = "";
    PowerManager.WakeLock wl;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try{
            //Lock the screen 
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            //Set up the output screen
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.textview);
            tv.setBackgroundColor(Color.BLACK);
            tv.setTextColor(Color.GREEN);

            //Set up the telemetry feed 
            addr = InetAddress.getByName("192.168.0.12");
            s = new DatagramSocket();

            //Turning off the power management!!
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
            wl.acquire(); 

            //Set up the sensors
            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

            out += "Done\n";
            tv.setText(out);

        } catch (Exception e) {
            out += "\nInit Error:" + e.getMessage() + "\n";
            tv.setText(out);
        }

    }

    private static final float NS2S = 1.0f/1000000000.0f;
    private float timestamp;

    int down = 0; 
    float tstart = 0;
    @Override
    public void onSensorChanged(SensorEvent event) {            

        String type = "";
        float dT = 0;       
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            if (timestamp != 0) {
                accel = event.values;
                dT = (event.timestamp - timestamp);
                tstart += dT;
            }
            timestamp = event.timestamp;
            type = "G";
        }

        if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            if (timestamp != 0) {
                gyro = event.values;
                dT = (event.timestamp - timestamp);
                tstart += dT;
            }
            timestamp = event.timestamp;
            type = "A";
        }

        String out_packet = tstart/1000/1000 + " "+ type + " " + dT + " " + accel[0] + " " + accel[1] + " " + accel[2] + " " + gyro[0] + " " + gyro[1] + " " + gyro[2] + "\n\0";

        try{
            DatagramPacket p = new DatagramPacket(out_packet.getBytes(),out_packet.length(), addr,port);
            s.send(p);
        }
    catch (Exception e) {
            out += "Packet send error: " + e.getMessage() + "\n";
        }

        tv.setText(timestamp + "\n" + out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        out += "on resume\n";
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
        tv.setText(out);
    }

    @Override
    protected void onPause() {
        out += "on pause";
        sensorManager.unregisterListener(this);
        tv.setText(out);
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        out += "on destroy";
        wl.release();
        tv.setText(out);
        super.onDestroy();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
       //TODO Auto-generated method stub

    }
}
0
Droid 1 dan foydalanayotganda men shunga o'xshash anomaliyalarni sezmadim. Men har doim o'zimni aniqlashni istagan narsalar uchun (harakatlanish ketma-ketligini) ishlatishga majbur bo'ldim va har qanday harakat voqealarini aniqlashga urinmas edim.
qo'shib qo'ydi muallif Matt H, manba
Kodni qo'shdim. Umid qilamanki, bu men eshitayotgan voqealar haqida foydali bo'ladi.
qo'shib qo'ydi muallif MattieG, manba

2 javoblar

Kod yaxshi bo'lar edi .. bu demo kodini sinab ko'ring. Bilaman, u ishlaydi va grafik qiladi.

http://www.5laida.com/docs/android/samples/Sensors.html

Men ushbu kodni bir nechta qurilmalarda sinab ko'rdim. Agar shunga o'xshash glitchesni ko'rsatsa, u ehtimol sizning qurilmangiz.

0
qo'shib qo'ydi
Ha, albatta! Menga aql bovar qilmadi. Resursni o'z ichiga olgan savolni tahrirlash. Sizni ogohlantirishim kerak, men asosan C/C ++ dasturini ishlab chiqaruvchiman, mening Java juda zang. Shunday qilib, uning kodida xato bo'lishi mumkin. Menda bu tilda sezgi yo'q.
qo'shib qo'ydi muallif MattieG, manba

Nima bo'layotganini tushunib etdim. Ushbu qatorni kiritish kerak edi:

        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE ){
        return;
    }

Biroq, shuning uchun men endi gyroskopdan hech qanday qadr-qimmatga ega emasman. Hmm ...

0
qo'shib qo'ydi
Men event.accuracy yoki Sensor_Status_unrelaible uchun manbaga qarab emasman. Lekin event.accuarcy raqamli qiymatga ega ... shuning uchun siz eshikni o'rnatishingiz mumkin. Grafiğinize qarab, muammo to'liq tomchi. Agar o'zgarish tez va keskin bo'lsa, o'zgarishlarni bekor qilmaslik uchun, ba'zi hollarda, ba'zi hollarda, siz ham bo'lishi mumkin. Barcha qurilmalarni qo'llab-quvvatlash juda qiyin, ayniqsa OS farqli o'laroq, shuningdek, jismoniy telefon qurilishi sifati.
qo'shib qo'ydi muallif DJPlayer, manba
Yechimni tabriklayman. Agar qodir bo'lsang, iltimos, javobingizni "qabul qilingan" deb belgilab qo'ying, boshqalar sizdan muvaffaqiyat qozonishi uchun. Cheers ~
qo'shib qo'ydi muallif Andrew Kozak, manba
Va bu savolga javob: " stackoverflow.com/questions/5985514/…
qo'shib qo'ydi muallif MattieG, manba